1284990Scy#include "config.h"
2284990Scy#include "fileHandlingTest.h"
3284990Scy
4284990Scy#include "ntp_stdlib.h"
5284990Scy#include "ntp_types.h"
6284990Scy#include "crypto.h"
7284990Scy
8284990Scy#include "unity.h"
9284990Scy
10289997Sglebiusbool CompareKeys(struct key expected, struct key actual);
11289997Sglebiusbool CompareKeysAlternative(int key_id,int key_len,const char* type,const char* key_seq,struct key actual);
12289997Sglebiusvoid test_ReadEmptyKeyFile(void);
13289997Sglebiusvoid test_ReadASCIIKeys(void);
14289997Sglebiusvoid test_ReadHexKeys(void);
15289997Sglebiusvoid test_ReadKeyFileWithComments(void);
16289997Sglebiusvoid test_ReadKeyFileWithInvalidHex(void);
17284990Scy
18284990Scy
19289997Sglebiusbool
20294569SdelphijCompareKeys(
21294569Sdelphij	struct key	expected,
22294569Sdelphij	struct key	actual
23294569Sdelphij	)
24294569Sdelphij{
25294569Sdelphij	if (expected.key_id != actual.key_id) {
26294569Sdelphij		printf("Expected key_id: %d but was: %d\n",
27294569Sdelphij		       expected.key_id, actual.key_id);
28284990Scy		return FALSE;
29284990Scy	}
30294569Sdelphij	if (expected.key_len != actual.key_len) {
31294569Sdelphij		printf("Expected key_len: %d but was: %d\n",
32294569Sdelphij		       expected.key_len, actual.key_len);
33284990Scy		return FALSE;
34284990Scy	}
35330141Sdelphij	if (strcmp(expected.typen, actual.typen) != 0) {
36294569Sdelphij		printf("Expected key_type: %s but was: %s\n",
37330141Sdelphij		       expected.typen, actual.typen);
38284990Scy		return FALSE;
39284990Scy
40284990Scy	}
41294569Sdelphij	if (memcmp(expected.key_seq, actual.key_seq, expected.key_len) != 0) {
42284990Scy		printf("Key mismatch!\n");
43284990Scy		return FALSE;
44284990Scy	}
45284990Scy	return TRUE;
46284990Scy}
47284990Scy
48289997Sglebius
49289997Sglebiusbool
50294569SdelphijCompareKeysAlternative(
51294569Sdelphij	int		key_id,
52294569Sdelphij	int		key_len,
53294569Sdelphij	const char *	type,
54294569Sdelphij	const char *	key_seq,
55294569Sdelphij	struct key	actual
56294569Sdelphij	)
57294569Sdelphij{
58294569Sdelphij	struct key	temp;
59284990Scy
60284990Scy	temp.key_id = key_id;
61284990Scy	temp.key_len = key_len;
62330141Sdelphij	strlcpy(temp.typen, type, sizeof(temp.typen));
63284990Scy	memcpy(temp.key_seq, key_seq, key_len);
64284990Scy
65284990Scy	return CompareKeys(temp, actual);
66284990Scy}
67284990Scy
68284990Scy
69289997Sglebiusvoid
70294569Sdelphijtest_ReadEmptyKeyFile(void)
71294569Sdelphij{
72294569Sdelphij	struct key *	keys = NULL;
73294569Sdelphij	const char *	path = CreatePath("key-test-empty", INPUT_DIR);
74284990Scy
75293650Sglebius	TEST_ASSERT_NOT_NULL(path);
76293650Sglebius	TEST_ASSERT_EQUAL(0, auth_init(path, &keys));
77289997Sglebius	TEST_ASSERT_NULL(keys);
78293650Sglebius
79294569Sdelphij	DestroyPath(path);
80284990Scy}
81284990Scy
82289997Sglebius
83289997Sglebiusvoid
84294569Sdelphijtest_ReadASCIIKeys(void)
85294569Sdelphij{
86294569Sdelphij	struct key *	keys = NULL;
87294569Sdelphij	struct key *	result = NULL;
88294569Sdelphij	const char *	path = CreatePath("key-test-ascii", INPUT_DIR);
89284990Scy
90293650Sglebius	TEST_ASSERT_NOT_NULL(path);
91293650Sglebius	TEST_ASSERT_EQUAL(2, auth_init(path, &keys));
92289997Sglebius	TEST_ASSERT_NOT_NULL(keys);
93284990Scy
94294569Sdelphij	DestroyPath(path);
95293650Sglebius
96284990Scy	get_key(40, &result);
97289997Sglebius	TEST_ASSERT_NOT_NULL(result);
98284990Scy	TEST_ASSERT_TRUE(CompareKeysAlternative(40, 11, "MD5", "asciikeyTwo", *result));
99284990Scy
100284990Scy	result = NULL;
101284990Scy	get_key(50, &result);
102289997Sglebius	TEST_ASSERT_NOT_NULL(result);
103284990Scy	TEST_ASSERT_TRUE(CompareKeysAlternative(50, 11, "MD5", "asciikeyOne", *result));
104284990Scy}
105284990Scy
106289997Sglebius
107289997Sglebiusvoid
108294569Sdelphijtest_ReadHexKeys(void)
109294569Sdelphij{
110294569Sdelphij	struct key *	keys = NULL;
111294569Sdelphij	struct key *	result = NULL;
112294569Sdelphij	const char *	path = CreatePath("key-test-hex", INPUT_DIR);
113294569Sdelphij	char 		data1[15];
114294569Sdelphij	char 		data2[13];
115284990Scy
116293650Sglebius	TEST_ASSERT_NOT_NULL(path);
117293650Sglebius	TEST_ASSERT_EQUAL(3, auth_init(path, &keys));
118289997Sglebius	TEST_ASSERT_NOT_NULL(keys);
119294569Sdelphij	DestroyPath(path);
120284990Scy
121284990Scy	get_key(10, &result);
122289997Sglebius	TEST_ASSERT_NOT_NULL(result);
123284990Scy	TEST_ASSERT_TRUE(CompareKeysAlternative(10, 13, "MD5",
124284990Scy		 "\x01\x23\x45\x67\x89\xab\xcd\xef\x01\x23\x45\x67\x89", *result));
125284990Scy
126284990Scy	result = NULL;
127284990Scy	get_key(20, &result);
128289997Sglebius	TEST_ASSERT_NOT_NULL(result);
129294569Sdelphij
130294569Sdelphij	memset(data1, 0x11, 15);
131284990Scy	TEST_ASSERT_TRUE(CompareKeysAlternative(20, 15, "MD5", data1, *result));
132284990Scy
133284990Scy	result = NULL;
134284990Scy	get_key(30, &result);
135289997Sglebius	TEST_ASSERT_NOT_NULL(result);
136294569Sdelphij
137294569Sdelphij	memset(data2, 0x01, 13);
138284990Scy	TEST_ASSERT_TRUE(CompareKeysAlternative(30, 13, "MD5", data2, *result));
139284990Scy}
140284990Scy
141289997Sglebius
142289997Sglebiusvoid
143294569Sdelphijtest_ReadKeyFileWithComments(void)
144294569Sdelphij{
145294569Sdelphij	struct key *	keys = NULL;
146294569Sdelphij	struct key *	result = NULL;
147294569Sdelphij	const char *	path = CreatePath("key-test-comments", INPUT_DIR);
148294569Sdelphij	char 		data[15];
149284990Scy
150293650Sglebius	TEST_ASSERT_NOT_NULL(path);
151293650Sglebius	TEST_ASSERT_EQUAL(2, auth_init(path, &keys));
152289997Sglebius	TEST_ASSERT_NOT_NULL(keys);
153294569Sdelphij	DestroyPath(path);
154284990Scy
155284990Scy	get_key(10, &result);
156289997Sglebius	TEST_ASSERT_NOT_NULL(result);
157294569Sdelphij
158294569Sdelphij	memset(data, 0x01, 15);
159284990Scy	TEST_ASSERT_TRUE(CompareKeysAlternative(10, 15, "MD5", data, *result));
160284990Scy
161284990Scy	result = NULL;
162284990Scy	get_key(34, &result);
163289997Sglebius	TEST_ASSERT_NOT_NULL(result);
164284990Scy	TEST_ASSERT_TRUE(CompareKeysAlternative(34, 3, "MD5", "xyz", *result));
165284990Scy}
166284990Scy
167289997Sglebius
168289997Sglebiusvoid
169294569Sdelphijtest_ReadKeyFileWithInvalidHex(void)
170294569Sdelphij{
171294569Sdelphij	struct key *	keys = NULL;
172294569Sdelphij	struct key *	result = NULL;
173294569Sdelphij	const char *	path = CreatePath("key-test-invalid-hex", INPUT_DIR);
174294569Sdelphij	char 		data[15];
175284990Scy
176293650Sglebius	TEST_ASSERT_NOT_NULL(path);
177293650Sglebius	TEST_ASSERT_EQUAL(1, auth_init(path, &keys));
178289997Sglebius	TEST_ASSERT_NOT_NULL(keys);
179294569Sdelphij	DestroyPath(path);
180284990Scy
181284990Scy	get_key(10, &result);
182289997Sglebius	TEST_ASSERT_NOT_NULL(result);
183294569Sdelphij
184294569Sdelphij	memset(data, 0x01, 15);
185284990Scy	TEST_ASSERT_TRUE(CompareKeysAlternative(10, 15, "MD5", data, *result));
186284990Scy
187284990Scy	result = NULL;
188294569Sdelphij	get_key(30, &result); /* Should not exist, and result should remain NULL. */
189289997Sglebius	TEST_ASSERT_NULL(result);
190284990Scy}
191