vlib: improve code coverage in src/vlib
[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
18 u8 *vlib_validate_buffers (vlib_main_t * vm,
19                            u32 * buffers,
20                            uword next_buffer_stride,
21                            uword n_buffers,
22                            vlib_buffer_known_state_t known_state,
23                            uword follow_buffer_next);
24
25 static clib_error_t *
26 test_vlib_command_fn (vlib_main_t * vm,
27                       unformat_input_t * input, vlib_cli_command_t * cmd)
28 {
29   u32 bi;
30   u8 *res;
31   u32 allocated;
32   vlib_buffer_t *b;
33   vlib_buffer_t *last_b;
34   u8 junk[4] = { 1, 2, 3, 4 };
35   vlib_packet_template_t _t, *t = &_t;
36   u8 *data_copy = 0;
37
38   /* Cover vlib_packet_template_get_packet */
39   t->packet_data = format (0, "silly packet data");
40   t->min_n_buffers_each_alloc = 1;
41   t->name = (u8 *) "test template";
42
43   if (vlib_packet_template_get_packet (vm, t, &bi))
44     vlib_buffer_free_one (vm, bi);
45
46   vec_free (t->packet_data);
47
48   /* Get a buffer */
49   allocated = vlib_buffer_alloc (vm, &bi, 1);
50   if (allocated != 1)
51     return clib_error_return (0, "Buffer allocation failure!");
52
53   b = vlib_get_buffer (vm, bi);
54
55   /* Force buffer allocation */
56   b->current_length = 2048;
57   last_b = b;
58   vlib_buffer_chain_append_data_with_alloc (vm, b, &last_b,
59                                             junk, ARRAY_LEN (junk));
60
61   /* Cover vlib_buffer_length_in_chain_slow_path(...) */
62   b->flags &= ~(VLIB_BUFFER_TOTAL_LENGTH_VALID);
63   vlib_cli_output (vm, "buffer length %d",
64                    vlib_buffer_length_in_chain (vm, b));
65   b->flags &= ~(VLIB_BUFFER_TOTAL_LENGTH_VALID);
66   vlib_cli_output (vm, "%u", vlib_buffer_index_length_in_chain (vm, bi));
67
68   /* Add more data. Eat Mor Chikin. */
69   vlib_buffer_add_data (vm, &bi, junk, ARRAY_LEN (junk));
70
71   /* Dump the resulting two-chunk pkt */
72   vlib_cli_output (vm, "%U", format_vlib_buffer_and_data, b);
73
74   vec_validate (data_copy, vlib_buffer_length_in_chain (vm, b) - 1);
75   vlib_cli_output (vm, "%u", vlib_buffer_contents (vm, bi, data_copy));
76   vec_free (data_copy);
77
78   /* Cover simple functions in buffer.h / buffer_funcs.h */
79   vlib_cli_output (vm, "%llx", vlib_buffer_get_va (b));
80   vlib_cli_output (vm, "%llx", vlib_buffer_get_current_va (b));
81   vlib_cli_output (vm, "%d", vlib_buffer_has_space (b, 100ll));
82   vlib_buffer_reset (b);
83   vlib_cli_output (vm, "%llx", vlib_buffer_get_tail (b));
84   vlib_buffer_put_uninit (b, 0);
85   vlib_buffer_push_uninit (b, 0);
86   vlib_buffer_make_headroom (b, 0);
87   (void) vlib_buffer_pull (b, 0);
88   vlib_cli_output (vm, "%llx", vlib_buffer_get_pa (vm, b));
89   vlib_cli_output (vm, "%llx", vlib_buffer_get_current_pa (vm, b));
90
91   /* Validate it one way */
92   res = vlib_validate_buffer (vm, bi, 1 /* follow_buffer_next */ );
93   if (res)
94     return clib_error_return (0, "%v", res);
95
96   /* Validate it a different way */
97   res = vlib_validate_buffers (vm, &bi, 0 /* stride */ ,
98                                1, VLIB_BUFFER_KNOWN_ALLOCATED,
99                                1 /* follow_buffer_next */ );
100   if (res)
101     return clib_error_return (0, "%v", res);
102
103   /* Free it */
104   vlib_buffer_free_one (vm, bi);
105   /* It will be free */
106   res = vlib_validate_buffers (vm, &bi, 0 /* stride */ ,
107                                1, VLIB_BUFFER_KNOWN_FREE,
108                                1 /* follow_buffer_next */ );
109   if (res)
110     return clib_error_return (0, "%v", res);
111
112   /* It will not be allocated, exercise error path */
113   res = vlib_validate_buffers (vm, &bi, 0 /* stride */ ,
114                                1, VLIB_BUFFER_KNOWN_ALLOCATED,
115                                1 /* follow_buffer_next */ );
116   if (res)
117     return clib_error_return (0, "%v", res);
118
119   return 0;
120 }
121
122 /* *INDENT-OFF* */
123 VLIB_CLI_COMMAND (test_vlib_command, static) =
124 {
125   .path = "test vlib",
126   .short_help = "vlib code coverate unit test",
127   .function = test_vlib_command_fn,
128 };
129 /* *INDENT-ON* */
130
131 /*
132  * fd.io coding-style-patch-verification: ON
133  *
134  * Local Variables:
135  * eval: (c-set-style "gnu")
136  * End:
137  */