1276da39aSCy Schubert #include "config.h"
2276da39aSCy Schubert #include "fileHandlingTest.h"
3276da39aSCy Schubert
4276da39aSCy Schubert #include "ntp_stdlib.h"
5276da39aSCy Schubert #include "ntp_types.h"
6276da39aSCy Schubert #include "crypto.h"
7276da39aSCy Schubert
8276da39aSCy Schubert #include "unity.h"
9276da39aSCy Schubert
109034852cSGleb Smirnoff bool CompareKeys(struct key expected, struct key actual);
119034852cSGleb Smirnoff bool CompareKeysAlternative(int key_id,int key_len,const char* type,const char* key_seq,struct key actual);
129034852cSGleb Smirnoff void test_ReadEmptyKeyFile(void);
139034852cSGleb Smirnoff void test_ReadASCIIKeys(void);
149034852cSGleb Smirnoff void test_ReadHexKeys(void);
159034852cSGleb Smirnoff void test_ReadKeyFileWithComments(void);
169034852cSGleb Smirnoff void test_ReadKeyFileWithInvalidHex(void);
17276da39aSCy Schubert
18276da39aSCy Schubert
199034852cSGleb Smirnoff bool
CompareKeys(struct key expected,struct key actual)2068ba7e87SXin LI CompareKeys(
2168ba7e87SXin LI struct key expected,
2268ba7e87SXin LI struct key actual
2368ba7e87SXin LI )
2468ba7e87SXin LI {
25276da39aSCy Schubert if (expected.key_id != actual.key_id) {
2668ba7e87SXin LI printf("Expected key_id: %d but was: %d\n",
2768ba7e87SXin LI expected.key_id, actual.key_id);
28276da39aSCy Schubert return FALSE;
29276da39aSCy Schubert }
30276da39aSCy Schubert if (expected.key_len != actual.key_len) {
3168ba7e87SXin LI printf("Expected key_len: %d but was: %d\n",
3268ba7e87SXin LI expected.key_len, actual.key_len);
33276da39aSCy Schubert return FALSE;
34276da39aSCy Schubert }
35*09100258SXin LI if (strcmp(expected.typen, actual.typen) != 0) {
3668ba7e87SXin LI printf("Expected key_type: %s but was: %s\n",
37*09100258SXin LI expected.typen, actual.typen);
38276da39aSCy Schubert return FALSE;
39276da39aSCy Schubert
40276da39aSCy Schubert }
41276da39aSCy Schubert if (memcmp(expected.key_seq, actual.key_seq, expected.key_len) != 0) {
42276da39aSCy Schubert printf("Key mismatch!\n");
43276da39aSCy Schubert return FALSE;
44276da39aSCy Schubert }
45276da39aSCy Schubert return TRUE;
46276da39aSCy Schubert }
47276da39aSCy Schubert
489034852cSGleb Smirnoff
499034852cSGleb Smirnoff bool
CompareKeysAlternative(int key_id,int key_len,const char * type,const char * key_seq,struct key actual)5068ba7e87SXin LI CompareKeysAlternative(
5168ba7e87SXin LI int key_id,
52276da39aSCy Schubert int key_len,
53276da39aSCy Schubert const char * type,
54276da39aSCy Schubert const char * key_seq,
5568ba7e87SXin LI struct key actual
5668ba7e87SXin LI )
5768ba7e87SXin LI {
58276da39aSCy Schubert struct key temp;
59276da39aSCy Schubert
60276da39aSCy Schubert temp.key_id = key_id;
61276da39aSCy Schubert temp.key_len = key_len;
62*09100258SXin LI strlcpy(temp.typen, type, sizeof(temp.typen));
63276da39aSCy Schubert memcpy(temp.key_seq, key_seq, key_len);
64276da39aSCy Schubert
65276da39aSCy Schubert return CompareKeys(temp, actual);
66276da39aSCy Schubert }
67276da39aSCy Schubert
68276da39aSCy Schubert
699034852cSGleb Smirnoff void
test_ReadEmptyKeyFile(void)7068ba7e87SXin LI test_ReadEmptyKeyFile(void)
7168ba7e87SXin LI {
72276da39aSCy Schubert struct key * keys = NULL;
733311ff84SXin LI const char * path = CreatePath("key-test-empty", INPUT_DIR);
74276da39aSCy Schubert
753311ff84SXin LI TEST_ASSERT_NOT_NULL(path);
763311ff84SXin LI TEST_ASSERT_EQUAL(0, auth_init(path, &keys));
779034852cSGleb Smirnoff TEST_ASSERT_NULL(keys);
783311ff84SXin LI
7968ba7e87SXin LI DestroyPath(path);
80276da39aSCy Schubert }
81276da39aSCy Schubert
829034852cSGleb Smirnoff
839034852cSGleb Smirnoff void
test_ReadASCIIKeys(void)8468ba7e87SXin LI test_ReadASCIIKeys(void)
8568ba7e87SXin LI {
86276da39aSCy Schubert struct key * keys = NULL;
8768ba7e87SXin LI struct key * result = NULL;
883311ff84SXin LI const char * path = CreatePath("key-test-ascii", INPUT_DIR);
89276da39aSCy Schubert
903311ff84SXin LI TEST_ASSERT_NOT_NULL(path);
913311ff84SXin LI TEST_ASSERT_EQUAL(2, auth_init(path, &keys));
929034852cSGleb Smirnoff TEST_ASSERT_NOT_NULL(keys);
93276da39aSCy Schubert
9468ba7e87SXin LI DestroyPath(path);
953311ff84SXin LI
96276da39aSCy Schubert get_key(40, &result);
979034852cSGleb Smirnoff TEST_ASSERT_NOT_NULL(result);
98276da39aSCy Schubert TEST_ASSERT_TRUE(CompareKeysAlternative(40, 11, "MD5", "asciikeyTwo", *result));
99276da39aSCy Schubert
100276da39aSCy Schubert result = NULL;
101276da39aSCy Schubert get_key(50, &result);
1029034852cSGleb Smirnoff TEST_ASSERT_NOT_NULL(result);
103276da39aSCy Schubert TEST_ASSERT_TRUE(CompareKeysAlternative(50, 11, "MD5", "asciikeyOne", *result));
104276da39aSCy Schubert }
105276da39aSCy Schubert
1069034852cSGleb Smirnoff
1079034852cSGleb Smirnoff void
test_ReadHexKeys(void)10868ba7e87SXin LI test_ReadHexKeys(void)
10968ba7e87SXin LI {
110276da39aSCy Schubert struct key * keys = NULL;
11168ba7e87SXin LI struct key * result = NULL;
1123311ff84SXin LI const char * path = CreatePath("key-test-hex", INPUT_DIR);
11368ba7e87SXin LI char data1[15];
11468ba7e87SXin LI char data2[13];
115276da39aSCy Schubert
1163311ff84SXin LI TEST_ASSERT_NOT_NULL(path);
1173311ff84SXin LI TEST_ASSERT_EQUAL(3, auth_init(path, &keys));
1189034852cSGleb Smirnoff TEST_ASSERT_NOT_NULL(keys);
11968ba7e87SXin LI DestroyPath(path);
120276da39aSCy Schubert
121276da39aSCy Schubert get_key(10, &result);
1229034852cSGleb Smirnoff TEST_ASSERT_NOT_NULL(result);
123276da39aSCy Schubert TEST_ASSERT_TRUE(CompareKeysAlternative(10, 13, "MD5",
124276da39aSCy Schubert "\x01\x23\x45\x67\x89\xab\xcd\xef\x01\x23\x45\x67\x89", *result));
125276da39aSCy Schubert
126276da39aSCy Schubert result = NULL;
127276da39aSCy Schubert get_key(20, &result);
1289034852cSGleb Smirnoff TEST_ASSERT_NOT_NULL(result);
12968ba7e87SXin LI
13068ba7e87SXin LI memset(data1, 0x11, 15);
131276da39aSCy Schubert TEST_ASSERT_TRUE(CompareKeysAlternative(20, 15, "MD5", data1, *result));
132276da39aSCy Schubert
133276da39aSCy Schubert result = NULL;
134276da39aSCy Schubert get_key(30, &result);
1359034852cSGleb Smirnoff TEST_ASSERT_NOT_NULL(result);
13668ba7e87SXin LI
13768ba7e87SXin LI memset(data2, 0x01, 13);
138276da39aSCy Schubert TEST_ASSERT_TRUE(CompareKeysAlternative(30, 13, "MD5", data2, *result));
139276da39aSCy Schubert }
140276da39aSCy Schubert
1419034852cSGleb Smirnoff
1429034852cSGleb Smirnoff void
test_ReadKeyFileWithComments(void)14368ba7e87SXin LI test_ReadKeyFileWithComments(void)
14468ba7e87SXin LI {
145276da39aSCy Schubert struct key * keys = NULL;
14668ba7e87SXin LI struct key * result = NULL;
1473311ff84SXin LI const char * path = CreatePath("key-test-comments", INPUT_DIR);
14868ba7e87SXin LI char data[15];
149276da39aSCy Schubert
1503311ff84SXin LI TEST_ASSERT_NOT_NULL(path);
1513311ff84SXin LI TEST_ASSERT_EQUAL(2, auth_init(path, &keys));
1529034852cSGleb Smirnoff TEST_ASSERT_NOT_NULL(keys);
15368ba7e87SXin LI DestroyPath(path);
154276da39aSCy Schubert
155276da39aSCy Schubert get_key(10, &result);
1569034852cSGleb Smirnoff TEST_ASSERT_NOT_NULL(result);
15768ba7e87SXin LI
15868ba7e87SXin LI memset(data, 0x01, 15);
159276da39aSCy Schubert TEST_ASSERT_TRUE(CompareKeysAlternative(10, 15, "MD5", data, *result));
160276da39aSCy Schubert
161276da39aSCy Schubert result = NULL;
162276da39aSCy Schubert get_key(34, &result);
1639034852cSGleb Smirnoff TEST_ASSERT_NOT_NULL(result);
164276da39aSCy Schubert TEST_ASSERT_TRUE(CompareKeysAlternative(34, 3, "MD5", "xyz", *result));
165276da39aSCy Schubert }
166276da39aSCy Schubert
1679034852cSGleb Smirnoff
1689034852cSGleb Smirnoff void
test_ReadKeyFileWithInvalidHex(void)16968ba7e87SXin LI test_ReadKeyFileWithInvalidHex(void)
17068ba7e87SXin LI {
171276da39aSCy Schubert struct key * keys = NULL;
17268ba7e87SXin LI struct key * result = NULL;
1733311ff84SXin LI const char * path = CreatePath("key-test-invalid-hex", INPUT_DIR);
17468ba7e87SXin LI char data[15];
175276da39aSCy Schubert
1763311ff84SXin LI TEST_ASSERT_NOT_NULL(path);
1773311ff84SXin LI TEST_ASSERT_EQUAL(1, auth_init(path, &keys));
1789034852cSGleb Smirnoff TEST_ASSERT_NOT_NULL(keys);
17968ba7e87SXin LI DestroyPath(path);
180276da39aSCy Schubert
181276da39aSCy Schubert get_key(10, &result);
1829034852cSGleb Smirnoff TEST_ASSERT_NOT_NULL(result);
18368ba7e87SXin LI
18468ba7e87SXin LI memset(data, 0x01, 15);
185276da39aSCy Schubert TEST_ASSERT_TRUE(CompareKeysAlternative(10, 15, "MD5", data, *result));
186276da39aSCy Schubert
187276da39aSCy Schubert result = NULL;
18868ba7e87SXin LI get_key(30, &result); /* Should not exist, and result should remain NULL. */
1899034852cSGleb Smirnoff TEST_ASSERT_NULL(result);
190276da39aSCy Schubert }
191