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