Mercurial > dovecot > core-2.2
annotate src/lib/test-strescape.c @ 22955:812e5c961328
fts: Indexing virtual mailbox didn't always index the last mails
author | Timo Sirainen <timo.sirainen@dovecot.fi> |
---|---|
date | Thu, 03 May 2018 18:33:00 +0300 |
parents | cb108f786fb4 |
children |
rev | line source |
---|---|
22713
cb108f786fb4
Updated copyright notices to include the year 2018.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
21390
diff
changeset
|
1 /* Copyright (c) 2009-2018 Dovecot authors, see the included COPYING file */ |
9484
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
2 |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
3 #include "test-lib.h" |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
4 #include "str.h" |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
5 #include "strescape.h" |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
6 |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
7 struct strinput { |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
8 const char *input; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
9 const char *output; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
10 }; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
11 |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
12 void test_strescape(void) |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
13 { |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
14 static struct strinput unesc[] = { |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
15 { "foo", "foo" }, |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
16 { "\\\\\\\\\\\"\\\"\\\'\\\'", "\\\\\"\"\'\'" }, |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
17 { "\\a\\n\\r\\", "anr" } |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
18 }; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
19 static struct strinput tabesc[] = { |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
20 { "foo", "foo" }, |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
21 { "\001", "\0011" }, |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
22 { "\t", "\001t" }, |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
23 { "\r", "\001r" }, |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
24 { "\n", "\001n" }, |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
25 { "\001\001\t\t\r\r\n\n", "\0011\0011\001t\001t\001r\001r\001n\001n" } |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
26 }; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
27 unsigned char buf[1 << CHAR_BIT]; |
17112
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
28 const char *escaped, *tabstr, *unesc_str; |
9484
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
29 string_t *str; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
30 unsigned int i; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
31 |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
32 test_begin("str_escape"); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
33 for (i = 1; i < sizeof(buf); i++) |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
34 buf[i-1] = i; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
35 buf[i-1] = '\0'; |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
36 |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
37 escaped = str_escape((char *)buf); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
38 test_assert(strlen(escaped) == (1 << CHAR_BIT) - 1 + 3); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
39 test_assert(escaped['\"'-1] == '\\'); /* 34 */ |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
40 test_assert(escaped['\"'] == '\"'); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
41 test_assert(escaped['\''+1-1] == '\\'); /* 39 */ |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
42 test_assert(escaped['\''+1] == '\''); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
43 test_assert(escaped['\\'+2-1] == '\\'); /* 92 */ |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
44 test_assert(escaped['\\'+2] == '\\'); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
45 test_assert(strcmp(str_escape("\\\\\"\"\'\'"), |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
46 "\\\\\\\\\\\"\\\"\\\'\\\'") == 0); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
47 test_end(); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
48 |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
49 str = t_str_new(256); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
50 test_begin("str_unescape"); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
51 for (i = 0; i < N_ELEMENTS(unesc); i++) { |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
52 test_assert(strcmp(str_unescape(t_strdup_noconst(unesc[i].input)), |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
53 unesc[i].output) == 0); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
54 str_truncate(str, 0); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
55 str_append_unescaped(str, unesc[i].input, strlen(unesc[i].input)); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
56 test_assert(strcmp(str_c(str), unesc[i].output) == 0); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
57 } |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
58 test_end(); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
59 |
17112
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
60 test_begin("str_unescape_next"); |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
61 escaped = "foo\"bar\\\"b\\\\az\"plop"; |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
62 test_assert(str_unescape_next(&escaped, &unesc_str) == 0); |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
63 test_assert(strcmp(unesc_str, "foo") == 0); |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
64 test_assert(str_unescape_next(&escaped, &unesc_str) == 0); |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
65 test_assert(strcmp(unesc_str, "bar\"b\\az") == 0); |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
66 test_assert(str_unescape_next(&escaped, &unesc_str) == -1); |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
67 escaped = "foo\\"; |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
68 test_assert(str_unescape_next(&escaped, &unesc_str) == -1); |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
69 test_end(); |
9735c6fb7e39
liblib: Added str_unescape_next()
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
70 |
9484
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
71 test_begin("str_tabescape"); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
72 for (i = 0; i < N_ELEMENTS(tabesc); i++) { |
20339
327fcf6d2205
lib: Added t_str_tabunescape()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
73 test_assert(strcmp(t_str_tabunescape(tabesc[i].output), |
327fcf6d2205
lib: Added t_str_tabunescape()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19552
diff
changeset
|
74 tabesc[i].input) == 0); |
9516 | 75 test_assert(strcmp(str_tabunescape(t_strdup_noconst(tabesc[i].output)), |
76 tabesc[i].input) == 0); | |
9484
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
77 test_assert(strcmp(str_tabescape(tabesc[i].input), |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
78 tabesc[i].output) == 0); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
79 str_truncate(str, 0); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
80 str_append_tabunescaped(str, tabesc[i].output, strlen(tabesc[i].output)); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
81 test_assert(strcmp(str_c(str), tabesc[i].input) == 0); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
82 } |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
83 str_truncate(str, 0); |
9516 | 84 tabstr = "\0012\001l\001"; |
85 str_append_tabunescaped(str, tabstr, strlen(tabstr)); | |
9484
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
86 test_assert(strcmp(str_c(str), "2l") == 0); |
9516 | 87 test_assert(strcmp(str_c(str), str_tabunescape(t_strdup_noconst(tabstr))) == 0); |
9484
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
88 test_end(); |
5a6fe52a0cfc
Added str_append_tabunescaped(). str_tabescape*() now escapes also CR. Added unit tests.
Timo Sirainen <tss@iki.fi>
parents:
diff
changeset
|
89 } |