1/* 2 * Copyright (c) 2009-2014 Petri Lehtinen <petri@digip.org> 3 * Copyright (c) 2010-2012 Graeme Smecher <graeme.smecher@mail.mcgill.ca> 4 * 5 * Jansson is free software; you can redistribute it and/or modify 6 * it under the terms of the MIT license. See LICENSE for details. 7 */ 8 9#include <string.h> 10#include <jansson.h> 11#include <stdio.h> 12#include "util.h" 13 14static void run_tests() 15{ 16 json_t *j, *j2; 17 int i1, i2, i3; 18 json_int_t I1; 19 int rv; 20 size_t z; 21 double f; 22 char *s; 23 24 json_error_t error; 25 26 /* 27 * Simple, valid json_pack cases 28 */ 29 30 /* true */ 31 rv = json_unpack(json_true(), "b", &i1); 32 if(rv || !i1) 33 fail("json_unpack boolean failed"); 34 35 /* false */ 36 rv = json_unpack(json_false(), "b", &i1); 37 if(rv || i1) 38 fail("json_unpack boolean failed"); 39 40 /* null */ 41 if(json_unpack(json_null(), "n")) 42 fail("json_unpack null failed"); 43 44 /* integer */ 45 j = json_integer(42); 46 rv = json_unpack(j, "i", &i1); 47 if(rv || i1 != 42) 48 fail("json_unpack integer failed"); 49 json_decref(j); 50 51 /* json_int_t */ 52 j = json_integer(5555555); 53 rv = json_unpack(j, "I", &I1); 54 if(rv || I1 != 5555555) 55 fail("json_unpack json_int_t failed"); 56 json_decref(j); 57 58 /* real */ 59 j = json_real(1.7); 60 rv = json_unpack(j, "f", &f); 61 if(rv || f != 1.7) 62 fail("json_unpack real failed"); 63 json_decref(j); 64 65 /* number */ 66 j = json_integer(12345); 67 rv = json_unpack(j, "F", &f); 68 if(rv || f != 12345.0) 69 fail("json_unpack (real or) integer failed"); 70 json_decref(j); 71 72 j = json_real(1.7); 73 rv = json_unpack(j, "F", &f); 74 if(rv || f != 1.7) 75 fail("json_unpack real (or integer) failed"); 76 json_decref(j); 77 78 /* string */ 79 j = json_string("foo"); 80 rv = json_unpack(j, "s", &s); 81 if(rv || strcmp(s, "foo")) 82 fail("json_unpack string failed"); 83 json_decref(j); 84 85 /* string with length (size_t) */ 86 j = json_string("foo"); 87 rv = json_unpack(j, "s%", &s, &z); 88 if(rv || strcmp(s, "foo") || z != 3) 89 fail("json_unpack string with length (size_t) failed"); 90 json_decref(j); 91 92 /* empty object */ 93 j = json_object(); 94 if(json_unpack(j, "{}")) 95 fail("json_unpack empty object failed"); 96 json_decref(j); 97 98 /* empty list */ 99 j = json_array(); 100 if(json_unpack(j, "[]")) 101 fail("json_unpack empty list failed"); 102 json_decref(j); 103 104 /* non-incref'd object */ 105 j = json_object(); 106 rv = json_unpack(j, "o", &j2); 107 if(rv || j2 != j || j->refcount != 1) 108 fail("json_unpack object failed"); 109 json_decref(j); 110 111 /* incref'd object */ 112 j = json_object(); 113 rv = json_unpack(j, "O", &j2); 114 if(rv || j2 != j || j->refcount != 2) 115 fail("json_unpack object failed"); 116 json_decref(j); 117 json_decref(j); 118 119 /* simple object */ 120 j = json_pack("{s:i}", "foo", 42); 121 rv = json_unpack(j, "{s:i}", "foo", &i1); 122 if(rv || i1 != 42) 123 fail("json_unpack simple object failed"); 124 json_decref(j); 125 126 /* simple array */ 127 j = json_pack("[iii]", 1, 2, 3); 128 rv = json_unpack(j, "[i,i,i]", &i1, &i2, &i3); 129 if(rv || i1 != 1 || i2 != 2 || i3 != 3) 130 fail("json_unpack simple array failed"); 131 json_decref(j); 132 133 /* object with many items & strict checking */ 134 j = json_pack("{s:i, s:i, s:i}", "a", 1, "b", 2, "c", 3); 135 rv = json_unpack(j, "{s:i, s:i, s:i}", "a", &i1, "b", &i2, "c", &i3); 136 if(rv || i1 != 1 || i2 != 2 || i3 != 3) 137 fail("json_unpack object with many items failed"); 138 json_decref(j); 139 140 /* 141 * Invalid cases 142 */ 143 144 j = json_integer(42); 145 if(!json_unpack_ex(j, &error, 0, "z")) 146 fail("json_unpack succeeded with invalid format character"); 147 check_error("Unexpected format character 'z'", "<format>", 1, 1, 1); 148 149 if(!json_unpack_ex(NULL, &error, 0, "[i]")) 150 fail("json_unpack succeeded with NULL root"); 151 check_error("NULL root value", "<root>", -1, -1, 0); 152 json_decref(j); 153 154 /* mismatched open/close array/object */ 155 j = json_pack("[]"); 156 if(!json_unpack_ex(j, &error, 0, "[}")) 157 fail("json_unpack failed to catch mismatched ']'"); 158 check_error("Unexpected format character '}'", "<format>", 1, 2, 2); 159 json_decref(j); 160 161 j = json_pack("{}"); 162 if(!json_unpack_ex(j, &error, 0, "{]")) 163 fail("json_unpack failed to catch mismatched '}'"); 164 check_error("Expected format 's', got ']'", "<format>", 1, 2, 2); 165 json_decref(j); 166 167 /* missing close array */ 168 j = json_pack("[]"); 169 if(!json_unpack_ex(j, &error, 0, "[")) 170 fail("json_unpack failed to catch missing ']'"); 171 check_error("Unexpected end of format string", "<format>", 1, 2, 2); 172 json_decref(j); 173 174 /* missing close object */ 175 j = json_pack("{}"); 176 if(!json_unpack_ex(j, &error, 0, "{")) 177 fail("json_unpack failed to catch missing '}'"); 178 check_error("Unexpected end of format string", "<format>", 1, 2, 2); 179 json_decref(j); 180 181 /* garbage after format string */ 182 j = json_pack("[i]", 42); 183 if(!json_unpack_ex(j, &error, 0, "[i]a", &i1)) 184 fail("json_unpack failed to catch garbage after format string"); 185 check_error("Garbage after format string", "<format>", 1, 4, 4); 186 json_decref(j); 187 188 j = json_integer(12345); 189 if(!json_unpack_ex(j, &error, 0, "ia", &i1)) 190 fail("json_unpack failed to catch garbage after format string"); 191 check_error("Garbage after format string", "<format>", 1, 2, 2); 192 json_decref(j); 193 194 /* NULL format string */ 195 j = json_pack("[]"); 196 if(!json_unpack_ex(j, &error, 0, NULL)) 197 fail("json_unpack failed to catch null format string"); 198 check_error("NULL or empty format string", "<format>", -1, -1, 0); 199 json_decref(j); 200 201 /* NULL string pointer */ 202 j = json_string("foobie"); 203 if(!json_unpack_ex(j, &error, 0, "s", NULL)) 204 fail("json_unpack failed to catch null string pointer"); 205 check_error("NULL string argument", "<args>", 1, 1, 1); 206 json_decref(j); 207 208 /* invalid types */ 209 j = json_integer(42); 210 j2 = json_string("foo"); 211 if(!json_unpack_ex(j, &error, 0, "s")) 212 fail("json_unpack failed to catch invalid type"); 213 check_error("Expected string, got integer", "<validation>", 1, 1, 1); 214 215 if(!json_unpack_ex(j, &error, 0, "n")) 216 fail("json_unpack failed to catch invalid type"); 217 check_error("Expected null, got integer", "<validation>", 1, 1, 1); 218 219 if(!json_unpack_ex(j, &error, 0, "b")) 220 fail("json_unpack failed to catch invalid type"); 221 check_error("Expected true or false, got integer", "<validation>", 1, 1, 1); 222 223 if(!json_unpack_ex(j2, &error, 0, "i")) 224 fail("json_unpack failed to catch invalid type"); 225 check_error("Expected integer, got string", "<validation>", 1, 1, 1); 226 227 if(!json_unpack_ex(j2, &error, 0, "I")) 228 fail("json_unpack failed to catch invalid type"); 229 check_error("Expected integer, got string", "<validation>", 1, 1, 1); 230 231 if(!json_unpack_ex(j, &error, 0, "f")) 232 fail("json_unpack failed to catch invalid type"); 233 check_error("Expected real, got integer", "<validation>", 1, 1, 1); 234 235 if(!json_unpack_ex(j2, &error, 0, "F")) 236 fail("json_unpack failed to catch invalid type"); 237 check_error("Expected real or integer, got string", "<validation>", 1, 1, 1); 238 239 if(!json_unpack_ex(j, &error, 0, "[i]")) 240 fail("json_unpack failed to catch invalid type"); 241 check_error("Expected array, got integer", "<validation>", 1, 1, 1); 242 243 if(!json_unpack_ex(j, &error, 0, "{si}", "foo")) 244 fail("json_unpack failed to catch invalid type"); 245 check_error("Expected object, got integer", "<validation>", 1, 1, 1); 246 247 json_decref(j); 248 json_decref(j2); 249 250 /* Array index out of range */ 251 j = json_pack("[i]", 1); 252 if(!json_unpack_ex(j, &error, 0, "[ii]", &i1, &i2)) 253 fail("json_unpack failed to catch index out of array bounds"); 254 check_error("Array index 1 out of range", "<validation>", 1, 3, 3); 255 json_decref(j); 256 257 /* NULL object key */ 258 j = json_pack("{si}", "foo", 42); 259 if(!json_unpack_ex(j, &error, 0, "{si}", NULL, &i1)) 260 fail("json_unpack failed to catch null string pointer"); 261 check_error("NULL object key", "<args>", 1, 2, 2); 262 json_decref(j); 263 264 /* Object key not found */ 265 j = json_pack("{si}", "foo", 42); 266 if(!json_unpack_ex(j, &error, 0, "{si}", "baz", &i1)) 267 fail("json_unpack failed to catch null string pointer"); 268 check_error("Object item not found: baz", "<validation>", 1, 3, 3); 269 json_decref(j); 270 271 /* 272 * Strict validation 273 */ 274 275 j = json_pack("[iii]", 1, 2, 3); 276 rv = json_unpack(j, "[iii!]", &i1, &i2, &i3); 277 if(rv || i1 != 1 || i2 != 2 || i3 != 3) 278 fail("json_unpack array with strict validation failed"); 279 json_decref(j); 280 281 j = json_pack("[iii]", 1, 2, 3); 282 if(!json_unpack_ex(j, &error, 0, "[ii!]", &i1, &i2)) 283 fail("json_unpack array with strict validation failed"); 284 check_error("1 array item(s) left unpacked", "<validation>", 1, 5, 5); 285 json_decref(j); 286 287 /* Like above, but with JSON_STRICT instead of '!' format */ 288 j = json_pack("[iii]", 1, 2, 3); 289 if(!json_unpack_ex(j, &error, JSON_STRICT, "[ii]", &i1, &i2)) 290 fail("json_unpack array with strict validation failed"); 291 check_error("1 array item(s) left unpacked", "<validation>", 1, 4, 4); 292 json_decref(j); 293 294 j = json_pack("{s:s, s:i}", "foo", "bar", "baz", 42); 295 rv = json_unpack(j, "{sssi!}", "foo", &s, "baz", &i1); 296 if(rv || strcmp(s, "bar") != 0 || i1 != 42) 297 fail("json_unpack object with strict validation failed"); 298 json_decref(j); 299 300 /* Unpack the same item twice */ 301 j = json_pack("{s:s, s:i}", "foo", "bar", "baz", 42); 302 if(!json_unpack_ex(j, &error, 0, "{s:s,s:s!}", "foo", &s, "foo", &s)) 303 fail("json_unpack object with strict validation failed"); 304 check_error("1 object item(s) left unpacked", "<validation>", 1, 10, 10); 305 json_decref(j); 306 307 j = json_pack("[i,{s:i,s:n},[i,i]]", 1, "foo", 2, "bar", 3, 4); 308 if(json_unpack_ex(j, NULL, JSON_STRICT | JSON_VALIDATE_ONLY, 309 "[i{sisn}[ii]]", "foo", "bar")) 310 fail("json_unpack complex value with strict validation failed"); 311 json_decref(j); 312 313 /* ! and * must be last */ 314 j = json_pack("[ii]", 1, 2); 315 if(!json_unpack_ex(j, &error, 0, "[i!i]", &i1, &i2)) 316 fail("json_unpack failed to catch ! in the middle of an array"); 317 check_error("Expected ']' after '!', got 'i'", "<format>", 1, 4, 4); 318 319 if(!json_unpack_ex(j, &error, 0, "[i*i]", &i1, &i2)) 320 fail("json_unpack failed to catch * in the middle of an array"); 321 check_error("Expected ']' after '*', got 'i'", "<format>", 1, 4, 4); 322 json_decref(j); 323 324 j = json_pack("{sssi}", "foo", "bar", "baz", 42); 325 if(!json_unpack_ex(j, &error, 0, "{ss!si}", "foo", &s, "baz", &i1)) 326 fail("json_unpack failed to catch ! in the middle of an object"); 327 check_error("Expected '}' after '!', got 's'", "<format>", 1, 5, 5); 328 329 if(!json_unpack_ex(j, &error, 0, "{ss*si}", "foo", &s, "baz", &i1)) 330 fail("json_unpack failed to catch ! in the middle of an object"); 331 check_error("Expected '}' after '*', got 's'", "<format>", 1, 5, 5); 332 json_decref(j); 333 334 /* Error in nested object */ 335 j = json_pack("{s{snsn}}", "foo", "bar", "baz"); 336 if(!json_unpack_ex(j, &error, 0, "{s{sn!}}", "foo", "bar")) 337 fail("json_unpack nested object with strict validation failed"); 338 check_error("1 object item(s) left unpacked", "<validation>", 1, 7, 7); 339 json_decref(j); 340 341 /* Error in nested array */ 342 j = json_pack("[[ii]]", 1, 2); 343 if(!json_unpack_ex(j, &error, 0, "[[i!]]", &i1)) 344 fail("json_unpack nested array with strict validation failed"); 345 check_error("1 array item(s) left unpacked", "<validation>", 1, 5, 5); 346 json_decref(j); 347 348 /* Optional values */ 349 j = json_object(); 350 i1 = 0; 351 if(json_unpack(j, "{s?i}", "foo", &i1)) 352 fail("json_unpack failed for optional key"); 353 if(i1 != 0) 354 fail("json_unpack unpacked an optional key"); 355 json_decref(j); 356 357 i1 = 0; 358 j = json_pack("{si}", "foo", 42); 359 if(json_unpack(j, "{s?i}", "foo", &i1)) 360 fail("json_unpack failed for an optional value"); 361 if(i1 != 42) 362 fail("json_unpack failed to unpack an optional value"); 363 json_decref(j); 364 365 j = json_object(); 366 i1 = i2 = i3 = 0; 367 if(json_unpack(j, "{s?[ii]s?{s{si}}}", 368 "foo", &i1, &i2, 369 "bar", "baz", "quux", &i3)) 370 fail("json_unpack failed for complex optional values"); 371 if(i1 != 0 || i2 != 0 || i3 != 0) 372 fail("json_unpack unexpectedly unpacked something"); 373 json_decref(j); 374 375 j = json_pack("{s{si}}", "foo", "bar", 42); 376 if(json_unpack(j, "{s?{s?i}}", "foo", "bar", &i1)) 377 fail("json_unpack failed for complex optional values"); 378 if(i1 != 42) 379 fail("json_unpack failed to unpack"); 380 json_decref(j); 381 382 /* Combine ? and ! */ 383 j = json_pack("{si}", "foo", 42); 384 i1 = i2 = 0; 385 if(json_unpack(j, "{sis?i!}", "foo", &i1, "bar", &i2)) 386 fail("json_unpack failed for optional values with strict mode"); 387 if(i1 != 42) 388 fail("json_unpack failed to unpack"); 389 if(i2 != 0) 390 fail("json_unpack failed to unpack"); 391 json_decref(j); 392 393 /* But don't compensate a missing key with an optional one. */ 394 j = json_pack("{sisi}", "foo", 42, "baz", 43); 395 i1 = i2 = i3 = 0; 396 if(!json_unpack_ex(j, &error, 0, "{sis?i!}", "foo", &i1, "bar", &i2)) 397 fail("json_unpack failed for optional values with strict mode and compensation"); 398 check_error("1 object item(s) left unpacked", "<validation>", 1, 8, 8); 399 json_decref(j); 400} 401