- test of header payload rewritten
authorJan Hutter <jhutter@hsr.ch>
Tue, 15 Nov 2005 08:52:48 +0000 (08:52 -0000)
committerJan Hutter <jhutter@hsr.ch>
Tue, 15 Nov 2005 08:52:48 +0000 (08:52 -0000)
Source/charon/generator.c
Source/charon/payloads/ike_header.c
Source/charon/payloads/ike_header.h
Source/charon/testcases/generator_test.c
Source/charon/testcases/testcases.c

index c600519..00788b5 100644 (file)
@@ -187,6 +187,11 @@ struct private_generator_s {
        u_int32_t last_payload_length_position_offset;
        
        /**
+        * Offset of the header length field in the buffer
+        */
+       u_int32_t header_length_position_offset;
+       
+       /**
         * Last SPI size
         */
        u_int8_t last_spi_size;
@@ -515,6 +520,14 @@ static status_t write_bytes_to_buffer_at_offset (private_generator_t *this,void
 static status_t write_to_chunk (private_generator_t *this,chunk_t *data)
 {
        size_t data_length = this->out_position - this->buffer;
+       u_int32_t header_length_field = data_length;
+       
+       /* write length into header length field */
+       if (this->header_length_position_offset > 0)
+       {
+               u_int32_t int32_val = htonl(header_length_field);
+               this->write_bytes_to_buffer_at_offset(this,&int32_val,sizeof(u_int32_t),this->header_length_position_offset);
+       }
 
        if (this->current_bit > 0)
        data_length++;
@@ -587,6 +600,7 @@ static status_t generate_payload (private_generator_t *this,payload_t *payload)
 
                        case HEADER_LENGTH:
                                /* header length is generated like an U_INT_32 */
+                               this->header_length_position_offset = (this->out_position - this->buffer);                              
                                status = this->generate_u_int_type(this,U_INT_32,rules[i].offset);
                                break;
                        case SPI_SIZE:
@@ -800,6 +814,7 @@ generator_t * generator_create()
        this->data_struct = NULL;
        this->current_bit = 0;
        this->last_payload_length_position_offset = 0;
+       this->header_length_position_offset = 0;
        this->logger = global_logger_manager->create_logger(global_logger_manager,GENERATOR,NULL);
        return &(this->public);
 }
index 3efe06e..49f228f 100644 (file)
@@ -134,6 +134,24 @@ encoding_rule_t ike_header_encodings[] = {
 };
 
 /**
+ * Implements ike_header_t's get_next_payload fuction.
+ * See #ike_header_t.get_next_payload  for description.
+ */
+static u_int8_t get_next_payload(private_ike_header_t *this)
+{
+       return this->next_payload;      
+}
+
+/**
+ * Implements ike_header_t's set_next_payload fuction.
+ * See #ike_header_t.set_next_payload  for description.
+ */
+static void set_next_payload(private_ike_header_t *this, u_int8_t next_payload)
+{
+       this->next_payload = next_payload;
+}
+
+/**
  * Implements ike_header_t's get_initiator_spi fuction.
  * See #ike_header_t.get_initiator_spi  for description.
  */
@@ -197,6 +215,15 @@ static bool get_response_flag(private_ike_header_t *this)
 }
 
 /**
+ * Implements ike_header_t's set_response_flag fuction.
+ * See #ike_header_t.set_response_flag  for description.
+ */
+static void set_response_flag(private_ike_header_t *this, bool response)
+{
+       this->flags.response = response;        
+}
+
+/**
  * Implements ike_header_t's get_version_flag fuction.
  * See #ike_header_t.get_version_flag  for description.
  */
@@ -215,6 +242,15 @@ static bool get_initiator_flag(private_ike_header_t *this)
 }
 
 /**
+ * Implements ike_header_t's set_initiator_flag fuction.
+ * See #ike_header_t.set_initiator_flag  for description.
+ */
+static void set_initiator_flag(private_ike_header_t *this, bool initiator)
+{
+       this->flags.initiator = initiator;      
+}
+
+/**
  * Implements ike_header_t's get_exchange_type function
  * See #ike_header_t.get_exchange_type  for description.
  */
@@ -318,6 +354,9 @@ ike_header_t *ike_header_create()
        this->public.payload_interface.destroy = (status_t (*) (payload_t *))destroy;
        this->public.destroy = destroy;
        
+
+       this->public.get_next_payload = (u_int8_t (*) (ike_header_t*))get_next_payload;
+       this->public.set_next_payload = (void (*) (ike_header_t*,u_int8_t))set_next_payload;
        this->public.get_initiator_spi = (u_int64_t (*) (ike_header_t*))get_initiator_spi;
        this->public.set_initiator_spi = (void (*) (ike_header_t*,u_int64_t))set_initiator_spi;
        this->public.get_responder_spi = (u_int64_t (*) (ike_header_t*))get_responder_spi;
@@ -325,8 +364,10 @@ ike_header_t *ike_header_create()
        this->public.get_maj_version = (u_int8_t (*) (ike_header_t*))get_maj_version;
        this->public.get_min_version = (u_int8_t (*) (ike_header_t*))get_min_version;
        this->public.get_response_flag = (bool (*) (ike_header_t*))get_response_flag;
+       this->public.set_response_flag = (void (*) (ike_header_t*,bool))set_response_flag;
        this->public.get_version_flag = (bool (*) (ike_header_t*))get_version_flag;
        this->public.get_initiator_flag = (bool (*) (ike_header_t*))get_initiator_flag;
+       this->public.set_initiator_flag = (void (*) (ike_header_t*,bool))set_initiator_flag;
        this->public.get_exchange_type = (u_int8_t (*) (ike_header_t*))get_exchange_type;
        this->public.set_exchange_type = (void (*) (ike_header_t*,u_int8_t))set_exchange_type;
        this->public.get_message_id = (u_int32_t (*) (ike_header_t*))get_message_id;
index fc8a597..aa1d8f9 100644 (file)
@@ -98,6 +98,22 @@ struct ike_header_s {
        payload_t payload_interface;
        
        /**
+        * @brief get the next payload type
+        *
+        * @param this                  ike_header_t object
+        * @return                              next payload type
+        */
+       u_int8_t (*get_next_payload) (ike_header_t *this);
+       
+       /**
+        * @brief set the next payload
+        *
+        * @param this                  ike_header_t object
+        * @param next_payload  next payload type
+        */
+       void (*set_next_payload) (ike_header_t *this, u_int8_t next_payload);
+       
+       /**
         * @brief get the initiator spi
         *
         * @param this                  ike_header_t object
@@ -154,6 +170,14 @@ struct ike_header_s {
        bool (*get_response_flag) (ike_header_t *this);
        
        /**
+        * @brief Set the response flag
+        *
+        * @param this                  ike_header_t object
+        * @param response              response flag
+        * 
+        */
+       void (*set_response_flag) (ike_header_t *this, bool response);
+       /**
         * @brief get "higher version supported"-flag
         *
         * @param this                  ike_header_t object
@@ -170,6 +194,15 @@ struct ike_header_s {
        bool (*get_initiator_flag) (ike_header_t *this);
        
        /**
+        * @brief Set the initiator flag
+        *
+        * @param this                  ike_header_t object
+        * @param initiator             initiator flag
+        * 
+        */
+       void (*set_initiator_flag) (ike_header_t *this, bool initiator);
+
+       /**
         * @brief get the exchange type
         *
         * @param this                  ike_header_t object
index 9083ee9..4dc9c00 100644 (file)
@@ -49,19 +49,14 @@ void test_generator_with_header_payload(tester_t *tester)
        logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"header payload");
        
        header_data = ike_header_create();
-       /*
-       header_data->initiator_spi = 1;
-       header_data->responder_spi = 2;
-       header_data->next_payload = 3;
-       header_data->maj_version = 4;
-       header_data->min_version = 5;
-       header_data->exchange_type = 6;
-       header_data->flags.initiator = TRUE;
-       header_data->flags.version = FALSE;
-       header_data->flags.response = TRUE;
-       header_data->message_id = 7;
-       header_data->length = 8;
-       */
+       header_data->set_initiator_spi(header_data,1);
+       header_data->set_responder_spi(header_data,2);
+       header_data->set_next_payload(header_data, 3);
+       header_data->set_exchange_type(header_data, 6);
+       header_data->set_initiator_flag(header_data, TRUE);
+       header_data->set_response_flag(header_data, TRUE);
+       header_data->set_message_id(header_data,7);
+
        generator = generator_create();
        tester->assert_true(tester,(generator != NULL), "generator create check");
        
@@ -75,31 +70,27 @@ void test_generator_with_header_payload(tester_t *tester)
                0x00,0x00,0x00,0x01,
                0x00,0x00,0x00,0x00,
                0x00,0x00,0x00,0x02,
-               0x03,0x45,0x06,0x28,
+               0x03,0x20,0x06,0x28,
                0x00,0x00,0x00,0x07,
-               0x00,0x00,0x00,0x08,
+               0x00,0x00,0x00,0x1C,
        };
 
-
+       logger->log_bytes(logger,RAW,"expected header",expected_generation,sizeof(expected_generation));
        tester->assert_true(tester,(generated_data.len == sizeof(expected_generation)), "compare generated data length");
        logger->log_chunk(logger,RAW,"generated header",&generated_data);               
        tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data 1");
        allocator_free_chunk(generated_data);
        
        tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
-       /*
-       header_data->initiator_spi = 0x22000054231234LL;
-       header_data->responder_spi = 0x122398;
-       header_data->next_payload = 0xF3;
-       header_data->maj_version = 0x2;
-       header_data->min_version = 0x0;
-       header_data->exchange_type = 0x12;
-       header_data->flags.initiator = TRUE;
-       header_data->flags.version = TRUE;
-       header_data->flags.response = TRUE;
-       header_data->message_id = 0x33AFF3;
-       header_data->length = 0xAA11F;
-       */
+
+       header_data->set_initiator_spi(header_data,0x22000054231234LL);
+       header_data->set_responder_spi(header_data,0x122398);
+       header_data->set_next_payload(header_data, 0xF3);
+       header_data->set_exchange_type(header_data, 0x12);
+       header_data->set_initiator_flag(header_data, TRUE);
+       header_data->set_response_flag(header_data, TRUE);
+       header_data->set_message_id(header_data,0x33AFF3);
+
        generator = generator_create();
        tester->assert_true(tester,(generator != NULL), "generator create check");
        
@@ -113,11 +104,13 @@ void test_generator_with_header_payload(tester_t *tester)
                0x54,0x23,0x12,0x34,
                0x00,0x00,0x00,0x00,
                0x00,0x12,0x23,0x98,
-               0xF3,0x20,0x12,0x38,
+               0xF3,0x20,0x12,0x28,
                0x00,0x33,0xAF,0xF3,
-               0x00,0x0A,0xA1,0x1F,
+               0x00,0x00,0x00,0x1C,
        };
        
+       logger->log_bytes(logger,RAW,"expected header",expected_generation2,sizeof(expected_generation2));
+       
        logger->log_chunk(logger,RAW,"generated header",&generated_data);
 
        tester->assert_true(tester,(memcmp(expected_generation2,generated_data.ptr,sizeof(expected_generation2)) == 0), "compare generated data 2");
index c649bfa..882ca6a 100644 (file)
@@ -136,18 +136,23 @@ test_t generator_test2 = {test_generator_with_transform_attribute,"Generator: tr
  */
 test_t generator_test3 = {test_generator_with_transform_substructure,"Generator: transform substructure"};
 
+/**
+ * Test 4for generator_t
+ */
+test_t generator_test4 = {test_generator_with_proposal_substructure,"Generator: proposal substructure"};
+
 
 /**
  * Test 2 for generator_t
  */
 
-test_t parser_test_header = {test_parser_with_header_payload, "Parser: header payload"};
+//test_t parser_test_header = {test_parser_with_header_payload, "Parser: header payload"};
 
 
 /**
  * Parser test for ike security association
  */
-test_t parser_test_sa_payload = {test_parser_with_sa_payload, "Parser: sa payload"};
+//test_t parser_test_sa_payload = {test_parser_with_sa_payload, "Parser: sa payload"};
 
 
 /**
@@ -202,9 +207,10 @@ logger_manager_t *global_logger_manager;
        &ike_sa_test,
        &generator_test1,
        &generator_test2,
-       &parser_test_header,
-       &parser_test_sa_payload,
+//     &parser_test_header,
+       //&parser_test_sa_payload,
        &generator_test3,
+       &generator_test4,
        &ike_sa_manager_test,
        &packet_test,
        NULL
@@ -221,8 +227,9 @@ logger_manager_t *global_logger_manager;
                
        tester_t *tester = tester_create(test_output, FALSE);
 
-       tester->perform_tests(tester,all_tests);
-//     tester->perform_test(tester,&parser_test_sa_payload);   
+//     tester->perform_tests(tester,all_tests);
+       tester->perform_test(tester,&generator_test1);   
+
        
        tester->destroy(tester);