kodDatabase.c revision 289999
1284990Scy#include "config.h" 2284990Scy 3284990Scy#include "ntp_types.h" 4284990Scy#include "sntptest.h" 5284990Scy#include "ntp_stdlib.h" 6284990Scy#include "sntp-opts.h" 7284990Scy#include "kod_management.h" 8289999Sglebius#include "ntp_io.h" 9284990Scy 10284990Scy#include "unity.h" 11284990Scy 12289999Sglebiusvoid setUp(void); 13289999Sglebiusvoid test_SingleEntryHandling(void); 14289999Sglebiusvoid test_MultipleEntryHandling(void); 15289999Sglebiusvoid test_NoMatchInSearch(void); 16289999Sglebiusvoid test_AddDuplicate(void); 17289999Sglebiusvoid test_DeleteEntry(void); 18289999Sglebius 19289999Sglebius 20289999Sglebiusvoid 21289999SglebiussetUp(void) { 22284990Scy kod_init_kod_db("/dev/null", TRUE); 23284990Scy} 24284990Scy 25284990Scy 26289999Sglebiusvoid 27289999Sglebiustest_SingleEntryHandling(void) { 28289999Sglebius const char HOST[] = "192.0.2.5"; 29289999Sglebius const char REASON[] = "DENY"; 30284990Scy 31284990Scy add_entry(HOST, REASON); 32284990Scy 33284990Scy struct kod_entry* result; 34284990Scy 35284990Scy TEST_ASSERT_EQUAL(1, search_entry(HOST, &result)); 36284990Scy TEST_ASSERT_EQUAL_STRING(HOST, result->hostname); 37284990Scy TEST_ASSERT_EQUAL_STRING(REASON, result->type); 38284990Scy} 39284990Scy 40284990Scy 41289999Sglebiusvoid 42289999Sglebiustest_MultipleEntryHandling(void) { 43289999Sglebius const char HOST1[] = "192.0.2.3"; 44289999Sglebius const char REASON1[] = "DENY"; 45284990Scy 46289999Sglebius const char HOST2[] = "192.0.5.5"; 47289999Sglebius const char REASON2[] = "RATE"; 48284990Scy 49289999Sglebius const char HOST3[] = "192.0.10.1"; 50289999Sglebius const char REASON3[] = "DENY"; 51289999Sglebius 52284990Scy add_entry(HOST1, REASON1); 53284990Scy add_entry(HOST2, REASON2); 54284990Scy add_entry(HOST3, REASON3); 55284990Scy 56284990Scy struct kod_entry* result; 57284990Scy 58284990Scy TEST_ASSERT_EQUAL(1, search_entry(HOST1, &result)); 59284990Scy TEST_ASSERT_EQUAL_STRING(HOST1, result->hostname); 60284990Scy TEST_ASSERT_EQUAL_STRING(REASON1, result->type); 61284990Scy 62284990Scy TEST_ASSERT_EQUAL(1, search_entry(HOST2, &result)); 63284990Scy TEST_ASSERT_EQUAL_STRING(HOST2, result->hostname); 64284990Scy TEST_ASSERT_EQUAL_STRING(REASON2, result->type); 65284990Scy 66284990Scy TEST_ASSERT_EQUAL(1, search_entry(HOST3, &result)); 67284990Scy TEST_ASSERT_EQUAL_STRING(HOST3, result->hostname); 68284990Scy TEST_ASSERT_EQUAL_STRING(REASON3, result->type); 69284990Scy 70284990Scy free(result); 71284990Scy} 72284990Scy 73284990Scy 74289999Sglebiusvoid 75289999Sglebiustest_NoMatchInSearch(void) { 76289999Sglebius const char HOST_ADD[] = "192.0.2.6"; 77289999Sglebius const char HOST_NOTADD[] = "192.0.6.1"; 78289999Sglebius const char REASON[] = "DENY"; 79289999Sglebius 80284990Scy add_entry(HOST_ADD, REASON); 81284990Scy 82284990Scy struct kod_entry* result; 83284990Scy 84284990Scy TEST_ASSERT_EQUAL(0, search_entry(HOST_NOTADD, &result)); 85284990Scy TEST_ASSERT_TRUE(result == NULL); 86284990Scy} 87284990Scy 88284990Scy 89289999Sglebiusvoid 90289999Sglebiustest_AddDuplicate(void) { 91289999Sglebius const char HOST[] = "192.0.2.3"; 92289999Sglebius const char REASON1[] = "RATE"; 93289999Sglebius const char REASON2[] = "DENY"; 94289999Sglebius 95284990Scy add_entry(HOST, REASON1); 96284990Scy struct kod_entry* result1; 97284990Scy TEST_ASSERT_EQUAL(1, search_entry(HOST, &result1)); 98284990Scy 99284990Scy /* 100284990Scy * Sleeps for two seconds since we want to ensure that 101284990Scy * the timestamp is updated to a new value. 102284990Scy */ 103284990Scy sleep(2); 104284990Scy 105284990Scy add_entry(HOST, REASON2); 106284990Scy struct kod_entry* result2; 107284990Scy TEST_ASSERT_EQUAL(1, search_entry(HOST, &result2)); 108284990Scy 109284990Scy TEST_ASSERT_FALSE(result1->timestamp == result2->timestamp); 110284990Scy 111284990Scy free(result1); 112284990Scy free(result2); 113284990Scy} 114284990Scy 115284990Scy 116289999Sglebiusvoid 117289999Sglebiustest_DeleteEntry(void) { 118289999Sglebius const char HOST1[] = "192.0.2.1"; 119289999Sglebius const char HOST2[] = "192.0.2.2"; 120289999Sglebius const char HOST3[] = "192.0.2.3"; 121289999Sglebius const char REASON[] = "DENY"; 122289999Sglebius 123284990Scy add_entry(HOST1, REASON); 124284990Scy add_entry(HOST2, REASON); 125284990Scy add_entry(HOST3, REASON); 126284990Scy 127284990Scy struct kod_entry* result; 128284990Scy 129284990Scy TEST_ASSERT_EQUAL(1, search_entry(HOST2, &result)); 130284990Scy free(result); 131284990Scy 132284990Scy delete_entry(HOST2, REASON); 133284990Scy 134284990Scy TEST_ASSERT_EQUAL(0, search_entry(HOST2, &result)); 135284990Scy 136284990Scy // Ensure that the other entry is still there. 137284990Scy TEST_ASSERT_EQUAL(1, search_entry(HOST1, &result)); 138284990Scy free(result); 139284990Scy} 140