summaryrefslogtreecommitdiffstats
path: root/contrib/ntp/sntp/tests/kodFile.c
blob: f99d8cf2084cc5405077143caa145294e9ce2314 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "config.h"

#include "ntp_types.h"
#include "ntp_stdlib.h" // For estrdup()
#include "fileHandlingTest.h"
#include "kod_management.h"

#include "unity.h"

/*
 * We access some parts of the kod database directly, without
 * going through the public interface
 */
extern int kod_db_cnt;
extern struct kod_entry** kod_db;
extern char* kod_db_file;

void setUp(void);
void test_ReadEmptyFile(void);
void test_ReadCorrectFile(void);
void test_ReadFileWithBlankLines(void);
void test_WriteEmptyFile(void);
void test_WriteFileWithSingleEntry(void);
void test_WriteFileWithMultipleEntries(void);


void
setUp(void) {
	kod_db_cnt = 0;
	kod_db = NULL;
	init_lib();
}


void
test_ReadEmptyFile(void) {
	kod_init_kod_db(CreatePath("kod-test-empty", INPUT_DIR), TRUE);

	TEST_ASSERT_EQUAL(0, kod_db_cnt);
}


void
test_ReadCorrectFile(void) {
	kod_init_kod_db(CreatePath("kod-test-correct", INPUT_DIR), TRUE);
	
	TEST_ASSERT_EQUAL(2, kod_db_cnt);

	struct kod_entry* res;

	TEST_ASSERT_EQUAL(1, search_entry("192.0.2.5", &res));
	TEST_ASSERT_EQUAL_STRING("DENY", res->type);
	TEST_ASSERT_EQUAL_STRING("192.0.2.5", res->hostname);
	TEST_ASSERT_EQUAL(0x12345678, res->timestamp);

	TEST_ASSERT_EQUAL(1, search_entry("192.0.2.100", &res));
	TEST_ASSERT_EQUAL_STRING("RSTR", res->type);
	TEST_ASSERT_EQUAL_STRING("192.0.2.100", res->hostname);
	TEST_ASSERT_EQUAL(0xfff, res->timestamp);
}


void
test_ReadFileWithBlankLines(void) {
	kod_init_kod_db(CreatePath("kod-test-blanks", INPUT_DIR), TRUE);

	TEST_ASSERT_EQUAL(3, kod_db_cnt);

	struct kod_entry* res;

	TEST_ASSERT_EQUAL(1, search_entry("192.0.2.5", &res));
	TEST_ASSERT_EQUAL_STRING("DENY", res->type);
	TEST_ASSERT_EQUAL_STRING("192.0.2.5", res->hostname);
	TEST_ASSERT_EQUAL(0x12345678, res->timestamp);

	TEST_ASSERT_EQUAL(1, search_entry("192.0.2.100", &res));
	TEST_ASSERT_EQUAL_STRING("RSTR", res->type);
	TEST_ASSERT_EQUAL_STRING("192.0.2.100", res->hostname);
	TEST_ASSERT_EQUAL(0xfff, res->timestamp);

	TEST_ASSERT_EQUAL(1, search_entry("example.com", &res));
	TEST_ASSERT_EQUAL_STRING("DENY", res->type);
	TEST_ASSERT_EQUAL_STRING("example.com", res->hostname);
	TEST_ASSERT_EQUAL(0xabcd, res->timestamp);
}


void
test_WriteEmptyFile(void) {
	kod_db_file = estrdup("kod-output-blank");
	write_kod_db();

	// Open file and ensure that the filesize is 0 bytes.
	FILE * is = fopen(kod_db_file, "rb");
	TEST_ASSERT_NOT_NULL(is);

	TEST_ASSERT_EQUAL(0, GetFileSize(is));

	fclose(is);
}


void
test_WriteFileWithSingleEntry(void) {
	kod_db_file = estrdup("kod-output-single"); 
	add_entry("host1", "DENY");

	// Here we must manipulate the timestamps, so they match the one in
	// the expected file.

	kod_db[0]->timestamp = 1;

	write_kod_db();

	// Open file and compare sizes.
	FILE * actual = fopen(kod_db_file, "rb");
	FILE * expected = fopen(CreatePath("kod-expected-single", INPUT_DIR),"rb");

	TEST_ASSERT_NOT_NULL(actual);
	TEST_ASSERT_NOT_NULL(expected);

	TEST_ASSERT_EQUAL(GetFileSize(expected), GetFileSize(actual));
	
	TEST_ASSERT_TRUE(CompareFileContent(expected, actual));
}


void
test_WriteFileWithMultipleEntries(void) {
	kod_db_file = estrdup("kod-output-multiple");
	add_entry("example.com", "RATE");
	add_entry("192.0.2.1", "DENY");
	add_entry("192.0.2.5", "RSTR");

	//
	// Manipulate timestamps. This is a bit of a hack, ideally these
	// tests should not care about the internal representation.
	//
	kod_db[0]->timestamp = 0xabcd;
	kod_db[1]->timestamp = 0xabcd;
	kod_db[2]->timestamp = 0xabcd;

	write_kod_db();

	// Open file and compare sizes and content.
	FILE * actual = fopen(kod_db_file, "rb");
	FILE * expected = fopen(CreatePath("kod-expected-multiple", INPUT_DIR),"rb");

	TEST_ASSERT_NOT_NULL(actual);
	TEST_ASSERT_NOT_NULL(expected);


	TEST_ASSERT_EQUAL(GetFileSize(expected), GetFileSize(actual));

	TEST_ASSERT_TRUE(CompareFileContent(expected, actual));
}
OpenPOWER on IntegriCloud