keyFile.c revision 294905
1104834Sobrien#include "config.h"
2104834Sobrien#include "fileHandlingTest.h"
3104834Sobrien
4104834Sobrien#include "ntp_stdlib.h"
5104834Sobrien#include "ntp_types.h"
6104834Sobrien#include "crypto.h"
7104834Sobrien
8104834Sobrien#include "unity.h"
9104834Sobrien
10104834Sobrienbool CompareKeys(struct key expected, struct key actual);
11104834Sobrienbool CompareKeysAlternative(int key_id,int key_len,const char* type,const char* key_seq,struct key actual);
12104834Sobrienvoid test_ReadEmptyKeyFile(void);
13104834Sobrienvoid test_ReadASCIIKeys(void);
14104834Sobrienvoid test_ReadHexKeys(void);
15104834Sobrienvoid test_ReadKeyFileWithComments(void);
16104834Sobrienvoid test_ReadKeyFileWithInvalidHex(void);
17104834Sobrien
18218822Sdim
19104834Sobrienbool
20104834SobrienCompareKeys(
21104834Sobrien	struct key	expected,
22104834Sobrien	struct key	actual
23104834Sobrien	)
24104834Sobrien{
25104834Sobrien	if (expected.key_id != actual.key_id) {
26104834Sobrien		printf("Expected key_id: %d but was: %d\n",
27104834Sobrien		       expected.key_id, actual.key_id);
28104834Sobrien		return FALSE;
29104834Sobrien	}
30104834Sobrien	if (expected.key_len != actual.key_len) {
31104834Sobrien		printf("Expected key_len: %d but was: %d\n",
32104834Sobrien		       expected.key_len, actual.key_len);
33104834Sobrien		return FALSE;
34104834Sobrien	}
35104834Sobrien	if (strcmp(expected.type, actual.type) != 0) {
36104834Sobrien		printf("Expected key_type: %s but was: %s\n",
37104834Sobrien		       expected.type, actual.type);
38104834Sobrien		return FALSE;
39104834Sobrien
40104834Sobrien	}
41104834Sobrien	if (memcmp(expected.key_seq, actual.key_seq, expected.key_len) != 0) {
42104834Sobrien		printf("Key mismatch!\n");
43104834Sobrien		return FALSE;
44104834Sobrien	}
45104834Sobrien	return TRUE;
46104834Sobrien}
47104834Sobrien
48104834Sobrien
49104834Sobrienbool
50104834SobrienCompareKeysAlternative(
51104834Sobrien	int		key_id,
52104834Sobrien	int		key_len,
53104834Sobrien	const char *	type,
54	const char *	key_seq,
55	struct key	actual
56	)
57{
58	struct key	temp;
59
60	temp.key_id = key_id;
61	temp.key_len = key_len;
62	strlcpy(temp.type, type, sizeof(temp.type));
63	memcpy(temp.key_seq, key_seq, key_len);
64
65	return CompareKeys(temp, actual);
66}
67
68
69void
70test_ReadEmptyKeyFile(void)
71{
72	struct key *	keys = NULL;
73	const char *	path = CreatePath("key-test-empty", INPUT_DIR);
74
75	TEST_ASSERT_NOT_NULL(path);
76	TEST_ASSERT_EQUAL(0, auth_init(path, &keys));
77	TEST_ASSERT_NULL(keys);
78
79	DestroyPath(path);
80}
81
82
83void
84test_ReadASCIIKeys(void)
85{
86	struct key *	keys = NULL;
87	struct key *	result = NULL;
88	const char *	path = CreatePath("key-test-ascii", INPUT_DIR);
89
90	TEST_ASSERT_NOT_NULL(path);
91	TEST_ASSERT_EQUAL(2, auth_init(path, &keys));
92	TEST_ASSERT_NOT_NULL(keys);
93
94	DestroyPath(path);
95
96	get_key(40, &result);
97	TEST_ASSERT_NOT_NULL(result);
98	TEST_ASSERT_TRUE(CompareKeysAlternative(40, 11, "MD5", "asciikeyTwo", *result));
99
100	result = NULL;
101	get_key(50, &result);
102	TEST_ASSERT_NOT_NULL(result);
103	TEST_ASSERT_TRUE(CompareKeysAlternative(50, 11, "MD5", "asciikeyOne", *result));
104}
105
106
107void
108test_ReadHexKeys(void)
109{
110	struct key *	keys = NULL;
111	struct key *	result = NULL;
112	const char *	path = CreatePath("key-test-hex", INPUT_DIR);
113	char 		data1[15];
114	char 		data2[13];
115
116	TEST_ASSERT_NOT_NULL(path);
117	TEST_ASSERT_EQUAL(3, auth_init(path, &keys));
118	TEST_ASSERT_NOT_NULL(keys);
119	DestroyPath(path);
120
121	get_key(10, &result);
122	TEST_ASSERT_NOT_NULL(result);
123	TEST_ASSERT_TRUE(CompareKeysAlternative(10, 13, "MD5",
124		 "\x01\x23\x45\x67\x89\xab\xcd\xef\x01\x23\x45\x67\x89", *result));
125
126	result = NULL;
127	get_key(20, &result);
128	TEST_ASSERT_NOT_NULL(result);
129
130	memset(data1, 0x11, 15);
131	TEST_ASSERT_TRUE(CompareKeysAlternative(20, 15, "MD5", data1, *result));
132
133	result = NULL;
134	get_key(30, &result);
135	TEST_ASSERT_NOT_NULL(result);
136
137	memset(data2, 0x01, 13);
138	TEST_ASSERT_TRUE(CompareKeysAlternative(30, 13, "MD5", data2, *result));
139}
140
141
142void
143test_ReadKeyFileWithComments(void)
144{
145	struct key *	keys = NULL;
146	struct key *	result = NULL;
147	const char *	path = CreatePath("key-test-comments", INPUT_DIR);
148	char 		data[15];
149
150	TEST_ASSERT_NOT_NULL(path);
151	TEST_ASSERT_EQUAL(2, auth_init(path, &keys));
152	TEST_ASSERT_NOT_NULL(keys);
153	DestroyPath(path);
154
155	get_key(10, &result);
156	TEST_ASSERT_NOT_NULL(result);
157
158	memset(data, 0x01, 15);
159	TEST_ASSERT_TRUE(CompareKeysAlternative(10, 15, "MD5", data, *result));
160
161	result = NULL;
162	get_key(34, &result);
163	TEST_ASSERT_NOT_NULL(result);
164	TEST_ASSERT_TRUE(CompareKeysAlternative(34, 3, "MD5", "xyz", *result));
165}
166
167
168void
169test_ReadKeyFileWithInvalidHex(void)
170{
171	struct key *	keys = NULL;
172	struct key *	result = NULL;
173	const char *	path = CreatePath("key-test-invalid-hex", INPUT_DIR);
174	char 		data[15];
175
176	TEST_ASSERT_NOT_NULL(path);
177	TEST_ASSERT_EQUAL(1, auth_init(path, &keys));
178	TEST_ASSERT_NOT_NULL(keys);
179	DestroyPath(path);
180
181	get_key(10, &result);
182	TEST_ASSERT_NOT_NULL(result);
183
184	memset(data, 0x01, 15);
185	TEST_ASSERT_TRUE(CompareKeysAlternative(10, 15, "MD5", data, *result));
186
187	result = NULL;
188	get_key(30, &result); /* Should not exist, and result should remain NULL. */
189	TEST_ASSERT_NULL(result);
190}
191