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