kodDatabase.c revision 293893
1284990Scy#include "config.h" 2284990Scy 3293893Sglebius#include "ntp_workimpl.h" 4284990Scy#include "ntp_types.h" 5284990Scy#include "sntptest.h" 6284990Scy#include "ntp_stdlib.h" 7284990Scy#include "sntp-opts.h" 8284990Scy#include "kod_management.h" 9289999Sglebius#include "ntp_io.h" 10284990Scy 11284990Scy#include "unity.h" 12284990Scy 13289999Sglebiusvoid setUp(void); 14289999Sglebiusvoid test_SingleEntryHandling(void); 15289999Sglebiusvoid test_MultipleEntryHandling(void); 16289999Sglebiusvoid test_NoMatchInSearch(void); 17289999Sglebiusvoid test_AddDuplicate(void); 18289999Sglebiusvoid test_DeleteEntry(void); 19289999Sglebius 20289999Sglebius 21289999Sglebiusvoid 22289999SglebiussetUp(void) { 23284990Scy kod_init_kod_db("/dev/null", TRUE); 24293893Sglebius init_lib(); 25284990Scy} 26284990Scy 27284990Scy 28289999Sglebiusvoid 29289999Sglebiustest_SingleEntryHandling(void) { 30289999Sglebius const char HOST[] = "192.0.2.5"; 31289999Sglebius const char REASON[] = "DENY"; 32284990Scy 33284990Scy add_entry(HOST, REASON); 34284990Scy 35284990Scy struct kod_entry* result; 36284990Scy 37284990Scy TEST_ASSERT_EQUAL(1, search_entry(HOST, &result)); 38284990Scy TEST_ASSERT_EQUAL_STRING(HOST, result->hostname); 39284990Scy TEST_ASSERT_EQUAL_STRING(REASON, result->type); 40284990Scy} 41284990Scy 42284990Scy 43289999Sglebiusvoid 44289999Sglebiustest_MultipleEntryHandling(void) { 45289999Sglebius const char HOST1[] = "192.0.2.3"; 46289999Sglebius const char REASON1[] = "DENY"; 47284990Scy 48289999Sglebius const char HOST2[] = "192.0.5.5"; 49289999Sglebius const char REASON2[] = "RATE"; 50284990Scy 51289999Sglebius const char HOST3[] = "192.0.10.1"; 52289999Sglebius const char REASON3[] = "DENY"; 53289999Sglebius 54284990Scy add_entry(HOST1, REASON1); 55284990Scy add_entry(HOST2, REASON2); 56284990Scy add_entry(HOST3, REASON3); 57284990Scy 58284990Scy struct kod_entry* result; 59284990Scy 60284990Scy TEST_ASSERT_EQUAL(1, search_entry(HOST1, &result)); 61284990Scy TEST_ASSERT_EQUAL_STRING(HOST1, result->hostname); 62284990Scy TEST_ASSERT_EQUAL_STRING(REASON1, result->type); 63284990Scy 64284990Scy TEST_ASSERT_EQUAL(1, search_entry(HOST2, &result)); 65284990Scy TEST_ASSERT_EQUAL_STRING(HOST2, result->hostname); 66284990Scy TEST_ASSERT_EQUAL_STRING(REASON2, result->type); 67284990Scy 68284990Scy TEST_ASSERT_EQUAL(1, search_entry(HOST3, &result)); 69284990Scy TEST_ASSERT_EQUAL_STRING(HOST3, result->hostname); 70284990Scy TEST_ASSERT_EQUAL_STRING(REASON3, result->type); 71284990Scy 72284990Scy free(result); 73284990Scy} 74284990Scy 75284990Scy 76289999Sglebiusvoid 77289999Sglebiustest_NoMatchInSearch(void) { 78289999Sglebius const char HOST_ADD[] = "192.0.2.6"; 79289999Sglebius const char HOST_NOTADD[] = "192.0.6.1"; 80289999Sglebius const char REASON[] = "DENY"; 81289999Sglebius 82284990Scy add_entry(HOST_ADD, REASON); 83284990Scy 84284990Scy struct kod_entry* result; 85284990Scy 86284990Scy TEST_ASSERT_EQUAL(0, search_entry(HOST_NOTADD, &result)); 87284990Scy TEST_ASSERT_TRUE(result == NULL); 88284990Scy} 89284990Scy 90284990Scy 91289999Sglebiusvoid 92289999Sglebiustest_AddDuplicate(void) { 93289999Sglebius const char HOST[] = "192.0.2.3"; 94289999Sglebius const char REASON1[] = "RATE"; 95289999Sglebius const char REASON2[] = "DENY"; 96289999Sglebius 97284990Scy add_entry(HOST, REASON1); 98284990Scy struct kod_entry* result1; 99284990Scy TEST_ASSERT_EQUAL(1, search_entry(HOST, &result1)); 100284990Scy 101284990Scy /* 102284990Scy * Sleeps for two seconds since we want to ensure that 103284990Scy * the timestamp is updated to a new value. 104284990Scy */ 105284990Scy sleep(2); 106284990Scy 107284990Scy add_entry(HOST, REASON2); 108284990Scy struct kod_entry* result2; 109284990Scy TEST_ASSERT_EQUAL(1, search_entry(HOST, &result2)); 110284990Scy 111284990Scy TEST_ASSERT_FALSE(result1->timestamp == result2->timestamp); 112284990Scy 113284990Scy free(result1); 114284990Scy free(result2); 115284990Scy} 116284990Scy 117284990Scy 118289999Sglebiusvoid 119289999Sglebiustest_DeleteEntry(void) { 120289999Sglebius const char HOST1[] = "192.0.2.1"; 121289999Sglebius const char HOST2[] = "192.0.2.2"; 122289999Sglebius const char HOST3[] = "192.0.2.3"; 123289999Sglebius const char REASON[] = "DENY"; 124289999Sglebius 125284990Scy add_entry(HOST1, REASON); 126284990Scy add_entry(HOST2, REASON); 127284990Scy add_entry(HOST3, REASON); 128284990Scy 129284990Scy struct kod_entry* result; 130284990Scy 131284990Scy TEST_ASSERT_EQUAL(1, search_entry(HOST2, &result)); 132284990Scy free(result); 133284990Scy 134284990Scy delete_entry(HOST2, REASON); 135284990Scy 136284990Scy TEST_ASSERT_EQUAL(0, search_entry(HOST2, &result)); 137284990Scy 138284990Scy // Ensure that the other entry is still there. 139284990Scy TEST_ASSERT_EQUAL(1, search_entry(HOST1, &result)); 140284990Scy free(result); 141284990Scy} 142