forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testmod_iconv.c
200 lines (151 loc) · 4.85 KB
/
testmod_iconv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#ifdef HAVE_KDBCONFIG_H
#include "kdbconfig.h"
#endif
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include "conv.h"
#include <langinfo.h>
#include <tests_internal.h>
#define NR_KEYS 1
void test_latin1_to_utf8 (void)
{
KeySet * latin1 = 0;
KeySet * utf8 = 0;
KeySet * conf =
ksNew (2, keyNew ("user/from", KEY_VALUE, "ISO8859-1", KEY_END), keyNew ("user/to", KEY_VALUE, "UTF-8", KEY_END), KS_END);
KeySet * modules = ksNew (0, KS_END);
elektraModulesInit (modules, 0);
Key * parentKey = keyNew (0);
Plugin * plugin = elektraPluginOpen ("iconv", modules, conf, 0);
exit_if_fail (plugin != 0, "could not open plugin");
latin1 =
#include "data_latin1.c"
utf8 =
#include "data_utf8.c"
succeed_if (plugin->kdbSet (plugin, latin1, 0) == NR_KEYS, "not the correct number of keys");
compare_keyset (latin1, utf8);
ksDel (latin1);
ksDel (utf8);
latin1 =
#include "data_latin1.c"
utf8 =
#include "data_utf8.c"
succeed_if (plugin->kdbGet (plugin, utf8, parentKey) == NR_KEYS, "not the correct number of keys");
compare_keyset (utf8, latin1);
ksDel (latin1);
ksDel (utf8);
keyDel (parentKey);
elektraPluginClose (plugin, 0);
elektraModulesClose (modules, 0);
ksDel (modules);
}
void test_utf8_to_latin1 (void)
{
KeySet * latin1 = 0;
KeySet * utf8 = 0;
Key * parentKey = keyNew (0);
KeySet * conf =
ksNew (2, keyNew ("user/from", KEY_VALUE, "UTF-8", KEY_END), keyNew ("user/to", KEY_VALUE, "ISO8859-1", KEY_END), KS_END);
KeySet * modules = ksNew (0, KS_END);
elektraModulesInit (modules, 0);
Plugin * plugin = elektraPluginOpen ("iconv", modules, conf, 0);
exit_if_fail (plugin != 0, "could not open plugin");
latin1 =
#include "data_latin1.c"
utf8 =
#include "data_utf8.c"
succeed_if (plugin->kdbGet (plugin, latin1, parentKey) == NR_KEYS, "not the correct number of keys");
compare_keyset (latin1, utf8);
ksDel (latin1);
ksDel (utf8);
latin1 =
#include "data_latin1.c"
utf8 =
#include "data_utf8.c"
succeed_if (plugin->kdbSet (plugin, utf8, 0) == NR_KEYS, "not the correct number of keys");
compare_keyset (utf8, latin1);
ksDel (latin1);
ksDel (utf8);
keyDel (parentKey);
elektraPluginClose (plugin, 0);
elektraModulesClose (modules, 0);
ksDel (modules);
}
void test_utf8_needed (void)
{
printf ("Test if utf8 conversation is needed\n");
KeySet * modules = ksNew (0, KS_END);
elektraModulesInit (modules, 0);
KeySet * conf =
ksNew (2, keyNew ("user/from", KEY_VALUE, "UTF-8", KEY_END), keyNew ("user/to", KEY_VALUE, "UTF-8", KEY_END), KS_END);
Plugin * plugin = elektraPluginOpen ("iconv", modules, conf, 0);
printf ("setlocale %s\n", setlocale (LC_CTYPE, ""));
printf ("langinfo %s\n", nl_langinfo (CODESET));
warn_if_fail (kdbbNeedsUTF8Conversion (plugin) == 0, "Your default needs conversation, use utf8 to avoid that");
/*
printf ("setlocale %s\n",setlocale (LC_CTYPE, "C"));
printf ("langinfo %s\n", nl_langinfo(CODESET));
warn_if_fail (kdbbNeedsUTF8Conversion(plugin) != 0, "C needs conversation");
*/
/*
printf ("%s\n",setlocale (LC_CTYPE, "de_AT.utf8"));
printf ("%s\n", nl_langinfo(CODESET));
succeed_if (kdbbNeedsUTF8Conversion(plugin) == 0, "UTF-8 does not need conversation");
*/
elektraPluginClose (plugin, 0);
elektraModulesClose (modules, 0);
ksDel (modules);
}
static void set_str (char ** str, size_t * len, char * newstr)
{
*len = strlen (newstr) + 1;
elektraRealloc ((void **) str, *len);
strcpy (*str, newstr);
}
void test_utf8_conversation (void)
{
KeySet * modules = ksNew (0, KS_END);
elektraModulesInit (modules, 0);
KeySet * conf =
ksNew (2, keyNew ("user/from", KEY_VALUE, "UTF-8", KEY_END), keyNew ("user/to", KEY_VALUE, "UTF-8", KEY_END), KS_END);
Plugin * plugin = elektraPluginOpen ("iconv", modules, conf, 0);
char * str = elektraMalloc (KDB_MAX_PATH_LENGTH);
size_t len;
printf ("Test utf8 conversation\n");
printf ("setlocale %s\n", setlocale (LC_ALL, "C"));
set_str (&str, &len, "only ascii");
succeed_if (kdbbUTF8Engine (plugin, UTF8_FROM, &str, &len) != -1, "could not use utf8engine");
succeed_if (strcmp ("only ascii", str) == 0, "ascii conversation incorrect");
/* leads to EILSEQ, means illegal byte sequence */
set_str (&str, &len, "Ug.ly:St@ri€n.g Key");
/* succeed_if (kdbbUTF8Engine (plugin, UTF8_FROM, &str, &len) != -1, "could use utf8engine"); */
/*succeed_if (errno == EILSEQ, "errno not set correctly");*/
elektraFree (str);
elektraPluginClose (plugin, 0);
elektraModulesClose (modules, 0);
ksDel (modules);
}
int main (int argc, char ** argv)
{
printf (" ICONV TESTS\n");
printf ("====================\n\n");
init (argc, argv);
test_latin1_to_utf8 ();
test_utf8_to_latin1 ();
test_utf8_needed ();
test_utf8_conversation ();
print_result ("test_iconv");
return nbError;
}