Changeset 679

Show
Ignore:
Timestamp:
08/01/09 17:34:01
Author:
ben
Message:

Refactor away a lot of repeated code.

Files:

Legend:

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

    r677 r679  
    9797    } 
    9898 
    99 static void test_dsa_signature_clearsign_file(const char *filename, 
    100                                               const ops_secret_key_t *skey) 
    101     { 
    102     char cmd[MAXBUF+1]; 
    103     char myfile[MAXBUF+1]; 
    104     char signed_file[MAXBUF+1]; 
     99static void check_sig_with_ops_core(ops_parse_info_t *pinfo, 
     100                                    ops_boolean_t use_armour, 
     101                                    validate_data_cb_arg_t *validate_arg) 
     102    { 
     103    ops_validate_result_t* result=ops_mallocz(sizeof (ops_validate_result_t)); 
    105104    int rtn=0; 
    106     ops_boolean_t overwrite; 
    107  
    108     // setup filenames 
    109     snprintf(myfile, sizeof myfile, "%s/%s", dir, filename); 
    110     snprintf(signed_file, sizeof signed_file, "%s.asc", myfile); 
    111  
    112     // sign file 
    113     overwrite=ops_true; 
    114     ops_sign_file_as_cleartext(myfile, NULL, skey, overwrite); 
    115  
    116     /* 
    117      * Validate output 
    118      */ 
    119  
    120     // Check with OPS 
    121  
    122     { 
     105 
     106    memset(validate_arg, '\0', sizeof *validate_arg); 
     107    validate_arg->result=result; 
     108    validate_arg->keyring=&pub_keyring; 
     109    validate_arg->rarg=ops_reader_get_arg_from_pinfo(pinfo); 
     110     
     111    ops_parse_options(pinfo, OPS_PTAG_SS_ALL, OPS_PARSE_PARSED); 
     112    pinfo->rinfo.accumulate=ops_true; 
     113     
     114    // Set up armour/passphrase options 
     115     
     116    if (use_armour) 
     117        ops_reader_push_dearmour(pinfo); 
     118     
     119    // Do the verification 
     120     
     121    rtn=ops_parse_and_print_errors(pinfo); 
     122    CU_ASSERT(rtn==1); 
     123     
     124    // Tidy up 
     125    if (use_armour) 
     126        ops_reader_pop_dearmour(pinfo); 
     127     
     128    ops_parse_info_delete(pinfo); 
     129    ops_validate_result_free(result); 
     130    } 
     131 
     132static void check_sig_with_ops(const char *signed_file) 
     133    { 
     134    validate_data_cb_arg_t validate_arg; 
    123135    int fd=0; 
    124136    ops_parse_info_t *pinfo=NULL; 
    125     validate_data_cb_arg_t validate_arg; 
    126     ops_validate_result_t* result=ops_mallocz(sizeof (ops_validate_result_t)); 
    127     int rtn=0; 
    128137     
    129138    if (debug) 
     
    139148     
    140149    // Set verification reader and handling options 
    141      
    142150    pinfo=ops_parse_info_new(); 
    143      
    144     memset(&validate_arg, '\0', sizeof validate_arg); 
    145     validate_arg.result=result; 
    146     validate_arg.keyring=&pub_keyring; 
    147     validate_arg.rarg=ops_reader_get_arg_from_pinfo(pinfo); 
    148      
    149     ops_parse_options(pinfo, OPS_PTAG_SS_ALL, OPS_PARSE_PARSED); 
     151    ops_reader_set_fd(pinfo, fd); 
    150152    ops_parse_cb_set(pinfo, callback_verify, &validate_arg); 
    151     ops_reader_set_fd(pinfo, fd); 
    152     pinfo->rinfo.accumulate=ops_true; 
    153      
    154     // Must de-armour because it's clearsigned 
    155      
    156     ops_reader_push_dearmour(pinfo); 
    157      
    158     // Do the verification 
    159      
    160     rtn=ops_parse(pinfo); 
    161     ops_print_errors(ops_parse_info_get_errors(pinfo)); 
    162     CU_ASSERT(rtn==1); 
    163      
    164     // Tidy up 
    165     //    if (has_armour) 
    166         ops_reader_pop_dearmour(pinfo); 
    167      
    168     ops_parse_info_delete(pinfo); 
    169      
     153 
     154    check_sig_with_ops_core(pinfo, ops_false, &validate_arg); 
     155 
    170156    close(fd); 
    171     ops_validate_result_free(result); 
    172     } 
    173  
    174     // Check signature with GPG 
    175     { 
     157    } 
     158 
     159static void check_sig_with_gpg(const char *signed_file) 
     160    { 
     161    char cmd[MAXBUF+1]; 
     162    int rtn; 
    176163 
    177164    snprintf(cmd, sizeof cmd, "%s --verify %s", gpgcmd, signed_file); 
     
    179166    CU_ASSERT(rtn == 0); 
    180167    } 
     168 
     169static void check_sig(const char *signed_file) 
     170    { 
     171    check_sig_with_ops(signed_file); 
     172    check_sig_with_gpg(signed_file); 
     173    } 
     174 
     175// make sure myfile and signed_file are big enough 
     176static void set_up_file_names(char myfile[MAXBUF], char signed_file[MAXBUF], 
     177                              const char *filename, const char *ext) 
     178    { 
     179    snprintf(myfile, MAXBUF, "%s/%s", dir, filename); 
     180    snprintf(signed_file, MAXBUF, "%s.%s", myfile, ext); 
     181    } 
     182 
     183static void test_dsa_signature_clearsign_file(const char *filename, 
     184                                              const ops_secret_key_t *skey) 
     185    { 
     186    char myfile[MAXBUF]; 
     187    char signed_file[MAXBUF]; 
     188    ops_boolean_t overwrite; 
     189 
     190    set_up_file_names(myfile, signed_file, filename, "asc"); 
     191 
     192    // sign file 
     193    overwrite=ops_true; 
     194    ops_sign_file_as_cleartext(myfile, NULL, skey, overwrite); 
     195 
     196    // validate output 
     197    check_sig(signed_file); 
    181198    } 
    182199 
     
    184201                                             const ops_secret_key_t *skey) 
    185202    { 
    186     char cmd[MAXBUF+1]; 
    187     char myfile[MAXBUF+1]; 
    188     char signed_file[MAXBUF+1]; 
    189     int rtn=0; 
     203    char myfile[MAXBUF]; 
     204    char signed_file[MAXBUF]; 
    190205    ops_memory_t *input=NULL; 
    191206    ops_memory_t *output=NULL; 
     
    193208    int errnum=0; 
    194209 
    195     // setup filenames  
    196210    // (we are testing the function which signs a buf, but still want 
    197211    // to read/write the buffers from/to files for external viewing 
    198  
    199     snprintf(myfile, sizeof myfile, "%s/%s", dir, filename); 
    200     snprintf(signed_file, sizeof signed_file, "%s.asc", myfile); 
     212    set_up_file_names(myfile, signed_file, filename, "asc"); 
    201213 
    202214    // read file contents 
    203215    input=ops_write_mem_from_file(myfile, &errnum); 
    204     CU_ASSERT(errnum==0); 
     216    CU_ASSERT(errnum == 0); 
    205217 
    206218    // sign file 
     
    213225                            ops_memory_get_length(output), overwrite); 
    214226 
    215     /* 
    216      * Validate output 
    217      */ 
    218  
    219     // Check with OPS 
    220  
    221     { 
    222     int fd=0; 
    223     ops_parse_info_t *pinfo=NULL; 
    224     validate_data_cb_arg_t validate_arg; 
    225     ops_validate_result_t* result=ops_mallocz(sizeof (ops_validate_result_t)); 
    226  
    227     int rtn=0; 
    228      
    229     if (debug) 
    230         { 
    231         fprintf(stderr,"\n***\n*** Starting to parse for validation\n***\n"); 
    232         } 
    233      
    234     // open signed file 
    235     fd=open(signed_file,O_RDONLY | O_BINARY); 
    236     if(fd < 0) 
    237         { 
    238         perror(signed_file); 
    239         exit(2); 
    240         } 
    241      
    242     // Set verification reader and handling options 
    243      
    244     pinfo=ops_parse_info_new(); 
    245      
    246     memset(&validate_arg, '\0', sizeof validate_arg); 
    247     validate_arg.result=result; 
    248     validate_arg.keyring=&pub_keyring; 
    249     validate_arg.rarg=ops_reader_get_arg_from_pinfo(pinfo); 
    250      
    251     ops_parse_options(pinfo, OPS_PTAG_SS_ALL, OPS_PARSE_PARSED); 
    252     ops_parse_cb_set(pinfo, callback_verify, &validate_arg); 
    253     ops_reader_set_fd(pinfo, fd); 
    254     pinfo->rinfo.accumulate=ops_true; 
    255      
    256     // Must de-armour because it's clearsigned 
    257      
    258     ops_reader_push_dearmour(pinfo); 
    259      
    260     // Do the verification 
    261      
    262     rtn=ops_parse(pinfo); 
    263     ops_print_errors(ops_parse_info_get_errors(pinfo)); 
    264     CU_ASSERT(rtn==1); 
    265      
    266     // Tidy up 
    267     //    if (has_armour) 
    268     ops_reader_pop_dearmour(pinfo); 
    269      
    270     ops_parse_info_delete(pinfo); 
    271      
    272     close(fd); 
    273     ops_validate_result_free(result); 
    274     } 
    275  
    276     // Check signature with GPG 
    277     { 
    278     snprintf(cmd, sizeof cmd, "%s --verify %s", gpgcmd, signed_file); 
    279     rtn=run(cmd); 
    280     CU_ASSERT(rtn == 0); 
    281     } 
     227    // validate output 
     228    check_sig(signed_file); 
    282229    } 
    283230 
     
    285232                                    const ops_secret_key_t *skey) 
    286233    { 
    287     char cmd[MAXBUF+1]; 
    288     char myfile[MAXBUF+1]; 
    289     char signed_file[MAXBUF+1]; 
     234    char myfile[MAXBUF]; 
     235    char signed_file[MAXBUF]; 
    290236    char *suffix= use_armour ? "asc" : "gpg"; 
    291     int rtn=0; 
    292237    ops_boolean_t overwrite=ops_true; 
    293238 
    294     // filenames 
    295     snprintf(myfile, sizeof myfile, "%s/%s", dir, filename); 
    296     snprintf(signed_file, sizeof signed_file, "%s.%s", myfile, suffix); 
     239    set_up_file_names(myfile, signed_file, filename, suffix); 
    297240 
    298241    ops_sign_file(myfile, signed_file, skey, use_armour, overwrite); 
    299     //ops_sign_file(myfile, NULL, skey, use_armour, overwrite); 
    300  
    301     /* 
    302      * Validate output 
    303      */ 
    304  
    305     // Check with OPS 
    306  
    307     { 
    308     int fd=0; 
    309     ops_parse_info_t *pinfo=NULL; 
    310     validate_data_cb_arg_t validate_arg; 
    311     ops_validate_result_t* result=ops_mallocz(sizeof (ops_validate_result_t));; 
    312     int rtn=0; 
    313      
    314     if (debug) 
    315         fprintf(stderr, "\n***\n*** Starting to parse for validation\n***\n"); 
    316      
    317     // open signed file 
    318     fd=open(signed_file, O_RDONLY | O_BINARY); 
    319     if(fd < 0) 
    320         { 
    321         perror(signed_file); 
    322         exit(2); 
    323         } 
    324      
    325     // Set verification reader and handling options 
    326      
    327     pinfo=ops_parse_info_new(); 
    328      
    329     memset(&validate_arg, '\0', sizeof validate_arg); 
    330     validate_arg.result=result; 
    331     validate_arg.keyring=&pub_keyring; 
    332     validate_arg.rarg=ops_reader_get_arg_from_pinfo(pinfo); 
    333      
    334     ops_parse_options(pinfo, OPS_PTAG_SS_ALL, OPS_PARSE_PARSED); 
    335     ops_parse_cb_set(pinfo, callback_verify, &validate_arg); 
    336     ops_reader_set_fd(pinfo, fd); 
    337     pinfo->rinfo.accumulate=ops_true; 
    338      
    339     // Set up armour/passphrase options 
    340      
    341     if (use_armour) 
    342         ops_reader_push_dearmour(pinfo); 
    343      
    344     // Do the verification 
    345      
    346     rtn=ops_parse_and_print_errors(pinfo); 
    347     CU_ASSERT(rtn==1); 
    348      
    349     // Tidy up 
    350     if (use_armour) 
    351         ops_reader_pop_dearmour(pinfo); 
    352      
    353     ops_parse_info_delete(pinfo); 
    354      
    355     close(fd); 
    356     ops_validate_result_free(result); 
    357     } 
    358  
    359     // Check signature with GPG 
    360     { 
    361     snprintf(cmd, sizeof cmd, "%s --verify %s", gpgcmd, signed_file); 
    362     rtn=run(cmd); 
    363     CU_ASSERT(rtn == 0); 
    364     } 
     242 
     243    // validate output 
     244    check_sig(signed_file); 
    365245    } 
    366246 
     
    370250                                           const ops_secret_key_t *skey) 
    371251    { 
    372     int rtn=0; 
    373252    ops_memory_t* mem=NULL; 
    374253    ops_parse_info_t *pinfo=NULL; 
    375254    validate_data_cb_arg_t validate_arg; 
    376     ops_validate_result_t* result=ops_mallocz(sizeof (ops_validate_result_t)); 
    377      
    378  
    379     // filenames 
    380255 
    381256    mem=ops_sign_buf(input, input_len, OPS_SIG_TEXT, skey, use_armour); 
    382  
    383     /* 
    384      * Validate output 
    385      */ 
    386257 
    387258    if (debug) 
    388259        fprintf(stderr,"\n***\n*** Starting to parse for validation\n***\n"); 
    389      
     260 
     261    // validate output 
    390262    ops_write_file_from_buf("/tmp/memory.asc", ops_memory_get_data(mem), 
    391263                            ops_memory_get_length(mem),ops_true); 
    392264 
    393     // Set verification reader and handling options 
    394      
    395265    ops_setup_memory_read(&pinfo, mem, &validate_arg, callback_verify, 
    396266                          ops_true); 
    397267     
    398     memset(&validate_arg, '\0', sizeof validate_arg); 
    399     validate_arg.result=result; 
    400     validate_arg.keyring=&pub_keyring; 
    401     validate_arg.rarg=ops_reader_get_arg_from_pinfo(pinfo); 
    402      
    403     ops_parse_options(pinfo, OPS_PTAG_SS_ALL, OPS_PARSE_PARSED); 
    404     pinfo->rinfo.accumulate=ops_true; 
    405      
    406     // Set up armour/passphrase options 
    407      
    408     if (use_armour) 
    409         ops_reader_push_dearmour(pinfo); 
    410      
    411     // Do the verification 
    412      
    413     rtn=ops_parse_and_print_errors(pinfo); 
    414     CU_ASSERT(rtn==1); 
    415      
    416     // Tidy up 
    417     if (use_armour) 
    418         ops_reader_pop_dearmour(pinfo); 
    419      
    420     ops_parse_info_delete(pinfo); 
     268    check_sig_with_ops_core(pinfo, use_armour, &validate_arg); 
     269 
    421270    ops_memory_free(mem); 
    422     ops_validate_result_free(result); 
    423271    } 
    424272