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 */ 14 extern int kod_db_cnt; 15 extern struct kod_entry** kod_db; 16 extern char* kod_db_file; 17 18 void setUp(void); 19 void test_ReadEmptyFile(void); 20 void test_ReadCorrectFile(void); 21 void test_ReadFileWithBlankLines(void); 22 void test_WriteEmptyFile(void); 23 void test_WriteFileWithSingleEntry(void); 24 void test_WriteFileWithMultipleEntries(void); 25 26 27 void 28 setUp(void) { 29 kod_db_cnt = 0; 30 kod_db = NULL; 31 } 32 33 34 void 35 test_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 42 void 43 test_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 62 void 63 test_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 87 void 88 test_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 102 void 103 test_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 127 void 128 test_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