Merge branch '2.3'
[jansson.git] / test / suites / api / test_load.c
1 /*
2  * Copyright (c) 2009-2012 Petri Lehtinen <petri@digip.org>
3  *
4  * Jansson is free software; you can redistribute it and/or modify
5  * it under the terms of the MIT license. See LICENSE for details.
6  */
7
8 #include <jansson.h>
9 #include <string.h>
10 #include "util.h"
11
12 static void file_not_found()
13 {
14     json_t *json;
15     json_error_t error;
16     char *pos;
17
18     json = json_load_file("/path/to/nonexistent/file.json", 0, &error);
19     if(json)
20         fail("json_load_file returned non-NULL for a nonexistent file");
21     if(error.line != -1)
22         fail("json_load_file returned an invalid line number");
23
24     /* The error message is locale specific, only check the beginning
25        of the error message. */
26
27     pos = strchr(error.text, ':');
28     if(!pos)
29         fail("json_load_file returne an invalid error message");
30
31     *pos = '\0';
32
33     if(strcmp(error.text, "unable to open /path/to/nonexistent/file.json") != 0)
34         fail("json_load_file returned an invalid error message");
35 }
36
37 static void reject_duplicates()
38 {
39     json_error_t error;
40
41     if(json_loads("{\"foo\": 1, \"foo\": 2}", JSON_REJECT_DUPLICATES, &error))
42         fail("json_loads did not detect a duplicate key");
43     check_error("duplicate object key near '\"foo\"'", "<string>", 1, 16, 16);
44 }
45
46 static void disable_eof_check()
47 {
48     json_error_t error;
49     json_t *json;
50
51     const char *text = "{\"foo\": 1} garbage";
52
53     if(json_loads(text, 0, &error))
54         fail("json_loads did not detect garbage after JSON text");
55     check_error("end of file expected near 'garbage'", "<string>", 1, 18, 18);
56
57     json = json_loads(text, JSON_DISABLE_EOF_CHECK, &error);
58     if(!json)
59         fail("json_loads failed with JSON_DISABLE_EOF_CHECK");
60
61     json_decref(json);
62 }
63
64 static void decode_any()
65 {
66     json_t *json;
67     json_error_t error;
68
69     json = json_loads("\"foo\"", JSON_DECODE_ANY, &error);
70     if (!json || !json_is_string(json))
71         fail("json_load decoded any failed - string");
72     json_decref(json);
73
74     json = json_loads("42", JSON_DECODE_ANY, &error);
75     if (!json || !json_is_integer(json))
76         fail("json_load decoded any failed - integer");
77     json_decref(json);
78
79     json = json_loads("true", JSON_DECODE_ANY, &error);
80     if (!json || !json_is_true(json))
81         fail("json_load decoded any failed - boolean");
82     json_decref(json);
83
84     json = json_loads("null", JSON_DECODE_ANY, &error);
85     if (!json || !json_is_null(json))
86         fail("json_load decoded any failed - null");
87     json_decref(json);
88 }
89
90 static void load_wrong_args()
91 {
92     json_t *json;
93     json_error_t error;
94
95     json = json_loads(NULL, 0, &error);
96     if (json)
97         fail("json_loads should return NULL if the first argument is NULL");
98
99     json = json_loadb(NULL, 0, 0, &error);
100     if (json)
101         fail("json_loadb should return NULL if the first argument is NULL");
102
103     json = json_loadf(NULL, 0, &error);
104     if (json)
105         fail("json_loadf should return NULL if the first argument is NULL");
106
107     json = json_load_file(NULL, 0, &error);
108     if (json)
109         fail("json_loadf should return NULL if the first argument is NULL");
110 }
111
112 static void position()
113 {
114     json_t *json;
115     size_t flags = JSON_DISABLE_EOF_CHECK;
116     json_error_t error;
117
118     json = json_loads("{\"foo\": \"bar\"}", 0, &error);
119     if(error.position != 14)
120         fail("json_loads returned a wrong position");
121     json_decref(json);
122
123     json = json_loads("{\"foo\": \"bar\"} baz quux", flags, &error);
124     if(error.position != 14)
125         fail("json_loads returned a wrong position");
126     json_decref(json);
127 }
128
129 static void run_tests()
130 {
131     file_not_found();
132     reject_duplicates();
133     disable_eof_check();
134     decode_any();
135     load_wrong_args();
136     position();
137 }