From 909a7595ad4e4c6e48ed960263e0480a85078b66 Mon Sep 17 00:00:00 2001 From: Anoop Saldanha Date: Tue, 22 May 2012 00:46:30 +0530 Subject: [PATCH 2/3] bug #454 - add unittests for the address/port conf var validation function --- src/detect-engine-address.c | 151 +++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 151 insertions(+), 0 deletions(-) diff --git a/src/detect-engine-address.c b/src/detect-engine-address.c index edcae6a..ac71a32 100644 --- a/src/detect-engine-address.c +++ b/src/detect-engine-address.c @@ -35,6 +35,7 @@ #include "util-unittest.h" #include "util-rule-vars.h" #include "conf.h" +#include "conf-yaml-loader.h" #include "detect-engine-siggroup.h" #include "detect-engine-address.h" @@ -4165,6 +4166,150 @@ int AddressTestParseInvalidMask03(void) return result; } +int AddressConfVarsTest01(void) +{ + static const char *dummy_conf_string = + "%YAML 1.1\n" + "---\n" + "\n" + "vars:\n" + "\n" + " address-groups:\n" + "\n" + " HOME_NET: \"any\"\n" + "\n" + " EXTERNAL_NET: \"!any\"\n" + "\n" + " port-groups:\n" + "\n" + " HTTP_PORTS: \"any\"\n" + "\n" + " SHELLCODE_PORTS: \"!any\"\n" + "\n"; + + int result = 0; + + ConfCreateContextBackup(); + ConfInit(); + ConfYamlLoadString(dummy_conf_string, strlen(dummy_conf_string)); + + if (DetectAddressTestConfVars() < 0 && DetectPortTestConfVars() < 0) + result = 1; + + ConfDeInit(); + ConfRestoreContextBackup(); + + return result; +} + +int AddressConfVarsTest02(void) +{ + static const char *dummy_conf_string = + "%YAML 1.1\n" + "---\n" + "\n" + "vars:\n" + "\n" + " address-groups:\n" + "\n" + " HOME_NET: \"any\"\n" + "\n" + " EXTERNAL_NET: \"any\"\n" + "\n" + " port-groups:\n" + "\n" + " HTTP_PORTS: \"any\"\n" + "\n" + " SHELLCODE_PORTS: \"!any\"\n" + "\n"; + + int result = 0; + + ConfCreateContextBackup(); + ConfInit(); + ConfYamlLoadString(dummy_conf_string, strlen(dummy_conf_string)); + + if (DetectAddressTestConfVars() == 0 && DetectPortTestConfVars() < 0) + result = 1; + + ConfDeInit(); + ConfRestoreContextBackup(); + + return result; +} + +int AddressConfVarsTest03(void) +{ + static const char *dummy_conf_string = + "%YAML 1.1\n" + "---\n" + "\n" + "vars:\n" + "\n" + " address-groups:\n" + "\n" + " HOME_NET: \"any\"\n" + "\n" + " EXTERNAL_NET: \"!$HOME_NET\"\n" + "\n" + " port-groups:\n" + "\n" + " HTTP_PORTS: \"any\"\n" + "\n" + " SHELLCODE_PORTS: \"!$HTTP_PORTS\"\n" + "\n"; + + int result = 0; + + ConfCreateContextBackup(); + ConfInit(); + ConfYamlLoadString(dummy_conf_string, strlen(dummy_conf_string)); + + if (DetectAddressTestConfVars() < 0 && DetectPortTestConfVars() < 0) + result = 1; + + ConfDeInit(); + ConfRestoreContextBackup(); + + return result; +} + +int AddressConfVarsTest04(void) +{ + static const char *dummy_conf_string = + "%YAML 1.1\n" + "---\n" + "\n" + "vars:\n" + "\n" + " address-groups:\n" + "\n" + " HOME_NET: \"any\"\n" + "\n" + " EXTERNAL_NET: \"$HOME_NET\"\n" + "\n" + " port-groups:\n" + "\n" + " HTTP_PORTS: \"any\"\n" + "\n" + " SHELLCODE_PORTS: \"$HTTP_PORTS\"\n" + "\n"; + + int result = 0; + + ConfCreateContextBackup(); + ConfInit(); + ConfYamlLoadString(dummy_conf_string, strlen(dummy_conf_string)); + + if (DetectAddressTestConfVars() == 0 && DetectPortTestConfVars() == 0) + result = 1; + + ConfDeInit(); + ConfRestoreContextBackup(); + + return result; +} + #endif /* UNITTESTS */ void DetectAddressTests(void) @@ -4337,5 +4482,11 @@ void DetectAddressTests(void) AddressTestParseInvalidMask02, 1); UtRegisterTest("AddressTestParseInvalidMask03", AddressTestParseInvalidMask03, 1); + + UtRegisterTest("AddressConfVarsTest01 ", AddressConfVarsTest01, 1); + UtRegisterTest("AddressConfVarsTest02 ", AddressConfVarsTest02, 1); + UtRegisterTest("AddressConfVarsTest03 ", AddressConfVarsTest03, 1); + UtRegisterTest("AddressConfVarsTest04 ", AddressConfVarsTest04, 1); + #endif /* UNITTESTS */ } -- 1.7.1