libcli/cldap: make use of samba_tevent_context_init()
[Samba/gebeck_regimport.git] / lib / util / tests / data_blob.c
blobbdccef2ce8c912263a759bc1b2bea2a72b8c4bc4
1 /*
2 Unix SMB/CIFS implementation.
4 data blob testing
6 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
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.
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.
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/>.
22 #include "includes.h"
23 #include "torture/torture.h"
25 static bool test_string(struct torture_context *tctx)
27 DATA_BLOB blob = data_blob_string_const("bla");
29 torture_assert_int_equal(tctx, blob.length, 3, "blob length");
30 torture_assert_str_equal(tctx, (char *)blob.data, "bla", "blob data");
32 return true;
35 static bool test_string_null(struct torture_context *tctx)
37 DATA_BLOB blob = data_blob_string_const_null("bla");
39 torture_assert_int_equal(tctx, blob.length, 4, "blob length");
40 torture_assert_str_equal(tctx, (char *)blob.data, "bla", "blob data");
42 return true;
45 static bool test_zero(struct torture_context *tctx)
47 int i;
48 DATA_BLOB z = data_blob_talloc_zero(tctx, 4);
49 torture_assert_int_equal(tctx, z.length, 4, "length");
50 for (i = 0; i < z.length; i++)
51 torture_assert_int_equal(tctx, z.data[i], 0, "contents");
52 data_blob_free(&z);
53 return true;
57 static bool test_clear(struct torture_context *tctx)
59 int i;
60 DATA_BLOB z = data_blob("lalala", 6);
61 torture_assert_int_equal(tctx, z.length, 6, "length");
62 data_blob_clear(&z);
63 for (i = 0; i < z.length; i++)
64 torture_assert_int_equal(tctx, z.data[i], 0, "contents");
65 data_blob_free(&z);
66 return true;
69 static bool test_cmp(struct torture_context *tctx)
71 DATA_BLOB a = data_blob_string_const("bla");
72 DATA_BLOB b = data_blob_string_const("blae");
73 torture_assert(tctx, data_blob_cmp(&a, &b) != 0, "cmp different");
74 torture_assert(tctx, data_blob_cmp(&a, &a) == 0, "cmp self");
75 return true;
78 static bool test_hex_string(struct torture_context *tctx)
80 DATA_BLOB a = data_blob_string_const("\xC\xA\xF\xE");
81 torture_assert_str_equal(tctx, data_blob_hex_string_lower(tctx, &a), "0c0a0f0e", "hex string");
82 torture_assert_str_equal(tctx, data_blob_hex_string_upper(tctx, &a), "0C0A0F0E", "hex string");
83 return true;
86 struct torture_suite *torture_local_util_data_blob(TALLOC_CTX *mem_ctx)
88 struct torture_suite *suite = torture_suite_create(mem_ctx, "datablob");
90 torture_suite_add_simple_test(suite, "string", test_string);
91 torture_suite_add_simple_test(suite, "string_null", test_string_null);
92 torture_suite_add_simple_test(suite, "zero", test_zero);;
93 torture_suite_add_simple_test(suite, "clear", test_clear);
94 torture_suite_add_simple_test(suite, "cmp", test_cmp);
95 torture_suite_add_simple_test(suite, "hex string", test_hex_string);
97 return suite;