LCOV - code coverage report
Current view: top level - plugins/unittest - vlib_test.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 3 98 3.1 %
Date: 2023-07-05 22:20:52 Functions: 6 9 66.7 %

          Line data    Source code
       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           0 : 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           0 :   u8 junk[4] = { 1, 2, 3, 4 };
      36           0 :   vlib_packet_template_t _t, *t = &_t;
      37           0 :   u8 *data_copy = 0;
      38           0 :   vnet_main_t *vnm = vnet_get_main ();
      39           0 :   vnet_interface_main_t *im = &vnm->interface_main;
      40             : 
      41             :   /* Cover vlib_packet_template_get_packet */
      42           0 :   t->packet_data = format (0, "silly packet data");
      43           0 :   t->min_n_buffers_each_alloc = 1;
      44           0 :   t->name = (u8 *) "test template";
      45             : 
      46           0 :   if (vlib_packet_template_get_packet (vm, t, &bi))
      47           0 :     vlib_buffer_free_one (vm, bi);
      48             : 
      49           0 :   vec_free (t->packet_data);
      50             : 
      51             :   /* Get a buffer */
      52           0 :   allocated = vlib_buffer_alloc (vm, &bi, 1);
      53           0 :   if (allocated != 1)
      54           0 :     return clib_error_return (0, "Buffer allocation failure!");
      55             : 
      56           0 :   b = vlib_get_buffer (vm, bi);
      57             : 
      58             :   /* Force buffer allocation */
      59           0 :   b->current_length = 2048;
      60           0 :   last_b = b;
      61           0 :   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           0 :   b->flags &= ~(VLIB_BUFFER_TOTAL_LENGTH_VALID);
      66           0 :   vlib_cli_output (vm, "buffer length %d",
      67             :                    vlib_buffer_length_in_chain (vm, b));
      68           0 :   b->flags &= ~(VLIB_BUFFER_TOTAL_LENGTH_VALID);
      69           0 :   vlib_cli_output (vm, "%u", vlib_buffer_index_length_in_chain (vm, bi));
      70             : 
      71             :   /* Add more data. Eat Mor Chikin. */
      72           0 :   vlib_buffer_add_data (vm, &bi, junk, ARRAY_LEN (junk));
      73             : 
      74             :   /* Dump the resulting two-chunk pkt */
      75           0 :   vlib_cli_output (vm, "%U", format_vlib_buffer_and_data, b);
      76           0 :   vlib_cli_output (vm, "%U", format_vlib_buffer_data, b->data, 17);
      77             : 
      78           0 :   vec_validate (data_copy, vlib_buffer_length_in_chain (vm, b) - 1);
      79           0 :   vlib_cli_output (vm, "%u", vlib_buffer_contents (vm, bi, data_copy));
      80           0 :   vec_free (data_copy);
      81             : 
      82             :   /* Cover simple functions in buffer.h / buffer_funcs.h */
      83           0 :   vlib_cli_output (vm, "%llx", vlib_buffer_get_va (b));
      84           0 :   vlib_cli_output (vm, "%llx", vlib_buffer_get_current_va (b));
      85           0 :   vlib_cli_output (vm, "%d", vlib_buffer_has_space (b, 100ll));
      86           0 :   vlib_buffer_reset (b);
      87           0 :   vlib_cli_output (vm, "%llx", vlib_buffer_get_tail (b));
      88           0 :   vlib_buffer_put_uninit (b, 0);
      89           0 :   vlib_buffer_push_uninit (b, 0);
      90           0 :   vlib_buffer_make_headroom (b, 0);
      91           0 :   (void) vlib_buffer_pull (b, 0);
      92           0 :   vlib_cli_output (vm, "%llx", vlib_buffer_get_pa (vm, b));
      93           0 :   vlib_cli_output (vm, "%llx", vlib_buffer_get_current_pa (vm, b));
      94             : 
      95             :   /* Validate it one way */
      96           0 :   res = vlib_validate_buffer (vm, bi, 1 /* follow_buffer_next */ );
      97           0 :   if (res)
      98           0 :     return clib_error_return (0, "%v", res);
      99             : 
     100             :   /* Validate it a different way */
     101           0 :   res = vlib_validate_buffers (vm, &bi, 0 /* stride */ ,
     102             :                                1, VLIB_BUFFER_KNOWN_ALLOCATED,
     103             :                                1 /* follow_buffer_next */ );
     104           0 :   if (res)
     105           0 :     return clib_error_return (0, "%v", res);
     106             : 
     107             :   /* Free it */
     108           0 :   vlib_buffer_free_one (vm, bi);
     109             :   /* It will be free */
     110           0 :   res = vlib_validate_buffers (vm, &bi, 0 /* stride */ ,
     111             :                                1, VLIB_BUFFER_KNOWN_FREE,
     112             :                                1 /* follow_buffer_next */ );
     113           0 :   if (res)
     114           0 :     return clib_error_return (0, "%v", res);
     115             : 
     116             :   /* Misc */
     117           0 :   vlib_cli_output
     118             :     (vm, "%u",
     119           0 :      vlib_combined_counter_n_counters (im->combined_sw_if_counters));
     120             : 
     121             :   /* buffer will not be allocated at this point, exercise error path */
     122           0 :   res = vlib_validate_buffers (vm, &bi, 0 /* stride */ ,
     123             :                                1, VLIB_BUFFER_KNOWN_ALLOCATED,
     124             :                                1 /* follow_buffer_next */ );
     125           0 :   if (res)
     126           0 :     return clib_error_return (0, "%v", res);
     127             : 
     128             :   /* NOTREACHED */
     129           0 :   return 0;
     130             : }
     131             : 
     132             : /* *INDENT-OFF* */
     133       16239 : VLIB_CLI_COMMAND (test_vlib_command, static) =
     134             : {
     135             :   .path = "test vlib",
     136             :   .short_help = "vlib code coverage unit test",
     137             :   .function = test_vlib_command_fn,
     138             : };
     139             : /* *INDENT-ON* */
     140             : 
     141             : static clib_error_t *
     142           0 : test_format_vlib_command_fn (vlib_main_t * vm,
     143             :                              unformat_input_t * input,
     144             :                              vlib_cli_command_t * cmd)
     145             : {
     146           0 :   unformat_input_t _i, *i = &_i;
     147           0 :   int enable = -1, disable = -1;
     148           0 :   int twenty_seven = -1;;
     149           0 :   int rxtx = -1;
     150             : 
     151           0 :   memset (i, 0, sizeof (*i));
     152           0 :   unformat_init_string (i, "enable disable rx tx 27", 23);
     153             : 
     154           0 :   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
     155             :     {
     156           0 :       if (unformat (i, "%U", unformat_vlib_enable_disable, &enable))
     157             :         ;
     158           0 :       else if (unformat (i, "%U", unformat_vlib_enable_disable, &disable))
     159             :         ;
     160           0 :       else if (unformat (i, "%U", unformat_vlib_number, &twenty_seven))
     161             :         ;
     162           0 :       else if (unformat (i, "%U", unformat_vlib_rx_tx, &rxtx))
     163             :         ;
     164             :       else
     165           0 :         break;
     166             :     }
     167             : 
     168           0 :   rxtx = VLIB_TX;
     169           0 :   vlib_cli_output (vm, "%U", format_vlib_read_write, rxtx);
     170           0 :   vlib_cli_output (vm, "%U", format_vlib_rx_tx, rxtx);
     171             : 
     172           0 :   rxtx = VLIB_RX;
     173           0 :   vlib_cli_output (vm, "%U", format_vlib_read_write, rxtx);
     174           0 :   vlib_cli_output (vm, "%U", format_vlib_rx_tx, rxtx);
     175           0 :   rxtx = 12345;
     176           0 :   vlib_cli_output (vm, "%U", format_vlib_read_write, rxtx);
     177           0 :   vlib_cli_output (vm, "%U", format_vlib_rx_tx, rxtx);
     178             : 
     179           0 :   unformat_free (i);
     180           0 :   return 0;
     181             : }
     182             : 
     183             : /* *INDENT-OFF* */
     184       16239 : 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             : static clib_error_t *
     193           0 : test_vlib2_command_fn (vlib_main_t * vm,
     194             :                        unformat_input_t * input, vlib_cli_command_t * cmd)
     195             : {
     196             :   u8 *s;
     197             :   u8 **result;
     198             : 
     199           0 :   s = format (0, "show       ");
     200           0 :   result = vlib_cli_get_possible_completions (s);
     201           0 :   vec_free (result);
     202           0 :   vec_free (s);
     203             : 
     204           0 :   s = 0;
     205           0 :   vec_add1 (s, 0);
     206           0 :   result = vlib_cli_get_possible_completions (s);
     207           0 :   vec_free (result);
     208           0 :   vec_free (s);
     209             : 
     210           0 :   s = format (0, "show            ?");
     211           0 :   result = vlib_cli_get_possible_completions (s);
     212           0 :   vec_free (result);
     213           0 :   vec_free (s);
     214             : 
     215           0 :   return 0;
     216             : }
     217             : 
     218             : /* *INDENT-OFF* */
     219       16239 : VLIB_CLI_COMMAND (test_vlib2_command, static) =
     220             : {
     221             :   .path = "test vlib2",
     222             :   .short_help = "vlib code coverage unit test #2",
     223             :   .function = test_vlib2_command_fn,
     224             : };
     225             : /* *INDENT-ON* */
     226             : 
     227             : 
     228             : 
     229             : 
     230             : 
     231             : /*
     232             :  * fd.io coding-style-patch-verification: ON
     233             :  *
     234             :  * Local Variables:
     235             :  * eval: (c-set-style "gnu")
     236             :  * End:
     237             :  */

Generated by: LCOV version 1.14