]> git.ipfire.org Git - thirdparty/json-c.git/commitdiff
Increased the test coverage of json_util.c from 76% to 90.3%.
authorchenguoping <chenguopingdota@163.com>
Fri, 3 Jan 2020 02:17:05 +0000 (10:17 +0800)
committerdota17 <chenguopingdota@163.com>
Tue, 14 Apr 2020 02:10:47 +0000 (10:10 +0800)
tests/test_util_file.c
tests/test_util_file.expected

index ebc5edb9df45e37c1b2866fcecc39392276bf978..79061572c1c660c0623e600ea37651a4ada46560 100644 (file)
@@ -16,6 +16,7 @@
 #endif /* HAVE_UNISTD_H */
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <assert.h>
 
 #include "json.h"
 #include "json_util.h"
@@ -28,6 +29,7 @@ static void test_read_closed(void);
 
 static void test_write_to_file();
 static void stat_and_cat(const char *file);
+static void test_read_fd_equal(const char *testdir);
 
 #ifndef PATH_MAX
 #define PATH_MAX 256
@@ -82,7 +84,21 @@ static void test_write_to_file()
        if (rv == 0)
                stat_and_cat(outfile3);
 
+       const char *outfile4 = "./test_cast.test";
+       json_object_to_file_ext(outfile4, jso, JSON_C_TO_STRING_PRETTY);
+       json_object *new_jso = NULL;
+       assert(-1 == json_object_to_file(outfile4, new_jso));
+       d = open(outfile4, O_WRONLY|O_CREAT, 0600);
+       if (d < 0)
+       {
+               printf("FAIL: unable to open %s %s\n", outfile4, strerror(errno));
+               return;
+       }
+       assert(-1 == json_object_to_fd(d, new_jso, JSON_C_TO_STRING_PRETTY));
+       close(d);       
+
        json_object_put(jso);
+       json_object_put(new_jso);
 }
 
 static void stat_and_cat(const char *file)
@@ -125,6 +141,8 @@ int main(int argc, char **argv)
        //      json_object_to_file(file, obj);
        //      json_object_to_file_ext(file, obj, flags);
 
+       _json_c_strerror(0);
+       json_util_get_last_err();
        _json_c_strerror_enable = 1;
 
        const char *testdir;
@@ -157,6 +175,7 @@ int main(int argc, char **argv)
        test_read_nonexistant();
        test_read_closed();
        test_write_to_file();
+       test_read_fd_equal(testdir);
        return EXIT_SUCCESS;
 }
 
@@ -196,6 +215,7 @@ static void test_read_valid_nested_with_fd(const char *testdir)
                fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
                exit(EXIT_FAILURE);
        }
+       assert(NULL == json_object_from_fd_ex(d, -2));
        json_object *jso = json_object_from_fd_ex(d, 20);
        if (jso != NULL)
        {
@@ -275,3 +295,28 @@ static void test_read_closed()
               "expecting NULL, EBADF, got:NULL, %s\n",
               json_util_get_last_err());
 }
+
+static void test_read_fd_equal(const char *testdir)
+{
+       char filename[PATH_MAX];
+       (void)snprintf(filename, sizeof(filename), "%s/valid_nested.json", testdir);
+       
+       json_object *jso = json_object_from_file(filename);
+
+       assert(NULL == json_type_to_name(20));
+       int d = open(filename, O_RDONLY, 0);
+       if (d < 0)
+       {       
+               fprintf(stderr,
+                       "FAIL: unable to open %s: %s\n",
+                       filename, strerror(errno));
+               exit(EXIT_FAILURE);
+       }
+       json_object *new_jso = json_object_from_fd(d);
+       close(d);
+
+       printf("OK: json_object_from_file(valid.json)=%s\n", json_object_to_json_string(jso));
+       printf("OK: json_object_from_fd(valid.json)=%s\n", json_object_to_json_string(new_jso));
+       json_object_put(jso);
+       json_object_put(new_jso);
+}
index 8d87535bbd0b20c038d4b0de99cf8d3497bb386d..f149f8543a81c52463222cfa412dee26081e9ee7 100644 (file)
@@ -36,3 +36,5 @@ file[json3.out], size=703, contents={
   "foo8":"abcdefghijklmnopqrstuvwxyz",
   "foo9":"abcdefghijklmnopqrstuvwxyz"
 }{"foo":1234,"foo1":"abcdefghijklmnopqrstuvwxyz","foo2":"abcdefghijklmnopqrstuvwxyz","foo3":"abcdefghijklmnopqrstuvwxyz","foo4":"abcdefghijklmnopqrstuvwxyz","foo5":"abcdefghijklmnopqrstuvwxyz","foo6":"abcdefghijklmnopqrstuvwxyz","foo7":"abcdefghijklmnopqrstuvwxyz","foo8":"abcdefghijklmnopqrstuvwxyz","foo9":"abcdefghijklmnopqrstuvwxyz"}
+OK: json_object_from_file(valid.json)={ "foo": 123, "obj2": { "obj3": { "obj4": { "foo": 999 } } } }
+OK: json_object_from_fd(valid.json)={ "foo": 123, "obj2": { "obj3": { "obj4": { "foo": 999 } } } }