kodDatabase.c revision 284990
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 8284990Scy#include "kod_management.h" 9284990Scy 10284990Scy#include "unity.h" 11284990Scy 12284990Scyvoid setUp(void) 13284990Scy{ 14284990Scy kod_init_kod_db("/dev/null", TRUE); 15284990Scy} 16284990Scy 17284990Scyvoid tearDown(void) 18284990Scy{ 19284990Scy} 20284990Scy 21284990Scy 22284990Scyvoid test_SingleEntryHandling() { 23284990Scy char HOST[] = "192.0.2.5"; 24284990Scy char REASON[] = "DENY"; 25284990Scy 26284990Scy add_entry(HOST, REASON); 27284990Scy 28284990Scy struct kod_entry* result; 29284990Scy 30284990Scy TEST_ASSERT_EQUAL(1, search_entry(HOST, &result)); 31284990Scy TEST_ASSERT_EQUAL_STRING(HOST, result->hostname); 32284990Scy TEST_ASSERT_EQUAL_STRING(REASON, result->type); 33284990Scy} 34284990Scy 35284990Scyvoid test_MultipleEntryHandling() { 36284990Scy char HOST1[] = "192.0.2.3"; 37284990Scy char REASON1[] = "DENY"; 38284990Scy 39284990Scy char HOST2[] = "192.0.5.5"; 40284990Scy char REASON2[] = "RATE"; 41284990Scy 42284990Scy char HOST3[] = "192.0.10.1"; 43284990Scy char REASON3[] = "DENY"; 44284990Scy 45284990Scy add_entry(HOST1, REASON1); 46284990Scy add_entry(HOST2, REASON2); 47284990Scy add_entry(HOST3, REASON3); 48284990Scy 49284990Scy struct kod_entry* result; 50284990Scy 51284990Scy TEST_ASSERT_EQUAL(1, search_entry(HOST1, &result)); 52284990Scy TEST_ASSERT_EQUAL_STRING(HOST1, result->hostname); 53284990Scy TEST_ASSERT_EQUAL_STRING(REASON1, result->type); 54284990Scy 55284990Scy TEST_ASSERT_EQUAL(1, search_entry(HOST2, &result)); 56284990Scy TEST_ASSERT_EQUAL_STRING(HOST2, result->hostname); 57284990Scy TEST_ASSERT_EQUAL_STRING(REASON2, result->type); 58284990Scy 59284990Scy TEST_ASSERT_EQUAL(1, search_entry(HOST3, &result)); 60284990Scy TEST_ASSERT_EQUAL_STRING(HOST3, result->hostname); 61284990Scy TEST_ASSERT_EQUAL_STRING(REASON3, result->type); 62284990Scy 63284990Scy free(result); 64284990Scy} 65284990Scy 66284990Scyvoid test_NoMatchInSearch() { 67284990Scy char HOST_ADD[] = "192.0.2.6"; 68284990Scy char HOST_NOTADD[] = "192.0.6.1"; 69284990Scy char REASON[] = "DENY"; 70284990Scy 71284990Scy add_entry(HOST_ADD, REASON); 72284990Scy 73284990Scy struct kod_entry* result; 74284990Scy 75284990Scy TEST_ASSERT_EQUAL(0, search_entry(HOST_NOTADD, &result)); 76284990Scy TEST_ASSERT_TRUE(result == NULL); 77284990Scy} 78284990Scy 79284990Scyvoid test_AddDuplicate() { 80284990Scy char HOST[] = "192.0.2.3"; 81284990Scy char REASON1[] = "RATE"; 82284990Scy char REASON2[] = "DENY"; 83284990Scy 84284990Scy add_entry(HOST, REASON1); 85284990Scy struct kod_entry* result1; 86284990Scy TEST_ASSERT_EQUAL(1, search_entry(HOST, &result1)); 87284990Scy 88284990Scy /* 89284990Scy * Sleeps for two seconds since we want to ensure that 90284990Scy * the timestamp is updated to a new value. 91284990Scy */ 92284990Scy sleep(2); 93284990Scy 94284990Scy add_entry(HOST, REASON2); 95284990Scy struct kod_entry* result2; 96284990Scy TEST_ASSERT_EQUAL(1, search_entry(HOST, &result2)); 97284990Scy 98284990Scy TEST_ASSERT_FALSE(result1->timestamp == result2->timestamp); 99284990Scy 100284990Scy free(result1); 101284990Scy free(result2); 102284990Scy} 103284990Scy 104284990Scyvoid test_DeleteEntry() { 105284990Scy char HOST1[] = "192.0.2.1"; 106284990Scy char HOST2[] = "192.0.2.2"; 107284990Scy char HOST3[] = "192.0.2.3"; 108284990Scy char REASON[] = "DENY"; 109284990Scy 110284990Scy add_entry(HOST1, REASON); 111284990Scy add_entry(HOST2, REASON); 112284990Scy add_entry(HOST3, REASON); 113284990Scy 114284990Scy struct kod_entry* result; 115284990Scy 116284990Scy TEST_ASSERT_EQUAL(1, search_entry(HOST2, &result)); 117284990Scy free(result); 118284990Scy 119284990Scy delete_entry(HOST2, REASON); 120284990Scy 121284990Scy TEST_ASSERT_EQUAL(0, search_entry(HOST2, &result)); 122284990Scy 123284990Scy // Ensure that the other entry is still there. 124284990Scy TEST_ASSERT_EQUAL(1, search_entry(HOST1, &result)); 125284990Scy free(result); 126284990Scy} 127