LCOV - code coverage report
Current view: top level - lib/util/tests - test_byteorder_verify.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 143 144 99.3 %
Date: 2024-04-21 15:09:00 Functions: 9 9 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Unix SMB/CIFS implementation.
       3             :  *
       4             :  * Copyright (C) 2018-2019 Andreas Schneider <asn@samba.org>
       5             :  *
       6             :  * This program is free software; you can redistribute it and/or modify
       7             :  * it under the terms of the GNU General Public License as published by
       8             :  * the Free Software Foundation; either version 3 of the License, or
       9             :  * (at your option) any later version.
      10             :  *
      11             :  * This program is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :  * GNU General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU General Public License
      17             :  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
      18             :  */
      19             : 
      20             : #include <stdarg.h>
      21             : #include <stddef.h>
      22             : #include <stdint.h>
      23             : #include <setjmp.h>
      24             : #include <cmocka.h>
      25             : 
      26             : #include "lib/replace/replace.h"
      27             : #include "lib/util/bytearray.h"
      28             : #include "lib/util/byteorder.h"
      29             : 
      30           1 : static void torture_le_u8(void **state)
      31             : {
      32           1 :         uint8_t data[2] = {0};
      33           1 :         uint8_t result;
      34             : 
      35           1 :         (void)state;
      36             : 
      37             :         /* Test CVAL and SCVAL */
      38           1 :         PUSH_LE_U8(data, 0, 23);
      39           1 :         PUSH_LE_U8(data, 1, 42);
      40             : 
      41           1 :         result = CVAL(data, 0);
      42           1 :         assert_int_equal(result, 23);
      43             : 
      44           1 :         result = CVAL(data, 1);
      45           1 :         assert_int_equal(result, 42);
      46             : 
      47             :         /* Test CVAL_NC and PVAL */
      48           1 :         PUSH_LE_U8(data, 0, 23);
      49           1 :         PUSH_LE_U8(data, 1, 42);
      50             : 
      51           1 :         result = CVAL_NC(data, 0);
      52           1 :         assert_int_equal(result, 23);
      53             : 
      54           1 :         result = PVAL(data, 1);
      55           1 :         assert_int_equal(result, 42);
      56             : 
      57             :         /* Test SCVAL */
      58           1 :         SCVAL(data, 0, 42);
      59           1 :         SCVAL(data, 1, 23);
      60             : 
      61           1 :         result = PULL_LE_U8(data, 0);
      62           1 :         assert_int_equal(result, 42);
      63             : 
      64           1 :         result = PULL_LE_U8(data, 1);
      65           1 :         assert_int_equal(result, 23);
      66           1 : }
      67             : 
      68           1 : static void torture_le_u16(void **state)
      69             : {
      70           1 :         uint8_t data[2] = {0};
      71           1 :         uint16_t result;
      72             : 
      73           1 :         (void)state;
      74             : 
      75             :         /* Test SVAL */
      76           1 :         PUSH_LE_U16(data, 0, 0xff00);
      77           1 :         result = SVAL(data, 0);
      78           1 :         assert_int_equal(result, 0xff00);
      79             : 
      80             :         /* Test SSVAL */
      81           1 :         SSVAL(data, 0, 0x00ff);
      82           1 :         result = PULL_LE_U16(data, 0);
      83           1 :         assert_int_equal(result, 0x00ff);
      84             : 
      85             :         /* Test SSVALX */
      86           1 :         SSVALX(data, 0, 0x00fa);
      87           1 :         result = PULL_LE_U16(data, 0);
      88           1 :         assert_int_equal(result, 0x00fa);
      89             : 
      90             :         /* Test SSVALS */
      91           1 :         SSVALS(data, 0, 0x00fb);
      92           1 :         result = PULL_LE_U16(data, 0);
      93           1 :         assert_int_equal(result, 0x00fb);
      94           1 : }
      95             : 
      96           1 : static void torture_le_u32(void **state)
      97             : {
      98           1 :         uint8_t data[4] = {0};
      99           1 :         uint32_t result;
     100             : 
     101           1 :         (void)state;
     102             : 
     103             :         /* Test IVAL */
     104           1 :         PUSH_LE_U32(data, 0, 0xff000000);
     105           1 :         result = IVAL(data, 0);
     106           1 :         assert_int_equal(result, 0xff000000);
     107             : 
     108             :         /* Test SIVAL */
     109           1 :         SIVAL(data, 0, 0xffaabbcc);
     110           1 :         result = PULL_LE_U32(data, 0);
     111           1 :         assert_int_equal(result, 0xffaabbcc);
     112             : 
     113             :         /* Test SIVALX */
     114           1 :         SIVALX(data, 0, 0xffbbccdd);
     115           1 :         result = PULL_LE_U32(data, 0);
     116           1 :         assert_int_equal(result, 0xffbbccdd);
     117             : 
     118             :         /* Test SIVALS */
     119           1 :         SIVALS(data, 0, 0xffccddee);
     120           1 :         result = PULL_LE_U32(data, 0);
     121           1 :         assert_int_equal(result, 0xffccddee);
     122           1 : }
     123             : 
     124           1 : static void torture_le_u64(void **state)
     125             : {
     126           1 :         uint8_t data[8] = {0};
     127           1 :         uint64_t result;
     128             : 
     129           1 :         (void)state;
     130             : 
     131           1 :         PUSH_LE_U64(data, 0, 0xfffefffefffefffeUL);
     132           1 :         result = BVAL(data, 0);
     133           1 :         assert_int_equal(result, 0xfffefffefffefffeUL);
     134             : 
     135           1 :         SBVAL(data, 0, 0xfffafffafffafffaUL);
     136           1 :         result = PULL_LE_U64(data, 0);
     137           1 :         assert_int_equal(result, 0xfffafffafffafffaUL);
     138           1 : }
     139             : 
     140           1 : static void torture_be_u8(void **state)
     141             : {
     142           1 :         uint8_t data[2] = {0};
     143           1 :         uint8_t result;
     144             : 
     145           1 :         (void)state;
     146             : 
     147           1 :         PUSH_BE_U8(data, 0, 23);
     148           1 :         PUSH_BE_U8(data, 1, 42);
     149             : 
     150           1 :         result = CVAL(data, 0);
     151           1 :         assert_int_equal(result, 23);
     152             : 
     153           1 :         result = CVAL(data, 1);
     154           1 :         assert_int_equal(result, 42);
     155             : 
     156           1 :         SCVAL(data, 0, 42);
     157           1 :         SCVAL(data, 1, 23);
     158             : 
     159           1 :         result = PULL_BE_U8(data, 0);
     160           1 :         assert_int_equal(result, 42);
     161             : 
     162           1 :         result = PULL_BE_U8(data, 1);
     163           1 :         assert_int_equal(result, 23);
     164           1 : }
     165             : 
     166           1 : static void torture_be_u16(void **state)
     167             : {
     168           1 :         uint8_t data[2] = {0};
     169           1 :         uint16_t result;
     170             : 
     171           1 :         (void)state;
     172             : 
     173             :         /* Test RSVAL */
     174           1 :         PUSH_BE_U16(data, 0, 0xff00);
     175           1 :         result = RSVAL(data, 0);
     176           1 :         assert_int_equal(result, 0xff00);
     177             : 
     178             :         /* Test RSVALS */
     179           1 :         PUSH_BE_U16(data, 0, 0xffaa);
     180           1 :         result = RSVALS(data, 0);
     181           1 :         assert_int_equal(result, 0xffaa);
     182             : 
     183             :         /* Test RSSVAL */
     184           1 :         RSSVAL(data, 0, 0x00ff);
     185           1 :         result = PULL_BE_U16(data, 0);
     186           1 :         assert_int_equal(result, 0x00ff);
     187             : 
     188             :         /* Test RSSVALS */
     189           1 :         RSSVALS(data, 0, 0x00fa);
     190           1 :         result = PULL_BE_U16(data, 0);
     191           1 :         assert_int_equal(result, 0x00fa);
     192           1 : }
     193             : 
     194           1 : static void torture_be_u32(void **state)
     195             : {
     196           1 :         uint8_t data[4] = {0};
     197           1 :         uint32_t result;
     198             : 
     199           1 :         (void)state;
     200             : 
     201             :         /* Test RIVAL */
     202           1 :         PUSH_BE_U32(data, 0, 0xff000000);
     203           1 :         result = RIVAL(data, 0);
     204           1 :         assert_int_equal(result, 0xff000000);
     205             : 
     206             :         /* Test RIVALS */
     207           1 :         PUSH_BE_U32(data, 0, 0xff0000aa);
     208           1 :         result = RIVALS(data, 0);
     209           1 :         assert_int_equal(result, 0xff0000aa);
     210             : 
     211             :         /* Test RSIVAL */
     212           1 :         RSIVAL(data, 0, 0xffeeddcc);
     213           1 :         result = PULL_BE_U32(data, 0);
     214           1 :         assert_int_equal(result, 0xffeeddcc);
     215             : 
     216             :         /* Test RSIVALS */
     217           1 :         RSIVALS(data, 0, 0xffaaddcc);
     218           1 :         result = PULL_BE_U32(data, 0);
     219           1 :         assert_int_equal(result, 0xffaaddcc);
     220           1 : }
     221             : 
     222           1 : static void torture_be_u64(void **state)
     223             : {
     224           1 :         uint8_t data[8] = {0};
     225           1 :         uint64_t result;
     226             : 
     227           1 :         (void)state;
     228             : 
     229             :         /* Test RBVAL */
     230           1 :         PUSH_BE_U64(data, 0, 0xfffefffefffefffeUL);
     231           1 :         result = RBVAL(data, 0);
     232           1 :         assert_int_equal(result, 0xfffefffefffefffeUL);
     233             : 
     234             :         /* Test RBVALS */
     235           1 :         PUSH_BE_U64(data, 0, 0xfffafffafffafffaUL);
     236           1 :         result = RBVALS(data, 0);
     237           1 :         assert_int_equal(result, 0xfffafffafffafffaUL);
     238             : 
     239             :         /* Test RSBVAL */
     240           1 :         RSBVAL(data, 0, 0xfffbfffbfffbfffbUL);
     241           1 :         result = PULL_BE_U64(data, 0);
     242           1 :         assert_int_equal(result, 0xfffbfffbfffbfffbUL);
     243             : 
     244             :         /* Test RSBVALS */
     245           1 :         RSBVALS(data, 0, 0xfffcfffcfffcfffcUL);
     246           1 :         result = PULL_BE_U64(data, 0);
     247           1 :         assert_int_equal(result, 0xfffcfffcfffcfffcUL);
     248           1 : }
     249             : 
     250           1 : int main(int argc, char *argv[])
     251             : {
     252           1 :         int rc;
     253           1 :         const struct CMUnitTest tests[] = {
     254             :                 cmocka_unit_test(torture_le_u8),
     255             :                 cmocka_unit_test(torture_le_u16),
     256             :                 cmocka_unit_test(torture_le_u32),
     257             :                 cmocka_unit_test(torture_le_u64),
     258             : 
     259             :                 cmocka_unit_test(torture_be_u8),
     260             :                 cmocka_unit_test(torture_be_u16),
     261             :                 cmocka_unit_test(torture_be_u32),
     262             :                 cmocka_unit_test(torture_be_u64),
     263             :         };
     264             : 
     265           1 :         if (argc == 2) {
     266           0 :                 cmocka_set_test_filter(argv[1]);
     267             :         }
     268           1 :         cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
     269             : 
     270           1 :         rc = cmocka_run_group_tests(tests, NULL, NULL);
     271             : 
     272           1 :         return rc;
     273             : }

Generated by: LCOV version 1.14