Added SILC_STR_STRING and SILC_STR_STRING_APPEND and support for
[crypto.git] / lib / silcutil / tests / test_silcbuffmt.c
1 /* Buffer formatting tests */
2
3 #include "silc.h"
4
5 int print(SilcStack stack, SilcBuffer buf, void *value, void *context)
6 {
7   fwrite(silc_buffer_data(buf), 1, silc_buffer_len(buf), stdout);
8   if (!silc_buffer_strchr(buf, '\n', TRUE))
9     printf("\n");
10   fflush(stdout);
11   return silc_buffer_len(buf);
12 }
13
14 int main(int argc, char **argv)
15 {
16   SilcBool success = FALSE;
17   char string[1024], *astring;
18   SilcBufferStruct buf, buf2;
19
20   if (argc > 1 && !strcmp(argv[1], "-d")) {
21     silc_log_debug(TRUE);
22     silc_log_quick(TRUE);
23     silc_log_debug_hexdump(TRUE);
24     silc_log_set_debug_string("*buf*,*regex*,*errno*");
25   }
26
27   silc_snprintf(string, sizeof(string), "This is foobar");
28   silc_buffer_set(&buf, string, strlen(string));
29   SILC_LOG_DEBUG(("sed 's/foo/bar/'"));
30   SILC_LOG_DEBUG(("string: %s", string));
31   if (silc_buffer_format(&buf,
32                          SILC_STR_REGEX("foo", 0),
33                            SILC_STR_STRING("bar"),
34                          SILC_STR_END,
35                          SILC_STR_END) < 0)
36     goto err;
37   SILC_LOG_DEBUG(("string: %s", silc_buffer_data(&buf)));
38   if (strcmp("This is barbar", silc_buffer_data(&buf)))
39     goto err;
40
41   silc_snprintf(string, sizeof(string), "This is foobar string!!");
42   astring = silc_memdup(string, strlen(string));
43   silc_buffer_set(&buf, astring, strlen(astring) + 1);
44   SILC_LOG_DEBUG(("sed 's/foo/barbar/g'"));
45   SILC_LOG_DEBUG(("string: %s", astring));
46   if (silc_buffer_format(&buf,
47                          SILC_STR_REGEX("foo", SILC_STR_REGEX_ALL |
48                                                SILC_STR_REGEX_INCLUSIVE),
49                            SILC_STR_STRING_APPEND("barbar"),
50                          SILC_STR_END,
51                          SILC_STR_END) < 0)
52     goto err;
53   silc_buffer_printf(&buf, TRUE);
54   if (strcmp("This is barbarbar string!!", silc_buffer_data(&buf)))
55     goto err;
56   silc_buffer_purge(&buf);
57
58   silc_snprintf(string, sizeof(string), "This is foobar\n");
59   silc_buffer_set(&buf, string, strlen(string));
60   SILC_LOG_DEBUG(("sed 's/\\n/\\0/'"));
61   SILC_LOG_DEBUG(("string: %s", string));
62   if (silc_buffer_format(&buf,
63                          SILC_STR_REGEX("\n", 0),
64                            SILC_STR_UINT8(0),
65                          SILC_STR_END,
66                          SILC_STR_END) < 0)
67     goto err;
68   SILC_LOG_DEBUG(("string: %s", silc_buffer_data(&buf)));
69   if (strcmp("This is foobar", silc_buffer_data(&buf)))
70     goto err;
71
72   silc_snprintf(string, sizeof(string), "foo\nfoobar\nbarfoofoo\nbar\n\nfoo");
73   silc_buffer_set(&buf, string, strlen(string));
74   SILC_LOG_DEBUG(("sed 's/foo/bar/g'"));
75   SILC_LOG_DEBUG(("string: %s", string));
76   if (silc_buffer_format(&buf,
77                          SILC_STR_REGEX("foo", SILC_STR_REGEX_NL |
78                                                SILC_STR_REGEX_ALL),
79                            SILC_STR_STRING("bar"),
80                          SILC_STR_END,
81                          SILC_STR_END) < 0)
82     goto err;
83   SILC_LOG_DEBUG(("string: %s", silc_buffer_data(&buf)));
84   if (strcmp("bar\nbarbar\nbarbarbar\nbar\n\nbar", silc_buffer_data(&buf)))
85     goto err;
86
87   silc_snprintf(string, sizeof(string),
88         "foo\nbazfoobar\nbarfoofoo\nbar\nbaz\nbazfoo");
89   silc_buffer_set(&buf, string, strlen(string));
90   SILC_LOG_DEBUG(("sed '/baz/s/foo/bar/"));
91   SILC_LOG_DEBUG(("string: %s", string));
92   if (silc_buffer_format(&buf,
93                          SILC_STR_REGEX("baz", SILC_STR_REGEX_NL),
94                            SILC_STR_REGEX("foo", SILC_STR_REGEX_ALL),
95                              SILC_STR_STRING("bar"),
96                            SILC_STR_END,
97                          SILC_STR_END, SILC_STR_END) < 0)
98     goto err;
99   SILC_LOG_DEBUG(("string: %s", silc_buffer_data(&buf)));
100   if (strcmp("foo\nbazbarbar\nbarfoofoo\nbar\nbaz\nbazbar",
101         silc_buffer_data(&buf)))
102     goto err;
103
104   silc_snprintf(string, sizeof(string),
105         "foo\nbazfoobar\nbarfoofoo\nbar\nbaz\nbazfoo");
106   silc_buffer_set(&buf, string, strlen(string));
107   SILC_LOG_DEBUG(("sed '/baz/!s/foo/bar/"));
108   SILC_LOG_DEBUG(("string: %s", string));
109   if (silc_buffer_format(&buf,
110                          SILC_STR_REGEX("baz", SILC_STR_REGEX_NL |
111                                                SILC_STR_REGEX_NOT),
112                            SILC_STR_REGEX("foo", SILC_STR_REGEX_ALL),
113                              SILC_STR_STRING("bar"),
114                            SILC_STR_END,
115                          SILC_STR_END, SILC_STR_END) < 0)
116     goto err;
117   SILC_LOG_DEBUG(("string: %s", silc_buffer_data(&buf)));
118   if (strcmp("bar\nbazfoobar\nbarbarbar\nbar\nbaz\nbazfoo",
119         silc_buffer_data(&buf)))
120     goto err;
121
122   SILC_LOG_DEBUG(("Print all lines starting with 'R'"));
123   silc_snprintf(string, sizeof(string),
124         "Rfoo\nbazfoobar\nRbarfoofoo\nRbar\nbaz\nRbazfoo");
125   silc_buffer_set(&buf, string, strlen(string));
126   SILC_LOG_DEBUG(("string: %s", string));
127   if (silc_buffer_unformat(&buf,
128                            SILC_STR_REGEX("^R", SILC_STR_REGEX_NL),
129                              SILC_STR_FUNC(print, NULL, NULL),
130                            SILC_STR_END, SILC_STR_END) < 0)
131     goto err;
132
133   success = TRUE;
134
135  err:
136   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
137   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
138
139   return success;
140 }