]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/sntp/tests/keyFile.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / ntp / sntp / tests / keyFile.c
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 //typedef int bool;
11
12
13 bool CompareKeys(struct key expected, struct key actual) {
14         if (expected.key_id != actual.key_id){
15                 printf("Expected key_id: %d", expected.key_id);
16                 printf(" but was: %d\n", actual.key_id);
17                 return FALSE;
18         }
19         if (expected.key_len != actual.key_len){
20                 printf("Expected key_len: %d", expected.key_len);
21                 printf(" but was: %d\n", actual.key_len);
22                 return FALSE;
23         }
24         if (strcmp(expected.type, actual.type) != 0){
25                 printf("Expected key_type: %s", expected.type);
26                 printf(" but was: %s\n", actual.type);
27                 return FALSE;
28
29         }
30         if (memcmp(expected.key_seq, actual.key_seq, expected.key_len) != 0){
31                 printf("Key mismatch!\n");
32                 return FALSE;           
33         }
34         return TRUE;
35 }
36
37 bool CompareKeysAlternative(int key_id,
38                int key_len,
39                const char* type,
40                const char* key_seq,
41                struct key actual) {
42         struct key temp;
43
44         temp.key_id = key_id;
45         temp.key_len = key_len;
46         strlcpy(temp.type, type, sizeof(temp.type));
47         memcpy(temp.key_seq, key_seq, key_len);
48
49         return CompareKeys(temp, actual);
50 }
51
52
53 void test_ReadEmptyKeyFile() {
54         struct key* keys = NULL;
55
56         TEST_ASSERT_EQUAL(0, auth_init(CreatePath("key-test-empty", INPUT_DIR), &keys));
57
58         TEST_ASSERT_TRUE(keys == NULL);
59 }
60
61 void test_ReadASCIIKeys() {
62         struct key* keys = NULL;
63
64         TEST_ASSERT_EQUAL(2, auth_init(CreatePath("key-test-ascii", INPUT_DIR), &keys));
65
66         TEST_ASSERT_TRUE(keys != NULL);
67
68         struct key* result = NULL;
69         get_key(40, &result);
70         TEST_ASSERT_TRUE(result != NULL);
71         TEST_ASSERT_TRUE(CompareKeysAlternative(40, 11, "MD5", "asciikeyTwo", *result));
72
73         result = NULL;
74         get_key(50, &result);
75         TEST_ASSERT_TRUE(result != NULL);
76         TEST_ASSERT_TRUE(CompareKeysAlternative(50, 11, "MD5", "asciikeyOne", *result));
77 }
78
79 void test_ReadHexKeys() {
80         struct key* keys = NULL;
81
82         TEST_ASSERT_EQUAL(3, auth_init(CreatePath("key-test-hex", INPUT_DIR), &keys));
83
84         TEST_ASSERT_TRUE(keys != NULL);
85
86         struct key* result = NULL;
87         get_key(10, &result);
88         TEST_ASSERT_TRUE(result != NULL);
89         TEST_ASSERT_TRUE(CompareKeysAlternative(10, 13, "MD5",
90                  "\x01\x23\x45\x67\x89\xab\xcd\xef\x01\x23\x45\x67\x89", *result));
91
92         result = NULL;
93         get_key(20, &result);
94         TEST_ASSERT_TRUE(result != NULL);
95         char data1[15]; memset(data1, 0x11, 15);
96         TEST_ASSERT_TRUE(CompareKeysAlternative(20, 15, "MD5", data1, *result));
97
98         result = NULL;
99         get_key(30, &result);
100         TEST_ASSERT_TRUE(result != NULL);
101         char data2[13]; memset(data2, 0x01, 13);
102         TEST_ASSERT_TRUE(CompareKeysAlternative(30, 13, "MD5", data2, *result));
103 }
104
105 void test_ReadKeyFileWithComments() {
106         struct key* keys = NULL;
107
108         TEST_ASSERT_EQUAL(2, auth_init(CreatePath("key-test-comments", INPUT_DIR), &keys));
109         
110         TEST_ASSERT_TRUE(keys != NULL);
111
112         struct key* result = NULL;
113         get_key(10, &result);
114         TEST_ASSERT_TRUE(result != NULL);
115         char data[15]; memset(data, 0x01, 15);
116         TEST_ASSERT_TRUE(CompareKeysAlternative(10, 15, "MD5", data, *result));
117
118         result = NULL;
119         get_key(34, &result);
120         TEST_ASSERT_TRUE(result != NULL);
121         TEST_ASSERT_TRUE(CompareKeysAlternative(34, 3, "MD5", "xyz", *result));
122 }
123
124 void test_ReadKeyFileWithInvalidHex() {
125         struct key* keys = NULL;
126
127         TEST_ASSERT_EQUAL(1, auth_init(CreatePath("key-test-invalid-hex", INPUT_DIR), &keys));
128
129         TEST_ASSERT_TRUE(keys != NULL);
130
131         struct key* result = NULL;
132         get_key(10, &result);
133         TEST_ASSERT_TRUE(result != NULL);
134         char data[15]; memset(data, 0x01, 15);
135         TEST_ASSERT_TRUE(CompareKeysAlternative(10, 15, "MD5", data, *result));
136
137         result = NULL;
138         get_key(30, &result); // Should not exist, and result should remain NULL.
139         TEST_ASSERT_TRUE(result == NULL);
140 }