| 1 | /* Copyright (c) 2003, 2006, 2007 MySQL AB, 2009 Sun Microsystems, Inc. |
| 2 | Use is subject to license terms. |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU General Public License as |
| 6 | published by the Free Software Foundation; version 2 of the License. |
| 7 | |
| 8 | This program is distributed in the hope that it will be useful, but |
| 9 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program; if not, write to the Free Software |
| 15 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 16 | |
| 17 | #include <my_global.h> |
| 18 | #include <my_sys.h> |
| 19 | #include <tap.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | #define BASE64_LOOP_COUNT 500 |
| 23 | #define BASE64_ROWS 4 /* Number of ok(..) */ |
| 24 | |
| 25 | int |
| 26 | main(int argc __attribute__((unused)),char *argv[]) |
| 27 | { |
| 28 | int i, cmp; |
| 29 | size_t j, k, l, dst_len, needed_length; |
| 30 | MY_INIT(argv[0]); |
| 31 | |
| 32 | plan(BASE64_LOOP_COUNT * BASE64_ROWS); |
| 33 | |
| 34 | for (i= 0; i < BASE64_LOOP_COUNT; i++) |
| 35 | { |
| 36 | /* Create source data */ |
| 37 | const size_t src_len= rand() % 1000 + 1; |
| 38 | |
| 39 | char * src= (char *) malloc(src_len); |
| 40 | char * s= src; |
| 41 | char * str; |
| 42 | char * dst; |
| 43 | |
| 44 | for (j= 0; j<src_len; j++) |
| 45 | { |
| 46 | char c= rand(); |
| 47 | *s++= c; |
| 48 | } |
| 49 | |
| 50 | /* Encode */ |
| 51 | needed_length= my_base64_needed_encoded_length((int)src_len); |
| 52 | str= (char *) malloc(needed_length); |
| 53 | for (k= 0; k < needed_length; k++) |
| 54 | str[k]= 0xff; /* Fill memory to check correct NUL termination */ |
| 55 | ok(my_base64_encode(src, src_len, str) == 0, |
| 56 | "my_base64_encode: size %d" , i); |
| 57 | ok(needed_length == strlen(str) + 1, |
| 58 | "my_base64_needed_encoded_length: size %d" , i); |
| 59 | |
| 60 | /* Decode */ |
| 61 | dst= (char *) malloc(my_base64_needed_decoded_length((int)strlen(str))); |
| 62 | dst_len= my_base64_decode(str, strlen(str), dst, NULL, 0); |
| 63 | ok(dst_len == src_len, "Comparing lengths" ); |
| 64 | |
| 65 | cmp= memcmp(src, dst, src_len); |
| 66 | ok(cmp == 0, "Comparing encode-decode result" ); |
| 67 | if (cmp != 0) |
| 68 | { |
| 69 | /* FIXME: This only prints last value of the compared strings */ |
| 70 | char buf[80]; |
| 71 | diag(" --------- src --------- --------- dst ---------" ); |
| 72 | for (k= 0; k<src_len; k+=8) |
| 73 | { |
| 74 | sprintf(buf, "%.4x " , (uint) k); |
| 75 | for (l=0; l<8 && k+l<src_len; l++) |
| 76 | { |
| 77 | unsigned char c= src[k+l]; |
| 78 | sprintf(buf, "%.2x " , (unsigned)c); |
| 79 | } |
| 80 | |
| 81 | sprintf(buf, " " ); |
| 82 | |
| 83 | for (l=0; l<8 && k+l<dst_len; l++) |
| 84 | { |
| 85 | unsigned char c= dst[k+l]; |
| 86 | sprintf(buf, "%.2x " , (unsigned)c); |
| 87 | } |
| 88 | diag("%s" , buf); |
| 89 | } |
| 90 | diag("src length: %.8x, dst length: %.8x\n" , |
| 91 | (uint) src_len, (uint) dst_len); |
| 92 | } |
| 93 | free(dst); |
| 94 | free(str); |
| 95 | free(src); |
| 96 | } |
| 97 | my_end(0); |
| 98 | return exit_status(); |
| 99 | } |
| 100 | |