tests/test.c

Go to the documentation of this file.
00001 #include "test.h"
00002 #include "../engine/bits.h"
00003 #include "../decoders/decode.h"
00004 #include <stdio.h>
00005 #include <string.h>
00006 #include <stdlib.h>
00007 
00008 /**********************************
00009 * Put all the test includes here
00010 **********************************/
00011 #include "test_interface_name.h"
00012 #include "test_ethernet_type.h"
00013 #include "test_ethernet_src.h"
00014 #include "test_ethernet_dst.h"
00015 #include "test_ip_src.h"
00016 #include "test_ip_dst.h"
00017 #include "test_ip_proto.h"
00018 #include "test_ip_ttl.h"
00019 #include "test_icmp_type.h"
00020 #include "test_icmp_code.h"
00021 #include "test_tcp_port.h"
00022 #include "test_tcp_src.h"
00023 #include "test_tcp_dst.h"
00024 #include "test_tcp_content.h"
00025 #include "test_tcp_nocase.h"
00026 #include "test_tcp_listcontent.h"
00027 #include "test_tcp_listnocase.h"
00028 #include "test_tcp_flags.h"
00029 #include "test_tcp_offset.h"
00030 #include "test_tcp_regex.h"
00031 #include "test_udp_regex.h"
00032 #include "test_udp_src.h"
00033 #include "test_udp_dst.h"
00034 //#include "test_dns_numquestions.h"
00035 #include "test_udp_content.h"
00036 #include "test_udp_nocase.h"
00037 
00038 extern GlobalVars       Globals;
00039 
00040 //#define DEBUG
00041 #define DEBUG1
00042 
00048 int InitTests()
00049 {
00050 #ifdef DEBUGPATH
00051         printf("In InitTests\n");
00052 #endif
00053 
00054         if (!InitTestInterfaceName()) return FALSE;
00055         if (!InitTestEthernetType()) return FALSE;
00056         if (!InitTestEthernetSrc()) return FALSE;
00057         if (!InitTestEthernetDst()) return FALSE;
00058         if (!InitTestIPSrc()) return FALSE;
00059         if (!InitTestIPDst()) return FALSE;
00060         if (!InitTestIPProto()) return FALSE;
00061         if (!InitTestIPTTL()) return FALSE;
00062         if (!InitTestICMPType()) return FALSE;
00063         if (!InitTestICMPCode()) return FALSE;
00064         if (!InitTestTCPPort()) return FALSE;   
00065         if (!InitTestTCPSrc()) return FALSE;
00066         if (!InitTestTCPDst()) return FALSE;
00067         if (!InitTestTCPContent()) return FALSE;
00068         if (!InitTestTCPNoCase()) return FALSE; 
00069         if (!InitTestTCPListContent()) return FALSE;    
00070         if (!InitTestTCPListNoCase()) return FALSE;     
00071         if (!InitTestTCPFlags()) return FALSE;  
00072         if (!InitTestTCPOffset()) return FALSE;
00073         if (!InitTestTCPRegExp()) return FALSE;
00074         if (!InitTestUDPRegExp()) return FALSE;
00075         if (!InitTestUDPSrc()) return FALSE;
00076         if (!InitTestUDPDst()) return FALSE;
00077 //      if (!InitTestDNSNumQ()) return FALSE;
00078         if (!InitTestUDPContent()) return FALSE;
00079         if (!InitTestUDPNoCase()) return FALSE;
00080 
00081         return TRUE;
00082 }
00083 
00084 /*************************************
00085 * Given a name, return the test ID
00086 *************************************/
00087 int     GetTestByName(char* Name){
00088         int     i;
00089 
00090 #ifdef DEBUGPATH
00091         printf("In GetTestByName\n");
00092 #endif
00093 
00094         for (i=0;i<Globals.NumTests;i++)
00095                 if (strcasecmp(Name, Globals.Tests[i].Name)==0) return i;
00096 
00097         return TEST_NONE;
00098 }
00099 
00100 /**************************************
00101 * Allocate a test
00102 **************************************/
00103 int CreateTest(char* Name){
00104         int             TestID;
00105         
00106 #ifdef DEBUGPATH
00107         printf("In CreateTest\n");
00108 #endif  
00109 
00110         
00111         TestID=GetTestByName(Name);
00112         if (TestID!=TEST_NONE){ 
00113                 printf("There is already a test named %s\n",Name);
00114                 return TEST_NONE;
00115         }
00116         
00117         TestID=Globals.NumTests;
00118         Globals.NumTests++;
00119         
00120         snprintf(Globals.Tests[TestID].Name, MAX_NAME_LEN, "%s", Name);
00121         Globals.Tests[TestID].ID=TestID;
00122         
00123 #ifdef DEBUG
00124         printf("Allocating test \"%s\" in number %i\n",Globals.Tests[TestID].Name, TestID);     
00125 #endif  
00126 
00127         return TestID;
00128 }
00129 
00130 /**************************************************
00131 * Bind a test to a decoder
00132 **************************************************/
00133 int BindTestToDecoder(int TestID, char* Decoder){
00134         int DecoderID;
00135         
00136 #ifdef DEBUGPATH
00137         printf("In BindTestToDecoder\n");
00138 #endif
00139 
00140         DecoderID=GetDecoderByName(Decoder);
00141         Globals.Tests[TestID].DecoderID=DecoderID;
00142         if (DecoderID==DECODER_NONE) return FALSE;
00143 
00144         return DecoderAddTest(DecoderID, TestID);
00145 }
00146 
00147 /****************************************************
00148 * Add a dependency to the decoder
00149 * If a decoder fails, all the dependencies fail also
00150 * Used for fast pruning
00151 ****************************************************/
00152 int TestSetDependency(int TestID, int RuleID){
00153 #ifdef DEBUGPATH
00154         printf("In TestSetDependency\n");
00155 #endif
00156         
00157         if (TestID > Globals.NumTests) return FALSE;
00158         
00159         SetBit(Globals.Tests[TestID].DependencyMask, Globals.NumRules, RuleID, 1);
00160 
00161         return TRUE;
00162 }
00163 
00164 
00165 /*********************************************************
00166 * Add a node to the given test
00167 **********************************************************/
00168 int TestAddNode(int TestID, int RuleNum, void* Data){
00169         TestNode*       Node;
00170         TestNode*       New;
00171         DecoderRec*     Decoder;
00172 
00173 #ifdef DEBUGPATH
00174         printf("In TestAddNode\n");
00175 #endif  
00176         
00177         New=(TestNode*)calloc(sizeof(TestNode),1);
00178         New->RuleID=RuleNum;
00179         New->Data=Data;
00180         
00181         if (!Globals.Tests[TestID].TestNodes){
00182                 Globals.Tests[TestID].TestNodes=New;
00183         }else{
00184                 Node=Globals.Tests[TestID].TestNodes;
00185                 while (Node->Next) Node=Node->Next;
00186                 Node->Next=New;
00187         }       
00188         
00189         /*mark the test and decoder as active*/
00190 #ifdef DEBUG
00191         printf("Marking test \"%s\" as active\n",Globals.Tests[TestID].Name);
00192 #endif  
00193         Globals.Tests[TestID].Active=TRUE;
00194         TestSetDependency(TestID, RuleNum);
00195         
00196         Decoder=&Globals.Decoders[Globals.Tests[TestID].DecoderID];
00197         while (Decoder){
00198 #ifdef DEBUG
00199                 printf("Marking Decoder \"%s\" as active\n", Decoder->Name);
00200 #endif  
00201                 Decoder->Active=TRUE;   
00202                 DecoderSetDependency(Decoder->ID, RuleNum);
00203                 Decoder=Decoder->Parent;
00204         }
00205                 
00206         return TRUE;
00207 }
00208 
00209 /**********************************
00210 * Let all the tests know we're
00211 * finished adding rules
00212 **********************************/
00213 int TestsFinishSetup(){
00214         int     i;
00215         
00216 #ifdef DEBUGPATH
00217         printf("In TestsFinishSetup\n");
00218 #endif
00219 
00220         for (i=0;i<Globals.NumTests;i++){
00221                 if (Globals.Tests[i].FinishedSetup) Globals.Tests[i].FinishedSetup();
00222         }
00223         
00224         return TRUE;
00225 }

Generated on Sat Jul 7 23:33:10 2007 for HLBR by  doxygen 1.5.2