svm: minimal initial fifo
[vpp.git] / src / plugins / unittest / test_buffer.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 <vlib/buffer_funcs.h>
18
19 #define TEST_I(_cond, _comment, _args...)                       \
20 ({                                                              \
21   int _evald = (_cond);                                         \
22   if (!(_evald)) {                                              \
23     fformat(stderr, "FAIL:%d: " _comment "\n",                  \
24             __LINE__, ##_args);                                 \
25   } else {                                                      \
26     fformat(stderr, "PASS:%d: " _comment "\n",                  \
27             __LINE__, ##_args);                                 \
28   }                                                             \
29   _evald;                                                       \
30 })
31
32 #define TEST(_cond, _comment, _args...)                 \
33 {                                                               \
34     if (!TEST_I(_cond, _comment, ##_args)) {            \
35         return 1;                                               \
36     }                                                           \
37 }
38
39 /* test function for a specific case where current_data is negative, verify
40  * that there is no crash */
41 static int
42 linearize_negative_current_data (vlib_main_t * vm)
43 {
44   u32 bi[32];
45   TEST (ARRAY_LEN (bi) == vlib_buffer_alloc (vm, bi, ARRAY_LEN (bi)),
46         "buff alloc");
47   u32 data_size = vlib_buffer_get_default_data_size (vm);
48   u32 i;
49   for (i = 0; i < ARRAY_LEN (bi) - 1; ++i)
50     {
51       vlib_buffer_t *b = vlib_get_buffer (vm, bi[i]);
52       b->next_buffer = bi[i + 1];
53       b->flags |= VLIB_BUFFER_NEXT_PRESENT;
54       b->current_data = -14;
55       b->current_length = 14 + data_size;
56     }
57
58   (void) vlib_buffer_chain_linearize (vm, vlib_get_buffer (vm, bi[0]));
59
60   return 0;
61 }
62
63 static clib_error_t *
64 test_linearize_fn (vlib_main_t * vm, unformat_input_t * input,
65                    vlib_cli_command_t * cmd)
66 {
67
68   if (linearize_negative_current_data (vm))
69     {
70       return clib_error_return (0, "linearize_negative_current_data failed");
71     }
72
73   return (NULL);
74 }
75
76 /* *INDENT-OFF* */
77 VLIB_CLI_COMMAND (test_linearize_command, static) =
78 {
79   .path = "test chained-buffer-linearization",
80   .short_help = "test chained-buffer-linearization",
81   .function = test_linearize_fn,
82 };
83 /* *INDENT-ON* */
84
85 /*
86  * fd.io coding-style-patch-verification: ON
87  *
88  * Local Variables:
89  * eval: (c-set-style "gnu")
90  * End:
91  */