Test charset conversion with fallbacks
[platform/upstream/glib.git] / gio / tests / converter-stream.c
1 /* GLib testing framework examples and tests
2  * Copyright (C) 2009 Red Hat, Inc.
3  * Authors: Alexander Larsson <alexl@redhat.com>
4  *
5  * This work is provided "as is"; redistribution and modification
6  * in whole or in part, in any medium, physical or electronic is
7  * permitted without restriction.
8  *
9  * This work is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * In no event shall the authors or contributors be liable for any
14  * direct, indirect, incidental, special, exemplary, or consequential
15  * damages (including, but not limited to, procurement of substitute
16  * goods or services; loss of use, data, or profits; or business
17  * interruption) however caused and on any theory of liability, whether
18  * in contract, strict liability, or tort (including negligence or
19  * otherwise) arising in any way out of the use of this software, even
20  * if advised of the possibility of such damage.
21  */
22
23 #include <glib/glib.h>
24 #include <gio/gio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #define G_TYPE_EXPANDER_CONVERTER         (g_expander_converter_get_type ())
29 #define G_EXPANDER_CONVERTER(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_EXPANDER_CONVERTER, GExpanderConverter))
30 #define G_EXPANDER_CONVERTER_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST((k), G_TYPE_EXPANDER_CONVERTER, GExpanderConverterClass))
31 #define G_IS_EXPANDER_CONVERTER(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_EXPANDER_CONVERTER))
32 #define G_IS_EXPANDER_CONVERTER_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), G_TYPE_EXPANDER_CONVERTER))
33 #define G_EXPANDER_CONVERTER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), G_TYPE_EXPANDER_CONVERTER, GExpanderConverterClass))
34
35 typedef struct _GExpanderConverter       GExpanderConverter;
36 typedef struct _GExpanderConverterClass  GExpanderConverterClass;
37
38 struct _GExpanderConverterClass
39 {
40   GObjectClass parent_class;
41 };
42
43 GType       g_expander_converter_get_type (void) G_GNUC_CONST;
44 GConverter *g_expander_converter_new      (void);
45
46
47
48 static void g_expander_converter_iface_init          (GConverterIface *iface);
49
50 struct _GExpanderConverter
51 {
52   GObject parent_instance;
53 };
54
55 G_DEFINE_TYPE_WITH_CODE (GExpanderConverter, g_expander_converter, G_TYPE_OBJECT,
56                          G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
57                                                 g_expander_converter_iface_init))
58
59 static void
60 g_expander_converter_class_init (GExpanderConverterClass *klass)
61 {
62 }
63
64 static void
65 g_expander_converter_init (GExpanderConverter *local)
66 {
67 }
68
69 GConverter *
70 g_expander_converter_new (void)
71 {
72   GConverter *conv;
73
74   conv = g_object_new (G_TYPE_EXPANDER_CONVERTER, NULL);
75
76   return conv;
77 }
78
79 static void
80 g_expander_converter_reset (GConverter *converter)
81 {
82 }
83
84 static GConverterResult
85 g_expander_converter_convert (GConverter *converter,
86                               const void *inbuf,
87                               gsize       inbuf_size,
88                               void       *outbuf,
89                               gsize       outbuf_size,
90                               GConverterFlags flags,
91                               gsize      *bytes_read,
92                               gsize      *bytes_written,
93                               GError    **error)
94 {
95   GExpanderConverter  *conv;
96   const guint8 *in, *in_end;
97   guint8 v, *out;
98   int i;
99   gsize block_size;
100
101   conv = G_EXPANDER_CONVERTER (converter);
102
103   in = inbuf;
104   out = outbuf;
105   in_end = in + inbuf_size;
106
107   while (in < in_end)
108     {
109       v = *in;
110
111       if (v == 0)
112         block_size = 10;
113       else
114         block_size = v * 1000;
115
116       if (outbuf_size < block_size)
117         {
118           if (*bytes_read > 0)
119             return G_CONVERTER_CONVERTED;
120
121           g_set_error_literal (error, G_IO_ERROR,
122                                G_IO_ERROR_NO_SPACE,
123                                "No space in dest");
124           return G_CONVERTER_ERROR;
125         }
126
127       in++;
128       *bytes_read += 1;
129       *bytes_written += block_size;
130       outbuf_size -= block_size;
131       for (i = 0; i < block_size; i++)
132         *out++ = v;
133     }
134
135   if (in == in_end && (flags & G_CONVERTER_INPUT_AT_END))
136     return G_CONVERTER_FINISHED;
137   return G_CONVERTER_CONVERTED;
138 }
139
140 static void
141 g_expander_converter_iface_init (GConverterIface *iface)
142 {
143   iface->convert = g_expander_converter_convert;
144   iface->reset = g_expander_converter_reset;
145 }
146
147 #define G_TYPE_COMPRESSOR_CONVERTER         (g_compressor_converter_get_type ())
148 #define G_COMPRESSOR_CONVERTER(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_COMPRESSOR_CONVERTER, GCompressorConverter))
149 #define G_COMPRESSOR_CONVERTER_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST((k), G_TYPE_COMPRESSOR_CONVERTER, GCompressorConverterClass))
150 #define G_IS_COMPRESSOR_CONVERTER(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_COMPRESSOR_CONVERTER))
151 #define G_IS_COMPRESSOR_CONVERTER_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), G_TYPE_COMPRESSOR_CONVERTER))
152 #define G_COMPRESSOR_CONVERTER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), G_TYPE_COMPRESSOR_CONVERTER, GCompressorConverterClass))
153
154 typedef struct _GCompressorConverter       GCompressorConverter;
155 typedef struct _GCompressorConverterClass  GCompressorConverterClass;
156
157 struct _GCompressorConverterClass
158 {
159   GObjectClass parent_class;
160 };
161
162 GType       g_compressor_converter_get_type (void) G_GNUC_CONST;
163 GConverter *g_compressor_converter_new      (void);
164
165
166
167 static void g_compressor_converter_iface_init          (GConverterIface *iface);
168
169 struct _GCompressorConverter
170 {
171   GObject parent_instance;
172 };
173
174 G_DEFINE_TYPE_WITH_CODE (GCompressorConverter, g_compressor_converter, G_TYPE_OBJECT,
175                          G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
176                                                 g_compressor_converter_iface_init))
177
178 static void
179 g_compressor_converter_class_init (GCompressorConverterClass *klass)
180 {
181 }
182
183 static void
184 g_compressor_converter_init (GCompressorConverter *local)
185 {
186 }
187
188 GConverter *
189 g_compressor_converter_new (void)
190 {
191   GConverter *conv;
192
193   conv = g_object_new (G_TYPE_COMPRESSOR_CONVERTER, NULL);
194
195   return conv;
196 }
197
198 static void
199 g_compressor_converter_reset (GConverter *converter)
200 {
201 }
202
203 static GConverterResult
204 g_compressor_converter_convert (GConverter *converter,
205                                 const void *inbuf,
206                                 gsize       inbuf_size,
207                                 void       *outbuf,
208                                 gsize       outbuf_size,
209                                 GConverterFlags flags,
210                                 gsize      *bytes_read,
211                                 gsize      *bytes_written,
212                                 GError    **error)
213 {
214   GCompressorConverter  *conv;
215   const guint8 *in, *in_end;
216   guint8 v, *out;
217   int i;
218   gsize block_size;
219
220   conv = G_COMPRESSOR_CONVERTER (converter);
221
222   in = inbuf;
223   out = outbuf;
224   in_end = in + inbuf_size;
225
226   while (in < in_end)
227     {
228       v = *in;
229
230       if (v == 0)
231         {
232           block_size = 0;
233           while (in+block_size < in_end && *(in+block_size) == 0)
234             block_size ++;
235         }
236       else
237         block_size = v * 1000;
238
239       /* Not enough data */
240       if (in_end - in < block_size)
241         {
242           if (*bytes_read > 0)
243             break;
244           g_set_error_literal (error, G_IO_ERROR,
245                                G_IO_ERROR_PARTIAL_INPUT,
246                                "Need more data");
247           return G_CONVERTER_ERROR;
248         }
249
250       for (i = 0; i < block_size; i++)
251         {
252           if (*(in + i) != v)
253             {
254               if (*bytes_read > 0)
255                 break;
256               g_set_error_literal (error, G_IO_ERROR,
257                                    G_IO_ERROR_INVALID_DATA,
258                                    "invalid data");
259               return G_CONVERTER_ERROR;
260             }
261         }
262
263       if (v == 0 && in_end - in == block_size && (flags & G_CONVERTER_INPUT_AT_END) == 0)
264         {
265           if (*bytes_read > 0)
266             break;
267           g_set_error_literal (error, G_IO_ERROR,
268                                G_IO_ERROR_PARTIAL_INPUT,
269                                "Need more data");
270           return G_CONVERTER_ERROR;
271         }
272
273       in += block_size;
274       *out++ = v;
275       *bytes_read += block_size;
276       *bytes_written += 1;
277     }
278
279   if (in == in_end && (flags & G_CONVERTER_INPUT_AT_END))
280     return G_CONVERTER_FINISHED;
281   return G_CONVERTER_CONVERTED;
282 }
283
284 static void
285 g_compressor_converter_iface_init (GConverterIface *iface)
286 {
287   iface->convert = g_compressor_converter_convert;
288   iface->reset = g_compressor_converter_reset;
289 }
290
291 guint8 unexpanded_data[] = { 0,1,3,4,5,6,7,3,12,0,0};
292
293 static void
294 test_expander (void)
295 {
296   guint8 *converted1, *converted2, *ptr;
297   gsize n_read, n_written;
298   gsize total_read;
299   gssize res;
300   GConverterResult cres;
301   GInputStream *mem, *cstream;
302   GOutputStream *mem_out, *cstream_out;
303   GConverter *expander;
304   GError *error;
305   int i;
306
307   expander = g_expander_converter_new ();
308
309   converted1 = g_malloc (100*1000); /* Large enough */
310   converted2 = g_malloc (100*1000); /* Large enough */
311
312   cres = g_converter_convert (expander,
313                               unexpanded_data, sizeof(unexpanded_data),
314                               converted1, 100*1000,
315                               G_CONVERTER_INPUT_AT_END,
316                               &n_read, &n_written, NULL);
317
318   g_assert (cres == G_CONVERTER_FINISHED);
319   g_assert (n_read == 11);
320   g_assert (n_written == 41030);
321
322   g_converter_reset (expander);
323
324   mem = g_memory_input_stream_new_from_data (unexpanded_data,
325                                              sizeof (unexpanded_data),
326                                              NULL);
327   cstream = g_converter_input_stream_new (mem, expander);
328   g_assert (g_converter_input_stream_get_converter (G_CONVERTER_INPUT_STREAM (cstream)) == expander);
329   g_object_unref (mem);
330
331   total_read = 0;
332   ptr = converted2;
333   while (TRUE)
334     {
335       error = NULL;
336       res = g_input_stream_read (cstream,
337                                  ptr, 1,
338                                  NULL, &error);
339       g_assert (res != -1);
340       if (res == 0)
341         break;
342       ptr += res;
343       total_read += res;
344     }
345
346   g_assert (total_read == n_written);
347   g_assert (memcmp (converted1, converted2, n_written)  == 0);
348
349   g_converter_reset (expander);
350
351   mem_out = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
352   cstream_out = g_converter_output_stream_new (mem_out, expander);
353   g_object_unref (mem_out);
354
355   for (i = 0; i < sizeof(unexpanded_data); i++)
356     {
357       error = NULL;
358       res = g_output_stream_write (cstream_out,
359                                    unexpanded_data + i, 1,
360                                    NULL, &error);
361       g_assert (res != -1);
362       if (res == 0)
363         {
364           g_assert (i == sizeof(unexpanded_data) -1);
365           break;
366         }
367       g_assert (res == 1);
368     }
369
370   g_output_stream_close (cstream_out, NULL, NULL);
371
372   g_assert (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mem_out)) == n_written);
373   g_assert (memcmp (g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mem_out)),
374                     converted1,
375                     n_written)  == 0);
376
377   g_free (converted1);
378   g_free (converted2);
379   g_object_unref (cstream);
380   g_object_unref (cstream_out);
381   g_object_unref (expander);
382 }
383
384 static void
385 test_compressor (void)
386 {
387   guint8 *converted, *expanded, *ptr;
388   gsize n_read, expanded_size;
389   gsize total_read;
390   gssize res;
391   GConverterResult cres;
392   GInputStream *mem, *cstream;
393   GOutputStream *mem_out, *cstream_out;
394   GConverter *expander, *compressor;
395   GError *error;
396   int i;
397
398   expander = g_expander_converter_new ();
399   expanded = g_malloc (100*1000); /* Large enough */
400   cres = g_converter_convert (expander,
401                               unexpanded_data, sizeof(unexpanded_data),
402                               expanded, 100*1000,
403                               G_CONVERTER_INPUT_AT_END,
404                               &n_read, &expanded_size, NULL);
405   g_assert (cres == G_CONVERTER_FINISHED);
406   g_assert (n_read == 11);
407   g_assert (expanded_size == 41030);
408
409   compressor = g_compressor_converter_new ();
410
411   converted = g_malloc (100*1000); /* Large enough */
412
413   mem = g_memory_input_stream_new_from_data (expanded,
414                                              expanded_size,
415                                              NULL);
416   cstream = g_converter_input_stream_new (mem, compressor);
417   g_object_unref (mem);
418
419   total_read = 0;
420   ptr = converted;
421   while (TRUE)
422     {
423       error = NULL;
424       res = g_input_stream_read (cstream,
425                                  ptr, 1,
426                                  NULL, &error);
427       g_assert (res != -1);
428       if (res == 0)
429         break;
430       ptr += res;
431       total_read += res;
432     }
433
434   g_assert (total_read == n_read - 1); /* Last 2 zeros are combined */
435   g_assert (memcmp (converted, unexpanded_data, total_read)  == 0);
436
437   g_object_unref (cstream);
438
439   g_converter_reset (compressor);
440
441   mem_out = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
442   cstream_out = g_converter_output_stream_new (mem_out, compressor);
443   g_object_unref (mem_out);
444
445   for (i = 0; i < expanded_size; i++)
446     {
447       error = NULL;
448       res = g_output_stream_write (cstream_out,
449                                    expanded + i, 1,
450                                    NULL, &error);
451       g_assert (res != -1);
452       if (res == 0)
453         {
454           g_assert (i == expanded_size -1);
455           break;
456         }
457       g_assert (res == 1);
458     }
459
460   g_output_stream_close (cstream_out, NULL, NULL);
461
462   g_assert (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mem_out)) == n_read - 1); /* Last 2 zeros are combined */
463   g_assert (memcmp (g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mem_out)),
464                     unexpanded_data,
465                     g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mem_out)))  == 0);
466
467   g_object_unref (cstream_out);
468
469   g_converter_reset (compressor);
470
471   memset (expanded, 5, 5*1000*2);
472
473   mem = g_memory_input_stream_new_from_data (expanded,
474                                              5*1000,
475                                              NULL);
476   cstream = g_converter_input_stream_new (mem, compressor);
477   g_object_unref (mem);
478
479   total_read = 0;
480   ptr = converted;
481   while (TRUE)
482     {
483       error = NULL;
484       res = g_input_stream_read (cstream,
485                                  ptr, 1,
486                                  NULL, &error);
487       g_assert (res != -1);
488       if (res == 0)
489         break;
490       ptr += res;
491       total_read += res;
492     }
493
494   g_assert (total_read == 1);
495   g_assert (*converted == 5);
496
497   mem = g_memory_input_stream_new_from_data (expanded,
498                                              5*1000 * 2,
499                                              NULL);
500   cstream = g_converter_input_stream_new (mem, compressor);
501   g_object_unref (mem);
502
503   total_read = 0;
504   ptr = converted;
505   while (TRUE)
506     {
507       error = NULL;
508       res = g_input_stream_read (cstream,
509                                  ptr, 1,
510                                  NULL, &error);
511       g_assert (res != -1);
512       if (res == 0)
513         break;
514       ptr += res;
515       total_read += res;
516     }
517
518   g_assert (total_read == 2);
519   g_assert (converted[0] == 5);
520   g_assert (converted[1] == 5);
521
522   g_object_unref (cstream);
523
524   g_converter_reset (compressor);
525
526   mem = g_memory_input_stream_new_from_data (expanded,
527                                              5*1000 * 2 - 1,
528                                              NULL);
529   cstream = g_converter_input_stream_new (mem, compressor);
530   g_object_unref (mem);
531
532   total_read = 0;
533   ptr = converted;
534   while (TRUE)
535     {
536       error = NULL;
537       res = g_input_stream_read (cstream,
538                                  ptr, 1,
539                                  NULL, &error);
540       if (res == -1)
541         {
542           g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT);
543           break;
544         }
545
546       g_assert (res != 0);
547       ptr += res;
548       total_read += res;
549     }
550
551   g_assert (total_read == 1);
552   g_assert (converted[0] == 5);
553
554   g_object_unref (cstream);
555
556   g_free (expanded);
557   g_free (converted);
558   g_object_unref (expander);
559   g_object_unref (compressor);
560 }
561
562 #define DATA_LENGTH 1000000
563
564 static void
565 test_corruption (GZlibCompressorFormat format, gint level)
566 {
567   GError *error = NULL;
568   guint32 *data0, *data1;
569   gsize data1_size;
570   gint i;
571   GInputStream *istream0, *istream1, *cistream1;
572   GOutputStream *ostream1, *ostream2, *costream1;
573   GConverter *compressor, *decompressor;
574
575   data0 = g_malloc (DATA_LENGTH * sizeof (guint32));
576   for (i = 0; i < DATA_LENGTH; i++)
577     data0[i] = g_random_int ();
578
579   istream0 = g_memory_input_stream_new_from_data (data0,
580     DATA_LENGTH * sizeof (guint32), NULL);
581
582   ostream1 = g_memory_output_stream_new (NULL, 0, g_realloc, NULL);
583   compressor = G_CONVERTER (g_zlib_compressor_new (format, level));
584   costream1 = g_converter_output_stream_new (ostream1, compressor);
585   g_assert (g_converter_output_stream_get_converter (G_CONVERTER_OUTPUT_STREAM (costream1)) == compressor);
586
587   g_output_stream_splice (costream1, istream0, 0, NULL, &error);
588   g_assert_no_error (error);
589
590   g_object_unref (costream1);
591   g_object_unref (compressor);
592   data1 = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (ostream1));
593   data1_size = g_memory_output_stream_get_data_size (
594     G_MEMORY_OUTPUT_STREAM (ostream1));
595   g_object_unref (ostream1);
596   g_object_unref (istream0);
597
598   istream1 = g_memory_input_stream_new_from_data (data1, data1_size, NULL);
599   decompressor = G_CONVERTER (g_zlib_decompressor_new (format));
600   cistream1 = g_converter_input_stream_new (istream1, decompressor);
601
602   ostream2 = g_memory_output_stream_new (NULL, 0, g_realloc, NULL);
603
604   g_output_stream_splice (ostream2, cistream1, 0, NULL, &error);
605   g_assert_no_error (error);
606
607   g_assert_cmpuint (DATA_LENGTH * sizeof (guint32), ==,
608     g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (ostream2)));
609   g_assert (memcmp (data0, g_memory_output_stream_get_data (
610     G_MEMORY_OUTPUT_STREAM (ostream2)), DATA_LENGTH * sizeof (guint32)) == 0);
611 }
612
613 typedef struct {
614   const gchar *path;
615   GZlibCompressorFormat format;
616   gint level;
617 } CompressorTest;
618
619 static void
620 test_roundtrip (gconstpointer data)
621 {
622   const CompressorTest *test = data;
623
624   test_corruption (test->format, test->level);
625 }
626
627 typedef struct {
628   const gchar *path;
629   const gchar *charset_in;
630   const gchar *text_in;
631   const gchar *charset_out;
632   const gchar *text_out;
633   gint n_fallbacks;
634 } CharsetTest;
635
636 static void
637 test_charset (gconstpointer data)
638 {
639   const CharsetTest *test = data;
640   GInputStream *in, *in2;
641   GConverter *conv;
642   gchar *buffer;
643   gsize count;
644   gsize bytes_read;
645   GError *error;
646   gboolean fallback;
647
648   conv = (GConverter *)g_charset_converter_new (test->charset_out, test->charset_in, NULL);
649   g_object_get (conv, "use-fallback", &fallback, NULL);
650   g_assert (!fallback);
651
652   in = g_memory_input_stream_new_from_data (test->text_in, -1, NULL);
653   in2 = g_converter_input_stream_new (in, conv);
654
655   count = 2 * strlen (test->text_out);
656   buffer = g_malloc (count);
657   error = NULL;
658   g_input_stream_read_all (in2, buffer, count, &bytes_read, NULL, &error);
659   if (test->n_fallbacks == 0)
660     {
661       g_assert_no_error (error);
662       g_assert_cmpint (bytes_read, ==, strlen (test->text_out));
663       g_assert_cmpstr (buffer, ==, test->text_out);
664     }
665   else
666     g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA);
667
668   g_free (buffer);
669   g_object_unref (in2);
670   g_object_unref (in);
671
672   if (test->n_fallbacks == 0)
673     return;
674
675   g_converter_reset (conv);
676
677   g_assert (!g_charset_converter_get_use_fallback (G_CHARSET_CONVERTER (conv)));
678   g_charset_converter_set_use_fallback (G_CHARSET_CONVERTER (conv), TRUE);
679
680   in = g_memory_input_stream_new_from_data (test->text_in, -1, NULL);
681   in2 = g_converter_input_stream_new (in, conv);
682
683   count = 2 * strlen (test->text_out);
684   buffer = g_malloc (count);
685   error = NULL;
686   g_input_stream_read_all (in2, buffer, count, &bytes_read, NULL, &error);
687   g_assert_no_error (error);
688   g_assert_cmpstr (buffer, ==, test->text_out);
689   g_assert_cmpint (bytes_read, ==, strlen (test->text_out));
690   g_assert_cmpint (test->n_fallbacks, ==, g_charset_converter_get_num_fallbacks (G_CHARSET_CONVERTER (conv)));
691
692   g_free (buffer);
693   g_object_unref (in2);
694   g_object_unref (in);
695
696   g_object_unref (conv);
697 }
698
699 int
700 main (int   argc,
701       char *argv[])
702 {
703   CompressorTest compressor_tests[] = {
704     { "/converter-output-stream/corruption/zlib-0", G_ZLIB_COMPRESSOR_FORMAT_ZLIB, 0 },
705     { "/converter-output-stream/corruption/zlib-9", G_ZLIB_COMPRESSOR_FORMAT_ZLIB, 9 },
706     { "/converter-output-stream/corruption/gzip-0", G_ZLIB_COMPRESSOR_FORMAT_GZIP, 0 },
707     { "/converter-output-stream/corruption/gzip-9", G_ZLIB_COMPRESSOR_FORMAT_GZIP, 9 },
708     { "/converter-output-stream/corruption/raw-0", G_ZLIB_COMPRESSOR_FORMAT_RAW, 0 },
709     { "/converter-output-stream/corruption/raw-9", G_ZLIB_COMPRESSOR_FORMAT_RAW, 9 },
710   };
711   CharsetTest charset_tests[] = {
712     { "/converter-input-stream/charset/utf8->latin1", "UTF-8", "\303\205rr Sant\303\251", "ISO-8859-1", "\305rr Sant\351", 0 },
713     { "/converter-input-stream/charset/latin1->utf8", "ISO-8859-1", "\305rr Sant\351", "UTF-8", "\303\205rr Sant\303\251", 0 },
714     { "/converter-input-stream/charset/fallbacks", "UTF-8", "Some characters just don't fit into latin1: πא", "ISO-8859-1", "Some characters just don't fit into latin1: \\CF\\80\\D7\\90", 4 },
715
716   };
717
718   gint i;
719
720   g_type_init ();
721   g_test_init (&argc, &argv, NULL);
722
723   g_test_add_func ("/converter-input-stream/expander", test_expander);
724   g_test_add_func ("/converter-input-stream/compressor", test_compressor);
725
726   for (i = 0; i < G_N_ELEMENTS (compressor_tests); i++)
727     g_test_add_data_func (compressor_tests[i].path, &compressor_tests[i], test_roundtrip);
728
729   for (i = 0; i < G_N_ELEMENTS (charset_tests); i++)
730     g_test_add_data_func (charset_tests[i].path, &charset_tests[i], test_charset);
731
732
733   return g_test_run();
734 }