summaryrefslogtreecommitdiffstats
path: root/lib/util/tests/data_blob.c
blob: e1e8129a259a4bed237c84788e254da9bbfe1a83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* 
   Unix SMB/CIFS implementation.

   data blob testing

   Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "torture/torture.h"
#include "torture/local/proto.h"

static bool test_string(struct torture_context *tctx)
{
	DATA_BLOB blob = data_blob_string_const("bla");	

	torture_assert_int_equal(tctx, blob.length, 3, "blob length");
	torture_assert_str_equal(tctx, (char *)blob.data, "bla", "blob data");

	return true;
}

static bool test_string_null(struct torture_context *tctx)
{
	DATA_BLOB blob = data_blob_string_const_null("bla");	

	torture_assert_int_equal(tctx, blob.length, 4, "blob length");
	torture_assert_str_equal(tctx, (char *)blob.data, "bla", "blob data");

	return true;
}

static bool test_zero(struct torture_context *tctx)
{
	int i;
	DATA_BLOB z = data_blob_talloc_zero(tctx, 4);
	torture_assert_int_equal(tctx, z.length, 4, "length");
	for (i = 0; i < z.length; i++)
		torture_assert_int_equal(tctx, z.data[i], 0, "contents");
	data_blob_free(&z);
	return true;
}


static bool test_clear(struct torture_context *tctx)
{
	int i;
	DATA_BLOB z = data_blob("lalala", 6);
	torture_assert_int_equal(tctx, z.length, 6, "length");
	data_blob_clear(&z);
	for (i = 0; i < z.length; i++)
		torture_assert_int_equal(tctx, z.data[i], 0, "contents");
	data_blob_free(&z);
	return true;
}

static bool test_cmp(struct torture_context *tctx)
{
	DATA_BLOB a = data_blob_string_const("bla");
	DATA_BLOB b = data_blob_string_const("blae");
	torture_assert(tctx, data_blob_cmp(&a, &b) != 0, "cmp different");
	torture_assert(tctx, data_blob_cmp(&a, &a) == 0, "cmp self");
	return true;
}

static bool test_equal_const_time(struct torture_context *tctx)
{
	const char *test_string = "foobarfoo";

	DATA_BLOB null = data_blob_const(NULL, 0);
	DATA_BLOB foobar = data_blob_const(test_string, 6);
	DATA_BLOB bar = data_blob_const(test_string + 3, 3);

	/* These data blobs both contain 'foo', but at different addresses. */
	DATA_BLOB foo_same = data_blob_const(test_string, 3);
	DATA_BLOB foo_other = data_blob_const(test_string + 6, 3);

	/* Test all equality combinations behave as expected. */
	torture_assert(tctx, data_blob_equal_const_time(&null, &null), "null == null");
	torture_assert(tctx, !data_blob_equal_const_time(&null, &foobar), "null != 'foobar'");
	torture_assert(tctx, !data_blob_equal_const_time(&null, &bar), "null != 'bar'");
	torture_assert(tctx, !data_blob_equal_const_time(&null, &foo_same), "null != 'foo'");
	torture_assert(tctx, !data_blob_equal_const_time(&null, &foo_other), "null != 'foo'");

	torture_assert(tctx, !data_blob_equal_const_time(&foobar, &null), "'foobar' != null");
	torture_assert(tctx, data_blob_equal_const_time(&foobar, &foobar), "'foobar' == 'foobar'");
	torture_assert(tctx, !data_blob_equal_const_time(&foobar, &bar), "'foobar' != 'bar'");
	torture_assert(tctx, !data_blob_equal_const_time(&foobar, &foo_same), "'foobar' != 'foo'");
	torture_assert(tctx, !data_blob_equal_const_time(&foobar, &foo_other), "'foobar' != 'foo'");

	torture_assert(tctx, !data_blob_equal_const_time(&foo_same, &null), "'foo' != null");
	torture_assert(tctx, !data_blob_equal_const_time(&foo_same, &foobar), "'foo' != 'foobar'");
	torture_assert(tctx, !data_blob_equal_const_time(&foo_same, &bar), "'foo' != 'bar'");
	torture_assert(tctx, data_blob_equal_const_time(&foo_same, &foo_same), "'foo' == 'foo'");
	torture_assert(tctx, data_blob_equal_const_time(&foo_same, &foo_other), "'foo' == 'foo'");

	torture_assert(tctx, !data_blob_equal_const_time(&foo_other, &null), "'foo' != null");
	torture_assert(tctx, !data_blob_equal_const_time(&foo_other, &foobar), "'foo' != 'foobar'");
	torture_assert(tctx, !data_blob_equal_const_time(&foo_other, &bar), "'foo' != 'bar'");
	torture_assert(tctx, data_blob_equal_const_time(&foo_other, &foo_same), "'foo' == 'foo'");
	torture_assert(tctx, data_blob_equal_const_time(&foo_other, &foo_other), "'foo' == 'foo'");

	torture_assert(tctx, !data_blob_equal_const_time(&bar, &null), "'bar' != null");
	torture_assert(tctx, !data_blob_equal_const_time(&bar, &foobar), "'bar' != 'foobar'");
	torture_assert(tctx, data_blob_equal_const_time(&bar, &bar), "'bar' == 'bar'");
	torture_assert(tctx, !data_blob_equal_const_time(&bar, &foo_same), "'bar' != 'foo'");
	torture_assert(tctx, !data_blob_equal_const_time(&bar, &foo_other), "'bar' != 'foo'");

	return true;
}

static bool test_hex_string(struct torture_context *tctx)
{
	DATA_BLOB a = data_blob_string_const("\xC\xA\xF\xE");
	torture_assert_str_equal(tctx, data_blob_hex_string_lower(tctx, &a), "0c0a0f0e", "hex string");
	torture_assert_str_equal(tctx, data_blob_hex_string_upper(tctx, &a), "0C0A0F0E", "hex string");
	return true;
}

static bool test_append_NULL_0(struct torture_context *tctx)
{
	DATA_BLOB z = data_blob_talloc_zero(tctx, 0);
	torture_assert_int_equal(tctx, z.length, 0, "length");
	torture_assert(tctx, z.data == NULL, "data");
	torture_assert(tctx, data_blob_append(NULL, &z, NULL, 0), "append NULL,0");
	torture_assert(tctx, data_blob_append(NULL, &z, "", 0), "append '',0");
	torture_assert_int_equal(tctx, z.length, 0, "length");
	torture_assert(tctx, z.data == NULL, "data");
	return true;
}

static bool test_append_empty_0(struct torture_context *tctx)
{
	DATA_BLOB e = data_blob_talloc(tctx, "", 0);
	torture_assert_int_equal(tctx, e.length, 0, "length");
	torture_assert(tctx, e.data != NULL, "data");
	torture_assert(tctx, data_blob_append(NULL, &e, NULL, 0), "append NULL,0");
	torture_assert(tctx, data_blob_append(NULL, &e, "", 0), "append '',0");
	torture_assert_int_equal(tctx, e.length, 0, "length");
	torture_assert(tctx, e.data != NULL, "data");
	return true;
}

struct torture_suite *torture_local_util_data_blob(TALLOC_CTX *mem_ctx)
{
	struct torture_suite *suite = torture_suite_create(mem_ctx, "datablob");

	torture_suite_add_simple_test(suite, "string", test_string);
	torture_suite_add_simple_test(suite, "string_null", test_string_null);
	torture_suite_add_simple_test(suite, "zero", test_zero);;
	torture_suite_add_simple_test(suite, "clear", test_clear);
	torture_suite_add_simple_test(suite, "cmp", test_cmp);
	torture_suite_add_simple_test(suite, "equal_const_time", test_equal_const_time);
	torture_suite_add_simple_test(suite, "hex string", test_hex_string);
	torture_suite_add_simple_test(suite, "append_NULL_0", test_append_NULL_0);
	torture_suite_add_simple_test(suite, "append_empty_0", test_append_empty_0);

	return suite;
}