|
1 /* |
|
2 * Portions copyright (c) 2009 Nokia Corporation. All rights reserved. |
|
3 */ |
|
4 #include "config.h" |
|
5 |
|
6 #include <glib.h> |
|
7 #include <string.h> |
|
8 #ifdef HAVE_UNISTD_H |
|
9 #include <unistd.h> |
|
10 #endif |
|
11 #include <stdlib.h> |
|
12 #ifdef __SYMBIAN32__ |
|
13 #include "mrt2_glib2_test.h" |
|
14 #endif /*__SYMBIAN32__*/ |
|
15 |
|
16 #define DATA_SIZE 1024 |
|
17 #define BLOCK_SIZE 32 |
|
18 #define NUM_BLOCKS 32 |
|
19 static guchar data[DATA_SIZE]; |
|
20 |
|
21 static void |
|
22 test_incremental (gboolean line_break, |
|
23 gint length) |
|
24 { |
|
25 char *p; |
|
26 gsize len, decoded_len, max, input_len, block_size; |
|
27 int state, save; |
|
28 guint decoder_save; |
|
29 char *text; |
|
30 guchar *data2; |
|
31 |
|
32 data2 = g_malloc (length); |
|
33 text = g_malloc (length * 4); |
|
34 |
|
35 len = 0; |
|
36 state = 0; |
|
37 save = 0; |
|
38 input_len = 0; |
|
39 while (input_len < length) |
|
40 { |
|
41 block_size = MIN (BLOCK_SIZE, length - input_len); |
|
42 len += g_base64_encode_step (data + input_len, block_size, |
|
43 line_break, text + len, &state, &save); |
|
44 input_len += block_size; |
|
45 } |
|
46 len += g_base64_encode_close (line_break, text + len, &state, &save); |
|
47 |
|
48 if (line_break) |
|
49 max = length * 4 / 3 + length * 4 / (3 * 72) + 7; |
|
50 else |
|
51 max = length * 4 / 3 + 6; |
|
52 if (len > max) |
|
53 { |
|
54 g_print ("Too long encoded length: got %d, expected max %d\n", |
|
55 len, max); |
|
56 exit (1); |
|
57 } |
|
58 |
|
59 decoded_len = 0; |
|
60 state = 0; |
|
61 decoder_save = 0; |
|
62 p = text; |
|
63 while (len > 0) |
|
64 { |
|
65 int chunk_len = MIN (BLOCK_SIZE, len); |
|
66 decoded_len += g_base64_decode_step (p, |
|
67 chunk_len, |
|
68 data2 + decoded_len, |
|
69 &state, &decoder_save); |
|
70 p += chunk_len; |
|
71 len -= chunk_len; |
|
72 } |
|
73 |
|
74 if (decoded_len != length) |
|
75 { |
|
76 g_print ("Wrong decoded length: got %d, expected %d\n", |
|
77 decoded_len, length); |
|
78 exit (1); |
|
79 } |
|
80 |
|
81 if (memcmp (data, data2, length) != 0) |
|
82 { |
|
83 g_print ("Wrong decoded base64 data\n"); |
|
84 exit (1); |
|
85 } |
|
86 |
|
87 g_free (text); |
|
88 g_free (data2); |
|
89 } |
|
90 |
|
91 static void |
|
92 test_full (gint length) |
|
93 { |
|
94 char *text; |
|
95 guchar *data2; |
|
96 gsize len; |
|
97 |
|
98 text = g_base64_encode (data, length); |
|
99 data2 = g_base64_decode (text, &len); |
|
100 g_free (text); |
|
101 |
|
102 if (len != length) |
|
103 { |
|
104 g_print ("Wrong decoded length: got %d, expected %d\n", |
|
105 len, length); |
|
106 exit (1); |
|
107 } |
|
108 |
|
109 if (memcmp (data, data2, length) != 0) |
|
110 { |
|
111 g_print ("Wrong decoded base64 data\n"); |
|
112 exit (1); |
|
113 } |
|
114 |
|
115 g_free (data2); |
|
116 } |
|
117 |
|
118 int |
|
119 main (int argc, char *argv[]) |
|
120 { |
|
121 int i; |
|
122 |
|
123 #ifdef __SYMBIAN32__ |
|
124 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL); |
|
125 g_set_print_handler(mrtPrintHandler); |
|
126 #endif /*__SYMBIAN32__*/ |
|
127 |
|
128 for (i = 0; i < DATA_SIZE; i++) |
|
129 data[i] = (guchar)i; |
|
130 |
|
131 test_full (DATA_SIZE); |
|
132 test_full (1); |
|
133 test_full (2); |
|
134 test_full (3); |
|
135 |
|
136 test_incremental (FALSE, DATA_SIZE); |
|
137 test_incremental (TRUE, DATA_SIZE); |
|
138 |
|
139 test_incremental (FALSE, DATA_SIZE - 1); |
|
140 test_incremental (TRUE, DATA_SIZE - 1); |
|
141 |
|
142 test_incremental (FALSE, DATA_SIZE - 2); |
|
143 test_incremental (TRUE, DATA_SIZE - 2); |
|
144 |
|
145 test_incremental (FALSE, 1); |
|
146 test_incremental (FALSE, 2); |
|
147 test_incremental (FALSE, 3); |
|
148 #ifdef __SYMBIAN32__ |
|
149 testResultXml("base64-test"); |
|
150 #endif /* EMULATOR */ |
|
151 |
|
152 return 0; |
|
153 } |