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