eabba11df019de47ddbd142b8ed1430888cd7461
[collectd.git] / src / tests / test_common.c
1 /**
2  * collectd - src/common_test.c
3  *
4  * Copyright (C) 2013       Florian octo Forster
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *   Florian octo Forster <octo at collectd.org>
26  */
27
28 #include "tests/macros.h"
29 #include "common.h"
30
31 DEF_TEST(sstrncpy)
32 {
33   char buffer[16] = "";
34   char *ptr = &buffer[4];
35   char *ret;
36
37   buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0xff;
38   buffer[12] = buffer[13] = buffer[14] = buffer[15] = 0xff;
39
40   ret = sstrncpy (ptr, "foobar", 8);
41   OK(ret == ptr);
42   STREQ ("foobar", ptr);
43   OK(buffer[3] == buffer[12]);
44
45   ret = sstrncpy (ptr, "abc", 8);
46   OK(ret == ptr);
47   STREQ ("abc", ptr);
48   OK(buffer[3] == buffer[12]);
49
50   ret = sstrncpy (ptr, "collectd", 8);
51   OK(ret == ptr);
52   OK(ptr[7] == 0);
53   STREQ ("collect", ptr);
54   OK(buffer[3] == buffer[12]);
55
56   return (0);
57 }
58
59 DEF_TEST(ssnprintf)
60 {
61   char buffer[16] = "";
62   char *ptr = &buffer[4];
63   int status;
64
65   buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0xff;
66   buffer[12] = buffer[13] = buffer[14] = buffer[15] = 0xff;
67
68   status = ssnprintf (ptr, 8, "%i", 1337);
69   OK(status == 4);
70   STREQ ("1337", ptr);
71
72   status = ssnprintf (ptr, 8, "%s", "collectd");
73   OK(status == 8);
74   OK(ptr[7] == 0);
75   STREQ ("collect", ptr);
76   OK(buffer[3] == buffer[12]);
77
78   return (0);
79 }
80
81 DEF_TEST(sstrdup)
82 {
83   char *ptr;
84
85   ptr = sstrdup ("collectd");
86   OK(ptr != NULL);
87   STREQ ("collectd", ptr);
88
89   sfree(ptr);
90   OK(ptr == NULL);
91
92   ptr = sstrdup (NULL);
93   OK(ptr == NULL);
94
95   return (0);
96 }
97
98 DEF_TEST(strsplit)
99 {
100   char buffer[32];
101   char *fields[8];
102   int status;
103
104   strncpy (buffer, "foo bar", sizeof (buffer));
105   status = strsplit (buffer, fields, 8);
106   OK(status == 2);
107   STREQ ("foo", fields[0]);
108   STREQ ("bar", fields[1]);
109
110   strncpy (buffer, "foo \t bar", sizeof (buffer));
111   status = strsplit (buffer, fields, 8);
112   OK(status == 2);
113   STREQ ("foo", fields[0]);
114   STREQ ("bar", fields[1]);
115
116   strncpy (buffer, "one two\tthree\rfour\nfive", sizeof (buffer));
117   status = strsplit (buffer, fields, 8);
118   OK(status == 5);
119   STREQ ("one", fields[0]);
120   STREQ ("two", fields[1]);
121   STREQ ("three", fields[2]);
122   STREQ ("four", fields[3]);
123   STREQ ("five", fields[4]);
124
125   strncpy (buffer, "\twith trailing\n", sizeof (buffer));
126   status = strsplit (buffer, fields, 8);
127   OK(status == 2);
128   STREQ ("with", fields[0]);
129   STREQ ("trailing", fields[1]);
130
131   strncpy (buffer, "1 2 3 4 5 6 7 8 9 10 11 12 13", sizeof (buffer));
132   status = strsplit (buffer, fields, 8);
133   OK(status == 8);
134   STREQ ("7", fields[6]);
135   STREQ ("8", fields[7]);
136
137   strncpy (buffer, "single", sizeof (buffer));
138   status = strsplit (buffer, fields, 8);
139   OK(status == 1);
140   STREQ ("single", fields[0]);
141
142   strncpy (buffer, "", sizeof (buffer));
143   status = strsplit (buffer, fields, 8);
144   OK(status == 0);
145
146   return (0);
147 }
148
149 DEF_TEST(strjoin)
150 {
151   char buffer[16];
152   char *fields[4];
153   int status;
154
155   fields[0] = "foo";
156   fields[1] = "bar";
157   fields[2] = "baz";
158   fields[3] = "qux";
159
160   status = strjoin (buffer, sizeof (buffer), fields, 2, "!");
161   OK(status == 7);
162   STREQ ("foo!bar", buffer);
163
164   status = strjoin (buffer, sizeof (buffer), fields, 1, "!");
165   OK(status == 3);
166   STREQ ("foo", buffer);
167
168   status = strjoin (buffer, sizeof (buffer), fields, 0, "!");
169   OK(status < 0);
170
171   status = strjoin (buffer, sizeof (buffer), fields, 2, "rcht");
172   OK(status == 10);
173   STREQ ("foorchtbar", buffer);
174
175   status = strjoin (buffer, sizeof (buffer), fields, 4, "");
176   OK(status == 12);
177   STREQ ("foobarbazqux", buffer);
178
179   status = strjoin (buffer, sizeof (buffer), fields, 4, "!");
180   OK(status == 15);
181   STREQ ("foo!bar!baz!qux", buffer);
182
183   fields[0] = "0123";
184   fields[1] = "4567";
185   fields[2] = "8901";
186   fields[3] = "2345";
187   status = strjoin (buffer, sizeof (buffer), fields, 4, "-");
188   OK(status < 0);
189
190   return (0);
191 }
192
193 DEF_TEST(strunescape)
194 {
195   char buffer[16];
196   int status;
197
198   strncpy (buffer, "foo\\tbar", sizeof (buffer));
199   status = strunescape (buffer, sizeof (buffer));
200   OK(status == 0);
201   STREQ ("foo\tbar", buffer);
202
203   strncpy (buffer, "\\tfoo\\r\\n", sizeof (buffer));
204   status = strunescape (buffer, sizeof (buffer));
205   OK(status == 0);
206   STREQ ("\tfoo\r\n", buffer);
207
208   strncpy (buffer, "With \\\"quotes\\\"", sizeof (buffer));
209   status = strunescape (buffer, sizeof (buffer));
210   OK(status == 0);
211   STREQ ("With \"quotes\"", buffer);
212
213   /* Backslash before null byte */
214   strncpy (buffer, "\\tbackslash end\\", sizeof (buffer));
215   status = strunescape (buffer, sizeof (buffer));
216   OK(status != 0);
217   STREQ ("\tbackslash end", buffer);
218   return (0);
219
220   /* Backslash at buffer end */
221   strncpy (buffer, "\\t3\\56", sizeof (buffer));
222   status = strunescape (buffer, 4);
223   OK(status != 0);
224   OK(buffer[0] == '\t');
225   OK(buffer[1] == '3');
226   OK(buffer[2] == 0);
227   OK(buffer[3] == 0);
228   OK(buffer[4] == '5');
229   OK(buffer[5] == '6');
230   OK(buffer[6] == '7');
231
232   return (0);
233 }
234
235 int main (void)
236 {
237   RUN_TEST(sstrncpy);
238   RUN_TEST(ssnprintf);
239   RUN_TEST(sstrdup);
240   RUN_TEST(strsplit);
241   RUN_TEST(strjoin);
242   RUN_TEST(strunescape);
243
244   END_TEST;
245 }
246
247 /* vim: set sw=2 sts=2 et : */