source: tester.c @ b85c1c4

release-1.10release-1.6release-1.7release-1.8release-1.9
Last change on this file since b85c1c4 was 9be80b5, checked in by Anders Kaseorg <andersk@mit.edu>, 15 years ago
zcrypt: Add tests. Signed-off-by: Anders Kaseorg <andersk@mit.edu> Reviewed-by: Nelson Elhage <nelhage@mit.edu>
  • Property mode set to 100644
File size: 14.3 KB
Line 
1#include "owl.h"
2#include <unistd.h>
3#include <stdlib.h>
4
5owl_global g;
6
7int numtests;
8
9int owl_util_regtest(void);
10int owl_dict_regtest(void);
11int owl_variable_regtest(void);
12int owl_filter_regtest(void);
13int owl_obarray_regtest(void);
14int owl_editwin_regtest(void);
15#ifdef OWL_ENABLE_ZCRYPT
16int owl_zcrypt_regtest(void);
17#endif
18
19int main(int argc, char **argv, char **env)
20{
21  owl_errqueue_init(owl_global_get_errqueue(&g));
22  owl_obarray_init(&(g.obarray));
23  g.context_stack = NULL;
24  owl_global_push_context(&g, OWL_CTX_STARTUP, NULL, NULL);
25
26  numtests = 0;
27  int numfailures=0;
28  /*
29    printf("1..%d\n", OWL_UTIL_NTESTS+OWL_DICT_NTESTS+OWL_VARIABLE_NTESTS
30    +OWL_FILTER_NTESTS+OWL_OBARRAY_NTESTS);
31  */
32  numfailures += owl_util_regtest();
33  numfailures += owl_dict_regtest();
34  numfailures += owl_variable_regtest();
35  numfailures += owl_filter_regtest();
36  numfailures += owl_obarray_regtest();
37  numfailures += owl_editwin_regtest();
38#ifdef OWL_ENABLE_ZCRYPT
39  numfailures += owl_zcrypt_regtest();
40#endif
41  if (numfailures) {
42      fprintf(stderr, "# *** WARNING: %d failures total\n", numfailures);
43  }
44  printf("1..%d\n", numtests);
45  return(numfailures);
46}
47
48#define FAIL_UNLESS(desc,pred) do { int __pred = (pred);                \
49    numtests++;                                                         \
50    printf("%s %s", (__pred)?"ok":(numfailed++,"not ok"), desc);        \
51    if(!(__pred)) printf("\t(%s:%d)", __FILE__, __LINE__); printf("%c", '\n'); } while(0)
52
53
54int owl_util_regtest(void)
55{
56  int numfailed=0;
57
58  printf("# BEGIN testing owl_util\n");
59
60  FAIL_UNLESS("owl_util_substitute 1",
61              !strcmp("foo", owl_text_substitute("foo", "", "Y")));
62  FAIL_UNLESS("owl_text_substitute 2",
63              !strcmp("fYZYZ", owl_text_substitute("foo", "o", "YZ")));
64  FAIL_UNLESS("owl_text_substitute 3",
65              !strcmp("foo", owl_text_substitute("fYZYZ", "YZ", "o")));
66  FAIL_UNLESS("owl_text_substitute 4",
67              !strcmp("/u/foo/meep", owl_text_substitute("~/meep", "~", "/u/foo")));
68
69  FAIL_UNLESS("skiptokens 1", 
70              !strcmp("bar quux", skiptokens("foo bar quux", 1)));
71  FAIL_UNLESS("skiptokens 2", 
72              !strcmp("meep", skiptokens("foo 'bar quux' meep", 2)));
73
74  FAIL_UNLESS("expand_tabs 1",
75              !strcmp("        hi", owl_text_expand_tabs("\thi")));
76
77  FAIL_UNLESS("expand_tabs 2",
78              !strcmp("        hi\nword    tab", owl_text_expand_tabs("\thi\nword\ttab")));
79
80  FAIL_UNLESS("expand_tabs 3",
81              !strcmp("                2 tabs", owl_text_expand_tabs("\t\t2 tabs")));
82  FAIL_UNLESS("expand_tabs 4",
83              !strcmp("α       ααααααα!        ", owl_text_expand_tabs(\tααααααα!\t")));
84  FAIL_UNLESS("expand_tabs 5",
85              !strcmp("A      AAA!!        ", owl_text_expand_tabs("A\tAAA!!\t")));
86
87  FAIL_UNLESS("skiptokens 1",
88              !strcmp("world", skiptokens("hello world", 1)));
89
90  FAIL_UNLESS("skiptokens 2",
91              !strcmp("c d e", skiptokens("a   b c d e", 2)));
92
93  FAIL_UNLESS("skiptokens 3",
94              !strcmp("\"b\" c d e", skiptokens("a \"b\" c d e", 1)));
95
96  FAIL_UNLESS("skiptokens 4",
97              !strcmp("c d e", skiptokens("a \"b\" c d e", 2)));
98
99  FAIL_UNLESS("skiptokens 5",
100              !strcmp("c d e", skiptokens("a \"'\" c d e", 2)));
101
102  /* if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); */
103  printf("# END testing owl_util (%d failures)\n", numfailed);
104  return(numfailed);
105}
106
107int owl_dict_regtest(void) {
108  owl_dict d;
109  owl_list l;
110  int numfailed=0;
111  char *av="aval", *bv="bval", *cv="cval", *dv="dval";
112
113  printf("# BEGIN testing owl_dict\n");
114  FAIL_UNLESS("create", 0==owl_dict_create(&d));
115  FAIL_UNLESS("insert b", 0==owl_dict_insert_element(&d, "b", bv, owl_dict_noop_free));
116  FAIL_UNLESS("insert d", 0==owl_dict_insert_element(&d, "d", dv, owl_dict_noop_free));
117  FAIL_UNLESS("insert a", 0==owl_dict_insert_element(&d, "a", av, owl_dict_noop_free));
118  FAIL_UNLESS("insert c", 0==owl_dict_insert_element(&d, "c", cv, owl_dict_noop_free));
119  FAIL_UNLESS("reinsert d (no replace)", -2==owl_dict_insert_element(&d, "d", dv, 0));
120  FAIL_UNLESS("find a", av==owl_dict_find_element(&d, "a"));
121  FAIL_UNLESS("find b", bv==owl_dict_find_element(&d, "b"));
122  FAIL_UNLESS("find c", cv==owl_dict_find_element(&d, "c"));
123  FAIL_UNLESS("find d", dv==owl_dict_find_element(&d, "d"));
124  FAIL_UNLESS("find e (non-existent)", NULL==owl_dict_find_element(&d, "e"));
125  FAIL_UNLESS("remove d", dv==owl_dict_remove_element(&d, "d"));
126  FAIL_UNLESS("find d (post-removal)", NULL==owl_dict_find_element(&d, "d"));
127
128  FAIL_UNLESS("get_size", 3==owl_dict_get_size(&d));
129  FAIL_UNLESS("get_keys", 0==owl_dict_get_keys(&d, &l));
130  FAIL_UNLESS("get_keys result size", 3==owl_list_get_size(&l));
131 
132  /* these assume the returned keys are sorted */
133  FAIL_UNLESS("get_keys result val",0==strcmp("a",owl_list_get_element(&l,0)));
134  FAIL_UNLESS("get_keys result val",0==strcmp("b",owl_list_get_element(&l,1)));
135  FAIL_UNLESS("get_keys result val",0==strcmp("c",owl_list_get_element(&l,2)));
136
137  owl_list_free_all(&l, owl_free);
138  owl_dict_free_all(&d, NULL);
139
140  /*  if (numfailed) printf("*** WARNING: failures encountered with owl_dict\n"); */
141  printf("# END testing owl_dict (%d failures)\n", numfailed);
142  return(numfailed);
143}
144
145int owl_variable_regtest(void) {
146  owl_vardict vd;
147  int numfailed=0;
148  char buf[1024];
149  const void *v;
150
151  printf("# BEGIN testing owl_variable\n");
152  FAIL_UNLESS("setup", 0==owl_variable_dict_setup(&vd));
153
154  FAIL_UNLESS("get bool", 0==owl_variable_get_bool(&vd,"rxping"));
155  FAIL_UNLESS("get bool (no such)", -1==owl_variable_get_bool(&vd,"mumble"));
156  FAIL_UNLESS("get bool as string 1", 0==owl_variable_get_tostring(&vd,"rxping", buf, 1024));
157  FAIL_UNLESS("get bool as string 2", 0==strcmp(buf,"off"));
158  FAIL_UNLESS("set bool 1", 0==owl_variable_set_bool_on(&vd,"rxping"));
159  FAIL_UNLESS("get bool 2", 1==owl_variable_get_bool(&vd,"rxping"));
160  FAIL_UNLESS("set bool 3", 0==owl_variable_set_fromstring(&vd,"rxping","off",0,0));
161  FAIL_UNLESS("get bool 4", 0==owl_variable_get_bool(&vd,"rxping"));
162  FAIL_UNLESS("set bool 5", -1==owl_variable_set_fromstring(&vd,"rxping","xxx",0,0));
163  FAIL_UNLESS("get bool 6", 0==owl_variable_get_bool(&vd,"rxping"));
164
165
166  FAIL_UNLESS("get string", 0==strcmp("~/zlog/people", owl_variable_get_string(&vd,"logpath")));
167  FAIL_UNLESS("set string 7", 0==owl_variable_set_string(&vd,"logpath","whee"));
168  FAIL_UNLESS("get string", 0==strcmp("whee", owl_variable_get_string(&vd,"logpath")));
169
170  FAIL_UNLESS("get int", 8==owl_variable_get_int(&vd,"typewinsize"));
171  FAIL_UNLESS("get int (no such)", -1==owl_variable_get_int(&vd,"mmble"));
172  FAIL_UNLESS("get int as string 1", 0==owl_variable_get_tostring(&vd,"typewinsize", buf, 1024));
173  FAIL_UNLESS("get int as string 2", 0==strcmp(buf,"8"));
174  FAIL_UNLESS("set int 1", 0==owl_variable_set_int(&vd,"typewinsize",12));
175  FAIL_UNLESS("get int 2", 12==owl_variable_get_int(&vd,"typewinsize"));
176  FAIL_UNLESS("set int 1b", -1==owl_variable_set_int(&vd,"typewinsize",-3));
177  FAIL_UNLESS("get int 2b", 12==owl_variable_get_int(&vd,"typewinsize"));
178  FAIL_UNLESS("set int 3", 0==owl_variable_set_fromstring(&vd,"typewinsize","9",0,0));
179  FAIL_UNLESS("get int 4", 9==owl_variable_get_int(&vd,"typewinsize"));
180  FAIL_UNLESS("set int 5", -1==owl_variable_set_fromstring(&vd,"typewinsize","xxx",0,0));
181  FAIL_UNLESS("set int 6", -1==owl_variable_set_fromstring(&vd,"typewinsize","",0,0));
182  FAIL_UNLESS("get int 7", 9==owl_variable_get_int(&vd,"typewinsize"));
183
184  owl_variable_dict_newvar_string(&vd, "stringvar", "", "", "testval");
185  FAIL_UNLESS("get new string var", NULL != (v = owl_variable_get(&vd, "stringvar", OWL_VARIABLE_STRING)));
186  FAIL_UNLESS("get new string val", !strcmp("testval", owl_variable_get_string(&vd, "stringvar")));
187  owl_variable_set_string(&vd, "stringvar", "new val");
188  FAIL_UNLESS("update string val", !strcmp("new val", owl_variable_get_string(&vd, "stringvar")));
189
190  owl_variable_dict_newvar_int(&vd, "intvar", "", "", 47);
191  FAIL_UNLESS("get new int var", NULL != (v = owl_variable_get(&vd, "intvar", OWL_VARIABLE_INT)));
192  FAIL_UNLESS("get new int val", 47 == owl_variable_get_int(&vd, "intvar"));
193  owl_variable_set_int(&vd, "intvar", 17);
194  FAIL_UNLESS("update bool val", 17 == owl_variable_get_int(&vd, "intvar"));
195
196  owl_variable_dict_newvar_bool(&vd, "boolvar", "", "", 1);
197  FAIL_UNLESS("get new bool var", NULL != (v = owl_variable_get(&vd, "boolvar", OWL_VARIABLE_BOOL)));
198  FAIL_UNLESS("get new bool val", owl_variable_get_bool(&vd, "boolvar"));
199  owl_variable_set_bool_off(&vd, "boolvar");
200  FAIL_UNLESS("update string val", !owl_variable_get_bool(&vd, "boolvar"));
201
202  owl_variable_dict_free(&vd);
203
204  /* if (numfailed) printf("*** WARNING: failures encountered with owl_variable\n"); */
205  printf("# END testing owl_variable (%d failures)\n", numfailed);
206  return(numfailed);
207}
208
209static int owl_filter_test_string(const char *filt, const owl_message *m, int shouldmatch)
210{
211  owl_filter *f;
212  int ok;
213  int failed = 0;
214  if ((f = owl_filter_new_fromstring("test-filter", filt)) == NULL) {
215    printf("not ok can't parse %s\n", filt);
216    failed = 1;
217    goto out;
218  }
219  ok = owl_filter_message_match(f, m);
220  if((shouldmatch && !ok) || (!shouldmatch && ok)) {
221    printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch);
222    failed = 1;
223  }
224 out:
225  owl_filter_delete(f);
226  if(!failed) {
227    printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt);
228  }
229  return failed;
230}
231
232int owl_filter_regtest(void) {
233  int numfailed=0;
234  owl_message m;
235  owl_filter *f1, *f2, *f3, *f4, *f5;
236
237  owl_dict_create(&g.filters);
238  g.filterlist = NULL;
239  owl_message_init(&m);
240  owl_message_set_type_zephyr(&m);
241  owl_message_set_direction_in(&m);
242  owl_message_set_class(&m, "owl");
243  owl_message_set_instance(&m, "tester");
244  owl_message_set_sender(&m, "owl-user");
245  owl_message_set_recipient(&m, "joe");
246  owl_message_set_attribute(&m, "foo", "bar");
247
248#define TEST_FILTER(f, e) do {                          \
249    numtests++;                                         \
250    numfailed += owl_filter_test_string(f, &m, e);      \
251      } while(0)
252
253  TEST_FILTER("true", 1);
254  TEST_FILTER("false", 0);
255  TEST_FILTER("( true )", 1);
256  TEST_FILTER("not false", 1);
257  TEST_FILTER("( true ) or ( false )", 1);
258  TEST_FILTER("true and false", 0);
259  TEST_FILTER("( true or true ) or ( ( false ) )", 1);
260
261  TEST_FILTER("class owl", 1);
262  TEST_FILTER("class ^owl$", 1);
263  TEST_FILTER("instance test", 1);
264  TEST_FILTER("instance ^test$", 0);
265  TEST_FILTER("instance ^tester$", 1);
266
267  TEST_FILTER("foo bar", 1);
268  TEST_FILTER("class owl and instance tester", 1);
269  TEST_FILTER("type ^zephyr$ and direction ^in$ and ( class ^owl$ or instance ^owl$ )", 1);
270
271  /* Order of operations and precedence */
272  TEST_FILTER("not true or false", 0);
273  TEST_FILTER("true or true and false", 0);
274  TEST_FILTER("true and true and false or true", 1);
275  TEST_FILTER("false and false or true", 1);
276  TEST_FILTER("true and false or false", 0);
277
278  f1 = owl_filter_new_fromstring("f1", "class owl");
279  owl_global_add_filter(&g, f1);
280  TEST_FILTER("filter f1", 1);
281  owl_global_remove_filter(&g, "f1");
282
283  /* Test recursion prevention */
284  FAIL_UNLESS("self reference", (f2 = owl_filter_new_fromstring("test", "filter test")) == NULL);
285  owl_filter_delete(f2);
286
287  /* mutual recursion */
288  f3 = owl_filter_new_fromstring("f3", "filter f4");
289  owl_global_add_filter(&g, f3);
290  FAIL_UNLESS("mutual recursion", (f4 = owl_filter_new_fromstring("f4", "filter f3")) == NULL);
291  owl_global_remove_filter(&g, "f3");
292  owl_filter_delete(f4);
293
294  /* support referencing a filter several times */
295  FAIL_UNLESS("DAG", (f5 = owl_filter_new_fromstring("dag", "filter f1 or filter f1")) != NULL);
296  owl_filter_delete(f5);
297
298  return 0;
299}
300
301
302int owl_obarray_regtest(void) {
303  int numfailed = 0;
304  const char *p,*p2;
305
306  owl_obarray oa;
307  owl_obarray_init(&oa);
308
309  printf("# BEGIN testing owl_obarray\n");
310
311  p = owl_obarray_insert(&oa, "test");
312  FAIL_UNLESS("returned string is equal", p && !strcmp(p, "test"));
313  p2 = owl_obarray_insert(&oa, "test");
314  FAIL_UNLESS("returned string is equal", p2 && !strcmp(p2, "test"));
315  FAIL_UNLESS("returned the same string", p2 && p == p2);
316
317  p = owl_obarray_insert(&oa, "test2");
318  FAIL_UNLESS("returned string is equal", p && !strcmp(p, "test2"));
319  p2 = owl_obarray_find(&oa, "test2");
320  FAIL_UNLESS("returned the same string", p2 && !strcmp(p2, "test2"));
321
322  p = owl_obarray_find(&oa, "nothere");
323  FAIL_UNLESS("Didn't find a string that isn't there", p == NULL);
324
325  printf("# END testing owl_obarray (%d failures)\n", numfailed);
326
327  return numfailed;
328}
329
330int owl_editwin_regtest(void) {
331  int numfailed = 0;
332  const char *p;
333
334  printf("# BEGIN testing owl_editwin\n");
335
336  owl_editwin *oe;
337  oe = owl_editwin_allocate();
338  owl_editwin_init(oe, NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
339
340  /* TODO: make the strings a little more lenient w.r.t trailing whitespace */
341
342  /* check paragraph fill */
343  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah.\n\nblah");
344  owl_editwin_move_to_top(oe);
345  owl_editwin_fill_paragraph(oe);
346  p = owl_editwin_get_text(oe);
347  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
348                                                            "blah blah blah.\n"
349                                                            "\n"
350                                                            "blah"));
351
352  /* check that lines ending with ". " correctly fill */
353  owl_editwin_fullclear(oe);
354  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah. \n\nblah");
355  owl_editwin_move_to_top(oe);
356  owl_editwin_fill_paragraph(oe);
357  p = owl_editwin_get_text(oe);
358  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
359                                                            "blah blah blah. \n"
360                                                            "\n"
361                                                            "blah"));
362
363  owl_editwin_free(oe);
364
365  printf("# END testing owl_editwin (%d failures)\n", numfailed);
366
367  return numfailed;
368}
369
370#ifdef OWL_ENABLE_ZCRYPT
371int owl_zcrypt_regtest(void)
372{
373  int numfailed = 0;
374  char *encrypted, *decrypted;
375
376  printf("# BEGIN testing owl_zcrypt\n");
377
378  encrypted = owl_zcrypt_encrypt_with_key("Hello, world!", "seekritkey");
379  FAIL_UNLESS("zcrypt encrypt", strcmp(encrypted, "TLJMKQSIGKSJRIJRSIIPIJFFULKRJSPK") == 0);
380  decrypted = owl_zcrypt_decrypt_with_key(encrypted, "seekritkey");
381  FAIL_UNLESS("zcrypt decrypt", strcmp(decrypted, "Hello, world!") == 0);
382  owl_free(decrypted);
383  owl_free(encrypted);
384
385  printf("# END testing owl_zcrypt (%d failures)\n", numfailed);
386
387  return numfailed;
388}
389#endif
Note: See TracBrowser for help on using the repository browser.