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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
/* $%BEGINLICENSE%$
Copyright (C) 2007-2008 MySQL AB, 2008 Sun Microsystems, Inc
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$%ENDLICENSE%$ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "glib-ext.h"
#include "sql-tokenizer.h"
#if GLIB_CHECK_VERSION(2, 16, 0)
#define C(x) x, sizeof(x) - 1
#define START_TEST(x) void (x)(void)
#define END_TEST
/**
* tests for the SQL tokenizer
* @ingroup sql test
* @{
*/
/**
* @test check if SQL tokenizing works
*
*/
START_TEST(test_tokenizer) {
GPtrArray *tokens = NULL;
gsize i;
tokens = sql_tokens_new();
sql_tokenizer(tokens, C("SELEcT \"qq-end\"\"\", \"\"\"qq-start\", \"'\"`qq-mixed''\" FROM a AS `b`, `ABC``FOO` "));
for (i = 0; i < tokens->len; i++) {
sql_token *token = tokens->pdata[i];
#define T(t_id, t_text) \
g_assert_cmpint(token->token_id, ==, t_id); \
g_assert_cmpstr(token->text->str, ==, t_text);
switch (i) {
case 0: T(TK_SQL_SELECT, "SELEcT"); break;
case 1: T(TK_STRING, "qq-end\""); break;
case 2: T(TK_COMMA, ","); break;
case 3: T(TK_STRING, "\"qq-start"); break;
case 4: T(TK_COMMA, ","); break;
case 5: T(TK_STRING, "'\"`qq-mixed''"); break;
case 6: T(TK_SQL_FROM, "FROM"); break;
case 7: T(TK_LITERAL, "a"); break;
case 8: T(TK_SQL_AS, "AS"); break;
case 9: T(TK_LITERAL, "b"); break;
case 10: T(TK_COMMA, ","); break;
case 11: T(TK_LITERAL, "ABC`FOO"); break;
#undef T
default:
/**
* a self-writing test-case
*/
printf("case %"G_GSIZE_FORMAT": T(%s, \"%s\"); break;\n", i, sql_token_get_name(token->token_id), token->text->str);
break;
}
}
/* cleanup */
sql_tokens_free(tokens);
} END_TEST
/**
* @test table-names might start with a _ even without quoting
*
*/
START_TEST(test_table_name_underscore) {
GPtrArray *tokens = NULL;
gsize i;
tokens = sql_tokens_new();
sql_tokenizer(tokens, C("SELEcT * FROM __test_table "));
for (i = 0; i < tokens->len; i++) {
sql_token *token = tokens->pdata[i];
#define T(t_id, t_text) \
g_assert_cmpint(token->token_id, ==, t_id); \
g_assert_cmpstr(token->text->str, ==, t_text);
switch (i) {
case 0: T(TK_SQL_SELECT, "SELEcT"); break;
case 1: T(TK_STAR, "*"); break;
case 2: T(TK_SQL_FROM, "FROM"); break;
case 3: T(TK_LITERAL, "__test_table"); break;
#undef T
default:
/**
* a self-writing test-case
*/
printf("case %"G_GSIZE_FORMAT": T(%s, \"%s\"); break;\n", i, sql_token_get_name(token->token_id), token->text->str);
break;
}
}
/* cleanup */
sql_tokens_free(tokens);
} END_TEST
/**
* @test check if we can map all tokens to a name and back again
*
*/
START_TEST(test_token2name) {
gsize i;
/* convert tokens to id and back to name */
for (i = 0; i < TK_LAST_TOKEN; i++) {
const char *name;
g_assert((name = sql_token_get_name(i)));
}
} END_TEST
/**
* @test check if single line comments are recognized properly
*/
START_TEST(test_simple_dashdashcomment) {
gsize i;
GPtrArray *tokens = NULL;
tokens = sql_tokens_new();
sql_tokenizer(tokens, C("-- comment"));
for (i = 0; i < tokens->len; i++) {
sql_token *token = tokens->pdata[i];
#define T(t_id, t_text) \
g_assert_cmpint(token->token_id, ==, t_id); \
g_assert_cmpstr(token->text->str, ==, t_text);
switch (i) {
case 0: T(TK_COMMENT, "comment"); break;
default: g_assert(FALSE); break;
#undef T
}
}
sql_tokens_free(tokens);
} END_TEST
/**
* @test check if single line comments are recognized properly
*/
START_TEST(test_dashdashcomment) {
gsize i;
GPtrArray *tokens = NULL;
tokens = sql_tokens_new();
sql_tokenizer(tokens, C("-- comment\nSELECT 1 FROM dual"));
for (i = 0; i < tokens->len; i++) {
sql_token *token = tokens->pdata[i];
#define T(t_id, t_text) \
g_assert_cmpint(token->token_id, ==, t_id); \
g_assert_cmpstr(token->text->str, ==, t_text);
switch (i) {
case 0: T(TK_COMMENT, " comment"); break; /* note the leading whitespace here! */
case 1: T(TK_SQL_SELECT, "SELECT"); break;
case 2: T(TK_INTEGER, "1"); break;
case 3: T(TK_SQL_FROM, "FROM"); break;
case 4: T(TK_SQL_DUAL, "dual"); break;
default: g_assert(FALSE); break;
#undef T
}
}
sql_tokens_free(tokens);
} END_TEST
/**
* @test check that '--1' will not start a comment
*/
START_TEST(test_doubleminus) {
gsize i;
GPtrArray *tokens = NULL;
tokens = sql_tokens_new();
sql_tokenizer(tokens, C("SELECT 1--1 FROM DUAL"));
for (i = 0; i < tokens->len; i++) {
sql_token *token = tokens->pdata[i];
#define T(t_id, t_text) \
g_assert_cmpint(token->token_id, ==, t_id); \
g_assert_cmpstr(token->text->str, ==, t_text);
switch (i) {
case 0: T(TK_SQL_SELECT, "SELECT"); break;
case 1: T(TK_INTEGER, "1"); break;
case 2: T(TK_MINUS, "-"); break;
case 3: T(TK_MINUS, "-"); break;
case 4: T(TK_INTEGER, "1"); break;
case 5: T(TK_SQL_FROM, "FROM"); break;
case 6: T(TK_SQL_DUAL, "DUAL"); break;
default: g_assert(FALSE); break;
#undef T
}
}
sql_tokens_free(tokens);
} END_TEST
/**
* @test First test for bug 36506, where EOF encountered while the tokenizer is in a start start
* corrupts its internal state resulting in failure to correctly tokenize the subsequent query.
*/
START_TEST(test_startstate_reset_quoted) {
gsize i;
GPtrArray *tokens = NULL;
tokens = sql_tokens_new();
/* EOF encountered while sql-tokenizer is in QUOTED start state */
sql_tokenizer(tokens, C("SELECT \"foo"));
sql_tokens_free(tokens);
tokens = sql_tokens_new();
/* valid query, fails with an assertion when bug 36506 is unfixed */
sql_tokenizer(tokens, C("SELECT \"foo\""));
for (i = 0; i < tokens->len; i++) {
sql_token *token = tokens->pdata[i];
#define T(t_id, t_text) \
g_assert_cmpint(token->token_id, ==, t_id); \
g_assert_cmpstr(token->text->str, ==, t_text);
switch (i) {
case 0: T(TK_SQL_SELECT, "SELECT"); break;
case 1: T(TK_STRING, "foo"); break;
default: g_assert(FALSE); break;
#undef T
}
}
sql_tokens_free(tokens);
} END_TEST
/**
* @test Second test for bug 36506, where EOF encountered while the tokenizer is in a start start
* corrupts its internal state resulting in failure to correctly tokenize the subsequent query.
*/
START_TEST(test_startstate_reset_comment) {
gsize i;
GPtrArray *tokens = NULL;
/* test for C-style comments */
tokens = sql_tokens_new();
/* EOF encountered while sql-tokenizer is in COMMENT start state */
sql_tokenizer(tokens, C("SELECT /* foo"));
sql_tokens_free(tokens);
tokens = sql_tokens_new();
/* valid query, fails with an assertion when bug 36506 is unfixed */
sql_tokenizer(tokens, C("SELECT /*foo*/ 1"));
for (i = 0; i < tokens->len; i++) {
sql_token *token = tokens->pdata[i];
#define T(t_id, t_text) \
g_assert_cmpint(token->token_id, ==, t_id); \
g_assert_cmpstr(token->text->str, ==, t_text);
switch (i) {
case 0: T(TK_SQL_SELECT, "SELECT"); break;
case 1: T(TK_COMMENT, "foo"); break;
case 2: T(TK_INTEGER, "1"); break;
default: g_assert(FALSE); break;
#undef T
}
}
sql_tokens_free(tokens);
/* test for line comments */
tokens = sql_tokens_new();
/* EOF encountered while sql-tokenizer is in LINECOMMENT start state */
sql_tokenizer(tokens, C("SELECT -- foo"));
sql_tokens_free(tokens);
tokens = sql_tokens_new();
/* valid query, fails with an assertion when bug 36506 is unfixed */
sql_tokenizer(tokens, C("SELECT -- foo\n1"));
for (i = 0; i < tokens->len; i++) {
sql_token *token = tokens->pdata[i];
#define T(t_id, t_text) \
g_assert_cmpint(token->token_id, ==, t_id); \
g_assert_cmpstr(token->text->str, ==, t_text);
switch (i) {
case 0: T(TK_SQL_SELECT, "SELECT"); break;
case 1: T(TK_COMMENT, "foo"); break;
case 2: T(TK_INTEGER, "1"); break;
default: g_assert(FALSE); break;
#undef T
}
}
sql_tokens_free(tokens);
} END_TEST
/* @} */
/**
* get all keywords and try if we get all the ids
*/
void test_tokenizer_keywords() {
gsize i;
for (i = 0; sql_token_get_name(i); i++) {
const char *keyword;
/** only tokens with TK_SQL_* are keyworks */
if (0 != strncmp(sql_token_get_name(i), "TK_SQL_", sizeof("TK_SQL_") - 1)) continue;
keyword = sql_token_get_name(i) + sizeof("TK_SQL_") - 1;
g_assert_cmpint(sql_token_get_id(keyword), ==, i);
}
g_assert_cmpint(sql_token_get_id("COMMIT"), ==, TK_LITERAL);
}
int main(int argc, char **argv) {
g_test_init(&argc, &argv, NULL);
g_test_bug_base("http://bugs.mysql.com/");
g_test_add_func("/core/tokenizer_keywords", test_tokenizer_keywords);
g_test_add_func("/core/tokenizer", test_tokenizer);
g_test_add_func("/core/tokenizer_token2name", test_token2name);
g_test_add_func("/core/tokenizer_table_name_underscore", test_table_name_underscore);
g_test_add_func("/core/tokenizer_simple_dashdashcomment", test_simple_dashdashcomment);
g_test_add_func("/core/tokenizer_dashdashcomment", test_dashdashcomment);
g_test_add_func("/core/tokenizer_doubleminus", test_doubleminus);
g_test_add_func("/core/tokenizer_startstate_reset_quoted", test_startstate_reset_quoted);
g_test_add_func("/core/tokenizer_startstate_reset_comment", test_startstate_reset_comment);
return g_test_run();
}
#else
int main() {
return 77;
}
#endif
|