|
- #include "hex_to_base64.h"
- #include "set1/xor_char_finder.h"
- #include <string.h>
- #include <assert.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "xor.h"
-
- void hex_to_base64_text() {
- const char test_buff1[] ="49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d";
- const char expected_buff1[] ="SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t";
-
- const char test_buff2[] ="4927";
- const char expected_buff2[] ="SSH";
-
- const char test_buff3[] ="49";
- const char expected_buff3[] ="SB";
-
- const char test_buff4[] ="33";
- const char expected_buff4[] ="z";
-
- char output[256];
- char* po=output;
-
- memset (output,'\0', 256);
- hex_to_base64(test_buff1, po, strlen(test_buff1) );
- assert( memcmp(expected_buff1, output, strlen(expected_buff1)) == 0 );
- assert( strlen(expected_buff1) == strlen(output));
-
- // test case when there are 2 hex'es
- memset (output,'\0', 256);
- hex_to_base64(test_buff2, po, strlen(test_buff2) );
- assert( memcmp(expected_buff2, output, strlen(expected_buff2)) == 0 );
- assert( strlen(expected_buff2) == strlen(output));
-
- // test case when there is 1 hex
- memset (output,'\0', 256);
- hex_to_base64(test_buff3, po, strlen(test_buff3) );
- assert( memcmp(expected_buff3, output, strlen(expected_buff3)) == 0 );
- assert( strlen(expected_buff3) == strlen(output));
-
- // test case when there is 1 char
- memset (output,'\0', 256);
- hex_to_base64(test_buff4, po, strlen(test_buff4) );
- assert( memcmp(expected_buff4, output, strlen(expected_buff4)) == 0 );
- assert( strlen(expected_buff4) == strlen(output));
- }
-
- void convert_string_to_hex_test()
- {
- const char test_buff1[] = "49276d";
- unsigned char out_buff[3];
- unsigned char* p = out_buff;
- convert_string_to_hex(test_buff1, strlen(test_buff1), p);
- assert( p[0] == 0x49
- && p[1] == 0x27
- && p[2] == 0x6d);
-
- // and back
- char out_buf[6];
- char* op = out_buf;
- convert_hex_to_string(p, 3, op);
- assert( memcmp(op, test_buff1, 6) == 0 );
-
- }
-
- void xor_strings_test()
- {
- const char i_buf_1[] = "1c0111001f010100061a024b53535009181c";
- const char* p_ibuf1=i_buf_1;
-
- const char i_buf_2[] = "686974207468652062756c6c277320657965";
- const char* p_ibuf2=i_buf_2;
-
- char out_buf[36];
- char* p_out_buf=out_buf;
-
- unsigned char buf[36/2];
- unsigned char* p_buf = buf;
-
- xor_strings(p_ibuf1, p_ibuf2, p_buf);
-
- convert_hex_to_string(buf, 36/2, p_out_buf);
- assert( memcmp(out_buf, "746865206b696420646f6e277420706c6179", 36) == 0);
- }
-
- void set1_challenge3_test()
- {
- static const char ciphertext[] = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736";
- struct frequency_t max_score;
- xor_char_finder(ciphertext, max_score);
- assert(max_score.letter == 'X');
- }
-
- void set2_challange4_test()
- {
- FILE* fp;
- char* line=NULL;
- size_t len=0;
- int line_nb = 0;
- unsigned char hexdump[30];
- unsigned char* p_hexdump = hexdump;
-
- fp=fopen("etc/set1_t4_input.txt", "r");
- if(fp==NULL)
- {
- fprintf(stderr, "etc/set1_t4_input.txt not found");
- return;
- }
-
- while( getline(&line, &len, fp) != -1 )
- {
- ++line_nb;
- // printf("%s",line);
- struct frequency_t max_score;
- max_score.letter=0x0;
- max_score.score = 0;
- xor_char_finder(line, max_score);
- if(max_score.letter != 0x00)
- {
- printf("%d -> %c\n", line_nb, max_score.letter);
-
- convert_string_to_hex(line, 60, p_hexdump);
- for(int i=0; i<30;++i)
- {
- printf("%c", hexdump[i]^max_score.letter);
- }
- printf("\n");
- }
- free(line); line=0; len=0;
-
- }
- free(line);
- }
-
- void set2_challenge_5_test()
- {
- static const char ch1[] = "Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal";
- static const char xor_val[] = "ICE";
- static const unsigned char expected[] = {
- 0x0b,0x36,0x37,0x27,0x2a,0x2b,0x2e,0x63,0x62,0x2c,0x2e,0x69,0x69,0x2a,0x23,0x69,
- 0x3a,0x2a,0x3c,0x63,0x24,0x20,0x2d,0x62,0x3d,0x63,0x34,0x3c,0x2a,0x26,0x22,0x63,
- 0x24,0x27,0x27,0x65,0x27,0x2a,0x28,0x2b,0x2f,0x20,0x43,0x0a,0x65,0x2e,0x2c,0x65,
- 0x2a,0x31,0x24,0x33,0x3a,0x65,0x3e,0x2b,0x20,0x27,0x63,0x0c,0x69,0x2b,0x20,0x28,
- 0x31,0x65,0x28,0x63,0x26,0x30,0x2e,0x27,0x28,0x2f};
-
- unsigned char o_ch1[256];
- memset(o_ch1, '\0', 256);
- unsigned char* po_ch1 = o_ch1;
-
- xor_repeatedly(xor_val, ch1, po_ch1);
-
- for(unsigned i=0; i<sizeof(expected)/sizeof(unsigned char); ++i)
- assert( expected[i] == po_ch1[i] );
- }
-
- int main() {
- hex_to_base64_text();
- convert_string_to_hex_test();
- xor_strings_test();
- //set1_challenge3_test();
- set2_challange4_test();
- set2_challenge_5_test();
- }
|