ofproto-dpif: Use sequence number to wake up main thread for
[sliver-openvswitch.git] / tests / test-json.c
index bb9fadb..43cb9fa 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009 Nicira Networks.
+ * Copyright (c) 2009, 2010, 2014 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -24,7 +24,7 @@
 #include <stdio.h>
 
 #include "util.h"
-
+#include "ovstest.h"
 /* --pretty: If set, the JSON output is pretty-printed, instead of printed as
  * compactly as possible.  */
 static int pretty = 0;
@@ -67,23 +67,17 @@ refill(FILE *file, void *buffer, size_t buffer_size, size_t *n, size_t *used)
 }
 
 static bool
-parse_multiple(const char *input_file)
+parse_multiple(FILE *stream)
 {
     struct json_parser *parser;
     char buffer[BUFSIZ];
     size_t n, used;
-    FILE *file;
     bool ok;
 
-    file = fopen(input_file, "r");
-    if (!file) {
-        ovs_fatal(errno, "Cannot open \"%s\"", input_file);
-    }
-
     parser = NULL;
     n = used = 0;
     ok = true;
-    while (used < n || refill(file, buffer, sizeof buffer, &n, &used)) {
+    while (used < n || refill(stream, buffer, sizeof buffer, &n, &used)) {
         if (!parser && isspace((unsigned char) buffer[used])) {
             /* Skip white space. */
             used++;
@@ -92,7 +86,7 @@ parse_multiple(const char *input_file)
                 parser = json_parser_create(0);
             }
 
-            used = n - json_parser_feed(parser, &buffer[used], n - used);
+            used += json_parser_feed(parser, &buffer[used], n - used);
             if (used < n) {
                 if (!print_and_free_json(json_parser_finish(parser))) {
                     ok = false;
@@ -109,10 +103,11 @@ parse_multiple(const char *input_file)
     return ok;
 }
 
-int
-main(int argc, char *argv[])
+static void
+test_json_main(int argc, char *argv[])
 {
     const char *input_file;
+    FILE *stream;
     bool ok;
 
     set_program_name(argv[0]);
@@ -146,15 +141,20 @@ main(int argc, char *argv[])
     }
 
     input_file = argv[optind];
-    if (!strcmp(input_file, "-")) {
-        input_file = "/dev/stdin";
+    stream = !strcmp(input_file, "-") ? stdin : fopen(input_file, "r");
+    if (!stream) {
+        ovs_fatal(errno, "Cannot open \"%s\"", input_file);
     }
 
     if (multiple) {
-        ok = parse_multiple(input_file);
+        ok = parse_multiple(stream);
     } else {
-        ok = print_and_free_json(json_from_file(input_file));
+        ok = print_and_free_json(json_from_stream(stream));
     }
 
-    return !ok;
+    fclose(stream);
+
+    exit(!ok);
 }
+
+OVSTEST_REGISTER("test-json", test_json_main);