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
10289764Sglebiusbool CompareKeys(struct key expected, struct key actual);
11289764Sglebiusbool CompareKeysAlternative(int key_id,int key_len,const char* type,const char* key_seq,struct key actual);
12289764Sglebiusvoid test_ReadEmptyKeyFile(void);
13289764Sglebiusvoid test_ReadASCIIKeys(void);
14289764Sglebiusvoid test_ReadHexKeys(void);
15289764Sglebiusvoid test_ReadKeyFileWithComments(void);
16289764Sglebiusvoid test_ReadKeyFileWithInvalidHex(void);
17284990Scy
18284990Scy
19289764Sglebiusbool
20294554SdelphijCompareKeys(
21294554Sdelphij	struct key	expected,
22294554Sdelphij	struct key	actual
23294554Sdelphij	)
24294554Sdelphij{
25294554Sdelphij	if (expected.key_id != actual.key_id) {
26294554Sdelphij		printf("Expected key_id: %d but was: %d\n",
27294554Sdelphij		       expected.key_id, actual.key_id);
28284990Scy		return FALSE;
29284990Scy	}
30294554Sdelphij	if (expected.key_len != actual.key_len) {
31294554Sdelphij		printf("Expected key_len: %d but was: %d\n",
32294554Sdelphij		       expected.key_len, actual.key_len);
33284990Scy		return FALSE;
34284990Scy	}
35330106Sdelphij	if (strcmp(expected.typen, actual.typen) != 0) {
36294554Sdelphij		printf("Expected key_type: %s but was: %s\n",
37330106Sdelphij		       expected.typen, actual.typen);
38284990Scy		return FALSE;
39284990Scy
40284990Scy	}
41294554Sdelphij	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
48289764Sglebius
49289764Sglebiusbool
50294554SdelphijCompareKeysAlternative(
51294554Sdelphij	int		key_id,
52294554Sdelphij	int		key_len,
53294554Sdelphij	const char *	type,
54294554Sdelphij	const char *	key_seq,
55294554Sdelphij	struct key	actual
56294554Sdelphij	)
57294554Sdelphij{
58294554Sdelphij	struct key	temp;
59284990Scy
60284990Scy	temp.key_id = key_id;
61284990Scy	temp.key_len = key_len;
62330106Sdelphij	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
69289764Sglebiusvoid
70294554Sdelphijtest_ReadEmptyKeyFile(void)
71294554Sdelphij{
72294554Sdelphij	struct key *	keys = NULL;
73294554Sdelphij	const char *	path = CreatePath("key-test-empty", INPUT_DIR);
74284990Scy
75293423Sdelphij	TEST_ASSERT_NOT_NULL(path);
76293423Sdelphij	TEST_ASSERT_EQUAL(0, auth_init(path, &keys));
77289764Sglebius	TEST_ASSERT_NULL(keys);
78293423Sdelphij
79294554Sdelphij	DestroyPath(path);
80284990Scy}
81284990Scy
82289764Sglebius
83289764Sglebiusvoid
84294554Sdelphijtest_ReadASCIIKeys(void)
85294554Sdelphij{
86294554Sdelphij	struct key *	keys = NULL;
87294554Sdelphij	struct key *	result = NULL;
88294554Sdelphij	const char *	path = CreatePath("key-test-ascii", INPUT_DIR);
89284990Scy
90293423Sdelphij	TEST_ASSERT_NOT_NULL(path);
91293423Sdelphij	TEST_ASSERT_EQUAL(2, auth_init(path, &keys));
92289764Sglebius	TEST_ASSERT_NOT_NULL(keys);
93284990Scy
94294554Sdelphij	DestroyPath(path);
95293423Sdelphij
96284990Scy	get_key(40, &result);
97289764Sglebius	TEST_ASSERT_NOT_NULL(result);
98284990Scy	TEST_ASSERT_TRUE(CompareKeysAlternative(40, 11, "MD5", "asciikeyTwo", *result));
99284990Scy
100284990Scy	result = NULL;
101284990Scy	get_key(50, &result);
102289764Sglebius	TEST_ASSERT_NOT_NULL(result);
103284990Scy	TEST_ASSERT_TRUE(CompareKeysAlternative(50, 11, "MD5", "asciikeyOne", *result));
104284990Scy}
105284990Scy
106289764Sglebius
107289764Sglebiusvoid
108294554Sdelphijtest_ReadHexKeys(void)
109294554Sdelphij{
110294554Sdelphij	struct key *	keys = NULL;
111294554Sdelphij	struct key *	result = NULL;
112294554Sdelphij	const char *	path = CreatePath("key-test-hex", INPUT_DIR);
113294554Sdelphij	char 		data1[15];
114294554Sdelphij	char 		data2[13];
115284990Scy
116293423Sdelphij	TEST_ASSERT_NOT_NULL(path);
117293423Sdelphij	TEST_ASSERT_EQUAL(3, auth_init(path, &keys));
118289764Sglebius	TEST_ASSERT_NOT_NULL(keys);
119294554Sdelphij	DestroyPath(path);
120284990Scy
121284990Scy	get_key(10, &result);
122289764Sglebius	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);
128289764Sglebius	TEST_ASSERT_NOT_NULL(result);
129294554Sdelphij
130294554Sdelphij	memset(data1, 0x11, 15);
131284990Scy	TEST_ASSERT_TRUE(CompareKeysAlternative(20, 15, "MD5", data1, *result));
132284990Scy
133284990Scy	result = NULL;
134284990Scy	get_key(30, &result);
135289764Sglebius	TEST_ASSERT_NOT_NULL(result);
136294554Sdelphij
137294554Sdelphij	memset(data2, 0x01, 13);
138284990Scy	TEST_ASSERT_TRUE(CompareKeysAlternative(30, 13, "MD5", data2, *result));
139284990Scy}
140284990Scy
141289764Sglebius
142289764Sglebiusvoid
143294554Sdelphijtest_ReadKeyFileWithComments(void)
144294554Sdelphij{
145294554Sdelphij	struct key *	keys = NULL;
146294554Sdelphij	struct key *	result = NULL;
147294554Sdelphij	const char *	path = CreatePath("key-test-comments", INPUT_DIR);
148294554Sdelphij	char 		data[15];
149284990Scy
150293423Sdelphij	TEST_ASSERT_NOT_NULL(path);
151293423Sdelphij	TEST_ASSERT_EQUAL(2, auth_init(path, &keys));
152289764Sglebius	TEST_ASSERT_NOT_NULL(keys);
153294554Sdelphij	DestroyPath(path);
154284990Scy
155284990Scy	get_key(10, &result);
156289764Sglebius	TEST_ASSERT_NOT_NULL(result);
157294554Sdelphij
158294554Sdelphij	memset(data, 0x01, 15);
159284990Scy	TEST_ASSERT_TRUE(CompareKeysAlternative(10, 15, "MD5", data, *result));
160284990Scy
161284990Scy	result = NULL;
162284990Scy	get_key(34, &result);
163289764Sglebius	TEST_ASSERT_NOT_NULL(result);
164284990Scy	TEST_ASSERT_TRUE(CompareKeysAlternative(34, 3, "MD5", "xyz", *result));
165284990Scy}
166284990Scy
167289764Sglebius
168289764Sglebiusvoid
169294554Sdelphijtest_ReadKeyFileWithInvalidHex(void)
170294554Sdelphij{
171294554Sdelphij	struct key *	keys = NULL;
172294554Sdelphij	struct key *	result = NULL;
173294554Sdelphij	const char *	path = CreatePath("key-test-invalid-hex", INPUT_DIR);
174294554Sdelphij	char 		data[15];
175284990Scy
176293423Sdelphij	TEST_ASSERT_NOT_NULL(path);
177293423Sdelphij	TEST_ASSERT_EQUAL(1, auth_init(path, &keys));
178289764Sglebius	TEST_ASSERT_NOT_NULL(keys);
179294554Sdelphij	DestroyPath(path);
180284990Scy
181284990Scy	get_key(10, &result);
182289764Sglebius	TEST_ASSERT_NOT_NULL(result);
183294554Sdelphij
184294554Sdelphij	memset(data, 0x01, 15);
185284990Scy	TEST_ASSERT_TRUE(CompareKeysAlternative(10, 15, "MD5", data, *result));
186284990Scy
187284990Scy	result = NULL;
188294554Sdelphij	get_key(30, &result); /* Should not exist, and result should remain NULL. */
189289764Sglebius	TEST_ASSERT_NULL(result);
190284990Scy}
191