]> git.notmuchmail.org Git - notmuch/commitdiff
test/hex-xcode: new test binary
authorDavid Bremner <bremner@debian.org>
Thu, 14 Jun 2012 22:08:37 +0000 (01:08 +0300)
committerDavid Bremner <bremner@debian.org>
Sun, 2 Dec 2012 13:14:59 +0000 (09:14 -0400)
This program is used both as a test-bed/unit-tester for
../util/hex-escape.c, and also as a utility in future tests of dump
and restore.

test/.gitignore
test/Makefile.local
test/basic
test/hex-xcode.c [new file with mode: 0644]

index e63c689dcd1a895cbdcb042787d22a1c2d3e3266..be7ab5eddc536a1200d0c5a189bd78e435d37897 100644 (file)
@@ -3,4 +3,5 @@ corpus.mail
 smtp-dummy
 symbol-test
 arg-test
+hex-xcode
 tmp.*
index 9ae130a2d1e44b09f1c7e483afe18c451689c939..8da4c56e02745851674da4b26e2eb222a8169e6c 100644 (file)
@@ -13,6 +13,9 @@ smtp_dummy_modules = $(smtp_dummy_srcs:.c=.o)
 $(dir)/arg-test: $(dir)/arg-test.o command-line-arguments.o util/libutil.a
        $(call quiet,CC) -I. $^ -o $@
 
+$(dir)/hex-xcode: $(dir)/hex-xcode.o command-line-arguments.o util/libutil.a
+       $(call quiet,CC) -I. $^ -o $@ -ltalloc
+
 $(dir)/smtp-dummy: $(smtp_dummy_modules)
        $(call quiet,CC) $^ -o $@
 
@@ -24,8 +27,13 @@ $(dir)/parse-time: $(dir)/parse-time.o parse-time-string/parse-time-string.o
 
 .PHONY: test check
 
-test-binaries: $(dir)/arg-test $(dir)/smtp-dummy $(dir)/symbol-test \
-       $(dir)/parse-time
+TEST_BINARIES=$(dir)/arg-test \
+             $(dir)/hex-xcode \
+             $(dir)/parse-time \
+             $(dir)/smtp-dummy \
+             $(dir)/symbol-test
+
+test-binaries: $(TEST_BINARIES)
 
 test:  all test-binaries
        @${dir}/notmuch-test $(OPTIONS)
@@ -36,5 +44,6 @@ SRCS := $(SRCS) $(smtp_dummy_srcs)
 CLEAN := $(CLEAN) $(dir)/smtp-dummy $(dir)/smtp-dummy.o \
         $(dir)/symbol-test $(dir)/symbol-test.o \
         $(dir)/arg-test $(dir)/arg-test.o \
+        $(dir)/hex-xcode $(dir)/hex-xcode.o \
         $(dir)/parse-time $(dir)/parse-time.o \
         $(dir)/corpus.mail $(dir)/test-results $(dir)/tmp.*
index b7feb07ef7bbb3645e9d7549a13d3928cbe31c88..c448ef8c199cb64cdff2ad0f2b87190f70d38fbe 100755 (executable)
@@ -56,6 +56,7 @@ tests_in_suite=$(for i in $TESTS; do echo $i; done | sort)
 available=$(find "$TEST_DIRECTORY" -maxdepth 1 -type f -perm +111  \
     ! -name aggregate-results.sh       \
     ! -name arg-test                   \
+    ! -name hex-xcode                  \
     ! -name notmuch-test               \
     ! -name parse-time                 \
     ! -name smtp-dummy                 \
diff --git a/test/hex-xcode.c b/test/hex-xcode.c
new file mode 100644 (file)
index 0000000..65d4956
--- /dev/null
@@ -0,0 +1,109 @@
+/* No, nothing to to with IDE from Apple Inc.
+ * testbed for ../util/hex-escape.c.
+ *
+ * usage:
+ * hex-xcode [--direction=(encode|decode)] [--omit-newline] < file
+ * hex-xcode [--direction=(encode|decode)] [--omit-newline] [--in-place] arg1 arg2 arg3 ...
+ *
+ */
+
+#include "notmuch-client.h"
+#include "hex-escape.h"
+#include <assert.h>
+
+enum direction {
+    ENCODE,
+    DECODE
+};
+
+static int inplace = FALSE;
+
+static int
+xcode (void *ctx, enum direction dir, char *in, char **buf_p, size_t *size_p)
+{
+    hex_status_t status;
+
+    if (dir == ENCODE)
+       status = hex_encode (ctx, in, buf_p, size_p);
+    else
+       if (inplace) {
+           status = hex_decode_inplace (in);
+           *buf_p = in;
+           *size_p = strlen(in);
+       } else {
+           status = hex_decode (ctx, in, buf_p, size_p);
+       }
+
+    if (status == HEX_SUCCESS)
+       fputs (*buf_p, stdout);
+
+    return status;
+}
+
+int
+main (int argc, char **argv)
+{
+
+    enum direction dir = DECODE;
+    int omit_newline = FALSE;
+
+    notmuch_opt_desc_t options[] = {
+       { NOTMUCH_OPT_KEYWORD, &dir, "direction", 'd',
+         (notmuch_keyword_t []){ { "encode", ENCODE },
+                                 { "decode", DECODE },
+                                 { 0, 0 } } },
+       { NOTMUCH_OPT_BOOLEAN, &omit_newline, "omit-newline", 'n', 0 },
+       { NOTMUCH_OPT_BOOLEAN, &inplace, "in-place", 'i', 0 },
+       { 0, 0, 0, 0, 0 }
+    };
+
+    int opt_index = parse_arguments (argc, argv, options, 1);
+
+    if (opt_index < 0)
+       exit (1);
+
+    void *ctx = talloc_new (NULL);
+
+    char *line = NULL;
+    size_t line_size;
+    ssize_t line_len;
+
+    char *buffer = NULL;
+    size_t buf_size = 0;
+
+    notmuch_bool_t read_stdin = TRUE;
+
+    for (; opt_index < argc; opt_index++) {
+
+       if (xcode (ctx, dir, argv[opt_index],
+                  &buffer, &buf_size) != HEX_SUCCESS)
+           return 1;
+
+       if (! omit_newline)
+           putchar ('\n');
+
+       read_stdin = FALSE;
+    }
+
+    if (! read_stdin)
+       return 0;
+
+    while ((line_len = getline (&line, &line_size, stdin)) != -1) {
+
+       chomp_newline (line);
+
+       if (xcode (ctx, dir, line, &buffer, &buf_size) != HEX_SUCCESS)
+           return 1;
+
+       if (! omit_newline)
+           putchar ('\n');
+
+    }
+
+    if (line)
+       free (line);
+
+    talloc_free (ctx);
+
+    return 0;
+}