svm: minimal initial fifo
[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   /* *INDENT-OFF* */
26   ELOG_TYPE_DECLARE (e) =
27     {
28       .format = "deliberate crash: touching %x",
29       .format_args = "i4",
30     };
31   /* *INDENT-ON* */
32   elog (&vm->elog_main, &e, 0xdefec8ed);
33
34   *p = 0xdeadbeef;
35
36   /* Not so much... */
37   return 0;
38 }
39
40 /* *INDENT-OFF* */
41 VLIB_CLI_COMMAND (test_crash_command, static) =
42 {
43   .path = "test crash",
44   .short_help = "crash the bus!",
45   .function = test_crash_command_fn,
46 };
47 /* *INDENT-ON* */
48
49 static clib_error_t *
50 test_hash_command_fn (vlib_main_t * vm,
51                       unformat_input_t * input, vlib_cli_command_t * cmd)
52 {
53   uword hash1, hash2;
54   u8 *baseaddr;
55   u8 *key_loc;
56
57   baseaddr = mmap (NULL, 8192, PROT_READ | PROT_WRITE,
58                    MAP_PRIVATE | MAP_ANONYMOUS, -1, 0 /* offset */ );
59
60   if (baseaddr == 0)
61     {
62       clib_unix_warning ("mmap");
63       return 0;
64     }
65
66   if (mprotect (baseaddr + (4 << 10), (4 << 10), PROT_NONE) < 0)
67     {
68       clib_unix_warning ("mprotect");
69       return 0;
70     }
71
72   key_loc = baseaddr + (4 << 10) - 4;
73   key_loc[0] = 0xde;
74   key_loc[1] = 0xad;
75   key_loc[2] = 0xbe;
76   key_loc[3] = 0xef;
77
78   hash1 = hash_memory (key_loc, 4, 0ULL);
79
80   vlib_cli_output (vm, "hash1 is %llx", hash1);
81
82   key_loc = baseaddr;
83
84   key_loc[0] = 0xde;
85   key_loc[1] = 0xad;
86   key_loc[2] = 0xbe;
87   key_loc[3] = 0xef;
88
89   hash2 = hash_memory (key_loc, 4, 0ULL);
90
91   vlib_cli_output (vm, "hash2 is %llx", hash2);
92
93   if (hash1 == hash2)
94     vlib_cli_output (vm, "PASS...");
95   else
96     vlib_cli_output (vm, "FAIL...");
97
98   return 0;
99 }
100
101 /* *INDENT-OFF* */
102 VLIB_CLI_COMMAND (test_hash_command, static) =
103 {
104   .path = "test hash_memory",
105   .short_help = "page boundary crossing test",
106   .function = test_hash_command_fn,
107 };
108 /* *INDENT-ON* */
109
110 /*
111  * fd.io coding-style-patch-verification: ON
112  *
113  * Local Variables:
114  * eval: (c-set-style "gnu")
115  * End:
116  */