1 /* $NetBSD: keyFile.c,v 1.3 2020/05/25 20:47:35 christos Exp $ */
2
3 #include "config.h"
4 #include "fileHandlingTest.h"
5
6 #include "ntp_stdlib.h"
7 #include "ntp_types.h"
8 #include "crypto.h"
9
10 #include "unity.h"
11
12 bool CompareKeys(struct key expected, struct key actual);
13 bool CompareKeysAlternative(int key_id,int key_len,const char* type,const char* key_seq,struct key actual);
14 void test_ReadEmptyKeyFile(void);
15 void test_ReadASCIIKeys(void);
16 void test_ReadHexKeys(void);
17 void test_ReadKeyFileWithComments(void);
18 void test_ReadKeyFileWithInvalidHex(void);
19
20
21 bool
CompareKeys(struct key expected,struct key actual)22 CompareKeys(
23 struct key expected,
24 struct key actual
25 )
26 {
27 if (expected.key_id != actual.key_id) {
28 printf("Expected key_id: %d but was: %d\n",
29 expected.key_id, actual.key_id);
30 return FALSE;
31 }
32 if (expected.key_len != actual.key_len) {
33 printf("Expected key_len: %d but was: %d\n",
34 expected.key_len, actual.key_len);
35 return FALSE;
36 }
37 if (strcmp(expected.typen, actual.typen) != 0) {
38 printf("Expected key_type: %s but was: %s\n",
39 expected.typen, actual.typen);
40 return FALSE;
41
42 }
43 if (memcmp(expected.key_seq, actual.key_seq, expected.key_len) != 0) {
44 printf("Key mismatch!\n");
45 return FALSE;
46 }
47 return TRUE;
48 }
49
50
51 bool
CompareKeysAlternative(int key_id,int key_len,const char * type,const char * key_seq,struct key actual)52 CompareKeysAlternative(
53 int key_id,
54 int key_len,
55 const char * type,
56 const char * key_seq,
57 struct key actual
58 )
59 {
60 struct key temp;
61
62 temp.key_id = key_id;
63 temp.key_len = key_len;
64 strlcpy(temp.typen, type, sizeof(temp.typen));
65 memcpy(temp.key_seq, key_seq, key_len);
66
67 return CompareKeys(temp, actual);
68 }
69
70
71 void
test_ReadEmptyKeyFile(void)72 test_ReadEmptyKeyFile(void)
73 {
74 struct key * keys = NULL;
75 const char * path = CreatePath("key-test-empty", INPUT_DIR);
76
77 TEST_ASSERT_NOT_NULL(path);
78 TEST_ASSERT_EQUAL(0, auth_init(path, &keys));
79 TEST_ASSERT_NULL(keys);
80
81 DestroyPath(path);
82 }
83
84
85 void
test_ReadASCIIKeys(void)86 test_ReadASCIIKeys(void)
87 {
88 struct key * keys = NULL;
89 struct key * result = NULL;
90 const char * path = CreatePath("key-test-ascii", INPUT_DIR);
91
92 TEST_ASSERT_NOT_NULL(path);
93 TEST_ASSERT_EQUAL(2, auth_init(path, &keys));
94 TEST_ASSERT_NOT_NULL(keys);
95
96 DestroyPath(path);
97
98 get_key(40, &result);
99 TEST_ASSERT_NOT_NULL(result);
100 TEST_ASSERT_TRUE(CompareKeysAlternative(40, 11, "MD5", "asciikeyTwo", *result));
101
102 result = NULL;
103 get_key(50, &result);
104 TEST_ASSERT_NOT_NULL(result);
105 TEST_ASSERT_TRUE(CompareKeysAlternative(50, 11, "MD5", "asciikeyOne", *result));
106 }
107
108
109 void
test_ReadHexKeys(void)110 test_ReadHexKeys(void)
111 {
112 struct key * keys = NULL;
113 struct key * result = NULL;
114 const char * path = CreatePath("key-test-hex", INPUT_DIR);
115 char data1[15];
116 char data2[13];
117
118 TEST_ASSERT_NOT_NULL(path);
119 TEST_ASSERT_EQUAL(3, auth_init(path, &keys));
120 TEST_ASSERT_NOT_NULL(keys);
121 DestroyPath(path);
122
123 get_key(10, &result);
124 TEST_ASSERT_NOT_NULL(result);
125 TEST_ASSERT_TRUE(CompareKeysAlternative(10, 13, "MD5",
126 "\x01\x23\x45\x67\x89\xab\xcd\xef\x01\x23\x45\x67\x89", *result));
127
128 result = NULL;
129 get_key(20, &result);
130 TEST_ASSERT_NOT_NULL(result);
131
132 memset(data1, 0x11, 15);
133 TEST_ASSERT_TRUE(CompareKeysAlternative(20, 15, "MD5", data1, *result));
134
135 result = NULL;
136 get_key(30, &result);
137 TEST_ASSERT_NOT_NULL(result);
138
139 memset(data2, 0x01, 13);
140 TEST_ASSERT_TRUE(CompareKeysAlternative(30, 13, "MD5", data2, *result));
141 }
142
143
144 void
test_ReadKeyFileWithComments(void)145 test_ReadKeyFileWithComments(void)
146 {
147 struct key * keys = NULL;
148 struct key * result = NULL;
149 const char * path = CreatePath("key-test-comments", INPUT_DIR);
150 char data[15];
151
152 TEST_ASSERT_NOT_NULL(path);
153 TEST_ASSERT_EQUAL(2, auth_init(path, &keys));
154 TEST_ASSERT_NOT_NULL(keys);
155 DestroyPath(path);
156
157 get_key(10, &result);
158 TEST_ASSERT_NOT_NULL(result);
159
160 memset(data, 0x01, 15);
161 TEST_ASSERT_TRUE(CompareKeysAlternative(10, 15, "MD5", data, *result));
162
163 result = NULL;
164 get_key(34, &result);
165 TEST_ASSERT_NOT_NULL(result);
166 TEST_ASSERT_TRUE(CompareKeysAlternative(34, 3, "MD5", "xyz", *result));
167 }
168
169
170 void
test_ReadKeyFileWithInvalidHex(void)171 test_ReadKeyFileWithInvalidHex(void)
172 {
173 struct key * keys = NULL;
174 struct key * result = NULL;
175 const char * path = CreatePath("key-test-invalid-hex", INPUT_DIR);
176 char data[15];
177
178 TEST_ASSERT_NOT_NULL(path);
179 TEST_ASSERT_EQUAL(1, auth_init(path, &keys));
180 TEST_ASSERT_NOT_NULL(keys);
181 DestroyPath(path);
182
183 get_key(10, &result);
184 TEST_ASSERT_NOT_NULL(result);
185
186 memset(data, 0x01, 15);
187 TEST_ASSERT_TRUE(CompareKeysAlternative(10, 15, "MD5", data, *result));
188
189 result = NULL;
190 get_key(30, &result); /* Should not exist, and result should remain NULL. */
191 TEST_ASSERT_NULL(result);
192 }
193