1284990Scy#include "config.h"
2289997Sglebius
3284990Scy#include "ntp_types.h"
4284990Scy#include "ntp_stdlib.h" // For estrdup()
5284990Scy#include "fileHandlingTest.h"
6284990Scy#include "kod_management.h"
7284990Scy
8284990Scy#include "unity.h"
9284990Scy
10284990Scy/*
11284990Scy * We access some parts of the kod database directly, without
12284990Scy * going through the public interface
13284990Scy */
14284990Scyextern int kod_db_cnt;
15284990Scyextern struct kod_entry** kod_db;
16284990Scyextern char* kod_db_file;
17284990Scy
18289997Sglebiusvoid setUp(void);
19289997Sglebiusvoid test_ReadEmptyFile(void);
20289997Sglebiusvoid test_ReadCorrectFile(void);
21289997Sglebiusvoid test_ReadFileWithBlankLines(void);
22289997Sglebiusvoid test_WriteEmptyFile(void);
23289997Sglebiusvoid test_WriteFileWithSingleEntry(void);
24289997Sglebiusvoid test_WriteFileWithMultipleEntries(void);
25284990Scy
26289997Sglebius
27289997Sglebiusvoid
28289997SglebiussetUp(void) {
29289997Sglebius	kod_db_cnt = 0;
30289997Sglebius	kod_db = NULL;
31293650Sglebius	init_lib();
32284990Scy}
33284990Scy
34284990Scy
35289997Sglebiusvoid
36289997Sglebiustest_ReadEmptyFile(void) {
37284990Scy	kod_init_kod_db(CreatePath("kod-test-empty", INPUT_DIR), TRUE);
38284990Scy
39284990Scy	TEST_ASSERT_EQUAL(0, kod_db_cnt);
40284990Scy}
41284990Scy
42289997Sglebius
43289997Sglebiusvoid
44289997Sglebiustest_ReadCorrectFile(void) {
45284990Scy	kod_init_kod_db(CreatePath("kod-test-correct", INPUT_DIR), TRUE);
46284990Scy
47284990Scy	TEST_ASSERT_EQUAL(2, kod_db_cnt);
48284990Scy
49284990Scy	struct kod_entry* res;
50284990Scy
51284990Scy	TEST_ASSERT_EQUAL(1, search_entry("192.0.2.5", &res));
52284990Scy	TEST_ASSERT_EQUAL_STRING("DENY", res->type);
53284990Scy	TEST_ASSERT_EQUAL_STRING("192.0.2.5", res->hostname);
54284990Scy	TEST_ASSERT_EQUAL(0x12345678, res->timestamp);
55284990Scy
56284990Scy	TEST_ASSERT_EQUAL(1, search_entry("192.0.2.100", &res));
57284990Scy	TEST_ASSERT_EQUAL_STRING("RSTR", res->type);
58284990Scy	TEST_ASSERT_EQUAL_STRING("192.0.2.100", res->hostname);
59284990Scy	TEST_ASSERT_EQUAL(0xfff, res->timestamp);
60284990Scy}
61284990Scy
62289997Sglebius
63289997Sglebiusvoid
64289997Sglebiustest_ReadFileWithBlankLines(void) {
65284990Scy	kod_init_kod_db(CreatePath("kod-test-blanks", INPUT_DIR), TRUE);
66284990Scy
67284990Scy	TEST_ASSERT_EQUAL(3, kod_db_cnt);
68284990Scy
69284990Scy	struct kod_entry* res;
70284990Scy
71284990Scy	TEST_ASSERT_EQUAL(1, search_entry("192.0.2.5", &res));
72284990Scy	TEST_ASSERT_EQUAL_STRING("DENY", res->type);
73284990Scy	TEST_ASSERT_EQUAL_STRING("192.0.2.5", res->hostname);
74284990Scy	TEST_ASSERT_EQUAL(0x12345678, res->timestamp);
75284990Scy
76284990Scy	TEST_ASSERT_EQUAL(1, search_entry("192.0.2.100", &res));
77284990Scy	TEST_ASSERT_EQUAL_STRING("RSTR", res->type);
78284990Scy	TEST_ASSERT_EQUAL_STRING("192.0.2.100", res->hostname);
79284990Scy	TEST_ASSERT_EQUAL(0xfff, res->timestamp);
80284990Scy
81284990Scy	TEST_ASSERT_EQUAL(1, search_entry("example.com", &res));
82284990Scy	TEST_ASSERT_EQUAL_STRING("DENY", res->type);
83284990Scy	TEST_ASSERT_EQUAL_STRING("example.com", res->hostname);
84284990Scy	TEST_ASSERT_EQUAL(0xabcd, res->timestamp);
85284990Scy}
86284990Scy
87289997Sglebius
88289997Sglebiusvoid
89289997Sglebiustest_WriteEmptyFile(void) {
90284990Scy	kod_db_file = estrdup("kod-output-blank");
91284990Scy	write_kod_db();
92284990Scy
93284990Scy	// Open file and ensure that the filesize is 0 bytes.
94289997Sglebius	FILE * is = fopen(kod_db_file, "rb");
95289997Sglebius	TEST_ASSERT_NOT_NULL(is);
96289997Sglebius
97284990Scy	TEST_ASSERT_EQUAL(0, GetFileSize(is));
98284990Scy
99284990Scy	fclose(is);
100284990Scy}
101284990Scy
102289997Sglebius
103289997Sglebiusvoid
104289997Sglebiustest_WriteFileWithSingleEntry(void) {
105284990Scy	kod_db_file = estrdup("kod-output-single");
106284990Scy	add_entry("host1", "DENY");
107284990Scy
108284990Scy	// Here we must manipulate the timestamps, so they match the one in
109284990Scy	// the expected file.
110289997Sglebius
111284990Scy	kod_db[0]->timestamp = 1;
112284990Scy
113284990Scy	write_kod_db();
114284990Scy
115284990Scy	// Open file and compare sizes.
116284990Scy	FILE * actual = fopen(kod_db_file, "rb");
117284990Scy	FILE * expected = fopen(CreatePath("kod-expected-single", INPUT_DIR),"rb");
118284990Scy
119289997Sglebius	TEST_ASSERT_NOT_NULL(actual);
120289997Sglebius	TEST_ASSERT_NOT_NULL(expected);
121289997Sglebius
122284990Scy	TEST_ASSERT_EQUAL(GetFileSize(expected), GetFileSize(actual));
123289997Sglebius
124284990Scy	TEST_ASSERT_TRUE(CompareFileContent(expected, actual));
125284990Scy}
126284990Scy
127289997Sglebius
128289997Sglebiusvoid
129289997Sglebiustest_WriteFileWithMultipleEntries(void) {
130284990Scy	kod_db_file = estrdup("kod-output-multiple");
131284990Scy	add_entry("example.com", "RATE");
132284990Scy	add_entry("192.0.2.1", "DENY");
133284990Scy	add_entry("192.0.2.5", "RSTR");
134284990Scy
135284990Scy	//
136284990Scy	// Manipulate timestamps. This is a bit of a hack, ideally these
137284990Scy	// tests should not care about the internal representation.
138284990Scy	//
139284990Scy	kod_db[0]->timestamp = 0xabcd;
140284990Scy	kod_db[1]->timestamp = 0xabcd;
141284990Scy	kod_db[2]->timestamp = 0xabcd;
142284990Scy
143284990Scy	write_kod_db();
144284990Scy
145284990Scy	// Open file and compare sizes and content.
146284990Scy	FILE * actual = fopen(kod_db_file, "rb");
147284990Scy	FILE * expected = fopen(CreatePath("kod-expected-multiple", INPUT_DIR),"rb");
148284990Scy
149289997Sglebius	TEST_ASSERT_NOT_NULL(actual);
150289997Sglebius	TEST_ASSERT_NOT_NULL(expected);
151289997Sglebius
152289997Sglebius
153284990Scy	TEST_ASSERT_EQUAL(GetFileSize(expected), GetFileSize(actual));
154284990Scy
155284990Scy	TEST_ASSERT_TRUE(CompareFileContent(expected, actual));
156284990Scy}
157