LCOV - code coverage report
Current view: top level - lib/util/tests - data_blob.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 94 101 93.1 %
Date: 2024-04-21 15:09:00 Functions: 10 10 100.0 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    data blob testing
       5             : 
       6             :    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
       7             :    
       8             :    This program is free software; you can redistribute it and/or modify
       9             :    it under the terms of the GNU General Public License as published by
      10             :    the Free Software Foundation; either version 3 of the License, or
      11             :    (at your option) any later version.
      12             :    
      13             :    This program is distributed in the hope that it will be useful,
      14             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :    GNU General Public License for more details.
      17             :    
      18             :    You should have received a copy of the GNU General Public License
      19             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      20             : */
      21             : 
      22             : #include "includes.h"
      23             : #include "torture/torture.h"
      24             : #include "torture/local/proto.h"
      25             : 
      26           1 : static bool test_string(struct torture_context *tctx)
      27             : {
      28           1 :         DATA_BLOB blob = data_blob_string_const("bla");       
      29             : 
      30           1 :         torture_assert_int_equal(tctx, blob.length, 3, "blob length");
      31           1 :         torture_assert_str_equal(tctx, (char *)blob.data, "bla", "blob data");
      32             : 
      33           0 :         return true;
      34             : }
      35             : 
      36           1 : static bool test_string_null(struct torture_context *tctx)
      37             : {
      38           1 :         DATA_BLOB blob = data_blob_string_const_null("bla");  
      39             : 
      40           1 :         torture_assert_int_equal(tctx, blob.length, 4, "blob length");
      41           1 :         torture_assert_str_equal(tctx, (char *)blob.data, "bla", "blob data");
      42             : 
      43           0 :         return true;
      44             : }
      45             : 
      46           1 : static bool test_zero(struct torture_context *tctx)
      47             : {
      48           1 :         int i;
      49           1 :         DATA_BLOB z = data_blob_talloc_zero(tctx, 4);
      50           1 :         torture_assert_int_equal(tctx, z.length, 4, "length");
      51           5 :         for (i = 0; i < z.length; i++)
      52           4 :                 torture_assert_int_equal(tctx, z.data[i], 0, "contents");
      53           1 :         data_blob_free(&z);
      54           1 :         return true;
      55             : }
      56             : 
      57             : 
      58           1 : static bool test_clear(struct torture_context *tctx)
      59             : {
      60           1 :         int i;
      61           1 :         DATA_BLOB z = data_blob("lalala", 6);
      62           1 :         torture_assert_int_equal(tctx, z.length, 6, "length");
      63           1 :         data_blob_clear(&z);
      64           8 :         for (i = 0; i < z.length; i++)
      65           6 :                 torture_assert_int_equal(tctx, z.data[i], 0, "contents");
      66           1 :         data_blob_free(&z);
      67           1 :         return true;
      68             : }
      69             : 
      70           1 : static bool test_cmp(struct torture_context *tctx)
      71             : {
      72           1 :         DATA_BLOB a = data_blob_string_const("bla");
      73           1 :         DATA_BLOB b = data_blob_string_const("blae");
      74           1 :         torture_assert(tctx, data_blob_cmp(&a, &b) != 0, "cmp different");
      75           1 :         torture_assert(tctx, data_blob_cmp(&a, &a) == 0, "cmp self");
      76           0 :         return true;
      77             : }
      78             : 
      79           1 : static bool test_equal_const_time(struct torture_context *tctx)
      80             : {
      81           1 :         const char *test_string = "foobarfoo";
      82             : 
      83           1 :         DATA_BLOB null = data_blob_const(NULL, 0);
      84           1 :         DATA_BLOB foobar = data_blob_const(test_string, 6);
      85           1 :         DATA_BLOB bar = data_blob_const(test_string + 3, 3);
      86             : 
      87             :         /* These data blobs both contain 'foo', but at different addresses. */
      88           1 :         DATA_BLOB foo_same = data_blob_const(test_string, 3);
      89           1 :         DATA_BLOB foo_other = data_blob_const(test_string + 6, 3);
      90             : 
      91             :         /* Test all equality combinations behave as expected. */
      92           1 :         torture_assert(tctx, data_blob_equal_const_time(&null, &null), "null == null");
      93           1 :         torture_assert(tctx, !data_blob_equal_const_time(&null, &foobar), "null != 'foobar'");
      94           1 :         torture_assert(tctx, !data_blob_equal_const_time(&null, &bar), "null != 'bar'");
      95           1 :         torture_assert(tctx, !data_blob_equal_const_time(&null, &foo_same), "null != 'foo'");
      96           1 :         torture_assert(tctx, !data_blob_equal_const_time(&null, &foo_other), "null != 'foo'");
      97             : 
      98           1 :         torture_assert(tctx, !data_blob_equal_const_time(&foobar, &null), "'foobar' != null");
      99           1 :         torture_assert(tctx, data_blob_equal_const_time(&foobar, &foobar), "'foobar' == 'foobar'");
     100           1 :         torture_assert(tctx, !data_blob_equal_const_time(&foobar, &bar), "'foobar' != 'bar'");
     101           1 :         torture_assert(tctx, !data_blob_equal_const_time(&foobar, &foo_same), "'foobar' != 'foo'");
     102           1 :         torture_assert(tctx, !data_blob_equal_const_time(&foobar, &foo_other), "'foobar' != 'foo'");
     103             : 
     104           1 :         torture_assert(tctx, !data_blob_equal_const_time(&foo_same, &null), "'foo' != null");
     105           1 :         torture_assert(tctx, !data_blob_equal_const_time(&foo_same, &foobar), "'foo' != 'foobar'");
     106           1 :         torture_assert(tctx, !data_blob_equal_const_time(&foo_same, &bar), "'foo' != 'bar'");
     107           1 :         torture_assert(tctx, data_blob_equal_const_time(&foo_same, &foo_same), "'foo' == 'foo'");
     108           1 :         torture_assert(tctx, data_blob_equal_const_time(&foo_same, &foo_other), "'foo' == 'foo'");
     109             : 
     110           1 :         torture_assert(tctx, !data_blob_equal_const_time(&foo_other, &null), "'foo' != null");
     111           1 :         torture_assert(tctx, !data_blob_equal_const_time(&foo_other, &foobar), "'foo' != 'foobar'");
     112           1 :         torture_assert(tctx, !data_blob_equal_const_time(&foo_other, &bar), "'foo' != 'bar'");
     113           1 :         torture_assert(tctx, data_blob_equal_const_time(&foo_other, &foo_same), "'foo' == 'foo'");
     114           1 :         torture_assert(tctx, data_blob_equal_const_time(&foo_other, &foo_other), "'foo' == 'foo'");
     115             : 
     116           1 :         torture_assert(tctx, !data_blob_equal_const_time(&bar, &null), "'bar' != null");
     117           1 :         torture_assert(tctx, !data_blob_equal_const_time(&bar, &foobar), "'bar' != 'foobar'");
     118           1 :         torture_assert(tctx, data_blob_equal_const_time(&bar, &bar), "'bar' == 'bar'");
     119           1 :         torture_assert(tctx, !data_blob_equal_const_time(&bar, &foo_same), "'bar' != 'foo'");
     120           1 :         torture_assert(tctx, !data_blob_equal_const_time(&bar, &foo_other), "'bar' != 'foo'");
     121             : 
     122           0 :         return true;
     123             : }
     124             : 
     125           1 : static bool test_hex_string(struct torture_context *tctx)
     126             : {
     127           1 :         DATA_BLOB a = data_blob_string_const("\xC\xA\xF\xE");
     128           1 :         torture_assert_str_equal(tctx, data_blob_hex_string_lower(tctx, &a), "0c0a0f0e", "hex string");
     129           1 :         torture_assert_str_equal(tctx, data_blob_hex_string_upper(tctx, &a), "0C0A0F0E", "hex string");
     130           0 :         return true;
     131             : }
     132             : 
     133           1 : static bool test_append_NULL_0(struct torture_context *tctx)
     134             : {
     135           1 :         DATA_BLOB z = data_blob_talloc_zero(tctx, 0);
     136           1 :         torture_assert_int_equal(tctx, z.length, 0, "length");
     137           1 :         torture_assert(tctx, z.data == NULL, "data");
     138           1 :         torture_assert(tctx, data_blob_append(NULL, &z, NULL, 0), "append NULL,0");
     139           1 :         torture_assert(tctx, data_blob_append(NULL, &z, "", 0), "append '',0");
     140           1 :         torture_assert_int_equal(tctx, z.length, 0, "length");
     141           1 :         torture_assert(tctx, z.data == NULL, "data");
     142           0 :         return true;
     143             : }
     144             : 
     145           1 : static bool test_append_empty_0(struct torture_context *tctx)
     146             : {
     147           1 :         DATA_BLOB e = data_blob_talloc(tctx, "", 0);
     148           1 :         torture_assert_int_equal(tctx, e.length, 0, "length");
     149           1 :         torture_assert(tctx, e.data != NULL, "data");
     150           1 :         torture_assert(tctx, data_blob_append(NULL, &e, NULL, 0), "append NULL,0");
     151           1 :         torture_assert(tctx, data_blob_append(NULL, &e, "", 0), "append '',0");
     152           1 :         torture_assert_int_equal(tctx, e.length, 0, "length");
     153           1 :         torture_assert(tctx, e.data != NULL, "data");
     154           0 :         return true;
     155             : }
     156             : 
     157        2354 : struct torture_suite *torture_local_util_data_blob(TALLOC_CTX *mem_ctx)
     158             : {
     159        2354 :         struct torture_suite *suite = torture_suite_create(mem_ctx, "datablob");
     160             : 
     161        2354 :         torture_suite_add_simple_test(suite, "string", test_string);
     162        2354 :         torture_suite_add_simple_test(suite, "string_null", test_string_null);
     163        2354 :         torture_suite_add_simple_test(suite, "zero", test_zero);;
     164        2354 :         torture_suite_add_simple_test(suite, "clear", test_clear);
     165        2354 :         torture_suite_add_simple_test(suite, "cmp", test_cmp);
     166        2354 :         torture_suite_add_simple_test(suite, "equal_const_time", test_equal_const_time);
     167        2354 :         torture_suite_add_simple_test(suite, "hex string", test_hex_string);
     168        2354 :         torture_suite_add_simple_test(suite, "append_NULL_0", test_append_NULL_0);
     169        2354 :         torture_suite_add_simple_test(suite, "append_empty_0", test_append_empty_0);
     170             : 
     171        2354 :         return suite;
     172             : }

Generated by: LCOV version 1.14