kodFile.c revision 290001
1#include "config.h"
2
3#include "ntp_types.h"
4#include "ntp_stdlib.h" // For estrdup()
5#include "fileHandlingTest.h"
6#include "kod_management.h"
7
8#include "unity.h"
9
10/*
11 * We access some parts of the kod database directly, without
12 * going through the public interface
13 */
14extern int kod_db_cnt;
15extern struct kod_entry** kod_db;
16extern char* kod_db_file;
17
18void setUp(void);
19void test_ReadEmptyFile(void);
20void test_ReadCorrectFile(void);
21void test_ReadFileWithBlankLines(void);
22void test_WriteEmptyFile(void);
23void test_WriteFileWithSingleEntry(void);
24void test_WriteFileWithMultipleEntries(void);
25
26
27void
28setUp(void) {
29	kod_db_cnt = 0;
30	kod_db = NULL;
31}
32
33
34void
35test_ReadEmptyFile(void) {
36	kod_init_kod_db(CreatePath("kod-test-empty", INPUT_DIR), TRUE);
37
38	TEST_ASSERT_EQUAL(0, kod_db_cnt);
39}
40
41
42void
43test_ReadCorrectFile(void) {
44	kod_init_kod_db(CreatePath("kod-test-correct", INPUT_DIR), TRUE);
45
46	TEST_ASSERT_EQUAL(2, kod_db_cnt);
47
48	struct kod_entry* res;
49
50	TEST_ASSERT_EQUAL(1, search_entry("192.0.2.5", &res));
51	TEST_ASSERT_EQUAL_STRING("DENY", res->type);
52	TEST_ASSERT_EQUAL_STRING("192.0.2.5", res->hostname);
53	TEST_ASSERT_EQUAL(0x12345678, res->timestamp);
54
55	TEST_ASSERT_EQUAL(1, search_entry("192.0.2.100", &res));
56	TEST_ASSERT_EQUAL_STRING("RSTR", res->type);
57	TEST_ASSERT_EQUAL_STRING("192.0.2.100", res->hostname);
58	TEST_ASSERT_EQUAL(0xfff, res->timestamp);
59}
60
61
62void
63test_ReadFileWithBlankLines(void) {
64	kod_init_kod_db(CreatePath("kod-test-blanks", INPUT_DIR), TRUE);
65
66	TEST_ASSERT_EQUAL(3, kod_db_cnt);
67
68	struct kod_entry* res;
69
70	TEST_ASSERT_EQUAL(1, search_entry("192.0.2.5", &res));
71	TEST_ASSERT_EQUAL_STRING("DENY", res->type);
72	TEST_ASSERT_EQUAL_STRING("192.0.2.5", res->hostname);
73	TEST_ASSERT_EQUAL(0x12345678, res->timestamp);
74
75	TEST_ASSERT_EQUAL(1, search_entry("192.0.2.100", &res));
76	TEST_ASSERT_EQUAL_STRING("RSTR", res->type);
77	TEST_ASSERT_EQUAL_STRING("192.0.2.100", res->hostname);
78	TEST_ASSERT_EQUAL(0xfff, res->timestamp);
79
80	TEST_ASSERT_EQUAL(1, search_entry("example.com", &res));
81	TEST_ASSERT_EQUAL_STRING("DENY", res->type);
82	TEST_ASSERT_EQUAL_STRING("example.com", res->hostname);
83	TEST_ASSERT_EQUAL(0xabcd, res->timestamp);
84}
85
86
87void
88test_WriteEmptyFile(void) {
89	kod_db_file = estrdup("kod-output-blank");
90	write_kod_db();
91
92	// Open file and ensure that the filesize is 0 bytes.
93	FILE * is = fopen(kod_db_file, "rb");
94	TEST_ASSERT_NOT_NULL(is);
95
96	TEST_ASSERT_EQUAL(0, GetFileSize(is));
97
98	fclose(is);
99}
100
101
102void
103test_WriteFileWithSingleEntry(void) {
104	kod_db_file = estrdup("kod-output-single");
105	add_entry("host1", "DENY");
106
107	// Here we must manipulate the timestamps, so they match the one in
108	// the expected file.
109
110	kod_db[0]->timestamp = 1;
111
112	write_kod_db();
113
114	// Open file and compare sizes.
115	FILE * actual = fopen(kod_db_file, "rb");
116	FILE * expected = fopen(CreatePath("kod-expected-single", INPUT_DIR),"rb");
117
118	TEST_ASSERT_NOT_NULL(actual);
119	TEST_ASSERT_NOT_NULL(expected);
120
121	TEST_ASSERT_EQUAL(GetFileSize(expected), GetFileSize(actual));
122
123	TEST_ASSERT_TRUE(CompareFileContent(expected, actual));
124}
125
126
127void
128test_WriteFileWithMultipleEntries(void) {
129	kod_db_file = estrdup("kod-output-multiple");
130	add_entry("example.com", "RATE");
131	add_entry("192.0.2.1", "DENY");
132	add_entry("192.0.2.5", "RSTR");
133
134	//
135	// Manipulate timestamps. This is a bit of a hack, ideally these
136	// tests should not care about the internal representation.
137	//
138	kod_db[0]->timestamp = 0xabcd;
139	kod_db[1]->timestamp = 0xabcd;
140	kod_db[2]->timestamp = 0xabcd;
141
142	write_kod_db();
143
144	// Open file and compare sizes and content.
145	FILE * actual = fopen(kod_db_file, "rb");
146	FILE * expected = fopen(CreatePath("kod-expected-multiple", INPUT_DIR),"rb");
147
148	TEST_ASSERT_NOT_NULL(actual);
149	TEST_ASSERT_NOT_NULL(expected);
150
151
152	TEST_ASSERT_EQUAL(GetFileSize(expected), GetFileSize(actual));
153
154	TEST_ASSERT_TRUE(CompareFileContent(expected, actual));
155}
156