vlib: improve code coverage, part deux
[vpp.git] / src / plugins / unittest / vlib_test.c
1 /*
2  * Copyright (c) 2019 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 <vnet/vnet.h>
18
19 u8 *vlib_validate_buffers (vlib_main_t * vm,
20                            u32 * buffers,
21                            uword next_buffer_stride,
22                            uword n_buffers,
23                            vlib_buffer_known_state_t known_state,
24                            uword follow_buffer_next);
25
26 static clib_error_t *
27 test_vlib_command_fn (vlib_main_t * vm,
28                       unformat_input_t * input, vlib_cli_command_t * cmd)
29 {
30   u32 bi;
31   u8 *res;
32   u32 allocated;
33   vlib_buffer_t *b;
34   vlib_buffer_t *last_b;
35   u8 junk[4] = { 1, 2, 3, 4 };
36   vlib_packet_template_t _t, *t = &_t;
37   u8 *data_copy = 0;
38   vnet_main_t *vnm = vnet_get_main ();
39   vnet_interface_main_t *im = &vnm->interface_main;
40
41   /* Cover vlib_packet_template_get_packet */
42   t->packet_data = format (0, "silly packet data");
43   t->min_n_buffers_each_alloc = 1;
44   t->name = (u8 *) "test template";
45
46   if (vlib_packet_template_get_packet (vm, t, &bi))
47     vlib_buffer_free_one (vm, bi);
48
49   vec_free (t->packet_data);
50
51   /* Get a buffer */
52   allocated = vlib_buffer_alloc (vm, &bi, 1);
53   if (allocated != 1)
54     return clib_error_return (0, "Buffer allocation failure!");
55
56   b = vlib_get_buffer (vm, bi);
57
58   /* Force buffer allocation */
59   b->current_length = 2048;
60   last_b = b;
61   vlib_buffer_chain_append_data_with_alloc (vm, b, &last_b,
62                                             junk, ARRAY_LEN (junk));
63
64   /* Cover vlib_buffer_length_in_chain_slow_path(...) */
65   b->flags &= ~(VLIB_BUFFER_TOTAL_LENGTH_VALID);
66   vlib_cli_output (vm, "buffer length %d",
67                    vlib_buffer_length_in_chain (vm, b));
68   b->flags &= ~(VLIB_BUFFER_TOTAL_LENGTH_VALID);
69   vlib_cli_output (vm, "%u", vlib_buffer_index_length_in_chain (vm, bi));
70
71   /* Add more data. Eat Mor Chikin. */
72   vlib_buffer_add_data (vm, &bi, junk, ARRAY_LEN (junk));
73
74   /* Dump the resulting two-chunk pkt */
75   vlib_cli_output (vm, "%U", format_vlib_buffer_and_data, b);
76   vlib_cli_output (vm, "%U", format_vlib_buffer_data, b->data, 17);
77
78   vec_validate (data_copy, vlib_buffer_length_in_chain (vm, b) - 1);
79   vlib_cli_output (vm, "%u", vlib_buffer_contents (vm, bi, data_copy));
80   vec_free (data_copy);
81
82   /* Cover simple functions in buffer.h / buffer_funcs.h */
83   vlib_cli_output (vm, "%llx", vlib_buffer_get_va (b));
84   vlib_cli_output (vm, "%llx", vlib_buffer_get_current_va (b));
85   vlib_cli_output (vm, "%d", vlib_buffer_has_space (b, 100ll));
86   vlib_buffer_reset (b);
87   vlib_cli_output (vm, "%llx", vlib_buffer_get_tail (b));
88   vlib_buffer_put_uninit (b, 0);
89   vlib_buffer_push_uninit (b, 0);
90   vlib_buffer_make_headroom (b, 0);
91   (void) vlib_buffer_pull (b, 0);
92   vlib_cli_output (vm, "%llx", vlib_buffer_get_pa (vm, b));
93   vlib_cli_output (vm, "%llx", vlib_buffer_get_current_pa (vm, b));
94
95   /* Validate it one way */
96   res = vlib_validate_buffer (vm, bi, 1 /* follow_buffer_next */ );
97   if (res)
98     return clib_error_return (0, "%v", res);
99
100   /* Validate it a different way */
101   res = vlib_validate_buffers (vm, &bi, 0 /* stride */ ,
102                                1, VLIB_BUFFER_KNOWN_ALLOCATED,
103                                1 /* follow_buffer_next */ );
104   if (res)
105     return clib_error_return (0, "%v", res);
106
107   /* Free it */
108   vlib_buffer_free_one (vm, bi);
109   /* It will be free */
110   res = vlib_validate_buffers (vm, &bi, 0 /* stride */ ,
111                                1, VLIB_BUFFER_KNOWN_FREE,
112                                1 /* follow_buffer_next */ );
113   if (res)
114     return clib_error_return (0, "%v", res);
115
116   /* Misc */
117   vlib_cli_output
118     (vm, "%u",
119      vlib_combined_counter_n_counters (im->combined_sw_if_counters));
120
121   /* buffer will not be allocated at this point, exercise error path */
122   res = vlib_validate_buffers (vm, &bi, 0 /* stride */ ,
123                                1, VLIB_BUFFER_KNOWN_ALLOCATED,
124                                1 /* follow_buffer_next */ );
125   if (res)
126     return clib_error_return (0, "%v", res);
127
128   /* NOTREACHED */
129   return 0;
130 }
131
132 /* *INDENT-OFF* */
133 VLIB_CLI_COMMAND (test_vlib_command, static) =
134 {
135   .path = "test vlib",
136   .short_help = "vlib code coverate unit test",
137   .function = test_vlib_command_fn,
138 };
139 /* *INDENT-ON* */
140
141 static clib_error_t *
142 test_format_vlib_command_fn (vlib_main_t * vm,
143                              unformat_input_t * input,
144                              vlib_cli_command_t * cmd)
145 {
146   unformat_input_t _i, *i = &_i;
147   int enable = -1, disable = -1;
148   int twenty_seven = -1;;
149   int rxtx = -1;
150
151   memset (i, 0, sizeof (*i));
152   unformat_init_string (i, "enable disable rx tx 27", 23);
153
154   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
155     {
156       if (unformat (i, "%U", unformat_vlib_enable_disable, &enable))
157         ;
158       else if (unformat (i, "%U", unformat_vlib_enable_disable, &disable))
159         ;
160       else if (unformat (i, "%U", unformat_vlib_number, &twenty_seven))
161         ;
162       else if (unformat (i, "%U", unformat_vlib_rx_tx, &rxtx))
163         ;
164       else
165         break;
166     }
167
168   rxtx = VLIB_TX;
169   vlib_cli_output (vm, "%U", format_vlib_read_write, rxtx);
170   vlib_cli_output (vm, "%U", format_vlib_rx_tx, rxtx);
171
172   rxtx = VLIB_RX;
173   vlib_cli_output (vm, "%U", format_vlib_read_write, rxtx);
174   vlib_cli_output (vm, "%U", format_vlib_rx_tx, rxtx);
175   rxtx = 12345;
176   vlib_cli_output (vm, "%U", format_vlib_read_write, rxtx);
177   vlib_cli_output (vm, "%U", format_vlib_rx_tx, rxtx);
178
179   unformat_free (i);
180   return 0;
181 }
182
183 /* *INDENT-OFF* */
184 VLIB_CLI_COMMAND (test_format_vlib_command, static) =
185 {
186   .path = "test format-vlib",
187   .short_help = "vlib format code coverate unit test",
188   .function = test_format_vlib_command_fn,
189 };
190 /* *INDENT-ON* */
191
192 /*
193  * fd.io coding-style-patch-verification: ON
194  *
195  * Local Variables:
196  * eval: (c-set-style "gnu")
197  * End:
198  */