summaryrefslogtreecommitdiff
path: root/sntp/tests/kodFile.cpp
blob: e1775f6374fa6c6c50d5b355fb90ec3ac215d64e (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
#include "fileHandlingTest.h"

extern "C" {
#include "kod_management.h"

#include "ntp_stdlib.h" // For estrdup()
};

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

class kodFileTest : public fileHandlingTest {
protected:
	virtual void SetUp() {
		kod_db_cnt = 0;
		kod_db = NULL;
	}

	virtual void TearDown() {
	}
};

TEST_F(kodFileTest, ReadEmptyFile) {
	kod_init_kod_db(CreatePath("kod-test-empty", INPUT_DIR).c_str(), TRUE);

	EXPECT_EQ(0, kod_db_cnt);
}

TEST_F(kodFileTest, ReadCorrectFile) {
	kod_init_kod_db(CreatePath("kod-test-correct", INPUT_DIR).c_str(), TRUE);
	
	EXPECT_EQ(2, kod_db_cnt);

	kod_entry* res;

	ASSERT_EQ(1, search_entry("192.0.2.5", &res));
	EXPECT_STREQ("DENY", res->type);
	EXPECT_STREQ("192.0.2.5", res->hostname);
	EXPECT_EQ(0x12345678, res->timestamp);

	ASSERT_EQ(1, search_entry("192.0.2.100", &res));
	EXPECT_STREQ("RSTR", res->type);
	EXPECT_STREQ("192.0.2.100", res->hostname);
	EXPECT_EQ(0xfff, res->timestamp);
}

TEST_F(kodFileTest, ReadFileWithBlankLines) {
	kod_init_kod_db(CreatePath("kod-test-blanks", INPUT_DIR).c_str(), TRUE);

	EXPECT_EQ(3, kod_db_cnt);

	kod_entry* res;

	ASSERT_EQ(1, search_entry("192.0.2.5", &res));
	EXPECT_STREQ("DENY", res->type);
	EXPECT_STREQ("192.0.2.5", res->hostname);
	EXPECT_EQ(0x12345678, res->timestamp);

	ASSERT_EQ(1, search_entry("192.0.2.100", &res));
	EXPECT_STREQ("RSTR", res->type);
	EXPECT_STREQ("192.0.2.100", res->hostname);
	EXPECT_EQ(0xfff, res->timestamp);

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

TEST_F(kodFileTest, WriteEmptyFile) {
	kod_db_file = estrdup(CreatePath("kod-output-blank", OUTPUT_DIR).c_str());

	write_kod_db();

	// Open file and ensure that the filesize is 0 bytes.
	std::ifstream is(kod_db_file, std::ios::binary);
	ASSERT_FALSE(is.fail());
	
	EXPECT_EQ(0, GetFileSize(is));

	is.close();
}

TEST_F(kodFileTest, WriteFileWithSingleEntry) {
	kod_db_file = estrdup(CreatePath("kod-output-single", OUTPUT_DIR).c_str());

	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.
	ifstream actual(kod_db_file, ios::binary);
	ifstream expected(CreatePath("kod-expected-single", INPUT_DIR).c_str());
	ASSERT_TRUE(actual.good());
	ASSERT_TRUE(expected.good());

	ASSERT_EQ(GetFileSize(expected), GetFileSize(actual));

	CompareFileContent(expected, actual);
}

TEST_F(kodFileTest, WriteFileWithMultipleEntries) {
	kod_db_file = estrdup(CreatePath("kod-output-multiple", OUTPUT_DIR).c_str());

	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.
	ifstream actual(kod_db_file, ios::binary);
	ifstream expected(CreatePath("kod-expected-multiple", INPUT_DIR).c_str());
	ASSERT_TRUE(actual.good());
	ASSERT_TRUE(expected.good());
	
	ASSERT_EQ(GetFileSize(expected), GetFileSize(actual));

	CompareFileContent(expected, actual);
}