http_static: sanitize path before file read
[vpp.git] / src / plugins / unittest / util_test.c
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <vlib/vlib.h>
17 #include <sys/mman.h>
18
19 static clib_error_t *
20 test_crash_command_fn (vlib_main_t * vm,
21                        unformat_input_t * input, vlib_cli_command_t * cmd)
22 {
23   u64 *p = (u64 *) 0xdefec8ed;
24
25   ELOG_TYPE_DECLARE (e) =
26     {
27       .format = "deliberate crash: touching %x",
28       .format_args = "i4",
29     };
30   elog (&vlib_global_main.elog_main, &e, 0xdefec8ed);
31
32   *p = 0xdeadbeef;
33
34   /* Not so much... */
35   return 0;
36 }
37
38 VLIB_CLI_COMMAND (test_crash_command, static) =
39 {
40   .path = "test crash",
41   .short_help = "crash the bus!",
42   .function = test_crash_command_fn,
43 };
44
45 static clib_error_t *
46 test_hash_command_fn (vlib_main_t * vm,
47                       unformat_input_t * input, vlib_cli_command_t * cmd)
48 {
49   uword hash1, hash2;
50   u8 *baseaddr;
51   u8 *key_loc;
52
53   baseaddr = mmap (NULL, 8192, PROT_READ | PROT_WRITE,
54                    MAP_PRIVATE | MAP_ANONYMOUS, -1, 0 /* offset */ );
55
56   if (baseaddr == 0)
57     {
58       clib_unix_warning ("mmap");
59       return 0;
60     }
61
62   if (mprotect (baseaddr + (4 << 10), (4 << 10), PROT_NONE) < 0)
63     {
64       clib_unix_warning ("mprotect");
65       return 0;
66     }
67
68   key_loc = baseaddr + (4 << 10) - 4;
69   key_loc[0] = 0xde;
70   key_loc[1] = 0xad;
71   key_loc[2] = 0xbe;
72   key_loc[3] = 0xef;
73
74   hash1 = hash_memory (key_loc, 4, 0ULL);
75
76   vlib_cli_output (vm, "hash1 is %llx", hash1);
77
78   key_loc = baseaddr;
79
80   key_loc[0] = 0xde;
81   key_loc[1] = 0xad;
82   key_loc[2] = 0xbe;
83   key_loc[3] = 0xef;
84
85   hash2 = hash_memory (key_loc, 4, 0ULL);
86
87   vlib_cli_output (vm, "hash2 is %llx", hash2);
88
89   if (hash1 == hash2)
90     vlib_cli_output (vm, "PASS...");
91   else
92     vlib_cli_output (vm, "FAIL...");
93
94   return 0;
95 }
96
97 VLIB_CLI_COMMAND (test_hash_command, static) =
98 {
99   .path = "test hash_memory",
100   .short_help = "page boundary crossing test",
101   .function = test_hash_command_fn,
102 };
103
104 /*
105  * fd.io coding-style-patch-verification: ON
106  *
107  * Local Variables:
108  * eval: (c-set-style "gnu")
109  * End:
110  */