File: check_blob.c

package info (click to toggle)
libdumbnet 1.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,768 kB
  • sloc: ansic: 11,563; sh: 4,203; python: 261; makefile: 92
file content (187 lines) | stat: -rw-r--r-- 4,384 bytes parent folder | download | duplicates (2)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187

#include <sys/types.h>

#include <dumbnet.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <check.h>

START_TEST(test_blob_newfree)
{
	blob_t *b;
	
	fail_unless((b = blob_new()) != NULL, "new failed");
	fail_unless((b = blob_free(b)) == NULL, "free failed");
}
END_TEST

START_TEST(test_blob_readwrite)
{
	blob_t *b;
	char tmp[32];
	
	b = blob_new();
	blob_write(b, "foobar", 7);
	blob_write(b, "spazzo", 7);
	blob_write(b, "doofus", 7);
	blob_rewind(b);

	blob_read(b, tmp, 7);
	fail_unless(strcmp(tmp, "foobar") == 0, "read1 failed");
	blob_read(b, tmp, 7);
	fail_unless(strcmp(tmp, "spazzo") == 0, "read2 failed");
	blob_read(b, tmp, 7);
	fail_unless(strcmp(tmp, "doofus") == 0, "read3 failed");
	blob_free(b);
}
END_TEST

START_TEST(test_blob_insertdelete)
{
	blob_t *b, bf;
	char tmp[32];
	
	b = blob_new();
	fail_unless(blob_insert(b, "foo", 3) == 3, "insert1 failed");
	blob_rewind(b);
	fail_unless(blob_insert(b, "bar", 3) == 3, "insert2 failed");
	blob_rewind(b);
	blob_read(b, tmp, 6);
	tmp[6] = 0;
	fail_unless(strcmp(tmp,"barfoo") == 0, "read failed");
	blob_rewind(b);
	fail_unless(blob_delete(b, NULL, 3) == 3, "delete failed");
	blob_rewind(b);
	blob_read(b, tmp, 3);
	tmp[3] = 0;
	fail_unless(strcmp(tmp,"foo") == 0, "read failed");
	
	fail_unless(blob_delete(b, NULL, 4) < 0, "deleted more than size");
	b = blob_free(b);

	bf.base = "foobar";
	bf.end = 6;
	bf.off = bf.size = 0;

	fail_unless(blob_insert(&bf, "foobar", 6) < 0, "inserted into fixed");
	fail_unless(blob_delete(&bf, NULL, 3) < 0, "deleted from fixed"); 
}
END_TEST

START_TEST(test_blob_packunpack)
{
	blob_t *b;
	uint32_t D, d;
	uint16_t H, h;
	u_char c;
	char s[128];
	u_char buf[6];

	b = blob_new();

	D = 0xdeadbeef;
	H = 0xbabe;
	memcpy(buf, "f\x00\x00bar", 6);
	c = 'c';
	d = 555;
	h = 666;
	strcpy(s, "donkey");
#if 0	
	printf("D: 0x%x H: 0x%x c: %c d: %d h: %d s: %s\n",
	    D, H, c, d, h, s);
#endif
	fail_unless(blob_pack(b, "whee:%D%H%*b%c%d%h%s\r\n",
	    D, H, sizeof(buf), buf, c, d, h, s) == 0,
	    "pack failed");
	
	blob_rewind(b);
#if 0
	blob_print(b, "hexl", blob_left(b));
#endif
	fail_unless(blob_unpack(b, "whee:%D%H%*b%c%d%h%*s\r\n",
	    &D, &H, sizeof(buf), buf, &c, &d, &h, sizeof(s), s) == 0,
	    "unpack failed");
#if 0
	printf("D: 0x%x H: 0x%x c: %c d: %d h: %d s: %s\n",
	    D, H, c, d, h, s);
#endif
	if (D != 0xdeadbeef || H != 0xbabe ||
	    memcmp(buf, "f\x00\x00bar", 6) != 0 || c != 'c' || d != 555 ||
	    h != 666 || strcmp(s, "donkey") != 0)
		fail("unpacked weird crap");
	
	blob_free(b);
}
END_TEST

START_TEST(test_blob_seek)
{
	blob_t *b;

	b = blob_new();
	blob_insert(b, "foobar", 6);
	blob_rewind(b);
	fail_unless(blob_skip(b, 3) == 3, "skip failed");
	fail_unless(blob_skip(b, 3) == 6, "skip to end failed");
	fail_unless(blob_skip(b, 1) < 0, "skipped past end");
	fail_unless(blob_seek(b, -1, SEEK_END) == 5, "end seek failed");
	fail_unless(blob_seek(b, 1, SEEK_SET) == 1, "set seek failed");
	blob_rewind(b);
	fail_unless(blob_seek(b, -1, SEEK_CUR) < 0, "seeked past 0");
	fail_unless(blob_seek(b, 3, SEEK_CUR) == 3, "cur seek failed");
	blob_free(b);
}
END_TEST

START_TEST(test_blob_index)
{
	blob_t *b;

	b = blob_new();
	blob_write(b, "this is only a test!", 20);
	blob_rewind(b);
	fail_unless(blob_index(b, "this", 4) == 0, "index start failed");
	fail_unless(blob_index(b, "!", 1) == 19, "index end failed");
	fail_unless(blob_index(b, "only ", 5) == 8, "index middle failed");
	fail_unless(blob_rindex(b, "!", 1) == 19, "rindex end failed");
	fail_unless(blob_rindex(b, "this", 4) == 0, "rindex start failed");
	fail_unless(blob_rindex(b, "only ", 5) == 8, "rindex middle failed");
	blob_free(b);
}
END_TEST

Suite *
blob_suite(void)
{
	Suite *s = suite_create("blob");
	TCase *tc_core = tcase_create("core");

	suite_add_tcase(s, tc_core);
	tcase_add_test(tc_core, test_blob_newfree);
	tcase_add_test(tc_core, test_blob_readwrite);
	tcase_add_test(tc_core, test_blob_insertdelete);
	tcase_add_test(tc_core, test_blob_packunpack);
	tcase_add_test(tc_core, test_blob_seek);
	tcase_add_test(tc_core, test_blob_index);
	
	return (s);
}

int
main(void)
{
	Suite *s = blob_suite();
	SRunner *sr = srunner_create(s);
	int nf;
#if 0
	srunner_set_fork_status(sr, CK_NOFORK);
#endif
	srunner_run_all (sr, CK_NORMAL);
	nf = srunner_ntests_failed(sr);
	srunner_free(sr);
	
	return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}