- fixed parser to use new payload mechanisms
authorMartin Willi <martin@strongswan.org>
Mon, 14 Nov 2005 09:43:18 +0000 (09:43 -0000)
committerMartin Willi <martin@strongswan.org>
Mon, 14 Nov 2005 09:43:18 +0000 (09:43 -0000)
Source/charon/parser.c
Source/charon/parser.h
Source/charon/payloads/payload.c
Source/charon/payloads/payload.h
Source/charon/testcases/parser_test.c
Source/charon/testcases/testcases.c

index 0175eca..ac7c056 100644 (file)
  * 
  * contains pointers and counters to store current state
  */
-typedef struct private_parser_context_s private_parser_context_t;
+typedef struct private_parser_s private_parser_t;
 
-struct private_parser_context_s {
+struct private_parser_s {
        /**
         * Public members
         */
-       parser_context_t public;
+       parser_t public;
 
        /**
         * Current bit for reading in input data
@@ -65,319 +65,273 @@ struct private_parser_context_s {
         */
        u_int8_t *input_roof;
        
-       
-};
-
-/**
- * implementation of parser_context_t.destroy
- */
-static status_t parser_context_destroy(private_parser_context_t *this)
-{
-       allocator_free(this);
-       
-       return SUCCESS; 
-}
-
-
-/**
- * @brief Private data of a parser_t object
- */
-typedef struct private_parser_s private_parser_t;
-
-struct private_parser_s {
-       /**
-        * Public part of a generator object
-        */
-        parser_t public;
-
-       /**
-        * list of payloads and their description
-        */
-       payload_info_t **payload_infos;
-       
        /**
         * logger object
         */
        logger_t *logger;
-};
-
-/**
- * implementation of parser_t.create_context
- */
-static private_parser_context_t *create_context(private_parser_t *this, chunk_t data)
-{
-       private_parser_context_t *context = allocator_alloc_thing(private_parser_context_t);
-       if (this == NULL)
-       {
-               return NULL;    
-       }
-       
-       context->public.destroy = (status_t(*)(parser_context_t*)) parser_context_destroy;
        
-       context->input = data.ptr;
-       context->byte_pos = data.ptr;
-       context->bit_pos = 0;
-       context->input_roof = data.ptr + data.len;
        
-       return context;
-}
+};
 
 /**
  * implementation of parser_context_t.parse_payload
  */
-static status_t parse_payload(private_parser_t *this, payload_type_t payload_type, void **data_struct, private_parser_context_t *context)
+static status_t parse_payload(private_parser_t *this, payload_type_t payload_type, payload_t **payload)
 {
-       payload_info_t *payload_info = NULL;
        
        this->logger->log(this->logger, CONTROL, "Parsing a %s payload", mapping_find(payload_type_t_mappings, payload_type));
        
        /* find payload in null terminated list*/
-       payload_info = *(this->payload_infos);
-       while (payload_info)
+
+       payload_t *pld;
+       void *output;
+       int current;
+       encoding_rule_t *rule;
+       size_t rule_count;
+       
+       /* ok, do the parsing */
+       pld = payload_create(payload_type);
+       if (pld == NULL)
        {
-               if (payload_info->payload_type == payload_type)
+               this->logger->log(this->logger, ERROR, "Payload not supported");
+               return NOT_SUPPORTED;   
+       }
+       
+       /* base pointer for output, avoids casting in every rule */
+       output = pld;
+       
+       pld->get_encoding_rules(pld, &rule, &rule_count);
+       
+       for (current = 0; current < rule_count; current++)
+       {
+               switch (rule->type)
                {
-                       void *output;
-                       int current;
-                       
-                       /* ok, do the parsing */
-                       output = allocator_alloc(payload_info->data_struct_length);
-                       
-                       for (current = 0; current < payload_info->encoding_rules_count; current++)
+                       case U_INT_4:
                        {
-                               encoding_rule_t *rule = &(payload_info->ecoding_rules[current]);
-                               switch (rule->type)
+                               u_int8_t *output_pos = output + rule->offset;
+                               if (this->byte_pos + sizeof(u_int8_t) > this->input_roof)
                                {
-                                       case U_INT_4:
-                                       {
-                                               u_int8_t *output_pos = output + rule->offset;
-                                               if (context->byte_pos + sizeof(u_int8_t) > context->input_roof)
-                                               {
-                                                       this->logger->log(this->logger, ERROR, "not enough input to parse U_INT_4");
-                                                       allocator_free(output);
-                                                       return PARSE_ERROR;
-                                               }
-                                               switch (context->bit_pos)
-                                               {
-                                                       case 0:
-                                                               *output_pos = *(context->byte_pos) >> 4;
-                                                               context->bit_pos = 4;
-                                                               break;
-                                                       case 4: 
-                                                               *output_pos = *(context->byte_pos) & 0x0F;
-                                                               context->bit_pos = 0;
-                                                               context->byte_pos++;
-                                                               break;
-                                                       default:
-                                                               this->logger->log(this->logger, ERROR, "found rule U_INT_4 on bitpos %d", context->bit_pos);
-                                                               allocator_free(output);
-                                                               return PARSE_ERROR;
-                                               }
-                                               break;
-                                       }
-                                       case U_INT_8:
-                                       {
-                                               u_int8_t *output_pos = output + rule->offset;
-                                               if (context->byte_pos + sizeof(u_int8_t)  > context->input_roof)
-                                               {
-                                                       this->logger->log(this->logger, ERROR, "not enough input to parse U_INT_8");
-                                                       allocator_free(output);
-                                                       return PARSE_ERROR;
-                                               }
-                                               if (context->bit_pos)
-                                               {
-                                                       this->logger->log(this->logger, ERROR, "found rule U_INT_8 on bitpos %d", context->bit_pos);
-                                                       allocator_free(output);
-                                                       return PARSE_ERROR;
-                                               }
-
-                                               *output_pos = *(context->byte_pos);
-                                               context->byte_pos++;
-                                               break;
-                                       }
-                                       case U_INT_16:
-                                       {
-                                               u_int16_t *output_pos = output + rule->offset;
-                                               if (context->byte_pos + sizeof(u_int16_t) > context->input_roof)
-                                               {
-                                                       this->logger->log(this->logger, ERROR, "not enough input to parse U_INT_16");
-                                                       allocator_free(output);
-                                                       return PARSE_ERROR;
-                                               }
-                                               if (context->bit_pos)
-                                               {
-                                                       this->logger->log(this->logger, ERROR, "found rule U_INT_16 on bitpos %d", context->bit_pos);
-                                                       allocator_free(output);
-                                                       return PARSE_ERROR;
-                                               }
-                                               if ((int)context->byte_pos % 2)
-                                               {
-                                                       this->logger->log(this->logger, ERROR, "found rule U_INT_16 on odd bytepos");
-                                                       allocator_free(output);
-                                                       return PARSE_ERROR;
-                                               }
-                                               *output_pos = ntohs(*((u_int16_t*)context->byte_pos));
-                                               context->byte_pos += 2;
-                                               break;                                  
-                                       }
-                                       case U_INT_32:
-                                       {
-                                               u_int32_t *output_pos = output + rule->offset;
-                                               if (context->byte_pos + sizeof(u_int32_t) > context->input_roof)
-                                               {
-                                                       this->logger->log(this->logger, ERROR, "not enough input to parse U_INT_32");
-                                                       allocator_free(output);
-                                                       return PARSE_ERROR;
-                                               }
-                                               if (context->bit_pos)
-                                               {
-                                                       this->logger->log(this->logger, ERROR, "found rule U_INT_32 on bitpos %d", context->bit_pos);
-                                                       allocator_free(output);
-                                                       return PARSE_ERROR;
-                                               }
-                                               if ((int)context->byte_pos % 4)
-                                               {
-                                                       this->logger->log(this->logger, ERROR, "found rule U_INT_32 on unaligned bytepos");
-                                                       allocator_free(output);
-                                                       return PARSE_ERROR;
-                                               }
-                                               *output_pos = ntohl(*((u_int32_t*)context->byte_pos));
-                                               context->byte_pos += 4;
-                                               break;          
-                                       }
-                                       case U_INT_64:
-                                       {
-                                               u_int32_t *output_pos = output + rule->offset;
-                                               if (context->byte_pos + 2 * sizeof(u_int32_t) > context->input_roof)
-                                               {
-                                                       this->logger->log(this->logger, ERROR, "not enough input to parse U_INT_64");
-                                                       allocator_free(output);
-                                                       return PARSE_ERROR;
-                                               }
-                                               if (context->bit_pos)
-                                               {
-                                                       this->logger->log(this->logger, ERROR, "found rule U_INT_64 on bitpos %d", context->bit_pos);
-                                                       allocator_free(output);
-                                                       return PARSE_ERROR;
-                                               }
-                                               if ((int)context->byte_pos % 8)
-                                               {
-                                                       this->logger->log(this->logger, ERROR, "found rule U_INT_64 on unaligned bytepos");
-                                                       allocator_free(output);
-                                                       return PARSE_ERROR;
-                                               }
-                                               /* assuming little endian host order */
-                                               *(output_pos + 1) = ntohl(*((u_int32_t*)context->byte_pos));
-                                               context->byte_pos += 4;
-                                               *output_pos = ntohl(*((u_int32_t*)context->byte_pos));
-                                               context->byte_pos += 4;
-                                               
-                                               break;  
-                                       }
-                                       case RESERVED_BIT:
-                                       {
-                                               if (context->byte_pos > context->input_roof)
-                                               {
-                                                       this->logger->log(this->logger, ERROR, "not enough input to parse RESERVED_BIT");
-                                                       allocator_free(output);
-                                                       return PARSE_ERROR;
-                                               }
-                                               context->bit_pos = (context->bit_pos + 1) % 8;
-                                               if (context->bit_pos == 0) 
-                                               {
-                                                       context->byte_pos++;    
-                                               }
-                                               break;
-                                       }
-                                       case RESERVED_BYTE:
-                                       {
-                                               if (context->byte_pos > context->input_roof)
-                                               {
-                                                       this->logger->log(this->logger, ERROR, "not enough input to parse RESERVED_BYTE");
-                                                       allocator_free(output);
-                                                       return PARSE_ERROR;
-                                               }
-                                               if (context->bit_pos)
-                                               {
-                                                       this->logger->log(this->logger, ERROR, "found rule RESERVED_BYTE on bitpos %d", context->bit_pos);
-                                                       allocator_free(output);
-                                                       return PARSE_ERROR;
-                                               }
-                                               context->byte_pos++;    
+                                       this->logger->log(this->logger, ERROR, "not enough input to parse U_INT_4");
+                                       pld->destroy(pld);
+                                       return PARSE_ERROR;
+                               }
+                               switch (this->bit_pos)
+                               {
+                                       case 0:
+                                               *output_pos = *(this->byte_pos) >> 4;
+                                               this->bit_pos = 4;
                                                break;
-                                       }
-                                       case FLAG:
-                                       {
-                                               bool *output_pos = output + rule->offset;
-                                               u_int8_t mask;
-                                               if (context->byte_pos > context->input_roof)
-                                               {
-                                                       this->logger->log(this->logger, ERROR, "not enough input to parse FLAG");
-                                                       allocator_free(output);
-                                                       return PARSE_ERROR;
-                                               }
-                                               mask = 0x01 << (7 - context->bit_pos);
-                                               *output_pos = *context->byte_pos & mask;
-                                       
-                                               if (*output_pos)
-                                               {
-                                                       /* set to a "clean", comparable true */
-                                                       *output_pos = TRUE;
-                                               } 
-                                               context->bit_pos = (context->bit_pos + 1) % 8;
-                                               if (context->bit_pos == 0) 
-                                               {
-                                                       context->byte_pos++;    
-                                               }
+                                       case 4: 
+                                               *output_pos = *(this->byte_pos) & 0x0F;
+                                               this->bit_pos = 0;
+                                               this->byte_pos++;
                                                break;
-                                       }
-                                       case LENGTH:
-                                       {
-                                               u_int32_t *output_pos = output + rule->offset;
-                                               if (context->byte_pos + sizeof(u_int32_t) > context->input_roof)
-                                               {
-                                                       this->logger->log(this->logger, ERROR, "not enough input to parse LENGTH");
-                                                       allocator_free(output);
-                                                       return PARSE_ERROR;
-                                               }
-                                               if (context->bit_pos)
-                                               {
-                                                       this->logger->log(this->logger, ERROR, "found rule LENGTH on bitpos %d", context->bit_pos);
-                                                       allocator_free(output);
-                                                       return PARSE_ERROR;
-                                               }
-                                               if ((int)context->byte_pos % 4)
-                                               {
-                                                       this->logger->log(this->logger, ERROR, "found rule LENGTH on unaligned bytepos");
-                                                       allocator_free(output);
-                                                       return PARSE_ERROR;
-                                               }
-                                               *output_pos = ntohl(*((u_int32_t*)context->byte_pos));
-                                               context->byte_pos += 4;
-                                               break;          
-                                       
-                                       }
-                                       case SPI_SIZE:
-                                       {
-                                               
-                                       }
                                        default:
-                                       {
-                                               this->logger->log(this->logger, ERROR, "parser found unknown type");
-                                               allocator_free(output);
+                                               this->logger->log(this->logger, ERROR, "found rule U_INT_4 on bitpos %d", this->bit_pos);
+                                               pld->destroy(pld);
                                                return PARSE_ERROR;
-                                       }
-                               }       
+                               }
+                               break;
+                       }
+                       case U_INT_8:
+                       {
+                               u_int8_t *output_pos = output + rule->offset;
+                               if (this->byte_pos + sizeof(u_int8_t)  > this->input_roof)
+                               {
+                                       this->logger->log(this->logger, ERROR, "not enough input to parse U_INT_8");
+                                       pld->destroy(pld);
+                                       return PARSE_ERROR;
+                               }
+                               if (this->bit_pos)
+                               {
+                                       this->logger->log(this->logger, ERROR, "found rule U_INT_8 on bitpos %d", this->bit_pos);
+                                       pld->destroy(pld);
+                                       return PARSE_ERROR;
+                               }
+
+                               *output_pos = *(this->byte_pos);
+                               this->byte_pos++;
+                               break;
+                       }
+                       case U_INT_16:
+                       {
+                               u_int16_t *output_pos = output + rule->offset;
+                               if (this->byte_pos + sizeof(u_int16_t) > this->input_roof)
+                               {
+                                       this->logger->log(this->logger, ERROR, "not enough input to parse U_INT_16");
+                                       pld->destroy(pld);
+                                       return PARSE_ERROR;
+                               }
+                               if (this->bit_pos)
+                               {
+                                       this->logger->log(this->logger, ERROR, "found rule U_INT_16 on bitpos %d", this->bit_pos);
+                                       pld->destroy(pld);
+                                       return PARSE_ERROR;
+                               }
+                               if ((int)this->byte_pos % 2)
+                               {
+                                       this->logger->log(this->logger, ERROR, "found rule U_INT_16 on odd bytepos");
+                                       pld->destroy(pld);
+                                       return PARSE_ERROR;
+                               }
+                               *output_pos = ntohs(*((u_int16_t*)this->byte_pos));
+                               this->byte_pos += 2;
+                               break;                                  
+                       }
+                       case U_INT_32:
+                       {
+                               u_int32_t *output_pos = output + rule->offset;
+                               if (this->byte_pos + sizeof(u_int32_t) > this->input_roof)
+                               {
+                                       this->logger->log(this->logger, ERROR, "not enough input to parse U_INT_32");
+                                       pld->destroy(pld);
+                                       return PARSE_ERROR;
+                               }
+                               if (this->bit_pos)
+                               {
+                                       this->logger->log(this->logger, ERROR, "found rule U_INT_32 on bitpos %d", this->bit_pos);
+                                       pld->destroy(pld);
+                                       return PARSE_ERROR;
+                               }
+                               if ((int)this->byte_pos % 4)
+                               {
+                                       this->logger->log(this->logger, ERROR, "found rule U_INT_32 on unaligned bytepos");
+                                       pld->destroy(pld);
+                                       return PARSE_ERROR;
+                               }
+                               *output_pos = ntohl(*((u_int32_t*)this->byte_pos));
+                               this->byte_pos += 4;
+                               break;          
+                       }
+                       case U_INT_64:
+                       {
+                               u_int32_t *output_pos = output + rule->offset;
+                               if (this->byte_pos + 2 * sizeof(u_int32_t) > this->input_roof)
+                               {
+                                       this->logger->log(this->logger, ERROR, "not enough input to parse U_INT_64");
+                                       pld->destroy(pld);
+                                       return PARSE_ERROR;
+                               }
+                               if (this->bit_pos)
+                               {
+                                       this->logger->log(this->logger, ERROR, "found rule U_INT_64 on bitpos %d", this->bit_pos);
+                                       pld->destroy(pld);
+                                       return PARSE_ERROR;
+                               }
+                               if ((int)this->byte_pos % 8)
+                               {
+                                       this->logger->log(this->logger, ERROR, "found rule U_INT_64 on unaligned bytepos");
+                                       pld->destroy(pld);
+                                       return PARSE_ERROR;
+                               }
+                               /* assuming little endian host order */
+                               *(output_pos + 1) = ntohl(*((u_int32_t*)this->byte_pos));
+                               this->byte_pos += 4;
+                               *output_pos = ntohl(*((u_int32_t*)this->byte_pos));
+                               this->byte_pos += 4;
+                               
+                               break;  
                        }
+                       case RESERVED_BIT:
+                       {
+                               if (this->byte_pos > this->input_roof)
+                               {
+                                       this->logger->log(this->logger, ERROR, "not enough input to parse RESERVED_BIT");
+                                       pld->destroy(pld);
+                                       return PARSE_ERROR;
+                               }
+                               this->bit_pos = (this->bit_pos + 1) % 8;
+                               if (this->bit_pos == 0) 
+                               {
+                                       this->byte_pos++;       
+                               }
+                               break;
+                       }
+                       case RESERVED_BYTE:
+                       {
+                               if (this->byte_pos > this->input_roof)
+                               {
+                                       this->logger->log(this->logger, ERROR, "not enough input to parse RESERVED_BYTE");
+                                       pld->destroy(pld);
+                                       return PARSE_ERROR;
+                               }
+                               if (this->bit_pos)
+                               {
+                                       this->logger->log(this->logger, ERROR, "found rule RESERVED_BYTE on bitpos %d", this->bit_pos);
+                                       pld->destroy(pld);
+                                       return PARSE_ERROR;
+                               }
+                               this->byte_pos++;       
+                               break;
+                       }
+                       case FLAG:
+                       {
+                               bool *output_pos = output + rule->offset;
+                               u_int8_t mask;
+                               if (this->byte_pos > this->input_roof)
+                               {
+                                       this->logger->log(this->logger, ERROR, "not enough input to parse FLAG");
+                                       pld->destroy(pld);
+                                       return PARSE_ERROR;
+                               }
+                               mask = 0x01 << (7 - this->bit_pos);
+                               *output_pos = *this->byte_pos & mask;
                        
-                       *data_struct = output;
-                       return SUCCESS;
+                               if (*output_pos)
+                               {
+                                       /* set to a "clean", comparable true */
+                                       *output_pos = TRUE;
+                               } 
+                               this->bit_pos = (this->bit_pos + 1) % 8;
+                               if (this->bit_pos == 0) 
+                               {
+                                       this->byte_pos++;       
+                               }
+                               break;
+                       }
+                       case LENGTH:
+                       {
+                               u_int32_t *output_pos = output + rule->offset;
+                               if (this->byte_pos + sizeof(u_int32_t) > this->input_roof)
+                               {
+                                       this->logger->log(this->logger, ERROR, "not enough input to parse LENGTH");
+                                       pld->destroy(pld);
+                                       return PARSE_ERROR;
+                               }
+                               if (this->bit_pos)
+                               {
+                                       this->logger->log(this->logger, ERROR, "found rule LENGTH on bitpos %d", this->bit_pos);
+                                       pld->destroy(pld);
+                                       return PARSE_ERROR;
+                               }
+                               if ((int)this->byte_pos % 4)
+                               {
+                                       this->logger->log(this->logger, ERROR, "found rule LENGTH on unaligned bytepos");
+                                       pld->destroy(pld);
+                                       return PARSE_ERROR;
+                               }
+                               *output_pos = ntohl(*((u_int32_t*)this->byte_pos));
+                               this->byte_pos += 4;
+                               break;          
+                       
+                       }
+                       case SPI_SIZE:
+                       {
+                               
+                       }
+                       default:
+                       {
+                               this->logger->log(this->logger, ERROR, "parser found unknown type");
+                               pld->destroy(pld);
+                               return PARSE_ERROR;
+                       }
                }
-               payload_info++;
+               /* process next rulue */
+               rule++;
        }
        
-       this->logger->log(this->logger, ERROR, "Payload not supported");
-       return NOT_SUPPORTED;
+       *payload = pld;
+       return SUCCESS;
 }
 
 /**
@@ -394,7 +348,7 @@ static status_t destroy(private_parser_t *this)
 /*
  * see header file
  */
-parser_t *parser_create(payload_info_t **payload_infos)
+parser_t *parser_create(chunk_t data)
 {
        private_parser_t *this = allocator_alloc_thing(private_parser_t);
        
@@ -403,19 +357,23 @@ parser_t *parser_create(payload_info_t **payload_infos)
                return NULL;
        }
        
-       this->logger = global_logger_manager->create_logger(global_logger_manager,PARSER, NULL);
+       this->logger = global_logger_manager->create_logger(global_logger_manager, PARSER, NULL);
        
        if (this->logger == NULL)
        {
                allocator_free(this);
                return NULL;
        }
-       this->public.create_context = (parser_context_t*(*)(parser_t*,chunk_t)) create_context;
-       this->public.parse_payload = (status_t(*)(parser_t*,payload_type_t,void**,parser_context_t*)) parse_payload;
+       
+       this->public.parse_payload = (status_t(*)(parser_t*,payload_type_t,payload_t**)) parse_payload;
        this->public.destroy = (status_t(*)(parser_t*)) destroy;
        
-       this->payload_infos = payload_infos;    
        
+       this->input = data.ptr;
+       this->byte_pos = data.ptr;
+       this->bit_pos = 0;
+       this->input_roof = data.ptr + data.len;
        
        return (parser_t*)this;
 }
+
index fb49111..b414351 100644 (file)
 #include "payloads/encodings.h"
 #include "payloads/payload.h"
 
-/**
- * @brief The parser context stores state information for a parsing session.
- */
-typedef struct parser_context_s parser_context_t;
-
-struct parser_context_s {
-       /**
-        * @brief destructor of parsing_context
-        * 
-        * called it when finished a parsing session
-        * 
-        * @param this          the parser_context_t to destroy
-        * @return
-        *                                      - SUCCESS in any case
-        */
-       status_t (*destroy) (parser_context_t *this);
-};
-
 
 /**
  * @brief A parser_t object which parses payloads of specific type
@@ -54,34 +36,20 @@ typedef struct parser_s parser_t;
 struct parser_s {
        
        /**
-        * @brief generates a context for parsing
-        *
-        * a context is used for a parsing session. It safes the state, such as
-        * parsing position, available size, ...
-        *
-        * @param parser                        parser Object
-        * @param chunk                         chunk of data to parse in this session
-        * @return                                      the parsing_context, or NULL if failed
-        */
-
-       parser_context_t *(*create_context) (parser_t *this, chunk_t data);
-       
-       /**
         * @brief parses the next payload in the current context
         * 
         * @warning caller is responsible for freeing allocated data_struct
         *
         * @param parser                        parser Object
         * @param payload_type          payload to parse
-        * @param[out] data_struct      pointer where parsed data will be allocated
-        * @param context                       the parsing context, describing the current parsing session
+        * @param[out] payload          pointer where parsed payload was allocated
         * @return                      
         *                                                      - SUCCESSFUL if succeeded,
         *                                                      - NOT_SUPPORTED if payload_type is not supported
         *                                                      - OUT_OF_RES if out of ressources
         *                                                      - PARSE_ERROR if corrupted data found
         */
-       status_t (*parse_payload) (parser_t *this, payload_type_t payload_type, void **data_struct, parser_context_t* context);
+       status_t (*parse_payload) (parser_t *this, payload_type_t payload_type, payload_t **payload);
        
        /**
         * @brief Destroys a parser object
@@ -99,9 +67,10 @@ struct parser_s {
  * The parser uses a set of payload_infos to know how to
  * parse different payloads.
  * 
- * @param payload_infos                        list of payload_info_t 
+ * @param chunk                                chunk of data to parse in this session
+ * @return                                     the parser, or NULL if failed
  *
  */
-parser_t *parser_create();
+parser_t *parser_create(chunk_t data);
 
 #endif /*PARSER_H_*/
index e244460..a221601 100644 (file)
@@ -58,7 +58,7 @@ mapping_t payload_type_t_mappings[] = {
 /*
  * see header
  */
-payload_t *create_payload(payload_type_t type)
+payload_t *payload_create(payload_type_t type)
 {
        switch (type)
        {
index bfb8df8..1625a86 100644 (file)
@@ -192,6 +192,6 @@ struct payload_s {
  *                                     - NULL if failed
  */
  
-payload_t *create_payload(payload_type_t type);
+payload_t *payload_create(payload_type_t type);
 
 #endif /*PAYLOAD_H_*/
index e15cfd8..36ed25b 100644 (file)
@@ -30,7 +30,6 @@
 #include "../payloads/encodings.h"
 #include "../payloads/ike_header.h"
 
-extern payload_info_t *payload_infos[];
 
 extern logger_manager_t *global_logger_manager;
 
@@ -41,16 +40,11 @@ extern logger_manager_t *global_logger_manager;
 void test_parser_with_header_payload(tester_t *tester)
 {
        parser_t *parser;
-       parser_context_t *parser_context;
-       ike_header_t *header_data;
+       ike_header_t *ike_header;
        status_t status;
-       chunk_t test_chunk;
+       chunk_t header_chunk;
        
-       logger_t *logger;
-       
-       logger = global_logger_manager->create_logger(global_logger_manager,TESTER, "header payload");
-       
-       u_int8_t test_bytes[] = {
+       u_int8_t header_bytes[] = {
                0x00,0x00,0x00,0x00,
                0x00,0x00,0x00,0x01,
                0x00,0x00,0x00,0x00,
@@ -59,36 +53,29 @@ void test_parser_with_header_payload(tester_t *tester)
                0x00,0x00,0x00,0x07,
                0x00,0x00,0x00,0x08,
        };
-       test_chunk.ptr = test_bytes;
-       test_chunk.len = sizeof(test_bytes);
+       header_chunk.ptr = header_bytes;
+       header_chunk.len = sizeof(header_bytes);
 
        
-       parser = parser_create(payload_infos);
+       parser = parser_create(header_chunk);
        tester->assert_true(tester,(parser != NULL), "parser create check");
        
-       parser_context = parser->create_context(parser, test_chunk);
-       tester->assert_true(tester,(parser_context != NULL), "parser_context create check");
-
-       status = parser->parse_payload(parser, HEADER, (void**)&header_data, parser_context);
+       status = parser->parse_payload(parser, HEADER, (payload_t**)&ike_header);
        tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
        
-       tester->assert_true(tester,(header_data->initiator_spi == 1),"parsed initiator_spi value");
-       tester->assert_true(tester,(header_data->responder_spi == 2),"parsed responder_spi value");
-       tester->assert_true(tester,(header_data->next_payload == 3),"parsed next_payload value");
-       tester->assert_true(tester,(header_data->maj_version == 4),"parsed maj_version value");
-       tester->assert_true(tester,(header_data->min_version == 5),"parsed min_version value");
-       tester->assert_true(tester,(header_data->exchange_type == 6),"parsed exchange_type value");
-       tester->assert_true(tester,(header_data->flags.initiator == TRUE),"parsed flags.initiator value");
-       tester->assert_true(tester,(header_data->flags.version == FALSE),"parsed flags.version value");
-       tester->assert_true(tester,(header_data->flags.response == TRUE),"parsed flags.response value");
-       tester->assert_true(tester,(header_data->message_id == 7),"parsed message_id value");
-       tester->assert_true(tester,(header_data->length == 8),"parsed length value");
+       tester->assert_true(tester,(ike_header->initiator_spi == 1),"parsed initiator_spi value");
+       tester->assert_true(tester,(ike_header->responder_spi == 2),"parsed responder_spi value");
+       tester->assert_true(tester,(ike_header->next_payload == 3),"parsed next_payload value");
+       tester->assert_true(tester,(ike_header->maj_version == 4),"parsed maj_version value");
+       tester->assert_true(tester,(ike_header->min_version == 5),"parsed min_version value");
+       tester->assert_true(tester,(ike_header->exchange_type == 6),"parsed exchange_type value");
+       tester->assert_true(tester,(ike_header->flags.initiator == TRUE),"parsed flags.initiator value");
+       tester->assert_true(tester,(ike_header->flags.version == FALSE),"parsed flags.version value");
+       tester->assert_true(tester,(ike_header->flags.response == TRUE),"parsed flags.response value");
+       tester->assert_true(tester,(ike_header->message_id == 7),"parsed message_id value");
+       tester->assert_true(tester,(ike_header->length == 8),"parsed length value");
        
-       
-       parser_context->destroy(parser_context);
        tester->assert_true(tester,(parser->destroy(parser) == SUCCESS), "parser destroy call check");
        
-       logger->log_bytes(logger, RAW, "Header", (void*)header_data, sizeof(ike_header_t));
-       
-       allocator_free(header_data);
+       ike_header->destroy(ike_header);
 }
index 1d935ef..db0704a 100644 (file)
@@ -43,7 +43,7 @@
 #include "ike_sa_test.h"
 #include "ike_sa_manager_test.h"
 #include "generator_test.h"
-//#include "parser_test.h"
+#include "parser_test.h"
 #include "packet_test.h"
 
 
@@ -129,7 +129,7 @@ test_t generator_test2 = {test_generator_with_header_payload,"Generator: header
 /**
  * Test 2 for generator_t
  */
-//test_t parser_test = {test_parser_with_header_payload, "Parser: header payload"};
+test_t parser_test = {test_parser_with_header_payload, "Parser: header payload"};
 
 
 /**
@@ -183,7 +183,7 @@ logger_manager_t *global_logger_manager;
        &ike_sa_id_test,
        &ike_sa_test,
        &generator_test2,
-//     &parser_test,
+       &parser_test,
        &ike_sa_manager_test,
        &packet_test,
        NULL