LCOV - code coverage report
Current view: top level - lib/util/tests - test_byteorder.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 277 278 99.6 %
Date: 2024-04-21 15:09:00 Functions: 15 15 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/byteorder.h"
      28             : 
      29           1 : static void torture_pull_le_u8(void **state)
      30             : {
      31           1 :         uint8_t data[2] = {0};
      32           1 :         uint8_t result;
      33             : 
      34           1 :         (void)state;
      35             : 
      36           1 :         result = CVAL(data, 0);
      37           1 :         assert_int_equal(result, 0);
      38             : 
      39           1 :         data[0] = 0x2a;
      40           1 :         result = CVAL(data, 0);
      41           1 :         assert_int_equal(result, 42);
      42             : 
      43             : 
      44           1 :         data[0] = 0xf;
      45           1 :         result = CVAL(data, 0);
      46           1 :         assert_int_equal(result, 0xf);
      47             : 
      48           1 :         data[0] = 0xff;
      49           1 :         result = CVAL(data, 0);
      50           1 :         assert_int_equal(result, 0xff);
      51             : 
      52           1 :         data[1] = 0x2a;
      53           1 :         result = CVAL(data, 1);
      54           1 :         assert_int_equal(result, 42);
      55           1 : }
      56             : 
      57           1 : static void torture_pull_le_u16(void **state)
      58             : {
      59           1 :         uint8_t data[2] = {0, 0};
      60           1 :         uint16_t result;
      61             : 
      62           1 :         (void)state;
      63             : 
      64           1 :         result = SVAL(data, 0);
      65           1 :         assert_int_equal(result, 0);
      66             : 
      67           1 :         data[0] = 0x2a;
      68           1 :         data[1] = 0x00;
      69           1 :         result = SVAL(data, 0);
      70           1 :         assert_int_equal(result, 42);
      71             : 
      72           1 :         data[0] = 0xff;
      73           1 :         data[1] = 0x00;
      74           1 :         result = SVAL(data, 0);
      75           1 :         assert_int_equal(result, 0x00ff);
      76             : 
      77           1 :         data[0] = 0x00;
      78           1 :         data[1] = 0xff;
      79           1 :         result = SVAL(data, 0);
      80           1 :         assert_int_equal(result, 0xff00);
      81             : 
      82           1 :         data[0] = 0xff;
      83           1 :         data[1] = 0xff;
      84           1 :         result = SVAL(data, 0);
      85           1 :         assert_int_equal(result, 0xffff);
      86           1 : }
      87             : 
      88           1 : static void torture_pull_le_u32(void **state)
      89             : {
      90           1 :         uint8_t data[4] = {0, 0, 0, 0};
      91           1 :         uint32_t result;
      92             : 
      93           1 :         (void)state;
      94             : 
      95           1 :         result = IVAL(data, 0);
      96           1 :         assert_int_equal(result, 0);
      97             : 
      98           1 :         data[0] = 0x2a;
      99           1 :         data[1] = 0x00;
     100           1 :         data[2] = 0x00;
     101           1 :         data[3] = 0x00;
     102           1 :         result = IVAL(data, 0);
     103           1 :         assert_int_equal(result, 42);
     104             : 
     105           1 :         data[0] = 0xff;
     106           1 :         data[1] = 0x00;
     107           1 :         data[2] = 0x00;
     108           1 :         data[3] = 0x00;
     109           1 :         result = IVAL(data, 0);
     110           1 :         assert_int_equal(result, 0x00ff);
     111             : 
     112           1 :         data[0] = 0x00;
     113           1 :         data[1] = 0xff;
     114           1 :         data[2] = 0x00;
     115           1 :         data[3] = 0x00;
     116           1 :         result = IVAL(data, 0);
     117           1 :         assert_int_equal(result, 0xff00);
     118             : 
     119           1 :         data[0] = 0x00;
     120           1 :         data[1] = 0x00;
     121           1 :         data[2] = 0xff;
     122           1 :         data[3] = 0x00;
     123           1 :         result = IVAL(data, 0);
     124           1 :         assert_int_equal(result, 0xff0000);
     125             : 
     126           1 :         data[0] = 0x00;
     127           1 :         data[1] = 0x00;
     128           1 :         data[2] = 0x00;
     129           1 :         data[3] = 0xff;
     130           1 :         result = IVAL(data, 0);
     131           1 :         assert_int_equal(result, 0xff000000);
     132             : 
     133           1 :         data[0] = 0xff;
     134           1 :         data[1] = 0xff;
     135           1 :         data[2] = 0xff;
     136           1 :         data[3] = 0xff;
     137           1 :         result = IVAL(data, 0);
     138           1 :         assert_int_equal(result, 0xffffffff);
     139           1 : }
     140             : 
     141           1 : static void torture_push_le_u8(void **state)
     142             : {
     143           1 :         uint8_t data[4] = {0, 0, 0, 0};
     144           1 :         uint8_t data2[4] = {42, 42, 42, 42};
     145             : 
     146           1 :         (void)state;
     147             : 
     148           1 :         SCVAL(data, 0, 42);
     149           1 :         SCVAL(data, 1, 42);
     150           1 :         SCVAL(data, 2, 42);
     151           1 :         SCVAL(data, 3, 42);
     152           1 :         assert_memory_equal(data, data2, sizeof(data));
     153           1 : }
     154             : 
     155           1 : static void torture_push_le_u16(void **state)
     156             : {
     157           1 :         uint8_t data[4] = {0, 0, 0, 0};
     158           1 :         uint8_t data2[4] = {0xa6, 0x7f, 0x2a, 0x00};
     159           1 :         uint16_t result;
     160             : 
     161           1 :         (void)state;
     162             : 
     163           1 :         SSVALX(data, 0, 32678);
     164           1 :         SSVALX(data, 2, 42);
     165           1 :         assert_memory_equal(data, data2, sizeof(data));
     166             : 
     167           1 :         result = SVAL(data, 2);
     168           1 :         assert_int_equal(result, 42);
     169             : 
     170           1 :         result = SVAL(data, 0);
     171           1 :         assert_int_equal(result, 32678);
     172           1 : }
     173             : 
     174           1 : static void torture_push_le_u32(void **state)
     175             : {
     176           1 :         uint8_t data[8] = {0};
     177           1 :         uint8_t data2[8] = {0xa6, 0x7f, 0x00, 0x00, 0x2a, 0x00, 0x00, 0x00};
     178           1 :         uint32_t result;
     179             : 
     180           1 :         (void)state;
     181             : 
     182           1 :         SIVALX(data, 0, 32678);
     183           1 :         SIVALX(data, 4, 42);
     184           1 :         assert_memory_equal(data, data2, sizeof(data));
     185             : 
     186           1 :         result = IVAL(data, 4);
     187           1 :         assert_int_equal(result, 42);
     188             : 
     189           1 :         result = IVAL(data, 0);
     190           1 :         assert_int_equal(result, 32678);
     191             : 
     192           1 :         SIVALX(data, 0, 0xfffefffe);
     193           1 :         result = IVAL(data, 0);
     194           1 :         assert_int_equal(result, 0xfffefffe);
     195           1 : }
     196             : 
     197           1 : static void torture_push_le_u64(void **state)
     198             : {
     199           1 :         uint8_t data[16] = {0};
     200           1 :         uint64_t result;
     201             : 
     202           1 :         (void)state;
     203             : 
     204           1 :         SBVAL(data, 0, 32678);
     205             : 
     206           1 :         result = BVAL(data, 0);
     207           1 :         assert_int_equal(result, 32678);
     208             : 
     209           1 :         SBVAL(data, 0, 0xfffefffefffefffeUL);
     210             : 
     211           1 :         result = BVAL(data, 0);
     212           1 :         assert_int_equal(result, 0xfffefffefffefffeUL);
     213           1 : }
     214             : 
     215             : /****************** BIG ENDIAN ********************/
     216             : 
     217           1 : static void torture_pull_be_u8(void **state)
     218             : {
     219           1 :         uint8_t data[2] = {0};
     220           1 :         uint8_t result;
     221             : 
     222           1 :         (void)state;
     223             : 
     224           1 :         result = CVAL(data, 0);
     225           1 :         assert_int_equal(result, 0);
     226             : 
     227           1 :         data[0] = 0x2a;
     228           1 :         result = CVAL(data, 0);
     229           1 :         assert_int_equal(result, 42);
     230             : 
     231             : 
     232           1 :         data[0] = 0xf;
     233           1 :         result = CVAL(data, 0);
     234           1 :         assert_int_equal(result, 0xf);
     235             : 
     236           1 :         data[0] = 0xff;
     237           1 :         result = CVAL(data, 0);
     238           1 :         assert_int_equal(result, 0xff);
     239             : 
     240           1 :         data[1] = 0x2a;
     241           1 :         result = CVAL(data, 1);
     242           1 :         assert_int_equal(result, 42);
     243           1 : }
     244             : 
     245           1 : static void torture_pull_be_u16(void **state)
     246             : {
     247           1 :         uint8_t data[2] = {0, 0};
     248           1 :         uint16_t result;
     249             : 
     250           1 :         (void)state;
     251             : 
     252           1 :         result = RSVAL(data, 0);
     253           1 :         assert_int_equal(result, 0);
     254             : 
     255           1 :         data[0] = 0x00;
     256           1 :         data[1] = 0x2a;
     257           1 :         result = RSVAL(data, 0);
     258           1 :         assert_int_equal(result, 42);
     259             : 
     260           1 :         data[0] = 0x00;
     261           1 :         data[1] = 0xff;
     262           1 :         result = RSVAL(data, 0);
     263           1 :         assert_int_equal(result, 0x00ff);
     264             : 
     265           1 :         data[0] = 0xff;
     266           1 :         data[1] = 0x00;
     267           1 :         result = RSVAL(data, 0);
     268           1 :         assert_int_equal(result, 0xff00);
     269             : 
     270           1 :         data[0] = 0xff;
     271           1 :         data[1] = 0xff;
     272           1 :         result = RSVAL(data, 0);
     273           1 :         assert_int_equal(result, 0xffff);
     274           1 : }
     275             : 
     276           1 : static void torture_pull_be_u32(void **state)
     277             : {
     278           1 :         uint8_t data[4] = {0, 0, 0, 0};
     279           1 :         uint32_t result;
     280             : 
     281           1 :         (void)state;
     282             : 
     283           1 :         result = RIVAL(data, 0);
     284           1 :         assert_int_equal(result, 0);
     285             : 
     286           1 :         data[0] = 0x00;
     287           1 :         data[1] = 0x00;
     288           1 :         data[2] = 0x00;
     289           1 :         data[3] = 0x2a;
     290           1 :         result = RIVAL(data, 0);
     291           1 :         assert_int_equal(result, 42);
     292             : 
     293           1 :         data[0] = 0x00;
     294           1 :         data[1] = 0x00;
     295           1 :         data[2] = 0x00;
     296           1 :         data[3] = 0xff;
     297           1 :         result = RIVAL(data, 0);
     298           1 :         assert_int_equal(result, 0x00ff);
     299             : 
     300           1 :         data[0] = 0x00;
     301           1 :         data[1] = 0x00;
     302           1 :         data[2] = 0xff;
     303           1 :         data[3] = 0x00;
     304           1 :         result = RIVAL(data, 0);
     305           1 :         assert_int_equal(result, 0xff00);
     306             : 
     307           1 :         data[0] = 0x00;
     308           1 :         data[1] = 0xff;
     309           1 :         data[2] = 0x00;
     310           1 :         data[3] = 0x00;
     311           1 :         result = RIVAL(data, 0);
     312           1 :         assert_int_equal(result, 0xff0000);
     313             : 
     314           1 :         data[0] = 0xff;
     315           1 :         data[1] = 0x00;
     316           1 :         data[2] = 0x00;
     317           1 :         data[3] = 0x00;
     318           1 :         result = RIVAL(data, 0);
     319           1 :         assert_int_equal(result, 0xff000000);
     320             : 
     321           1 :         data[0] = 0xff;
     322           1 :         data[1] = 0xff;
     323           1 :         data[2] = 0xff;
     324           1 :         data[3] = 0xff;
     325           1 :         result = RIVAL(data, 0);
     326           1 :         assert_int_equal(result, 0xffffffff);
     327           1 : }
     328             : 
     329           1 : static void torture_push_be_u8(void **state)
     330             : {
     331           1 :         uint8_t data[4] = {0, 0, 0, 0};
     332           1 :         uint8_t data2[4] = {42, 42, 42, 42};
     333             : 
     334           1 :         (void)state;
     335             : 
     336           1 :         SCVAL(data, 0, 42);
     337           1 :         SCVAL(data, 1, 42);
     338           1 :         SCVAL(data, 2, 42);
     339           1 :         SCVAL(data, 3, 42);
     340           1 :         assert_memory_equal(data, data2, sizeof(data));
     341           1 : }
     342             : 
     343           1 : static void torture_push_be_u16(void **state)
     344             : {
     345           1 :         uint8_t data[4] = {0, 0, 0, 0};
     346           1 :         uint8_t data2[4] = {0x7f, 0xa6, 0x00, 0x2a};
     347           1 :         uint16_t result;
     348             : 
     349           1 :         (void)state;
     350             : 
     351           1 :         RSSVALS(data, 0, 32678);
     352           1 :         RSSVALS(data, 2, 42);
     353           1 :         assert_memory_equal(data, data2, sizeof(data));
     354             : 
     355           1 :         result = RSVAL(data, 2);
     356           1 :         assert_int_equal(result, 42);
     357             : 
     358           1 :         result = RSVAL(data, 0);
     359           1 :         assert_int_equal(result, 32678);
     360           1 : }
     361             : 
     362           1 : static void torture_push_be_u32(void **state)
     363             : {
     364           1 :         uint8_t data[8] = {0};
     365           1 :         uint8_t data2[8] = {0x00, 0x00, 0x7f, 0xa6, 0x00, 0x00, 0x00, 0x2a};
     366           1 :         uint32_t result;
     367             : 
     368           1 :         (void)state;
     369             : 
     370           1 :         RSIVALS(data, 0, 32678);
     371           1 :         RSIVALS(data, 4, 42);
     372           1 :         assert_memory_equal(data, data2, sizeof(data));
     373             : 
     374           1 :         result = RIVAL(data, 4);
     375           1 :         assert_int_equal(result, 42);
     376             : 
     377           1 :         result = RIVAL(data, 0);
     378           1 :         assert_int_equal(result, 32678);
     379             : 
     380           1 :         RSIVALS(data, 0, 0xfffefffe);
     381           1 :         result = RIVAL(data, 0);
     382           1 :         assert_int_equal(result, 0xfffefffe);
     383           1 : }
     384             : 
     385           1 : static void torture_push_be_u64(void **state)
     386             : {
     387           1 :         uint8_t data[16] = {0};
     388           1 :         uint64_t result;
     389             : 
     390           1 :         (void)state;
     391             : 
     392           1 :         RSBVALS(data, 0, 32678);
     393             : 
     394           1 :         result = RBVAL(data, 0);
     395           1 :         assert_int_equal(result, 32678);
     396             : 
     397           1 :         SBVAL(data, 8, 0xfffefffe);
     398             : 
     399           1 :         result = BVAL(data, 8);
     400           1 :         assert_int_equal(result, 0xfffefffe);
     401           1 : }
     402             : 
     403           1 : int main(int argc, char *argv[])
     404             : {
     405           1 :         int rc;
     406           1 :         const struct CMUnitTest tests[] = {
     407             :                 cmocka_unit_test(torture_pull_le_u8),
     408             :                 cmocka_unit_test(torture_pull_le_u16),
     409             :                 cmocka_unit_test(torture_pull_le_u32),
     410             : 
     411             :                 cmocka_unit_test(torture_push_le_u8),
     412             :                 cmocka_unit_test(torture_push_le_u16),
     413             :                 cmocka_unit_test(torture_push_le_u32),
     414             :                 cmocka_unit_test(torture_push_le_u64),
     415             : 
     416             :                 /* BIG ENDIAN */
     417             :                 cmocka_unit_test(torture_pull_be_u8),
     418             :                 cmocka_unit_test(torture_pull_be_u16),
     419             :                 cmocka_unit_test(torture_pull_be_u32),
     420             : 
     421             :                 cmocka_unit_test(torture_push_be_u8),
     422             :                 cmocka_unit_test(torture_push_be_u16),
     423             :                 cmocka_unit_test(torture_push_be_u32),
     424             :                 cmocka_unit_test(torture_push_be_u64),
     425             :         };
     426             : 
     427           1 :         if (argc == 2) {
     428           0 :                 cmocka_set_test_filter(argv[1]);
     429             :         }
     430           1 :         cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
     431             : 
     432           1 :         rc = cmocka_run_group_tests(tests, NULL, NULL);
     433             : 
     434           1 :         return rc;
     435             : }

Generated by: LCOV version 1.14