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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257 | /** @file suite_csvtranscode.c
* @brief Check testsuite template
* @author François Cerbelle (Fanfan), francois@cerbelle.net
*
* @internal
* Created: 20/03/2022
* Revision: none
* Last modified: 2024-11-11 21:33
* Compiler: gcc
* Organization: Cerbelle.net
* Copyright: Copyright (c) 2024, François Cerbelle
*
* This source code is released for free distribution under the terms of the
* GNU General Public License as published by the Free Software Foundation.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <check.h>
#include "checktools.inc"
#include "csv.c"
/** Dummy test to trigger fixtures
*
* This test is defined and added to all testcases in order to execute at
* least one test test in each testcase of the testsuite, trigger the
* fixtures and have a decent coverage report.
*/
START_TEST(csvtranscode_test) {
/* This test is supposed to trigger a SIGABRT(6) and will crash the
* whole testsuite if not caught or not in a child process */
forktest_only;
abort();
}
END_TEST
int csvtranscode_txt2csv_test(const char* txt, const char* exp) {
char* src;
char* dst;
int retval;
src=strdup(txt);
dst = txt2csv(src);
retval=strcmp(dst,exp);
free(src);
free(dst);
return retval;
}
START_TEST(csvtranscode_txt2csv) {
ck_assert(0==csvtranscode_txt2csv_test("",""));
ck_assert(0==csvtranscode_txt2csv_test("ab","ab"));
ck_assert(0==csvtranscode_txt2csv_test(",ab","\",ab\""));
ck_assert(0==csvtranscode_txt2csv_test("a,b","\"a,b\""));
ck_assert(0==csvtranscode_txt2csv_test("ab,","\"ab,\""));
ck_assert(0==csvtranscode_txt2csv_test(" ab"," ab"));
ck_assert(0==csvtranscode_txt2csv_test("a b","a b"));
ck_assert(0==csvtranscode_txt2csv_test("ab ","ab "));
ck_assert(0==csvtranscode_txt2csv_test("\rab","\"\rab\""));
ck_assert(0==csvtranscode_txt2csv_test("a\rb","\"a\rb\""));
ck_assert(0==csvtranscode_txt2csv_test("ab\r","\"ab\r\""));
ck_assert(0==csvtranscode_txt2csv_test("\nab","\"\nab\""));
ck_assert(0==csvtranscode_txt2csv_test("a\nb","\"a\nb\""));
ck_assert(0==csvtranscode_txt2csv_test("ab\n","\"ab\n\""));
ck_assert(0==csvtranscode_txt2csv_test("\r\nab","\"\r\nab\""));
ck_assert(0==csvtranscode_txt2csv_test("a\r\nb","\"a\r\nb\""));
ck_assert(0==csvtranscode_txt2csv_test("ab\r\n","\"ab\r\n\""));
ck_assert(0==csvtranscode_txt2csv_test("\n\rab","\"\n\rab\""));
ck_assert(0==csvtranscode_txt2csv_test("a\n\rb","\"a\n\rb\""));
ck_assert(0==csvtranscode_txt2csv_test("ab\n\r","\"ab\n\r\""));
ck_assert(0==csvtranscode_txt2csv_test("\"ab","\"\"\"ab\""));
ck_assert(0==csvtranscode_txt2csv_test("a\"b","\"a\"\"b\""));
ck_assert(0==csvtranscode_txt2csv_test("ab\"","\"ab\"\"\""));
}
END_TEST
int csvtranscode_csvtok_test(char* csv, const char* expected) {
char* txt = csvtok(csv);
ck_assert(((NULL!=expected)&&(NULL!=txt))||
((NULL==expected)&&(NULL==txt))) ;
int retval = (NULL==txt?0:strcmp(txt,expected));<--- Null pointer dereference
free(txt);
return retval;
}
START_TEST(csvtranscode_csvtok_ok) {
char* csv = strdup(
" ,a, b,c ,\"\"\"\",\" ,a, b\",c , ,a, b,\"c ,\",\",a, b,c ,\",\"a\rb\",\"a\nb\",\"a\r\nb\",\"a\n\rb\", \r\n"
" ,a, b,c ,\"\"\"\",\" ,a, b\",c , ,a, b,\"c ,\",\",a, b,c ,\",\"a\rb\",\"a\nb\",\"a\r\nb\",\"a\n\rb\", ");
ck_assert(0==csvtranscode_csvtok_test(csv," "));
ck_assert(0==csvtranscode_csvtok_test(NULL,"a"));
ck_assert(0==csvtranscode_csvtok_test(NULL," b"));
ck_assert(0==csvtranscode_csvtok_test(NULL,"c "));
ck_assert(0==csvtranscode_csvtok_test(NULL,"\""));
ck_assert(0==csvtranscode_csvtok_test(NULL," ,a, b"));
ck_assert(0==csvtranscode_csvtok_test(NULL,"c "));
ck_assert(0==csvtranscode_csvtok_test(NULL," "));
ck_assert(0==csvtranscode_csvtok_test(NULL,"a"));
ck_assert(0==csvtranscode_csvtok_test(NULL," b"));
ck_assert(0==csvtranscode_csvtok_test(NULL,"c ,"));
ck_assert(0==csvtranscode_csvtok_test(NULL,",a, b,c ,"));
ck_assert(0==csvtranscode_csvtok_test(NULL,"a\rb"));
ck_assert(0==csvtranscode_csvtok_test(NULL,"a\nb"));
ck_assert(0==csvtranscode_csvtok_test(NULL,"a\r\nb"));
ck_assert(0==csvtranscode_csvtok_test(NULL,"a\n\rb"));
ck_assert(0==csvtranscode_csvtok_test(NULL," "));
ck_assert(0==csvtranscode_csvtok_test(NULL," "));
ck_assert(0==csvtranscode_csvtok_test(NULL,"a"));
ck_assert(0==csvtranscode_csvtok_test(NULL," b"));
ck_assert(0==csvtranscode_csvtok_test(NULL,"c "));
ck_assert(0==csvtranscode_csvtok_test(NULL,"\""));
ck_assert(0==csvtranscode_csvtok_test(NULL," ,a, b"));
ck_assert(0==csvtranscode_csvtok_test(NULL,"c "));
ck_assert(0==csvtranscode_csvtok_test(NULL," "));
ck_assert(0==csvtranscode_csvtok_test(NULL,"a"));
ck_assert(0==csvtranscode_csvtok_test(NULL," b"));
ck_assert(0==csvtranscode_csvtok_test(NULL,"c ,"));
ck_assert(0==csvtranscode_csvtok_test(NULL,",a, b,c ,"));
ck_assert(0==csvtranscode_csvtok_test(NULL,"a\rb"));
ck_assert(0==csvtranscode_csvtok_test(NULL,"a\nb"));
ck_assert(0==csvtranscode_csvtok_test(NULL,"a\r\nb"));
ck_assert(0==csvtranscode_csvtok_test(NULL,"a\n\rb"));
ck_assert(0==csvtranscode_csvtok_test(NULL," "));
ck_assert(0==csvtranscode_csvtok_test(NULL,NULL));
free(csv);
}
END_TEST
START_TEST(csvtranscode_csvtok_empty) {
char* csv = strdup("");
ck_assert(0==csvtranscode_csvtok_test(csv,NULL));
free(csv);
}
END_TEST
START_TEST(csvtranscode_csvtok_commazero) {
char* csv = strdup("a,");
ck_assert(0==csvtranscode_csvtok_test(csv,NULL));
free(csv);
}
END_TEST
START_TEST(csvtranscode_csvtok_commaCR) {
char* csv = strdup("a,\r");
/* Accepted because CR is not a separator by itself, alone, but a contents */
ck_assert(0==csvtranscode_csvtok_test(csv,"a"));
ck_assert(0==csvtranscode_csvtok_test(NULL,"\r"));
free(csv);
}
END_TEST
START_TEST(csvtranscode_csvtok_commaLF) {
char* csv = strdup("a,\n");
ck_assert(0==csvtranscode_csvtok_test(csv,NULL));
free(csv);
}
END_TEST
START_TEST(csvtranscode_csvtok_commaCRLF) {
char* csv = strdup("a,\r\n");
ck_assert(0==csvtranscode_csvtok_test(csv,NULL));
free(csv);
}
END_TEST
START_TEST(csvtranscode_csvtok_commaLFCR) {
char* csv = strdup("a,\n\r");
ck_assert(0==csvtranscode_csvtok_test(csv,NULL));<--- Calling function 'csvtranscode_csvtok_test', 2nd argument 'NULL' value is 0
free(csv);
}
END_TEST
START_TEST(csvtranscode_csvtok_spacebeforequote) {
char* csv = strdup("a, \"b\",c");
ck_assert(0==csvtranscode_csvtok_test(csv,"a"));
ck_assert(0==csvtranscode_csvtok_test(NULL,NULL));
free(csv);
}
END_TEST
START_TEST(csvtranscode_csvtok_spaceafterquote) {
char* csv = strdup("a,\"b\" ,c");
ck_assert(0==csvtranscode_csvtok_test(csv,"a"));
ck_assert(0==csvtranscode_csvtok_test(NULL,NULL));
free(csv);
}
END_TEST
START_TEST(csvtranscode_csvtok_unbalancedquotes) {
char* csv = strdup("a,\"b,c");
ck_assert(0==csvtranscode_csvtok_test(csv,"a"));
ck_assert(0==csvtranscode_csvtok_test(NULL,NULL));
free(csv);
}
END_TEST
/************************************************************************/
/* Begining of test (potentially in child) */
void csvtranscode_checked_uninitialized_setup() {
signals_catch();
forktest_gprofdir();
}
/* End of test (potentially in child) */
void csvtranscode_checked_uninitialized_teardown() {
signals_release();
}
/************************************************************************/
void csvtranscode_unchecked_common_setup() {
forktest_init();
}
void csvtranscode_unchecked_common_teardown() {
}
/************************************************************************/
Suite* csvtranscode_suite() {
Suite *s;
TCase *tc;
s = suite_create("CSVTranscode");
tc = tcase_create("csvtok");
tcase_set_tags(tc,"CSVTag CSVTag2");
/* tcase_set_timeout(tc,5); */ /* seconds */
tcase_add_checked_fixture(tc, csvtranscode_checked_uninitialized_setup, csvtranscode_checked_uninitialized_teardown);
tcase_add_unchecked_fixture(tc, csvtranscode_unchecked_common_setup, csvtranscode_unchecked_common_teardown);
suite_add_tcase(s, tc);
tcase_add_test_raise_signal(tc, csvtranscode_test,6);
tcase_add_test(tc, csvtranscode_txt2csv);
tcase_add_test(tc, csvtranscode_csvtok_empty);
tcase_add_test(tc, csvtranscode_csvtok_ok);
tcase_add_test(tc, csvtranscode_csvtok_commazero);
tcase_add_test(tc, csvtranscode_csvtok_commaLF);
tcase_add_test(tc, csvtranscode_csvtok_commaCR);
tcase_add_test(tc, csvtranscode_csvtok_commaCRLF);
tcase_add_test(tc, csvtranscode_csvtok_commaLFCR);
tcase_add_test(tc, csvtranscode_csvtok_spacebeforequote);
tcase_add_test(tc, csvtranscode_csvtok_spaceafterquote);
tcase_add_test(tc, csvtranscode_csvtok_unbalancedquotes);
return s;
}
/* vim: set tw=80: */
|