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