LCOV - code coverage report
Current view: top level - plugins/unittest - util_test.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 2 33 6.1 %
Date: 2023-07-05 22:20:52 Functions: 4 6 66.7 %

          Line data    Source code
       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           0 : test_crash_command_fn (vlib_main_t * vm,
      21             :                        unformat_input_t * input, vlib_cli_command_t * cmd)
      22             : {
      23           0 :   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           0 :   elog (&vlib_global_main.elog_main, &e, 0xdefec8ed);
      33             : 
      34           0 :   *p = 0xdeadbeef;
      35             : 
      36             :   /* Not so much... */
      37           0 :   return 0;
      38             : }
      39             : 
      40             : /* *INDENT-OFF* */
      41       16239 : 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           0 : 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           0 :   baseaddr = mmap (NULL, 8192, PROT_READ | PROT_WRITE,
      58             :                    MAP_PRIVATE | MAP_ANONYMOUS, -1, 0 /* offset */ );
      59             : 
      60           0 :   if (baseaddr == 0)
      61             :     {
      62           0 :       clib_unix_warning ("mmap");
      63           0 :       return 0;
      64             :     }
      65             : 
      66           0 :   if (mprotect (baseaddr + (4 << 10), (4 << 10), PROT_NONE) < 0)
      67             :     {
      68           0 :       clib_unix_warning ("mprotect");
      69           0 :       return 0;
      70             :     }
      71             : 
      72           0 :   key_loc = baseaddr + (4 << 10) - 4;
      73           0 :   key_loc[0] = 0xde;
      74           0 :   key_loc[1] = 0xad;
      75           0 :   key_loc[2] = 0xbe;
      76           0 :   key_loc[3] = 0xef;
      77             : 
      78           0 :   hash1 = hash_memory (key_loc, 4, 0ULL);
      79             : 
      80           0 :   vlib_cli_output (vm, "hash1 is %llx", hash1);
      81             : 
      82           0 :   key_loc = baseaddr;
      83             : 
      84           0 :   key_loc[0] = 0xde;
      85           0 :   key_loc[1] = 0xad;
      86           0 :   key_loc[2] = 0xbe;
      87           0 :   key_loc[3] = 0xef;
      88             : 
      89           0 :   hash2 = hash_memory (key_loc, 4, 0ULL);
      90             : 
      91           0 :   vlib_cli_output (vm, "hash2 is %llx", hash2);
      92             : 
      93           0 :   if (hash1 == hash2)
      94           0 :     vlib_cli_output (vm, "PASS...");
      95             :   else
      96           0 :     vlib_cli_output (vm, "FAIL...");
      97             : 
      98           0 :   return 0;
      99             : }
     100             : 
     101             : /* *INDENT-OFF* */
     102       16239 : 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             :  */

Generated by: LCOV version 1.14