- SA Payload is generated
authorJan Hutter <jhutter@hsr.ch>
Tue, 15 Nov 2005 09:40:58 +0000 (09:40 -0000)
committerJan Hutter <jhutter@hsr.ch>
Tue, 15 Nov 2005 09:40:58 +0000 (09:40 -0000)
Source/charon/generator.c
Source/charon/testcases/generator_test.c
Source/charon/testcases/generator_test.h
Source/charon/testcases/testcases.c

index 00788b5..4560f4a 100644 (file)
@@ -35,6 +35,7 @@
 #include "payloads/payload.h"
 #include "payloads/proposal_substructure.h"
 #include "payloads/transform_substructure.h"
+#include "payloads/sa_payload.h"
 
 
 extern logger_manager_t *global_logger_manager;
@@ -615,6 +616,58 @@ static status_t generate_payload (private_generator_t *this,payload_t *payload)
                                status = this->generate_from_chunk(this,rules[i].offset);
                                break;
                        }
+                       case PROPOSALS:
+                       {
+                               this->logger->log(this->logger,CONTROL_MORE,"Generate Proposals");
+                               /* before iterative generate the transforms, store the current length position */
+                               u_int32_t payload_length_position_offset = this->last_payload_length_position_offset;
+
+                               u_int16_t length_of_sa_payload = SA_PAYLOAD_HEADER_LENGTH;
+                               u_int16_t int16_val;
+                               linked_list_t *proposals = *((linked_list_t **)(this->data_struct + rules[i].offset));
+
+                               linked_list_iterator_t *iterator;
+                               /* create forward iterator */
+                               status = proposals->create_iterator(proposals,&iterator,TRUE);
+                               if (status != SUCCESS)
+                               {
+                                       return status;
+                               }
+                               while (iterator->has_next(iterator))
+                               {
+                                       payload_t *current_proposal;
+                                       u_int32_t before_generate_position_offset;
+                                       u_int32_t after_generate_position_offset;
+                                       
+                                       status = iterator->current(iterator,(void **)&current_proposal);
+                                       if (status != SUCCESS)
+                                       {
+                                               iterator->destroy(iterator);    
+                                               return status;
+                                       }
+                                       
+                                       before_generate_position_offset = (this->out_position - this->buffer);
+                                       status = this->public.generate_payload(&(this->public),current_proposal);
+                                       after_generate_position_offset = (this->out_position - this->buffer);
+                                       if (status != SUCCESS)
+                                       {
+                                               return status;
+                                       }
+                                       
+                                       /* increase size of transform */
+                                       length_of_sa_payload += (after_generate_position_offset - before_generate_position_offset);
+                               }
+                               
+                               iterator->destroy(iterator);
+                               
+                               this->logger->log(this->logger,CONTROL_MORE,"Length of Payload is %d, offset is %d",length_of_sa_payload,payload_length_position_offset);
+                               
+                               int16_val = htons(length_of_sa_payload);
+                               this->write_bytes_to_buffer_at_offset(this,&int16_val,sizeof(u_int16_t),payload_length_position_offset);
+                               
+                               break;
+                       }       
+
                        case TRANSFORMS:
                        {
                                this->logger->log(this->logger,CONTROL_MORE,"Generate Transforms");
@@ -646,8 +699,12 @@ static status_t generate_payload (private_generator_t *this,payload_t *payload)
                                        }
                                        
                                        before_generate_position_offset = (this->out_position - this->buffer);
-                                       this->public.generate_payload(&(this->public),current_transform);
+                                       status = this->public.generate_payload(&(this->public),current_transform);
                                        after_generate_position_offset = (this->out_position - this->buffer);
+                                       if (status != SUCCESS)
+                                       {
+                                               return status;
+                                       }
                                        
                                        /* increase size of transform */
                                        length_of_proposal += (after_generate_position_offset - before_generate_position_offset);
index a21d7ed..6a995d1 100644 (file)
@@ -34,6 +34,7 @@
 #include "../payloads/transform_attribute.h"
 #include "../payloads/transform_substructure.h"
 #include "../payloads/proposal_substructure.h"
+#include "../payloads/sa_payload.h"
 
 /*
  * Described in Header 
@@ -437,3 +438,176 @@ void test_generator_with_proposal_substructure(tester_t *tester)
        global_logger_manager->destroy_logger(global_logger_manager,logger);    
 }
 
+void test_generator_with_sa_payload(tester_t *tester)
+{
+       generator_t *generator;
+       transform_attribute_t *attribute1, *attribute2, *attribute3;
+       transform_substructure_t *transform1, *transform2;
+       proposal_substructure_t *proposal1, *proposal2;
+       sa_payload_t *sa_payload;
+       ike_header_t *ike_header;
+       
+       chunk_t data;
+       status_t status;
+       chunk_t generated_data;
+       logger_t *logger;
+       
+       logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"Message with SA Payload");
+       
+       /* create generator */
+       generator = generator_create();
+       tester->assert_true(tester,(generator != NULL), "generator create check");
+
+       /* create attribute 1 */        
+       attribute1 = transform_attribute_create();
+       char *stringval = "abcd";
+       data.ptr = (void *) stringval;
+       data.len = 4;
+       status = attribute1->set_value(attribute1,data);
+       tester->assert_true(tester,(status == SUCCESS),"set_value call check");
+       status = attribute1->set_attribute_type(attribute1,0);
+       tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
+       logger->log(logger,CONTROL,"attribute1 created");
+
+       /* create attribute 2 */
+       attribute2 = transform_attribute_create();
+       stringval = "efgh";
+       data.ptr = (void *) stringval;
+       data.len = 4;
+       status = attribute2->set_value(attribute2,data);
+       tester->assert_true(tester,(status == SUCCESS),"set_value call check");
+       status = attribute2->set_attribute_type(attribute2,0);
+       tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
+       logger->log(logger,CONTROL,"attribute2 created");
+
+       /* create attribute 3 */
+       attribute3 = transform_attribute_create();
+       stringval = "ijkl";
+       data.ptr = (void *) stringval;
+       data.len = 4;
+       status = attribute3->set_value(attribute3,data);
+       tester->assert_true(tester,(status == SUCCESS),"set_value call check");
+       status = attribute3->set_attribute_type(attribute3,0);
+       tester->assert_true(tester,(status == SUCCESS),"set_attribute_type call check");
+       logger->log(logger,CONTROL,"attribute3 created");
+
+       /* create transform 1*/
+       transform1 = transform_substructure_create();
+       tester->assert_true(tester,(transform1 != NULL), "transform create check");
+       status = transform1->add_transform_attribute(transform1,attribute1);
+       tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
+       status = transform1->add_transform_attribute(transform1,attribute2);
+       tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
+       status = transform1->set_transform_type(transform1,5); /* hex 5 */
+       tester->assert_true(tester,(status == SUCCESS),"set_transform_type call check");
+       status = transform1->set_transform_id(transform1,65000); /* hex FDE8 */
+       tester->assert_true(tester,(status == SUCCESS),"set_transform_id call check");
+       
+       /* create transform 2*/
+       transform2 = transform_substructure_create();
+       tester->assert_true(tester,(transform2 != NULL), "transform create check");
+       status = transform2->add_transform_attribute(transform2,attribute3);
+       tester->assert_true(tester,(status == SUCCESS),"add_transform_attribute call check");
+       status = transform2->set_transform_type(transform2,3); /* hex 3 */
+       tester->assert_true(tester,(status == SUCCESS),"set_transform_type call check");
+       status = transform2->set_transform_id(transform2,4); /* hex 4 */
+       tester->assert_true(tester,(status == SUCCESS),"set_transform_id call check");
+       
+       logger->log(logger,CONTROL,"transforms created");
+       
+       /* create proposal 1 */
+       proposal1 = proposal_substructure_create();
+       tester->assert_true(tester,(proposal1 != NULL), "proposal create check");
+       
+       stringval = "ABCDEFGH";
+       data.ptr = (void *) stringval;
+       data.len = 8;
+       
+       status = proposal1->add_transform_substructure(proposal1,transform1);
+       tester->assert_true(tester,(status == SUCCESS),"add_transform_substructure call check");
+       status = proposal1->add_transform_substructure(proposal1,transform2);
+       tester->assert_true(tester,(status == SUCCESS),"add_transform_substructure call check");        
+       status = proposal1->set_spi(proposal1,data);
+       tester->assert_true(tester,(status == SUCCESS),"set_spi call check");   
+       status = proposal1->set_proposal_number(proposal1,7);
+       tester->assert_true(tester,(status == SUCCESS),"set_proposal_number call check");       
+       status = proposal1->set_protocol_id(proposal1,4);
+       tester->assert_true(tester,(status == SUCCESS),"set_protocol_id call check");   
+       
+       /* create proposal 2 */
+       proposal2 = proposal_substructure_create();
+       tester->assert_true(tester,(proposal2 != NULL), "proposal create check");
+       status = proposal2->set_proposal_number(proposal2,7);
+       tester->assert_true(tester,(status == SUCCESS),"set_proposal_number call check");       
+       status = proposal2->set_protocol_id(proposal2,5);
+       tester->assert_true(tester,(status == SUCCESS),"set_protocol_id call check");
+
+       /* create sa_payload */
+       sa_payload = sa_payload_create();
+       
+       sa_payload->add_proposal_substructure(sa_payload,proposal1);
+       sa_payload->add_proposal_substructure(sa_payload,proposal2);
+       
+       ike_header = ike_header_create();
+       ike_header->set_initiator_spi(ike_header,0x22000054231234LL);
+       ike_header->set_responder_spi(ike_header,0x122398);
+       ((payload_t *) ike_header)->set_next_type((payload_t *) ike_header,SECURITY_ASSOCIATION);
+       ike_header->set_exchange_type(ike_header, 0x12);
+       ike_header->set_initiator_flag(ike_header, TRUE);
+       ike_header->set_response_flag(ike_header, TRUE);
+       ike_header->set_message_id(ike_header,0x33AFF3);
+
+       status = generator->generate_payload(generator,(payload_t *)ike_header);
+       tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
+       status = generator->generate_payload(generator,(payload_t *)sa_payload);
+       tester->assert_true(tester,(status == SUCCESS),"generate_payload call check");
+       tester->assert_true(tester,(generator->write_to_chunk(generator,&generated_data) == SUCCESS),"write_to_chunk call check");
+       logger->log_chunk(logger,RAW,"generated transform",&generated_data);    
+
+       u_int8_t expected_generation[] = {
+               /* sa payload header */
+               0x00,0x22,0x00,0x00,
+               0x54,0x23,0x12,0x34,
+               0x00,0x00,0x00,0x00,
+               0x00,0x12,0x23,0x98,
+               0x21,0x20,0x12,0x28,
+               0x00,0x33,0xAF,0xF3,
+               0x00,0x00,0x00,0x60,
+
+               /* sa payload header */
+               0x00,0x00,0x00,0x44,
+               /* proposal header */
+               0x00,0x00,0x00,0x38,
+               0x07,0x04,0x08,0x02,
+               /* SPI */
+               0x41,0x42,0x43,0x44,
+               0x45,0x46,0x47,0x48,
+               /* first transform */
+               0x03,0x00,0x00,0x18,
+               0x05,0x00,0xFD,0xE8,
+               /* first transform attributes */
+               0x00,0x00,0x00,0x04,
+               0x61,0x62,0x63,0x64,
+               0x00,0x00,0x00,0x04,
+               0x65,0x66,0x67,0x68,
+               /* second transform */
+               0x00,0x00,0x00,0x10,
+               0x03,0x00,0x00,0x04,
+               /* second transform attributes */
+               0x00,0x00,0x00,0x04,
+               0x69,0x6A,0x6B,0x6C,
+               /* proposal header 2*/
+               0x00,0x00,0x00,0x08,
+               0x07,0x05,0x00,0x00,
+
+       };
+       tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
+
+       allocator_free_chunk(generated_data);
+       tester->assert_true(tester,(sa_payload->destroy(sa_payload) == SUCCESS), "sa_payload destroy call check");
+       tester->assert_true(tester,(generator->destroy(generator) == SUCCESS), "generator destroy call check");
+       
+       
+       global_logger_manager->destroy_logger(global_logger_manager,logger);    
+       
+}
index faf9286..e1a0f32 100644 (file)
@@ -58,5 +58,13 @@ void test_generator_with_transform_substructure(tester_t *tester);
  */
 void test_generator_with_proposal_substructure(tester_t *tester);
 
+/**
+ * @brief Test function used to test the generator with SA payload
+ * 
+ *
+ * @param tester associated tester object
+ */
+void test_generator_with_sa_payload(tester_t *tester);
+
 
 #endif /*GENERATOR_TEST_H_*/
index 882ca6a..f435128 100644 (file)
@@ -137,10 +137,15 @@ 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 4 for generator_t
  */
 test_t generator_test4 = {test_generator_with_proposal_substructure,"Generator: proposal substructure"};
 
+/**
+ * Test 5 for generator_t
+ */
+test_t generator_test5 = {test_generator_with_sa_payload,"Generator: Message with SA Payload"};
+
 
 /**
  * Test 2 for generator_t
@@ -211,6 +216,7 @@ logger_manager_t *global_logger_manager;
        //&parser_test_sa_payload,
        &generator_test3,
        &generator_test4,
+       &generator_test5,
        &ike_sa_manager_test,
        &packet_test,
        NULL
@@ -228,7 +234,7 @@ logger_manager_t *global_logger_manager;
        tester_t *tester = tester_create(test_output, FALSE);
 
 //     tester->perform_tests(tester,all_tests);
-       tester->perform_test(tester,&generator_test1);   
+       tester->perform_test(tester,&generator_test5);   
 
        
        tester->destroy(tester);