LCOV - code coverage report
Current view: top level - librpc/tests - test_ndr.c (source / functions) Hit Total Coverage
Test: coverage report for master 2b515b7d Lines: 38 38 100.0 %
Date: 2024-02-28 12:06:22 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Tests for librpc ndr functions
       3             :  *
       4             :  * Copyright (C) Catalyst.NET Ltd 2020
       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             : 
      21             : /*
      22             :  * from cmocka.c:
      23             :  * These headers or their equivalents should be included prior to
      24             :  * including
      25             :  * this header file.
      26             :  *
      27             :  * #include <stdarg.h>
      28             :  * #include <stddef.h>
      29             :  * #include <setjmp.h>
      30             :  *
      31             :  * This allows test applications to use custom definitions of C standard
      32             :  * library functions and types.
      33             :  *
      34             :  */
      35             : #include "replace.h"
      36             : #include <setjmp.h>
      37             : #include <cmocka.h>
      38             : 
      39             : #include "librpc/ndr/libndr.h"
      40             : 
      41             : /*
      42             :  * Test NDR_PULL_NEED_BYTES integer overflow handling.
      43             :  */
      44           2 : static enum ndr_err_code wrap_NDR_PULL_NEED_BYTES(
      45             :         struct ndr_pull *ndr,
      46             :         uint32_t bytes) {
      47             : 
      48           2 :         NDR_PULL_NEED_BYTES(ndr, bytes);
      49             :         return NDR_ERR_SUCCESS;
      50             : }
      51             : 
      52           1 : static void test_NDR_PULL_NEED_BYTES(void **state)
      53             : {
      54           1 :         struct ndr_pull ndr = {0};
      55           1 :         enum ndr_err_code err;
      56             : 
      57           1 :         ndr.data_size = UINT32_MAX;
      58           1 :         ndr.offset = UINT32_MAX -1;
      59             : 
      60             :         /*
      61             :          * This will not cause an overflow
      62             :          */
      63           1 :         err = wrap_NDR_PULL_NEED_BYTES(&ndr, 1);
      64           1 :         assert_int_equal(NDR_ERR_SUCCESS, err);
      65             : 
      66             :         /*
      67             :          * This will cause an overflow
      68             :          * and (offset + n) will be less than data_size
      69             :          */
      70           1 :         err = wrap_NDR_PULL_NEED_BYTES(&ndr, 2);
      71           1 :         assert_int_equal(NDR_ERR_BUFSIZE, err);
      72           1 : }
      73             : 
      74             : /*
      75             :  * Test NDR_PULL_ALIGN integer overflow handling.
      76             :  */
      77           2 : static enum ndr_err_code wrap_NDR_PULL_ALIGN(
      78             :         struct ndr_pull *ndr,
      79             :         uint32_t bytes) {
      80             : 
      81           2 :         NDR_PULL_ALIGN(ndr, bytes);
      82             :         return NDR_ERR_SUCCESS;
      83             : }
      84             : 
      85           1 : static void test_NDR_PULL_ALIGN(void **state)
      86             : {
      87           1 :         struct ndr_pull ndr = {0};
      88           1 :         enum ndr_err_code err;
      89             : 
      90           1 :         ndr.data_size = UINT32_MAX;
      91           1 :         ndr.offset = UINT32_MAX -1;
      92             : 
      93             :         /*
      94             :          * This will not cause an overflow
      95             :          */
      96           1 :         err = wrap_NDR_PULL_ALIGN(&ndr, 2);
      97           1 :         assert_int_equal(NDR_ERR_SUCCESS, err);
      98             : 
      99             :         /*
     100             :          * This will cause an overflow
     101             :          * and (offset + n) will be less than data_size
     102             :          */
     103           1 :         err = wrap_NDR_PULL_ALIGN(&ndr, 4);
     104           1 :         assert_int_equal(NDR_ERR_BUFSIZE, err);
     105           1 : }
     106             : 
     107             : /*
     108             :  * Test ndr_pull_advance integer overflow handling.
     109             :  */
     110           1 : static void test_ndr_pull_advance(void **state)
     111             : {
     112           1 :         struct ndr_pull ndr = {0};
     113           1 :         enum ndr_err_code err;
     114             : 
     115           1 :         ndr.data_size = UINT32_MAX;
     116           1 :         ndr.offset = UINT32_MAX -1;
     117             : 
     118             :         /*
     119             :          * This will not cause an overflow
     120             :          */
     121           1 :         err = ndr_pull_advance(&ndr, 1);
     122           1 :         assert_int_equal(NDR_ERR_SUCCESS, err);
     123             : 
     124             :         /*
     125             :          * This will cause an overflow
     126             :          * and (offset + n) will be less than data_size
     127             :          */
     128           1 :         err = ndr_pull_advance(&ndr, 2);
     129           1 :         assert_int_equal(NDR_ERR_BUFSIZE, err);
     130           1 : }
     131             : 
     132           1 : int main(int argc, const char **argv)
     133             : {
     134           1 :         const struct CMUnitTest tests[] = {
     135             :                 cmocka_unit_test(test_NDR_PULL_NEED_BYTES),
     136             :                 cmocka_unit_test(test_NDR_PULL_ALIGN),
     137             :                 cmocka_unit_test(test_ndr_pull_advance),
     138             :         };
     139             : 
     140           1 :         cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
     141           1 :         return cmocka_run_group_tests(tests, NULL, NULL);
     142             : }

Generated by: LCOV version 1.14