source: tester.c @ 34132f7

release-1.8
Last change on this file since 34132f7 was 34132f7, checked in by David Benjamin <davidben@mit.edu>, 12 years ago
Rewrite call_filter to use g_spawn_async_with_pipes This simplifies the error-handling code. Also fixes a bug where file descriptors get double-closed in call_filter. Also adds a unit test. The separate prog argument is removed to avoid having to deal with G_SPAWN_FILE_AND_ARGV_ZERO, and since we don't really use it anyway.
  • Property mode set to 100644
File size: 34.5 KB
Line 
1#define OWL_PERL
2#define WINDOW FAKE_WINDOW
3#include "owl.h"
4#undef WINDOW
5#include "filterproc.h"
6
7#include <unistd.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#undef instr
13#include <ncursesw/curses.h>
14
15owl_global g;
16
17int numtests;
18
19int owl_regtest(void);
20int owl_util_regtest(void);
21int owl_dict_regtest(void);
22int owl_variable_regtest(void);
23int owl_filter_regtest(void);
24int owl_obarray_regtest(void);
25int owl_editwin_regtest(void);
26int owl_fmtext_regtest(void);
27int owl_smartfilter_regtest(void);
28int owl_history_regtest(void);
29int call_filter_regtest(void);
30
31extern void owl_perl_xs_init(pTHX);
32
33int main(int argc, char **argv, char **env)
34{
35  FILE *rnull;
36  FILE *wnull;
37  char *perlerr;
38  int status = 0;
39  SCREEN *screen;
40
41  if (argc <= 1) {
42    fprintf(stderr, "Usage: %s --builtin|TEST.t|-le CODE\n", argv[0]);
43    return 1;
44  }
45
46  /* initialize a fake ncurses, detached from std{in,out} */
47  wnull = fopen("/dev/null", "w");
48  rnull = fopen("/dev/null", "r");
49  screen = newterm("xterm", wnull, rnull);
50  /* initialize global structures */
51  owl_global_init(&g);
52
53  perlerr = owl_perlconfig_initperl(NULL, &argc, &argv, &env);
54  if (perlerr) {
55    endwin();
56    fprintf(stderr, "Internal perl error: %s\n", perlerr);
57    status = 1;
58    goto out;
59  }
60
61  owl_global_complete_setup(&g);
62  owl_global_setup_default_filters(&g);
63
64  owl_view_create(owl_global_get_current_view(&g), "main",
65                  owl_global_get_filter(&g, "all"),
66                  owl_global_get_style_by_name(&g, "default"));
67
68  owl_function_firstmsg();
69
70  ENTER;
71  SAVETMPS;
72
73  if (strcmp(argv[1], "--builtin") == 0) {
74    status = owl_regtest();
75  } else if (strcmp(argv[1], "-le") == 0 && argc > 2) {
76    /*
77     * 'prove' runs its harness perl with '-le CODE' to get some
78     * information out.
79     */
80    moreswitches("l");
81    eval_pv(argv[2], true);
82  } else {
83    sv_setpv(get_sv("0", false), argv[1]);
84    sv_setpv(get_sv("main::test_prog", TRUE), argv[1]);
85
86    eval_pv("do $main::test_prog; die($@) if($@)", true);
87  }
88
89  status = 0;
90
91  FREETMPS;
92  LEAVE;
93
94 out:
95  perl_destruct(owl_global_get_perlinterp(&g));
96  perl_free(owl_global_get_perlinterp(&g));
97  /* probably not necessary, but tear down the screen */
98  endwin();
99  delscreen(screen);
100  fclose(rnull);
101  fclose(wnull);
102  return status;
103}
104
105int owl_regtest(void) {
106  numtests = 0;
107  int numfailures=0;
108  /*
109    printf("1..%d\n", OWL_UTIL_NTESTS+OWL_DICT_NTESTS+OWL_VARIABLE_NTESTS
110    +OWL_FILTER_NTESTS+OWL_OBARRAY_NTESTS);
111  */
112  numfailures += owl_util_regtest();
113  numfailures += owl_dict_regtest();
114  numfailures += owl_variable_regtest();
115  numfailures += owl_filter_regtest();
116  numfailures += owl_editwin_regtest();
117  numfailures += owl_fmtext_regtest();
118  numfailures += owl_smartfilter_regtest();
119  numfailures += owl_history_regtest();
120  numfailures += call_filter_regtest();
121  if (numfailures) {
122      fprintf(stderr, "# *** WARNING: %d failures total\n", numfailures);
123  }
124  printf("1..%d\n", numtests);
125
126  return(numfailures);
127}
128
129#define FAIL_UNLESS(desc,pred) do { int __pred = (pred);                \
130    numtests++;                                                         \
131    printf("%s %d %s", (__pred) ? "ok" : (numfailed++, "not ok"), numtests, desc); \
132    if(!(__pred)) printf("\t(%s:%d)", __FILE__, __LINE__); printf("%c", '\n'); } while(0)
133
134
135int owl_util_regtest(void)
136{
137  int numfailed=0;
138  char *s, *path, *home;
139
140  printf("# BEGIN testing owl_util\n");
141
142#define CHECK_STR_AND_FREE(desc, expected, expr)         \
143    do {                                                 \
144      char *__value = (expr);                            \
145      FAIL_UNLESS((desc), !strcmp((expected), __value)); \
146      g_free(__value);                                 \
147    } while (0)
148
149  CHECK_STR_AND_FREE("owl_text_substitute 2", "fYZYZ",
150                     owl_text_substitute("foo", "o", "YZ"));
151  CHECK_STR_AND_FREE("owl_text_substitute 3", "foo",
152                     owl_text_substitute("fYZYZ", "YZ", "o"));
153  CHECK_STR_AND_FREE("owl_text_substitute 4", "/u/foo/meep",
154                     owl_text_substitute("~/meep", "~", "/u/foo"));
155
156  FAIL_UNLESS("skiptokens 1", 
157              !strcmp("bar quux", skiptokens("foo bar quux", 1)));
158  FAIL_UNLESS("skiptokens 2", 
159              !strcmp("meep", skiptokens("foo 'bar quux' meep", 2)));
160
161  CHECK_STR_AND_FREE("expand_tabs 1", "        hi", owl_text_expand_tabs("\thi"));
162
163  CHECK_STR_AND_FREE("expand_tabs 2", "        hi\nword    tab",
164                     owl_text_expand_tabs("\thi\nword\ttab"));
165
166  CHECK_STR_AND_FREE("expand_tabs 3", "                2 tabs",
167                     owl_text_expand_tabs("\t\t2 tabs"));
168  CHECK_STR_AND_FREE("expand_tabs 4", "α       ααααααα!        ",
169                     owl_text_expand_tabs(\tααααααα!\t"));
170  CHECK_STR_AND_FREE("expand_tabs 5", "A      AAA!!        ",
171                     owl_text_expand_tabs("A\tAAA!!\t"));
172
173  FAIL_UNLESS("skiptokens 1",
174              !strcmp("world", skiptokens("hello world", 1)));
175
176  FAIL_UNLESS("skiptokens 2",
177              !strcmp("c d e", skiptokens("a   b c d e", 2)));
178
179  FAIL_UNLESS("skiptokens 3",
180              !strcmp("\"b\" c d e", skiptokens("a \"b\" c d e", 1)));
181
182  FAIL_UNLESS("skiptokens 4",
183              !strcmp("c d e", skiptokens("a \"b\" c d e", 2)));
184
185  FAIL_UNLESS("skiptokens 5",
186              !strcmp("c d e", skiptokens("a \"'\" c d e", 2)));
187
188#define CHECK_QUOTING(desc, unquoted, quoted)           \
189  do {                                                  \
190      int __argc;                                       \
191      char *__quoted = owl_arg_quote(unquoted);         \
192      char **__argv;                                    \
193      FAIL_UNLESS(desc, !strcmp(quoted, __quoted));     \
194      __argv = owl_parseline(__quoted, &__argc);        \
195      FAIL_UNLESS(desc " - arg count", __argc == 1);    \
196      FAIL_UNLESS(desc " - null-terminated",            \
197                  __argv[__argc] == NULL);              \
198      FAIL_UNLESS(desc " - parsed",                     \
199                  !strcmp(__argv[0], unquoted));        \
200      g_strfreev(__argv);                               \
201      g_free(__quoted);                         \
202    } while (0)
203
204  CHECK_QUOTING("boring text", "mango", "mango");
205  CHECK_QUOTING("spaces", "mangos are tasty", "'mangos are tasty'");
206  CHECK_QUOTING("single quotes", "mango's", "\"mango's\"");
207  CHECK_QUOTING("double quotes", "he said \"mangos are tasty\"",
208                "'he said \"mangos are tasty\"'");
209  CHECK_QUOTING("both quotes",
210                "he said \"mango's are tasty even when you put in "
211                "a random apostrophe\"",
212                "\"he said \"'\"'\"mango's are tasty even when you put in "
213                "a random apostrophe\"'\"'\"\"");
214  CHECK_QUOTING("quote monster", "'\"\"'\"'''\"",
215                "\""
216                "'"
217                "\"'\"'\""
218                "\"'\"'\""
219                "'"
220                "\"'\"'\""
221                "'"
222                "'"
223                "'"
224                "\"'\"'\""
225                "\"");
226
227  GString *quoted = g_string_new("");
228  owl_string_appendf_quoted(quoted, "%q foo %q%q %s %", "hello", "world is", "can't");
229  FAIL_UNLESS("owl_string_appendf",
230              !strcmp(quoted->str, "hello foo 'world is'\"can't\" %s %"));
231  g_string_free(quoted, true);
232
233
234  s = owl_util_baseclass("barnowl");
235  FAIL_UNLESS("baseclass barnowl", !strcmp("barnowl", s));
236  g_free(s);
237  s = owl_util_baseclass("unbarnowl");
238  FAIL_UNLESS("baseclass unbarnowl", !strcmp("barnowl", s));
239  g_free(s);
240  s = owl_util_baseclass("unununbarnowl.d.d");
241  FAIL_UNLESS("baseclass unununbarnowl.d.d", !strcmp("barnowl", s));
242  g_free(s);
243  s = owl_util_baseclass("ununun.d.d");
244  FAIL_UNLESS("baseclass ununun.d.d", !strcmp("", s));
245  g_free(s);
246  s = owl_util_baseclass("d.d.d.d");
247  FAIL_UNLESS("baseclass d.d.d.d", !strcmp("d", s));
248  g_free(s);
249  s = owl_util_baseclass("n.d.d.d");
250  FAIL_UNLESS("baseclass n.d.d.d", !strcmp("n", s));
251  g_free(s);
252  s = owl_util_baseclass("ununun.");
253  FAIL_UNLESS("baseclass ununun.", !strcmp(".", s));
254  g_free(s);
255  s = owl_util_baseclass("unununu");
256  FAIL_UNLESS("baseclass unununu", !strcmp("u", s));
257  g_free(s);
258
259
260  s = owl_util_makepath("foo/bar");
261  FAIL_UNLESS("makepath foo/bar", !strcmp("foo/bar", s));
262  g_free(s);
263  s = owl_util_makepath("//foo///bar");
264  FAIL_UNLESS("makepath //foo///bar", !strcmp("/foo/bar", s));
265  g_free(s);
266  s = owl_util_makepath("foo/~//bar/");
267  FAIL_UNLESS("makepath foo/~//bar/", !strcmp("foo/~/bar/", s));
268  g_free(s);
269  s = owl_util_makepath("~thisuserhadreallybetternotexist/foobar/");
270  FAIL_UNLESS("makepath ~thisuserhadreallybetternotexist/foobar/",
271              !strcmp("~thisuserhadreallybetternotexist/foobar/", s));
272  g_free(s);
273
274  home = g_strdup(owl_global_get_homedir(&g));
275  s = owl_util_makepath("~");
276  FAIL_UNLESS("makepath ~", !strcmp(home, s));
277  g_free(s);
278
279  path = g_build_filename(home, "foo/bar/baz", NULL);
280  s = owl_util_makepath("~///foo/bar//baz");
281  FAIL_UNLESS("makepath ~///foo/bar//baz", !strcmp(path, s));
282  g_free(s);
283  g_free(path);
284  g_free(home);
285
286  home = owl_util_homedir_for_user("root");
287  if (home == NULL) {
288    /* Just make some noise so we notice. */
289    home = g_strdup("<WHAT>");
290    fprintf(stderr, "owl_util_homedir_for_user failed");
291  }
292
293  s = owl_util_makepath("~root");
294  FAIL_UNLESS("makepath ~root", !strcmp(home, s));
295  g_free(s);
296
297  path = g_build_filename(home, "foo/bar/baz", NULL);
298  s = owl_util_makepath("~root///foo/bar//baz");
299  FAIL_UNLESS("makepath ~root///foo/bar//baz", !strcmp(path, s));
300  g_free(s);
301  g_free(path);
302  g_free(home);
303
304  /* if (numfailed) printf("*** WARNING: failures encountered with owl_util\n"); */
305  printf("# END testing owl_util (%d failures)\n", numfailed);
306  return(numfailed);
307}
308
309int owl_dict_regtest(void) {
310  owl_dict d;
311  GPtrArray *l;
312  int numfailed=0;
313  char *av = g_strdup("aval"), *bv = g_strdup("bval"), *cv = g_strdup("cval"),
314    *dv = g_strdup("dval");
315
316  printf("# BEGIN testing owl_dict\n");
317  owl_dict_create(&d);
318  FAIL_UNLESS("insert b", 0==owl_dict_insert_element(&d, "b", bv, owl_dict_noop_delete));
319  FAIL_UNLESS("insert d", 0==owl_dict_insert_element(&d, "d", dv, owl_dict_noop_delete));
320  FAIL_UNLESS("insert a", 0==owl_dict_insert_element(&d, "a", av, owl_dict_noop_delete));
321  FAIL_UNLESS("insert c", 0==owl_dict_insert_element(&d, "c", cv, owl_dict_noop_delete));
322  FAIL_UNLESS("reinsert d (no replace)", -2==owl_dict_insert_element(&d, "d", dv, 0));
323  FAIL_UNLESS("find a", av==owl_dict_find_element(&d, "a"));
324  FAIL_UNLESS("find b", bv==owl_dict_find_element(&d, "b"));
325  FAIL_UNLESS("find c", cv==owl_dict_find_element(&d, "c"));
326  FAIL_UNLESS("find d", dv==owl_dict_find_element(&d, "d"));
327  FAIL_UNLESS("find e (non-existent)", NULL==owl_dict_find_element(&d, "e"));
328  FAIL_UNLESS("remove d", dv==owl_dict_remove_element(&d, "d"));
329  FAIL_UNLESS("find d (post-removal)", NULL==owl_dict_find_element(&d, "d"));
330
331  FAIL_UNLESS("get_size", 3==owl_dict_get_size(&d));
332  l = owl_dict_get_keys(&d);
333  FAIL_UNLESS("get_keys result size", 3 == l->len);
334 
335  /* these assume the returned keys are sorted */
336  FAIL_UNLESS("get_keys result val", 0 == strcmp("a", l->pdata[0]));
337  FAIL_UNLESS("get_keys result val", 0 == strcmp("b", l->pdata[1]));
338  FAIL_UNLESS("get_keys result val", 0 == strcmp("c", l->pdata[2]));
339
340  owl_ptr_array_free(l, g_free);
341  owl_dict_cleanup(&d, NULL);
342
343  g_free(av);
344  g_free(bv);
345  g_free(cv);
346  g_free(dv);
347
348  /*  if (numfailed) printf("*** WARNING: failures encountered with owl_dict\n"); */
349  printf("# END testing owl_dict (%d failures)\n", numfailed);
350  return(numfailed);
351}
352
353int owl_variable_regtest(void) {
354  owl_vardict vd;
355  owl_variable *var;
356  int numfailed=0;
357  char *value;
358  const void *v;
359
360  printf("# BEGIN testing owl_variable\n");
361  FAIL_UNLESS("setup", 0==owl_variable_dict_setup(&vd));
362
363  FAIL_UNLESS("get bool var", NULL != (var = owl_variable_get_var(&vd, "rxping")));
364  FAIL_UNLESS("get bool", 0 == owl_variable_get_bool(var));
365  FAIL_UNLESS("get bool (no such)", NULL == owl_variable_get_var(&vd, "mumble"));
366  FAIL_UNLESS("get bool as string",
367              !strcmp((value = owl_variable_get_tostring(var)), "off"));
368  g_free(value);
369  FAIL_UNLESS("set bool 1", 0 == owl_variable_set_bool_on(var));
370  FAIL_UNLESS("get bool 2", 1 == owl_variable_get_bool(var));
371  FAIL_UNLESS("set bool 3", 0 == owl_variable_set_fromstring(var, "off", 0));
372  FAIL_UNLESS("get bool 4", 0 == owl_variable_get_bool(var));
373  FAIL_UNLESS("set bool 5", -1 == owl_variable_set_fromstring(var, "xxx", 0));
374  FAIL_UNLESS("get bool 6", 0 == owl_variable_get_bool(var));
375
376
377  FAIL_UNLESS("get string var", NULL != (var = owl_variable_get_var(&vd, "logpath")));
378  FAIL_UNLESS("get string", 0 == strcmp("~/zlog/people", owl_variable_get_string(var)));
379  FAIL_UNLESS("set string 7", 0 == owl_variable_set_string(var, "whee"));
380  FAIL_UNLESS("get string", !strcmp("whee", owl_variable_get_string(var)));
381
382  FAIL_UNLESS("get int var", NULL != (var = owl_variable_get_var(&vd, "typewinsize")));
383  FAIL_UNLESS("get int", 8 == owl_variable_get_int(var));
384  FAIL_UNLESS("get int (no such)", NULL == owl_variable_get_var(&vd, "mumble"));
385  FAIL_UNLESS("get int as string",
386              !strcmp((value = owl_variable_get_tostring(var)), "8"));
387  g_free(value);
388  FAIL_UNLESS("set int 1", 0 == owl_variable_set_int(var, 12));
389  FAIL_UNLESS("get int 2", 12 == owl_variable_get_int(var));
390  FAIL_UNLESS("set int 1b", -1 == owl_variable_set_int(var, -3));
391  FAIL_UNLESS("get int 2b", 12 == owl_variable_get_int(var));
392  FAIL_UNLESS("set int 3", 0 == owl_variable_set_fromstring(var, "9", 0));
393  FAIL_UNLESS("get int 4", 9 == owl_variable_get_int(var));
394  FAIL_UNLESS("set int 5", -1 == owl_variable_set_fromstring(var, "xxx", 0));
395  FAIL_UNLESS("set int 6", -1 == owl_variable_set_fromstring(var, "", 0));
396  FAIL_UNLESS("get int 7", 9 == owl_variable_get_int(var));
397
398  owl_variable_dict_newvar_string(&vd, "stringvar", "", "", "testval");
399  FAIL_UNLESS("get new string var", NULL != (var = owl_variable_get_var(&vd, "stringvar")));
400  FAIL_UNLESS("get new string var", NULL != (v = owl_variable_get(var)));
401  FAIL_UNLESS("get new string val", !strcmp("testval", owl_variable_get_string(var)));
402  owl_variable_set_string(var, "new val");
403  FAIL_UNLESS("update string val", !strcmp("new val", owl_variable_get_string(var)));
404
405  owl_variable_dict_newvar_int(&vd, "intvar", "", "", 47);
406  FAIL_UNLESS("get new int var", NULL != (var = owl_variable_get_var(&vd, "intvar")));
407  FAIL_UNLESS("get new int var", NULL != (v = owl_variable_get(var)));
408  FAIL_UNLESS("get new int val", 47 == owl_variable_get_int(var));
409  owl_variable_set_int(var, 17);
410  FAIL_UNLESS("update int val", 17 == owl_variable_get_int(var));
411
412  owl_variable_dict_newvar_bool(&vd, "boolvar", "", "", 1);
413  FAIL_UNLESS("get new bool var", NULL != (var = owl_variable_get_var(&vd, "boolvar")));
414  FAIL_UNLESS("get new bool var", NULL != (v = owl_variable_get(var)));
415  FAIL_UNLESS("get new bool val", owl_variable_get_bool(var));
416  owl_variable_set_bool_off(var);
417  FAIL_UNLESS("update bool val", !owl_variable_get_bool(var));
418
419  owl_variable_dict_newvar_string(&vd, "nullstringvar", "", "", NULL);
420  FAIL_UNLESS("get new string (NULL) var", NULL != (var = owl_variable_get_var(&vd, "nullstringvar")));
421  FAIL_UNLESS("get string (NULL)", NULL == (value = owl_variable_get_tostring(var)));
422  g_free(value);
423  var = owl_variable_get_var(&vd, "zsigproc");
424  FAIL_UNLESS("get string (NULL) 2", NULL == (value = owl_variable_get_tostring(var)));
425  g_free(value);
426
427  owl_variable_dict_cleanup(&vd);
428
429  /* if (numfailed) printf("*** WARNING: failures encountered with owl_variable\n"); */
430  printf("# END testing owl_variable (%d failures)\n", numfailed);
431  return(numfailed);
432}
433
434static int owl_filter_test_string(const char *filt, const owl_message *m, int shouldmatch)
435{
436  owl_filter *f;
437  int ok;
438  int failed = 0;
439  if ((f = owl_filter_new_fromstring("test-filter", filt)) == NULL) {
440    printf("not ok can't parse %s\n", filt);
441    failed = 1;
442    goto out;
443  }
444  ok = owl_filter_message_match(f, m);
445  if((shouldmatch && !ok) || (!shouldmatch && ok)) {
446    printf("not ok match %s (got %d, expected %d)\n", filt, ok, shouldmatch);
447    failed = 1;
448  }
449 out:
450  owl_filter_delete(f);
451  if(!failed) {
452    printf("ok %s %s\n", shouldmatch ? "matches" : "doesn't match", filt);
453  }
454  return failed;
455}
456
457int owl_filter_regtest(void) {
458  int numfailed=0;
459  owl_message m;
460  owl_filter *f1, *f2, *f3, *f4, *f5;
461
462  owl_message_init(&m);
463  owl_message_set_type_zephyr(&m);
464  owl_message_set_direction_in(&m);
465  owl_message_set_class(&m, "owl");
466  owl_message_set_instance(&m, "tester");
467  owl_message_set_sender(&m, "owl-user");
468  owl_message_set_recipient(&m, "joe");
469  owl_message_set_attribute(&m, "foo", "bar");
470
471#define TEST_FILTER(f, e) do {                          \
472    numtests++;                                         \
473    numfailed += owl_filter_test_string(f, &m, e);      \
474      } while(0)
475
476  TEST_FILTER("true", 1);
477  TEST_FILTER("false", 0);
478  TEST_FILTER("( true )", 1);
479  TEST_FILTER("not false", 1);
480  TEST_FILTER("( true ) or ( false )", 1);
481  TEST_FILTER("true and false", 0);
482  TEST_FILTER("( true or true ) or ( ( false ) )", 1);
483
484  TEST_FILTER("class owl", 1);
485  TEST_FILTER("class ^owl$", 1);
486  TEST_FILTER("instance test", 1);
487  TEST_FILTER("instance ^test$", 0);
488  TEST_FILTER("instance ^tester$", 1);
489
490  TEST_FILTER("foo bar", 1);
491  TEST_FILTER("class owl and instance tester", 1);
492  TEST_FILTER("type ^zephyr$ and direction ^in$ and ( class ^owl$ or instance ^owl$ )", 1);
493
494  /* Order of operations and precedence */
495  TEST_FILTER("not true or false", 0);
496  TEST_FILTER("true or true and false", 0);
497  TEST_FILTER("true and true and false or true", 1);
498  TEST_FILTER("false and false or true", 1);
499  TEST_FILTER("true and false or false", 0);
500
501  f1 = owl_filter_new_fromstring("f1", "class owl");
502  owl_global_add_filter(&g, f1);
503  TEST_FILTER("filter f1", 1);
504  owl_global_remove_filter(&g, "f1");
505
506  /* Test recursion prevention */
507  FAIL_UNLESS("self reference", (f2 = owl_filter_new_fromstring("test", "filter test")) == NULL);
508  owl_filter_delete(f2);
509
510  /* mutual recursion */
511  f3 = owl_filter_new_fromstring("f3", "filter f4");
512  owl_global_add_filter(&g, f3);
513  FAIL_UNLESS("mutual recursion", (f4 = owl_filter_new_fromstring("f4", "filter f3")) == NULL);
514  owl_global_remove_filter(&g, "f3");
515  owl_filter_delete(f4);
516
517  /* support referencing a filter several times */
518  FAIL_UNLESS("DAG", (f5 = owl_filter_new_fromstring("dag", "filter f1 or filter f1")) != NULL);
519  owl_filter_delete(f5);
520
521  return 0;
522}
523
524int owl_editwin_regtest(void) {
525  int numfailed = 0;
526  const char *p;
527  owl_editwin *oe;
528  const char *autowrap_string = "we feel our owls should live "
529                                "closer to our ponies.";
530
531  printf("# BEGIN testing owl_editwin\n");
532
533  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
534
535  /* TODO: make the strings a little more lenient w.r.t trailing whitespace */
536
537  /* check paragraph fill */
538  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah.\n\nblah");
539  owl_editwin_move_to_top(oe);
540  owl_editwin_fill_paragraph(oe);
541  p = owl_editwin_get_text(oe);
542  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
543                                                            "blah blah blah.\n"
544                                                            "\n"
545                                                            "blah"));
546
547  owl_editwin_unref(oe); oe = NULL;
548  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
549
550  /* check that lines ending with ". " correctly fill */
551  owl_editwin_insert_string(oe, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah. \n\nblah");
552  owl_editwin_move_to_top(oe);
553  owl_editwin_fill_paragraph(oe);
554  p = owl_editwin_get_text(oe);
555  FAIL_UNLESS("text was correctly wrapped", p && !strcmp(p, "blah blah blah blah blah blah blah blah blah blah blah blah blah blah\n"
556                                                            "blah blah blah. \n"
557                                                            "\n"
558                                                            "blah"));
559
560  owl_editwin_unref(oe); oe = NULL;
561
562  /* Test owl_editwin_move_to_beginning_of_line. */
563  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
564  owl_editwin_insert_string(oe, "\n");
565  owl_editwin_insert_string(oe, "12345678\n");
566  owl_editwin_insert_string(oe, "\n");
567  owl_editwin_insert_string(oe, "abcdefg\n");
568  owl_editwin_move_to_top(oe);
569  FAIL_UNLESS("already at beginning of line",
570              owl_editwin_move_to_beginning_of_line(oe) == 0);
571  owl_editwin_line_move(oe, 1);
572  owl_editwin_point_move(oe, 5);
573  FAIL_UNLESS("find beginning of line after empty first line",
574              owl_editwin_move_to_beginning_of_line(oe) == -5);
575  owl_editwin_line_move(oe, 1);
576  FAIL_UNLESS("find beginning empty middle line",
577              owl_editwin_move_to_beginning_of_line(oe) == 0);
578  owl_editwin_line_move(oe, 1);
579  owl_editwin_point_move(oe, 2);
580  FAIL_UNLESS("find beginning of line after empty middle line",
581              owl_editwin_move_to_beginning_of_line(oe) == -2);
582  owl_editwin_unref(oe); oe = NULL;
583
584  /* Test automatic line-wrapping. */
585  owl_global_set_edit_maxwrapcols(&g, 10);
586  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
587  for (p = autowrap_string; *p; p++) {
588    owl_input j;
589    j.ch = *p;
590    j.uch = *p; /* Assuming ASCII. */
591    owl_editwin_process_char(oe, j);
592  }
593  p = owl_editwin_get_text(oe);
594  FAIL_UNLESS("text was automatically wrapped",
595              p && !strcmp(p, "we feel\n"
596                           "our owls\n"
597                           "should\n"
598                           "live\n"
599                           "closer to\n"
600                           "our\n"
601                           "ponies."));
602  owl_editwin_unref(oe); oe = NULL;
603  owl_global_set_edit_maxwrapcols(&g, 70);
604
605  /* Test owl_editwin_current_column. */
606  oe = owl_editwin_new(NULL, 80, 80, OWL_EDITWIN_STYLE_MULTILINE, NULL);
607  FAIL_UNLESS("initial column zero", owl_editwin_current_column(oe) == 0);
608  owl_editwin_insert_string(oe, "abcdef");
609  FAIL_UNLESS("simple insert", owl_editwin_current_column(oe) == 6);
610  owl_editwin_insert_string(oe, "\t");
611  FAIL_UNLESS("insert tabs", owl_editwin_current_column(oe) == 8);
612  owl_editwin_insert_string(oe, "123\n12\t3");
613  FAIL_UNLESS("newline with junk", owl_editwin_current_column(oe) == 9);
614  owl_editwin_move_to_beginning_of_line(oe);
615  FAIL_UNLESS("beginning of line", owl_editwin_current_column(oe) == 0);
616  owl_editwin_unref(oe); oe = NULL;
617
618  printf("# END testing owl_editwin (%d failures)\n", numfailed);
619
620  return numfailed;
621}
622
623int owl_fmtext_regtest(void) {
624  int numfailed = 0;
625  int start, end;
626  owl_fmtext fm1;
627  owl_fmtext fm2;
628  owl_regex re;
629  char *str;
630
631  printf("# BEGIN testing owl_fmtext\n");
632
633  owl_fmtext_init_null(&fm1);
634  owl_fmtext_init_null(&fm2);
635
636  /* Verify text gets correctly appended. */
637  owl_fmtext_append_normal(&fm1, "1234567898");
638  owl_fmtext_append_fmtext(&fm2, &fm1);
639  FAIL_UNLESS("string lengths correct",
640              owl_fmtext_num_bytes(&fm2) == strlen(owl_fmtext_get_text(&fm2)));
641
642  /* Test owl_fmtext_num_lines. */
643  owl_fmtext_clear(&fm1);
644  FAIL_UNLESS("empty line correct", owl_fmtext_num_lines(&fm1) == 0);
645  owl_fmtext_append_normal(&fm1, "12345\n67898");
646  FAIL_UNLESS("trailing chars correct", owl_fmtext_num_lines(&fm1) == 2);
647  owl_fmtext_append_normal(&fm1, "\n");
648  FAIL_UNLESS("trailing newline correct", owl_fmtext_num_lines(&fm1) == 2);
649  owl_fmtext_append_bold(&fm1, "");
650  FAIL_UNLESS("trailing attributes correct", owl_fmtext_num_lines(&fm1) == 2);
651
652  /* Test owl_fmtext_truncate_lines */
653  owl_fmtext_clear(&fm1);
654  owl_fmtext_append_normal(&fm1, "0\n1\n2\n3\n4\n");
655
656  owl_fmtext_clear(&fm2);
657  owl_fmtext_truncate_lines(&fm1, 1, 3, &fm2);
658  str = owl_fmtext_print_plain(&fm2);
659  FAIL_UNLESS("lines corrected truncated",
660              str && !strcmp(str, "1\n2\n3\n"));
661  g_free(str);
662
663  owl_fmtext_clear(&fm2);
664  owl_fmtext_truncate_lines(&fm1, 1, 5, &fm2);
665  str = owl_fmtext_print_plain(&fm2);
666  FAIL_UNLESS("lines corrected truncated",
667              str && !strcmp(str, "1\n2\n3\n4\n"));
668  g_free(str);
669
670  /* Test owl_fmtext_truncate_cols. */
671  owl_fmtext_clear(&fm1);
672  owl_fmtext_append_normal(&fm1, "123456789012345\n");
673  owl_fmtext_append_normal(&fm1, "123456789\n");
674  owl_fmtext_append_normal(&fm1, "1234567890\n");
675
676  owl_fmtext_clear(&fm2);
677  owl_fmtext_truncate_cols(&fm1, 4, 9, &fm2);
678  str = owl_fmtext_print_plain(&fm2);
679  FAIL_UNLESS("columns correctly truncated",
680              str && !strcmp(str, "567890"
681                                  "56789\n"
682                                  "567890"));
683  g_free(str);
684
685  owl_fmtext_clear(&fm1);
686  owl_fmtext_append_normal(&fm1, "12\t1234");
687  owl_fmtext_append_bold(&fm1, "56\n");
688  owl_fmtext_append_bold(&fm1, "12345678\t\n");
689
690  owl_fmtext_clear(&fm2);
691  owl_fmtext_truncate_cols(&fm1, 4, 13, &fm2);
692  str = owl_fmtext_print_plain(&fm2);
693  FAIL_UNLESS("columns correctly truncated",
694              str && !strcmp(str, "    123456"
695                                  "5678      "));
696  g_free(str);
697
698  /* Test owl_fmtext_expand_tabs. */
699  owl_fmtext_clear(&fm1);
700  owl_fmtext_append_normal(&fm1, "12\t1234");
701  owl_fmtext_append_bold(&fm1, "567\t1\n12345678\t1");
702  owl_fmtext_clear(&fm2);
703  owl_fmtext_expand_tabs(&fm1, &fm2, 0);
704  str = owl_fmtext_print_plain(&fm2);
705  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
706  FAIL_UNLESS("tabs corrected expanded",
707              str && !strcmp(str, "12      1234567 1\n"
708                                  "12345678        1"));
709  g_free(str);
710
711  owl_fmtext_clear(&fm2);
712  owl_fmtext_expand_tabs(&fm1, &fm2, 1);
713  str = owl_fmtext_print_plain(&fm2);
714  FAIL_UNLESS("no tabs remaining", strchr(str, '\t') == NULL);
715  FAIL_UNLESS("tabs corrected expanded",
716              str && !strcmp(str, "12     1234567 1\n"
717                                  "12345678       1"));
718  g_free(str);
719
720  /* Test owl_fmtext_search. */
721  owl_fmtext_clear(&fm1);
722  owl_fmtext_append_normal(&fm1, "123123123123");
723  owl_regex_create(&re, "12");
724  {
725    int count = 0, offset;
726    offset = owl_fmtext_search(&fm1, &re, 0);
727    while (offset >= 0) {
728      FAIL_UNLESS("search matches",
729                  !strncmp("12", owl_fmtext_get_text(&fm1) + offset, 2));
730      count++;
731      offset = owl_fmtext_search(&fm1, &re, offset+1);
732    }
733    FAIL_UNLESS("exactly four matches", count == 4);
734  }
735  owl_regex_cleanup(&re);
736
737  /* Test owl_fmtext_line_number. */
738  owl_fmtext_clear(&fm1);
739  owl_fmtext_append_normal(&fm1, "123\n456\n");
740  owl_fmtext_append_bold(&fm1, "");
741  FAIL_UNLESS("lines start at 0", 0 == owl_fmtext_line_number(&fm1, 0));
742  FAIL_UNLESS("trailing formatting characters part of false line",
743              2 == owl_fmtext_line_number(&fm1, owl_fmtext_num_bytes(&fm1)));
744  owl_regex_create_quoted(&re, "456");
745  FAIL_UNLESS("correctly find second line (line 1)",
746              1 == owl_fmtext_line_number(&fm1, owl_fmtext_search(&fm1, &re, 0)));
747  owl_regex_cleanup(&re);
748
749  /* Test owl_fmtext_line_extents. */
750  owl_fmtext_clear(&fm1);
751  owl_fmtext_append_normal(&fm1, "123\n456\n789");
752  owl_fmtext_line_extents(&fm1, 1, &start, &end);
753  FAIL_UNLESS("line contents",
754              !strncmp("456\n", owl_fmtext_get_text(&fm1)+start, end-start));
755  owl_fmtext_line_extents(&fm1, 2, &start, &end);
756  FAIL_UNLESS("point to end of buffer", end == owl_fmtext_num_bytes(&fm1));
757
758  owl_fmtext_cleanup(&fm1);
759  owl_fmtext_cleanup(&fm2);
760
761  printf("# END testing owl_fmtext (%d failures)\n", numfailed);
762
763  return numfailed;
764}
765
766static int owl_smartfilter_test_equals(const char *filtname, const char *expected) {
767  owl_filter *f = NULL;
768  char *filtstr = NULL;
769  int failed = 0;
770  f = owl_global_get_filter(&g, filtname);
771  if (f == NULL) {
772    printf("not ok filter missing: %s\n", filtname);
773    failed = 1;
774    goto out;
775  }
776
777  /* TODO: Come up with a better way to test this. */
778  filtstr = owl_filter_print(f);
779  if (strcmp(expected, filtstr)) {
780    printf("not ok filter incorrect: |%s| instead of |%s|\n",
781           filtstr, expected);
782    failed = 1;
783    goto out;
784  }
785
786 out:
787  g_free(filtstr);
788  return failed;
789}
790
791static int owl_classinstfilt_test(const char *c, const char *i, int related, const char *expected) {
792  char *filtname = NULL;
793  int failed = 0;
794
795  filtname = owl_function_classinstfilt(c, i, related);
796  if (filtname == NULL) {
797    printf("not ok null filtname: %s %s %s\n", c, i ? i : "(null)",
798           related ? "related" : "not related");
799    failed = 1;
800    goto out;
801  }
802  if (owl_smartfilter_test_equals(filtname, expected)) {
803    failed = 1;
804    goto out;
805  }
806 out:
807  if (!failed) {
808    printf("ok %s\n", filtname);
809  }
810  if (filtname)
811    owl_global_remove_filter(&g, filtname);
812  g_free(filtname);
813  return failed;
814}
815
816static int owl_zuserfilt_test(const char *longuser, const char *expected) {
817  char *filtname = NULL;
818  int failed = 0;
819
820  filtname = owl_function_zuserfilt(longuser);
821  if (filtname == NULL) {
822    printf("not ok null filtname: %s\n", longuser);
823    failed = 1;
824    goto out;
825  }
826  if (owl_smartfilter_test_equals(filtname, expected)) {
827    failed = 1;
828    goto out;
829  }
830 out:
831  if (!failed) {
832    printf("ok %s\n", filtname);
833  }
834  if (filtname)
835    owl_global_remove_filter(&g, filtname);
836  g_free(filtname);
837  return failed;
838}
839
840
841int owl_smartfilter_regtest(void) {
842  int numfailed = 0;
843
844  printf("# BEGIN testing owl_smartfilter\n");
845
846  /* Check classinst making. */
847
848#define TEST_CLASSINSTFILT(c, i, r, e) do {             \
849    numtests++;                                         \
850    numfailed += owl_classinstfilt_test(c, i, r, e);    \
851  } while (0)
852  TEST_CLASSINSTFILT("message", NULL, false,
853                     "class ^message$\n");
854  TEST_CLASSINSTFILT("message", NULL, true,
855                     "class ^(un)*message(\\.d)*$\n");
856  TEST_CLASSINSTFILT("message", "personal", false,
857                     "class ^message$ and instance ^personal$\n");
858  TEST_CLASSINSTFILT("message", "personal", true,
859                     "class ^(un)*message(\\.d)*$ and ( instance ^(un)*personal(\\.d)*$ )\n");
860
861  TEST_CLASSINSTFILT("message", "evil\tinstance", false,
862                     "class ^message$ and instance '^evil\tinstance$'\n");
863  TEST_CLASSINSTFILT("message", "evil instance", false,
864                     "class ^message$ and instance '^evil instance$'\n");
865  TEST_CLASSINSTFILT("message", "evil'instance", false,
866                     "class ^message$ and instance \"^evil'instance$\"\n");
867  TEST_CLASSINSTFILT("message", "evil\"instance", false,
868                     "class ^message$ and instance '^evil\"instance$'\n");
869  TEST_CLASSINSTFILT("message", "evil$instance", false,
870                     "class ^message$ and instance ^evil\\$instance$\n");
871
872#define TEST_ZUSERFILT(l, e) do {                       \
873    numtests++;                                         \
874    numfailed += owl_zuserfilt_test(l, e);              \
875  } while (0)
876  TEST_ZUSERFILT("user",
877                 "( type ^zephyr$ and filter personal and "
878                 "( ( direction ^in$ and sender "
879                 "^user$"
880                 " ) or ( direction ^out$ and recipient "
881                 "^user$"
882                 " ) ) ) or ( ( class ^login$ ) and ( sender "
883                 "^user$"
884                 " ) )\n");
885  TEST_ZUSERFILT("very evil\t.user",
886                 "( type ^zephyr$ and filter personal and "
887                 "( ( direction ^in$ and sender "
888                 "'^very evil\t\\.user$'"
889                 " ) or ( direction ^out$ and recipient "
890                 "'^very evil\t\\.user$'"
891                 " ) ) ) or ( ( class ^login$ ) and ( sender "
892                 "'^very evil\t\\.user$'"
893                 " ) )\n");
894
895  printf("# END testing owl_smartfilter (%d failures)\n", numfailed);
896
897  return numfailed;
898}
899
900int owl_history_regtest(void)
901{
902  int numfailed = 0;
903  int i;
904  owl_history h;
905
906  printf("# BEGIN testing owl_history\n");
907  owl_history_init(&h);
908
909  /* Operations on empty history. */
910  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
911  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
912  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
913
914  /* Insert a few records. */
915  owl_history_store(&h, "a", false);
916  owl_history_store(&h, "b", false);
917  owl_history_store(&h, "c", false);
918  owl_history_store(&h, "d", true);
919
920  /* Walk up and down the history a bit. */
921  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
922  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
923  FAIL_UNLESS("touched", owl_history_is_touched(&h));
924  FAIL_UNLESS("next d", strcmp(owl_history_get_next(&h), "d") == 0);
925  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
926  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
927  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
928  FAIL_UNLESS("prev b", strcmp(owl_history_get_prev(&h), "b") == 0);
929  FAIL_UNLESS("prev a", strcmp(owl_history_get_prev(&h), "a") == 0);
930  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
931
932  /* Now insert something. It should reset and blow away 'd'. */
933  owl_history_store(&h, "e", false);
934  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
935  FAIL_UNLESS("next NULL", owl_history_get_next(&h) == NULL);
936  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
937  FAIL_UNLESS("touched", owl_history_is_touched(&h));
938  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
939  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
940
941  /* Lines get de-duplicated on insert. */
942  owl_history_store(&h, "e", false);
943  owl_history_store(&h, "e", false);
944  owl_history_store(&h, "e", false);
945  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
946  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
947
948  /* But a partial is not deduplicated, as it'll go away soon. */
949  owl_history_store(&h, "e", true);
950  FAIL_UNLESS("prev e", strcmp(owl_history_get_prev(&h), "e") == 0);
951  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
952  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
953  FAIL_UNLESS("next e", strcmp(owl_history_get_next(&h), "e") == 0);
954
955  /* Reset moves to the front... */
956  owl_history_store(&h, "f", true);
957  FAIL_UNLESS("prev e", strcmp(owl_history_get_prev(&h), "e") == 0);
958  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
959  owl_history_reset(&h);
960  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
961  /* ...and destroys any pending partial entry... */
962  FAIL_UNLESS("prev c", strcmp(owl_history_get_prev(&h), "c") == 0);
963  FAIL_UNLESS("prev b", strcmp(owl_history_get_prev(&h), "b") == 0);
964  /* ...but not non-partial ones. */
965  owl_history_reset(&h);
966  FAIL_UNLESS("untouched", !owl_history_is_touched(&h));
967
968  /* Finally, check we are bounded by OWL_HISTORYSIZE. */
969  for (i = 0; i < OWL_HISTORYSIZE; i++) {
970    char *string = g_strdup_printf("mango%d", i);
971    owl_history_store(&h, string, false);
972    g_free(string);
973  }
974  /* The OWL_HISTORYSIZE'th prev gets NULL. */
975  for (i = OWL_HISTORYSIZE - 2; i >= 0; i--) {
976    char *string = g_strdup_printf("mango%d", i);
977    FAIL_UNLESS("prev mango_N", strcmp(owl_history_get_prev(&h), string) == 0);
978    g_free(string);
979  }
980  FAIL_UNLESS("prev NULL", owl_history_get_prev(&h) == NULL);
981
982  owl_history_cleanup(&h);
983
984  printf("# END testing owl_history (%d failures)\n", numfailed);
985  return numfailed;
986}
987
988int call_filter_regtest(void)
989{
990  int numfailed = 0;
991  int ret;
992  char *out = NULL;
993  int status;
994
995  printf("# BEGIN testing call_filter\n");
996
997  const char *cat_argv[] = { "cat", NULL };
998  ret = call_filter(cat_argv, "Mangos!", &out, &status);
999  FAIL_UNLESS("call_filter cat", (ret == 0 &&
1000                                  status == 0 &&
1001                                  strcmp(out, "Mangos!") == 0));
1002  g_free(out); out = NULL;
1003
1004  ret = call_filter(cat_argv, "", &out, &status);
1005  FAIL_UNLESS("call_filter cat", (ret == 0 &&
1006                                  status == 0 &&
1007                                  strcmp(out, "") == 0));
1008  g_free(out); out = NULL;
1009
1010  ret = call_filter(cat_argv, NULL, &out, &status);
1011  FAIL_UNLESS("call_filter cat", (ret == 0 &&
1012                                  status == 0 &&
1013                                  strcmp(out, "") == 0));
1014  g_free(out); out = NULL;
1015
1016  printf("# END testing call_filter (%d failures)\n", numfailed);
1017  return numfailed;
1018}
Note: See TracBrowser for help on using the repository browser.