File: destroy_null_bytes.c

package info (click to toggle)
swirc 3.5.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,988 kB
  • sloc: ansic: 18,321; cpp: 17,795; sh: 968; python: 254; makefile: 104; javascript: 20
file content (123 lines) | stat: -rw-r--r-- 2,787 bytes parent folder | download | duplicates (3)
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
#include "common.h"

#include <setjmp.h>
#include <cmocka.h>

#include "network.h"

static void
canDestroyNullBytes_test1(void **state)
{
	char buf[] = { 'a',0,0,0,'b',0,0,0,'c',0,0,0 };
	const char expected[] = {
		'a','X','X','X',
		'b','X','X','X',
		'c',0,0,0
	};

	assert_true(sizeof buf == sizeof expected);
	destroy_null_bytes_exported(buf, sizeof buf);
	assert_memory_equal(buf, expected, sizeof buf);
	UNUSED_PARAM(state);
}

static void
canDestroyNullBytes_test2(void **state)
{
	char buf[] = { 0,'A',0,'B',0,'C',0 };
	const char expected[] = { 'X','A','X','B','X','C',0 };

	assert_true(sizeof buf == sizeof expected);
	destroy_null_bytes_exported(buf, sizeof buf);
	assert_memory_equal(buf, expected, sizeof buf);
	UNUSED_PARAM(state);
}

static void
canDestroyNullBytes_test3(void **state)
{
	char buf[] = {
		0,0,'a','a',
		0,0,'b','b',
		0,0,'c','c',
		0,0
	};
	const char expected[] = {
		'X','X','a','a',
		'X','X','b','b',
		'X','X','c','c',
		0,0
	};

	assert_true(sizeof buf == sizeof expected);
	destroy_null_bytes_exported(buf, sizeof buf);
	assert_memory_equal(buf, expected, sizeof buf);
	UNUSED_PARAM(state);
}

static void
canDestroyNullBytes_test4(void **state)
{
	char buf[] = { 0,'X',0 };
	const char expected[] = { 'X','X',0 };

	assert_true(sizeof buf == sizeof expected);
	destroy_null_bytes_exported(buf, sizeof buf);
	assert_memory_equal(buf, expected, sizeof buf);
	UNUSED_PARAM(state);
}

static void
leavesBufUnchanged_test1(void **state)
{
	char buf[] = { 0,0 };
	const char expected[] = { 0,0 };

	assert_true(sizeof buf == 2);
	assert_true(sizeof buf == sizeof expected);
	destroy_null_bytes_exported(buf, sizeof buf);
	assert_memory_equal(buf, expected, sizeof buf);
	UNUSED_PARAM(state);
}

static void
leavesBufUnchanged_test2(void **state)
{
	char buf[] = { 0 };
	const char expected[] = { 0 };

	assert_true(sizeof buf == 1);
	assert_true(sizeof buf == sizeof expected);
	destroy_null_bytes_exported(buf, sizeof buf);
	assert_memory_equal(buf, expected, sizeof buf);
	UNUSED_PARAM(state);
}

static void
leavesBufUnchanged_test3(void **state)
{
	char buf[] = { 'A',0 };
	const char expected[] = { 'A',0 };

	assert_true(sizeof buf == 2);
	assert_true(sizeof buf == sizeof expected);
	destroy_null_bytes_exported(buf, sizeof buf);
	assert_memory_equal(buf, expected, sizeof buf);
	UNUSED_PARAM(state);
}

int
main(void)
{
	const struct CMUnitTest tests[] = {
		cmocka_unit_test(canDestroyNullBytes_test1),
		cmocka_unit_test(canDestroyNullBytes_test2),
		cmocka_unit_test(canDestroyNullBytes_test3),
		cmocka_unit_test(canDestroyNullBytes_test4),
		cmocka_unit_test(leavesBufUnchanged_test1),
		cmocka_unit_test(leavesBufUnchanged_test2),
		cmocka_unit_test(leavesBufUnchanged_test3),
	};

	return cmocka_run_group_tests(tests, NULL, NULL);
}