- all header field types implemented and tested
authorJan Hutter <jhutter@hsr.ch>
Wed, 9 Nov 2005 16:07:33 +0000 (16:07 -0000)
committerJan Hutter <jhutter@hsr.ch>
Wed, 9 Nov 2005 16:07:33 +0000 (16:07 -0000)
Source/charon/generator.c
Source/charon/tests/generator_test.c

index d5d1be3..83c4b4a 100644 (file)
@@ -350,10 +350,7 @@ static status_t generate_u_int_type (private_generator_t *this,encoding_type_t i
                        return FAILED;
                        
        }
-       
 
-       
-       
        return SUCCESS;
 }
 
@@ -379,6 +376,7 @@ static status_t generate (private_generator_t *this,void * data_struct,encoding_
                status = SUCCESS;
                switch (encoding_rules[i].type)
                {
+                       /* all u int values are generated in generate_u_int_type */
                        case U_INT_4:
                        case U_INT_8:
                        case U_INT_16:
@@ -387,10 +385,51 @@ static status_t generate (private_generator_t *this,void * data_struct,encoding_
                                status = this->generate_u_int_type(this,encoding_rules[i].type,encoding_rules[i].offset,infos);
                                break;
                        case RESERVED_BIT:
+                       {
+                               u_int8_t reserved_bit = ~(1 << (7 - infos->current_bit));
+                               
+                               *(infos->out_position) = *(infos->out_position) & reserved_bit;
+                               
+                               infos->current_bit++;
+                               if (infos->current_bit >= 8)
+                               {
+                                       infos->current_bit = infos->current_bit % 8;
+                                       status = infos->make_space_available(infos,8);
+                                       infos->out_position++;
+                               }
+                               break;
+                       }
                        case RESERVED_BYTE:
+                       {
+                               if (infos->current_bit > 0)
+                               {
+                                       return FAILED;
+                               }
+                               *(infos->out_position) = 0x00;
+                               infos->out_position++;
+                               break;
+                       }
                        case FLAG:
+                       {
+                               u_int8_t flag_value = (*((bool *) (infos->data_struct + encoding_rules[i].offset))) ? 1 : 0;
+                               u_int8_t flag = (flag_value << (7 - infos->current_bit));
+                               
+                               *(infos->out_position) = *(infos->out_position) | flag;
+                               
+                               infos->current_bit++;
+                               if (infos->current_bit >= 8)
+                               {
+                                       infos->current_bit = infos->current_bit % 8;
+                                       status = infos->make_space_available(infos,8);
+                                       infos->out_position++;
+                               }
+                               break;
+                       }
                        case LENGTH:
+                               /* length is generated like an U_INT_32 */
+                               status = this->generate_u_int_type(this,U_INT_32,encoding_rules[i].offset,infos);
                        case SPI_SIZE:
+                               /* actually not implemented */
                        default:
                                break;
                }
index 49ffb08..228d501 100644 (file)
@@ -20,6 +20,7 @@
  * for more details.
  */
  
+#include <string.h>
 #include <freeswan.h>
 #include <pluto/constants.h>
 #include <pluto/defs.h>
@@ -33,6 +34,9 @@
 
 extern payload_info_t *payload_infos[];
 
+/*
+ * Described in Header 
+ */
 void test_generator_with_unsupported_payload(tester_t *tester)
 {
        generator_t *generator;
@@ -46,11 +50,15 @@ void test_generator_with_unsupported_payload(tester_t *tester)
        tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
 }
 
+/*
+ * Described in Header 
+ */
 void test_generator_with_header_payload(tester_t *tester)
 {
        generator_t *generator;
        ike_header_t header_data;
        chunk_t generated_data;
+       status_t status;
        
        header_data.initiator_spi = 1;
        header_data.responder_spi = 2;
@@ -59,24 +67,56 @@ void test_generator_with_header_payload(tester_t *tester)
        header_data.min_version = 5;
        header_data.exchange_type = 6;
        header_data.flags.initiator = TRUE;
-       header_data.flags.version = TRUE;
+       header_data.flags.version = FALSE;
        header_data.flags.response = TRUE;
        header_data.message_id = 7;
        header_data.length = 8;
        
        generator = generator_create(payload_infos);
        tester->assert_true(tester,(generator != NULL), "generator create check");
-       tester->assert_true(tester,(generator->generate_payload(generator,HEADER,&header_data,&generated_data) == SUCCESS),"generate_payload call check");
 
-       int i;
-       u_int8_t *data = generated_data.ptr;
-       
-       for (i = 0; i < generated_data.len;i++)
-       {
-               fprintf(stderr,"%x\n",data[i]);
-       }
+       status = generator->generate_payload(generator,HEADER,&header_data,&generated_data);
+       tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
 
+       u_int8_t expected_generation[] = {
+               0x00,0x00,0x00,0x00,
+               0x00,0x00,0x00,0x01,
+               0x00,0x00,0x00,0x00,
+               0x00,0x00,0x00,0x02,
+               0x03,0x45,0x06,0x28,
+               0x00,0x00,0x00,0x07,
+               0x00,0x00,0x00,0x08,
+       };
        
+       tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
+       allocator_free_chunk(generated_data);
+       
+       header_data.initiator_spi = 0x22000054231234;
+       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 = FALSE;
+       header_data.flags.version = FALSE;
+       header_data.flags.response = FALSE;
+       header_data.message_id = 0x33AFF3;
+       header_data.length = 0xAA11F;
+       
+       status = generator->generate_payload(generator,HEADER,&header_data,&generated_data);
+       tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
+
+       u_int8_t expected_generation2[] = {
+               0x00,0x22,0x00,0x00,
+               0x54,0x23,0x12,0x34,
+               0x00,0x00,0x00,0x00,
+               0x00,0x12,0x23,0x98,
+               0xF3,0x20,0x12,0x00,
+               0x00,0x33,0xAF,0xF3,
+               0x00,0x0A,0xA1,0x1F,
+       };
+
+       tester->assert_true(tester,(memcmp(expected_generation2,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
        allocator_free_chunk(generated_data);
        
        tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");