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