Changeset 475

Show
Ignore:
Timestamp:
05/14/07 16:32:31
Author:
rachel
Message:

Add results comparison to Literal Data test
Add binary data test for Literal Data test

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openpgpsdk/trunk/tests/test_packet_types.c

    r473 r475  
    1010#include "tests.h" 
    1111 
     12static unsigned char* data; 
     13 
    1214#define MAXBUF 128 
    1315 
     
    3739callback(const ops_parser_content_t *content_,ops_parse_cb_info_t *cbinfo) 
    3840    { 
     41    ops_parser_content_union_t* content=(ops_parser_content_union_t *)&content_->content; 
    3942 
    4043    OPS_USED(cbinfo); 
    4144 
    42     // just print it for now 
    43     // \todo read literal data packet into buffer 
    44  
    45     ops_print_packet(content_); 
     45    //    ops_print_packet(content_); 
     46 
     47    // Read data from packet into static buffer 
     48    switch(content_->tag) 
     49        { 
     50    case OPS_PTAG_CT_LITERAL_DATA_BODY: 
     51        data=ops_mallocz(content->literal_data_body.length+1); 
     52        memcpy(data,content->literal_data_body.data,content->literal_data_body.length); 
     53        break; 
     54 
     55    case OPS_PARSER_PTAG: 
     56    case OPS_PTAG_CT_LITERAL_DATA_HEADER: 
     57        // ignore 
     58        break; 
     59 
     60    default: 
     61        fprintf(stderr,"Unexpected packet tag=%d (0x%x)\n",content_->tag, 
     62                content_->tag); 
     63        assert(0); 
     64        } 
     65 
    4666    return OPS_RELEASE_MEMORY; 
    4767    } 
     
    5676    }; 
    5777 
     78static void init_for_memory_write(ops_create_info_t **cinfo, ops_memory_t **mem) 
     79    { 
     80    /* 
     81     * initialise needed structures for writing 
     82     */ 
     83 
     84    *cinfo=ops_create_info_new(); 
     85    *mem=ops_memory_new(); 
     86 
     87    ops_memory_init(*mem,MAXBUF); 
     88 
     89    ops_writer_set_memory(*cinfo,*mem); 
     90    } 
     91 
     92static void init_for_memory_read(ops_parse_info_t **pinfo, ops_memory_t *mem, 
     93                                 ops_parse_cb_return_t callback(const ops_parser_content_t *, ops_parse_cb_info_t *)) 
     94    { 
     95    /* 
     96     * initialise needed structures for reading 
     97     */ 
     98 
     99    *pinfo=ops_parse_info_new(); 
     100    ops_parse_cb_set(*pinfo,callback,NULL); 
     101    ops_reader_set_memory(*pinfo,mem->buf,mem->length); 
     102    } 
     103 
     104 
    58105static void test_literal_data_packet_text() 
    59106    { 
    60     char *in=ops_mallocz(MAXBUF); 
    61107    ops_create_info_t *cinfo; 
    62108    ops_parse_info_t *pinfo; 
    63109    ops_memory_t *mem; 
     110 
     111    char *in=ops_mallocz(MAXBUF); 
    64112    int rtn=0; 
    65113 
    66114    // create test string 
    67     create_testtext("literal data packet", &in[0], MAXBUF); 
    68  
    69     /* 
    70      * initialise needed structures for writing 
    71      */ 
    72  
    73     cinfo=ops_create_info_new(); 
    74     mem=ops_memory_new(); 
    75     ops_memory_init(mem,MAXBUF); 
    76     ops_writer_set_memory(cinfo,mem); 
     115    create_testtext("literal data packet text", &in[0], MAXBUF); 
     116 
     117    /* 
     118     * initialise needed structures for writing into memory 
     119     */ 
     120 
     121    init_for_memory_write(&cinfo,&mem); 
    77122 
    78123    /* 
     
    82127 
    83128    /* 
    84      * initialise needed structures for writing 
    85      */ 
    86  
    87     pinfo=ops_parse_info_new(); 
    88     ops_parse_cb_set(pinfo,callback,NULL); 
    89     ops_reader_set_memory(pinfo,mem->buf,mem->length); 
     129     * initialise needed structures for reading from memory 
     130     */ 
     131 
     132    init_for_memory_read(&pinfo,mem,callback); 
    90133 
    91134    // and parse it 
     
    98141     */ 
    99142 
    100     // \todo write a callback to read the literal data into buffer 
     143    CU_ASSERT(strncmp((char *)data,in,MAXBUF)==0); 
    101144 
    102145    // cleanup 
     
    105148    } 
    106149 
     150static void test_literal_data_packet_data() 
     151    { 
     152    ops_create_info_t *cinfo; 
     153    ops_parse_info_t *pinfo; 
     154    ops_memory_t *mem; 
     155 
     156    unsigned char *in=ops_mallocz(MAXBUF); 
     157    int rtn=0; 
     158 
     159    // create test data buffer 
     160    create_testdata("literal data packet data", &in[0], MAXBUF); 
     161 
     162    /* 
     163     * initialise needed structures for writing into memory 
     164     */ 
     165 
     166    init_for_memory_write(&cinfo,&mem); 
     167 
     168    /* 
     169     * create literal data packet 
     170     */ 
     171    ops_write_literal_data(in,MAXBUF,OPS_LDT_BINARY,cinfo); 
     172 
     173    /* 
     174     * initialise needed structures for reading from memory 
     175     */ 
     176 
     177    init_for_memory_read(&pinfo,mem,callback); 
     178 
     179    // and parse it 
     180 
     181    ops_parse_options(pinfo,OPS_PTAG_SS_ALL,OPS_PARSE_PARSED); 
     182    rtn=ops_parse(pinfo); 
     183 
     184    /* 
     185     * test it's the same 
     186     */ 
     187 
     188    CU_ASSERT(memcmp(data,in,MAXBUF)==0); 
     189 
     190    // cleanup 
     191    ops_memory_free(mem); 
     192    free (in); 
     193    } 
     194 
    107195CU_pSuite suite_packet_types() 
    108196{ 
     
    115203    // add tests to suite 
    116204     
    117     if (NULL == CU_add_test(suite, "Literal Data Text packet", test_literal_data_packet_text)) 
     205    if (NULL == CU_add_test(suite, "Literal Data (Text) packet (Tag 11)", test_literal_data_packet_text)) 
     206            return NULL; 
     207     
     208    if (NULL == CU_add_test(suite, "Literal Data (Data) packet (Tag 11)", test_literal_data_packet_data)) 
    118209            return NULL; 
    119210     
  • openpgpsdk/trunk/tests/tests.c

    r473 r475  
    8383    } 
    8484 
     85void create_testdata(const char *text, unsigned char *buf, const int maxlen) 
     86    { 
     87    char *preamble=" : Test Data :"; 
     88    int i=0; 
    8589 
     90    snprintf((char *)buf,maxlen,"%s%s", text, preamble); 
     91 
     92    for (i=strlen(text)+strlen(preamble); i<maxlen; i++) 
     93        { 
     94        buf[i]=(random() & 0xFF); 
     95        } 
     96    } 
     97 
     98 
  • openpgpsdk/trunk/tests/tests.h

    r473 r475  
    1414extern char dir[]; 
    1515void create_testtext(const char *text, char *buf, const int maxlen); 
     16void create_testdata(const char *text, unsigned char *buf, const int maxlen); 
    1617#define MAXBUF 128 
    1718