Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 71.18 KB | None | 0 0
  1. diff -rupN asterisk-11.18.0/build_tools/menuselect-deps.in asterisk-11.18.0-oauth/build_tools/menuselect-deps.in
  2. --- asterisk-11.18.0/build_tools/menuselect-deps.in 2015-06-04 01:44:42.000000000 +0100
  3. +++ asterisk-11.18.0-oauth/build_tools/menuselect-deps.in   2015-06-22 17:46:02.184534485 +0100
  4. @@ -25,6 +25,7 @@ IODBC=@PBX_IODBC@
  5.  ISDNNET=@PBX_ISDNNET@
  6.  IXJUSER=@PBX_IXJUSER@
  7.  JACK=@PBX_JACK@
  8. +JANSSON=@PBX_JANSSON@
  9.  KQUEUE=@PBX_KQUEUE@
  10.  LDAP=@PBX_LDAP@
  11.  LIBEDIT=@PBX_LIBEDIT@
  12. diff -rupN asterisk-11.18.0/configure.ac asterisk-11.18.0-oauth/configure.ac
  13. --- asterisk-11.18.0/configure.ac   2015-06-04 01:44:42.000000000 +0100
  14. +++ asterisk-11.18.0-oauth/configure.ac 2015-06-22 17:46:02.184534485 +0100
  15. @@ -408,6 +408,7 @@ AST_EXT_LIB_SETUP([INOTIFY], [inotify su
  16.  AST_EXT_LIB_SETUP([IODBC], [iODBC], [iodbc])
  17.  AST_EXT_LIB_SETUP([ISDNNET], [ISDN4Linux], [isdnnet])
  18.  AST_EXT_LIB_SETUP([JACK], [Jack Audio Connection Kit], [jack])
  19. +AST_EXT_LIB_SETUP([JANSSON], [Jansson JSON library], [jansson])
  20.  AST_EXT_LIB_SETUP([KQUEUE], [kqueue support], [kqueue])
  21.  AST_EXT_LIB_SETUP([LDAP], [OpenLDAP], [ldap])
  22.  AST_LIBCURL_CHECK_CONFIG([], [7.10.1])
  23. @@ -1883,6 +1884,8 @@ AST_EXT_LIB_CHECK([INOTIFY], [c], [inoti
  24.  
  25.  AST_EXT_LIB_CHECK([JACK], [jack], [jack_activate], [jack/jack.h])
  26.  
  27. +AST_EXT_LIB_CHECK([JANSSON], [jansson], [json_dumps], [jansson.h])
  28. +
  29.  # BSD (and OS X) equivalent of inotify
  30.  AST_EXT_LIB_CHECK([KQUEUE], [c], [kqueue], [sys/event.h])
  31.  
  32. diff -rupN asterisk-11.18.0/contrib/scripts/install_prereq asterisk-11.18.0-oauth/contrib/scripts/install_prereq
  33. --- asterisk-11.18.0/contrib/scripts/install_prereq 2015-06-04 01:44:42.000000000 +0100
  34. +++ asterisk-11.18.0-oauth/contrib/scripts/install_prereq   2015-06-22 17:46:02.180534485 +0100
  35. @@ -27,7 +27,7 @@ PACKAGES_DEBIAN="$PACKAGES_DEBIAN libcur
  36.  PACKAGES_DEBIAN="$PACKAGES_DEBIAN libpq-dev unixodbc-dev libsqlite0-dev libmysqlclient15-dev libneon27-dev libgmime-dev libusb-dev liblua5.1-0-dev lua5.1"
  37.  PACKAGES_DEBIAN="$PACKAGES_DEBIAN libopenh323-dev libvpb-dev libgtk2.0-dev libmysqlclient-dev libbluetooth-dev libradiusclient-ng-dev freetds-dev"
  38.  PACKAGES_DEBIAN="$PACKAGES_DEBIAN libsnmp-dev libiksemel-dev libcorosync-dev libnewt-dev libpopt-dev libical-dev libspandsp-dev libjack-dev"
  39. -PACKAGES_DEBIAN="$PACKAGES_DEBIAN libresample-dev libc-client-dev binutils-dev libsrtp-dev libgsm1-dev libedit-dev libldap-dev"
  40. +PACKAGES_DEBIAN="$PACKAGES_DEBIAN libresample-dev libc-client-dev binutils-dev libsrtp-dev libgsm1-dev libedit-dev libldap-dev libjansson-dev"
  41.  PACKAGES_RH="automake gcc gcc-c++ ncurses-devel openssl-devel libxml2-devel unixODBC-devel libcurl-devel libogg-devel libvorbis-devel speex-devel"
  42.  PACKAGES_RH="$PACKAGES_RH spandsp-devel freetds-devel net-snmp-devel iksemel-devel corosynclib-devel newt-devel popt-devel libtool-ltdl-devel lua-devel"
  43.  PACKAGES_RH="$PACKAGES_RH libsqlite3x-devel radiusclient-ng-devel portaudio-devel postgresql-devel libresample-devel neon-devel libical-devel"
  44. diff -rupN asterisk-11.18.0/include/asterisk/autoconfig.h.in asterisk-11.18.0-oauth/include/asterisk/autoconfig.h.in
  45. --- asterisk-11.18.0/include/asterisk/autoconfig.h.in   2015-06-04 01:44:42.000000000 +0100
  46. +++ asterisk-11.18.0-oauth/include/asterisk/autoconfig.h.in 2015-06-22 17:46:02.180534485 +0100
  47. @@ -370,6 +370,9 @@
  48.  /* Define to 1 if you have the Jack Audio Connection Kit library. */
  49.  #undef HAVE_JACK
  50.  
  51. +/* Define to 1 if you have the Jansson JSON library library. */
  52. +#undef HAVE_JANSSON
  53. +
  54.  /* Define to 1 if you have the `kevent64' function. */
  55.  #undef HAVE_KEVENT64
  56.  
  57. diff -rupN asterisk-11.18.0/include/asterisk/json.h asterisk-11.18.0-oauth/include/asterisk/json.h
  58. --- asterisk-11.18.0/include/asterisk/json.h    1970-01-01 01:00:00.000000000 +0100
  59. +++ asterisk-11.18.0-oauth/include/asterisk/json.h  2015-06-22 17:46:02.180534485 +0100
  60. @@ -0,0 +1,533 @@
  61. +/*
  62. + * Asterisk -- An open source telephony toolkit.
  63. + *
  64. + * Copyright (C) 2012 - 2013, Digium, Inc.
  65. + *
  66. + * David M. Lee, II <dlee@digium.com>
  67. + *
  68. + * See http://www.asterisk.org for more information about
  69. + * the Asterisk project. Please do not directly contact
  70. + * any of the maintainers of this project for assistance;
  71. + * the project provides a web site, mailing lists and IRC
  72. + * channels for your use.
  73. + *
  74. + * This program is free software, distributed under the terms of
  75. + * the GNU General Public License Version 2. See the LICENSE file
  76. + * at the top of the source tree.
  77. + */
  78. +
  79. +#ifndef _ASTERISK_JSON_H
  80. +#define _ASTERISK_JSON_H
  81. +
  82. +/*! \file
  83. + *
  84. + * \brief Asterisk JSON abstraction layer.
  85. + *
  86. + * This is a very thin wrapper around the Jansson API. For more details on it, see its
  87. + * docs at http://www.digip.org/jansson/doc/2.4/apiref.html.
  88. +
  89. + * \author David M. Lee, II <dlee@digium.com>
  90. + */
  91. +
  92. +/*!@{*/
  93. +
  94. +/*!
  95. + * \brief Specifies custom allocators instead of the standard ast_malloc() and ast_free().
  96. + *
  97. + * This is used by the unit tests to do JSON specific memory leak detection. Since it
  98. + * affects all users of the JSON library, shouldn't normally be used.
  99. + */
  100. +void ast_json_set_alloc_funcs(void *(*malloc_fn)(size_t), void (*free_fn)(void*));
  101. +
  102. +/*!
  103. + * \struct ast_json
  104. + * \brief Abstract JSON element (object, array, string, int)
  105. + */
  106. +struct ast_json;
  107. +
  108. +/*!
  109. + * \brief Increase refcount on \a value.
  110. + * \return The given \a value.
  111. + */
  112. +struct ast_json *ast_json_ref(struct ast_json *value);
  113. +
  114. +/*!
  115. + * \brief Decrease refcount on \a value. If refcount reaches zero, \a value is freed.
  116. + */
  117. +void ast_json_unref(struct ast_json *value);
  118. +
  119. +/*!@}*/
  120. +
  121. +/*!@{*/
  122. +
  123. +/*!
  124. + * \brief Valid types of a JSON element.
  125. + */
  126. +enum ast_json_type
  127. +{
  128. +   AST_JSON_OBJECT,
  129. +   AST_JSON_ARRAY,
  130. +   AST_JSON_STRING,
  131. +   AST_JSON_INTEGER,
  132. +   AST_JSON_REAL,
  133. +   AST_JSON_TRUE,
  134. +   AST_JSON_FALSE,
  135. +   AST_JSON_NULL,
  136. +};
  137. +
  138. +/*!
  139. + * \brief Return the type of \a value.
  140. + */
  141. +enum ast_json_type ast_json_typeof(const struct ast_json *value);
  142. +
  143. +/*!@}*/
  144. +
  145. +/*!@{*/
  146. +
  147. +/*!
  148. + * \brief Gets the JSON true value.
  149. + */
  150. +struct ast_json *ast_json_true(void);
  151. +
  152. +/*!
  153. + * \brief Gets the JSON false value.
  154. + */
  155. +struct ast_json *ast_json_false(void);
  156. +
  157. +/*!
  158. + * \brief Returns JSON false if \a value is 0, JSON true otherwise.
  159. + */
  160. +struct ast_json *ast_json_boolean(int value);
  161. +
  162. +/*!
  163. + * \brief Gets the JSON null value.
  164. + */
  165. +struct ast_json *ast_json_null(void);
  166. +
  167. +/*!
  168. + * \brief Returns true (non-zero) if \a value is the JSON true value, false (zero)
  169. + * otherwise.
  170. + */
  171. +int ast_json_is_true(const struct ast_json *value);
  172. +
  173. +/*!
  174. + * \brief Returns true (non-zero) if \a value is the JSON false value, false (zero)
  175. + * otherwise.
  176. + */
  177. +int ast_json_is_false(const struct ast_json *value);
  178. +
  179. +/*!
  180. + * \brief Returns true (non-zero) if \a value is the JSON null value, false (zero)
  181. + * otherwise.
  182. + */
  183. +int ast_json_is_null(const struct ast_json *value);
  184. +
  185. +/*!@}*/
  186. +
  187. +/*!@{*/
  188. +
  189. +/*!
  190. + * \brief Constructs a JSON string from \a value.
  191. + *
  192. + * \a value must be a valid ASCII or UTF-8 encoded string.
  193. + *
  194. + * \return Newly constructed string element, or \c NULL on failure.
  195. + */
  196. +struct ast_json *ast_json_string_create(const char *value);
  197. +
  198. +/*!
  199. + * \brief Returns the string value of \a string, or \c NULL if it's not a JSON string.
  200. + */
  201. +const char *ast_json_string_get(const struct ast_json *string);
  202. +
  203. +/*!
  204. + * \brief Changes the string value of \a string to \a value.
  205. + *
  206. + * \return 0 on success, or -1 on error.
  207. + */
  208. +int ast_json_string_set(struct ast_json *string, const char *value);
  209. +
  210. +/*!
  211. + * \brief Create a JSON string, printf style.
  212. + *
  213. + * \return Newly allocated string, or \c NULL if allocation fails.
  214. + */
  215. +struct ast_json *ast_json_stringf(const char *format, ...) __attribute__((format(printf, 1, 2)));
  216. +
  217. +/*!
  218. + * \brief Create a JSON string, vprintf style.
  219. + *
  220. + * \return Newly allocated string, or \c NULL if allocation fails.
  221. + */
  222. +struct ast_json *ast_json_vstringf(const char *format, va_list args) __attribute__((format(printf, 1, 0)));
  223. +
  224. +/*!@}*/
  225. +
  226. +/*!@{*/
  227. +
  228. +/*!
  229. + * \brief Create a JSON integer with the give \a value.
  230. + *
  231. + * \return Newly allocated integer, or \c NULL if allocation fails.
  232. + */
  233. +struct ast_json *ast_json_integer_create(intmax_t value);
  234. +
  235. +/*!
  236. + * \brief Get the value from \a integer.
  237. + *
  238. + * \return integer value, or 0 for non-integers.
  239. + */
  240. +intmax_t ast_json_integer_get(const struct ast_json *integer);
  241. +
  242. +/*!
  243. + * \brief Sets the value of \a integer.
  244. + *
  245. + * \return 0 on success, -1 on failure
  246. + */
  247. +int ast_json_integer_set(struct ast_json *integer, intmax_t value);
  248. +
  249. +/*!@}*/
  250. +
  251. +/*!@{*/
  252. +
  253. +/*!
  254. + * \brief Create a empty JSON array.
  255. + *
  256. + * \return Newly allocated array, or \c NULL if allocation fails.
  257. + */
  258. +struct ast_json *ast_json_array_create(void);
  259. +
  260. +/*!
  261. + * \brief Returns the size of \a array, or 0 if argument is not an array.
  262. + */
  263. +size_t ast_json_array_size(const struct ast_json *array);
  264. +
  265. +/*!
  266. + * \brief Returns the element in the \a index position from the \a array.
  267. + *
  268. + * The returned element is a borrowed reference; use ast_json_ref() to safely keep a
  269. + * pointer to it.
  270. + *
  271. + * \return The specified element, or \c NULL if \a array not an array or \a index is out
  272. + * of bounds.
  273. + */
  274. +struct ast_json *ast_json_array_get(const struct ast_json *array, size_t index);
  275. +
  276. +/*!
  277. + * \brief Changes the \a index element in \a array to \a value.
  278. + *
  279. + * The array steals the \a value reference; use ast_json_ref() to safely keep a pointer
  280. + * to it.
  281. + *
  282. + * \return 0 on success, or -1 on failure.
  283. + */
  284. +int ast_json_array_set(struct ast_json *array, size_t index, struct ast_json *value);
  285. +
  286. +/*!
  287. + * \brief Appends \a value to \a array.
  288. + *
  289. + * The array steals the \a value reference; use ast_json_ref() to safely keep a pointer
  290. + * to it.
  291. + *
  292. + * \return 0 on success, or -1 on failure.
  293. + */
  294. +int ast_json_array_append(struct ast_json *array, struct ast_json *value);
  295. +
  296. +/*!
  297. + * \brief Inserts \a value into \a array at position \a index.
  298. + *
  299. + * The array steals the \a value reference; use ast_json_ref() to safely keep a pointer
  300. + * to it.
  301. + *
  302. + * \return 0 on success, or -1 on failure.
  303. + */
  304. +int ast_json_array_insert(struct ast_json *array, size_t index, struct ast_json *value);
  305. +
  306. +/*!
  307. + * \brief Removes the element at position \a index from \a array.
  308. + *
  309. + * \return 0 on success, or -1 on failure.
  310. + */
  311. +int ast_json_array_remove(struct ast_json *array, size_t index);
  312. +
  313. +/*!
  314. + * \brief Removes all elements from \a array.
  315. + *
  316. + * \return 0 on success, or -1 on failure.
  317. + */
  318. +int ast_json_array_clear(struct ast_json *array);
  319. +
  320. +/*!
  321. + * \brief Appends all elements from \a tail to \a array.
  322. + *
  323. + * The \a tail argument is left alone, so ast_json_unref() it when you are done with it.
  324. + *
  325. + * \return 0 on success, or -1 on failure.
  326. + */
  327. +int ast_json_array_extend(struct ast_json *array, struct ast_json *tail);
  328. +
  329. +/*!@}*/
  330. +
  331. +/*!@{*/
  332. +
  333. +/*!
  334. + * \brief Create a new JSON object.
  335. + *
  336. + * \return Newly allocated object, or \c NULL if allocation fails.
  337. + */
  338. +struct ast_json *ast_json_object_create(void);
  339. +
  340. +/*!
  341. + * \brief Returns the size of \a object, or 0 if it's not a JSON object.
  342. + */
  343. +size_t ast_json_object_size(struct ast_json *object);
  344. +
  345. +/*!
  346. + * \brief Returns the element from \a object with the given \a key, or \c NULL on error.
  347. + */
  348. +struct ast_json *ast_json_object_get(struct ast_json *object, const char *key);
  349. +
  350. +/*!
  351. + * \brief Sets the value of \a key to \a value in \a object.
  352. + *
  353. + * The object steals the \a value reference; use ast_json_ref() to safely keep a pointer
  354. + * to it.
  355. + *
  356. + * \return 0 on success, or -1 on error.
  357. + */
  358. +int ast_json_object_set(struct ast_json *object, const char *key, struct ast_json *value);
  359. +
  360. +/*!
  361. + * \brief Deletes \a key from \a object.
  362. + *
  363. + * \return 0 on success, or -1 if key does not exist.
  364. + */
  365. +int ast_json_object_del(struct ast_json *object, const char *key);
  366. +
  367. +/*!
  368. + * \brief Deletes all elements from \a object.
  369. + *
  370. + * \return 0 on success, or -1 on error.
  371. + */
  372. +int ast_json_object_clear(struct ast_json *object);
  373. +
  374. +/*!
  375. + * \brief Updates the elements of \a object with all of the elements \a other, overwriting
  376. + * existing keys and adding new ones.
  377. + *
  378. + * The \a other argument is left alone, so ast_json_unref() it when you are done with it.
  379. + *
  380. + * \return 0 on success, or -1 on error.
  381. + */
  382. +int ast_json_object_update(struct ast_json *object, struct ast_json *other);
  383. +
  384. +/*!
  385. + * \brief Updates the elements of \a object with the elements of \a other, only
  386. + * overwriting existing keys.
  387. + *
  388. + * The \a other argument is left alone, so ast_json_unref() it when you are done with it.
  389. + *
  390. + * \return 0 on success, or -1 on error.
  391. + */
  392. +int ast_json_object_update_existing(struct ast_json *object, struct ast_json *other);
  393. +
  394. +/*!
  395. + * \brief Updates the elements of \a object with the elements of \a other, only adding new
  396. + * ones, not changing existing values.
  397. + *
  398. + * The \a other argument is left alone, so ast_json_unref() it when you are done with it.
  399. + *
  400. + * \return 0 on success, or -1 on error.
  401. + */
  402. +int ast_json_object_update_missing(struct ast_json *object, struct ast_json *other);
  403. +
  404. +/*!
  405. + * \struct ast_json_iter
  406. + * \brief Iterator for JSON object key/values. Note that iteration order is not specified,
  407. + * and may change as keys are added to and removed from the object.
  408. + */
  409. +struct ast_json_iter;
  410. +
  411. +/*!
  412. + * \brief Returns an iterator pointing to the first element in \a object, or \c NULL if
  413. + * \a object is \c NULL or empty.
  414. + */
  415. +struct ast_json_iter *ast_json_object_iter(struct ast_json *object);
  416. +
  417. +/*!
  418. + * \brief Returns an iterator pointing to a specified \a key in \a object. Iterating
  419. + * forward from this iterator may not to cover all elements in \a object.
  420. + */
  421. +struct ast_json_iter *ast_json_object_iter_at(struct ast_json *object, const char *key);
  422. +
  423. +/*!
  424. + * \brief Returns an iterator pointing to the next key-value pair after \a iter, or
  425. + * \c NULL if at the end.
  426. + */
  427. +struct ast_json_iter *ast_json_object_iter_next(struct ast_json *object, struct ast_json_iter *iter);
  428. +
  429. +/*!
  430. + * \brief Returns key from \a iter.
  431. + */
  432. +const char *ast_json_object_iter_key(struct ast_json_iter *iter);
  433. +
  434. +/*!
  435. + * \brief Returns value from \a iter.
  436. + *
  437. + * The returned element is a borrowed reference; use ast_json_ref() to safely
  438. + * keep a pointer to it.
  439. + */
  440. +struct ast_json *ast_json_object_iter_value(struct ast_json_iter *iter);
  441. +
  442. +/*!
  443. + * \brief Sets the value in \a object at \a iter to \a value.
  444. + *
  445. + * The array steals the value reference; use ast_json_ref() to safely keep a
  446. + * pointer to it.
  447. + *
  448. + * \return 0 on success, or -1 on error.
  449. + */
  450. +int ast_json_object_iter_set(struct ast_json *object, struct ast_json_iter *iter, struct ast_json *value);
  451. +
  452. +/*!@}*/
  453. +
  454. +/*!@{*/
  455. +
  456. +/*!
  457. + * \brief Returns JSON representation of \a root, or \c NULL on error.
  458. + *
  459. + * Returned string must be freed by calling ast_free().
  460. + */
  461. +char *ast_json_dump_string(struct ast_json *root);
  462. +
  463. +/*!
  464. + * \brief Writes JSON representation of \a root to \a dst.
  465. + *
  466. + * If \a dst is too small, it will be grown as needed.
  467. + *
  468. + * \return 0 on success, or -1 on error. On error, the contents of \a dst are
  469. + * undefined.
  470. + */
  471. +int ast_json_dump_str(struct ast_json *root, struct ast_str **dst);
  472. +
  473. +/*!
  474. + * \brief Writes JSON representation of \a root to \a output.
  475. + *
  476. + * \return 0 on success, or -1 on error.
  477. + */
  478. +int ast_json_dump_file(struct ast_json *root, FILE *output);
  479. +
  480. +/*!
  481. + * \brief Writes JSON representation of \a root to a file at \a path.
  482. + *
  483. + * \return 0 on success, or -1 on error.
  484. + */
  485. +int ast_json_dump_new_file(struct ast_json *root, const char *path);
  486. +
  487. +#define AST_JSON_ERROR_TEXT_LENGTH    160
  488. +#define AST_JSON_ERROR_SOURCE_LENGTH   80
  489. +
  490. +/*!
  491. + * \brief JSON parsing error information.
  492. + */
  493. +struct ast_json_error {
  494. +   /*! Line number error occured on */
  495. +   int line;
  496. +   /*! Character (not byte, can be different for UTF-8) column on which the error occurred. */
  497. +   int column;
  498. +   /*! Position in bytes from start of input */
  499. +   int position;
  500. +   /*! Error message */
  501. +   char text[AST_JSON_ERROR_TEXT_LENGTH];
  502. +   /*! Source of the error (filename or <string>) */
  503. +   char source[AST_JSON_ERROR_TEXT_LENGTH];
  504. +};
  505. +
  506. +/*!
  507. + * \brief Parses null terminated \a input string into a JSON object or array.
  508. + *
  509. + * \param[out] error Filled with information on error.
  510. + *
  511. + * \return Parsed JSON element, or \c NULL on error.
  512. + */
  513. +struct ast_json *ast_json_load_string(const char *input, struct ast_json_error *error);
  514. +
  515. +/*!
  516. + * \brief Parses null terminated \a input ast_str into a JSON object or array.
  517. + *
  518. + * \param[out] error Filled with information on error.
  519. + *
  520. + * \return Parsed JSON element, or \c NULL on error.
  521. + */
  522. +struct ast_json *ast_json_load_str(const struct ast_str *input, struct ast_json_error *error);
  523. +
  524. +/*!
  525. + * \brief Parses \a buffer with length \a buflen into a JSON object or array.
  526. + *
  527. + * \param[out] error Filled with information on error.
  528. + *
  529. + * \return Parsed JSON element, or \c NULL on error.
  530. + */
  531. +struct ast_json *ast_json_load_buf(const char *buffer, size_t buflen, struct ast_json_error *error);
  532. +
  533. +/*!
  534. + * \brief Parses \a input into JSON object or array.
  535. + *
  536. + * \param[out] error Filled with information on error.
  537. + *
  538. + * \return Parsed JSON element, or \c NULL on error.
  539. + */
  540. +struct ast_json *ast_json_load_file(FILE *input, struct ast_json_error *error);
  541. +
  542. +/*!
  543. + * \brief Parses file at \a path into JSON object or array.
  544. + *
  545. + * \param[out] error Filled with information on error.
  546. + *
  547. + * \return Parsed JSON element, or \c NULL on error.
  548. + */
  549. +struct ast_json *ast_json_load_new_file(const char *path, struct ast_json_error *error);
  550. +
  551. +/*!
  552. + * \brief Helper for creating complex JSON values.
  553. + *
  554. + * See original Jansson docs at http://www.digip.org/jansson/doc/2.4/apiref.html#apiref-pack
  555. + * for more details.
  556. + */
  557. +struct ast_json *ast_json_pack(char const *format, ...);
  558. +
  559. +/*!
  560. + * \brief Helper for creating complex JSON values simply.
  561. + *
  562. + * See original Jansson docs at http://www.digip.org/jansson/doc/2.4/apiref.html#apiref-pack
  563. + * for more details.
  564. + */
  565. +struct ast_json *ast_json_vpack(char const *format, va_list ap);
  566. +
  567. +/*!@}*/
  568. +
  569. +/*!@{*/
  570. +
  571. +/*!
  572. + * \brief Compares two JSON objects.
  573. + *
  574. + * Two JSON objects are equal if they are of the same type, and their contents are equal.
  575. + *
  576. + * \return true (non-zero) if \a lhs and \a rhs are equal, and false (zero) otherwise.
  577. + */
  578. +int ast_json_equal(const struct ast_json *lhs, const struct ast_json *rhs);
  579. +
  580. +/*!
  581. + * \brief Returns shallow copy (does not copy child elements) of \a value, or \c NULL
  582. + * on error.
  583. + */
  584. +struct ast_json *ast_json_copy(const struct ast_json *value);
  585. +
  586. +/*!
  587. + * \brief Returns deep copy (copies child elements) of \a value, or \c NULL on error.
  588. + */
  589. +struct ast_json *ast_json_deep_copy(const struct ast_json *value);
  590. +
  591. +/*!@}*/
  592. +
  593. +#endif /* _ASTERISK_JSON_H */
  594. diff -rupN asterisk-11.18.0/include/asterisk/test.h asterisk-11.18.0-oauth/include/asterisk/test.h
  595. --- asterisk-11.18.0/include/asterisk/test.h    2015-06-04 01:44:42.000000000 +0100
  596. +++ asterisk-11.18.0-oauth/include/asterisk/test.h  2015-06-22 17:46:02.180534485 +0100
  597. @@ -297,5 +297,22 @@ int __ast_test_status_update(const char
  598.         }                           \
  599.     } while(0)
  600.  
  601. +/*!
  602. + * \ref ast_test_check
  603. + */
  604. +enum ast_test_result_state __ast_test_check(const char *file, const char *function, int line, struct ast_test *test, const char *condition_str, enum ast_test_result_state current_res, int condition);
  605. +
  606. +/*!
  607. + * \brief Accumulate test results for a test.
  608. + *
  609. + * This macro evaluates \a condition, then updates \a res and the test status accordingly. If the condition evaluates to
  610. + * true (non-zero), nothing happens. If it evaluates to false (zero), then res is set to \a AST_TEST_FAIL and the failure
  611. + * is printed using \ref ast_test_status_update.
  612. + *
  613. + * \param \a res Local variable of type \a ast_test_result_state.
  614. + * \param \a condition Boolean condition to check.
  615. + */
  616. +#define ast_test_check(res, condition) res = __ast_test_check( __FILE__, __PRETTY_FUNCTION__, __LINE__, test, #condition, (res), (condition))
  617. +
  618.  #endif /* TEST_FRAMEWORK */
  619.  #endif /* _AST_TEST_H */
  620. diff -rupN asterisk-11.18.0/main/test.c asterisk-11.18.0-oauth/main/test.c
  621. --- asterisk-11.18.0/main/test.c    2015-06-04 01:44:42.000000000 +0100
  622. +++ asterisk-11.18.0-oauth/main/test.c  2015-06-22 17:46:02.176534485 +0100
  623. @@ -936,6 +936,15 @@ static void test_shutdown(void)
  624.     ast_cli_unregister_multiple(test_cli, ARRAY_LEN(test_cli));
  625.  }
  626.  
  627. +enum ast_test_result_state __ast_test_check(const char *file, const char *function, int line, struct ast_test *test, const char *condition_str, enum ast_test_result_state current_res, int condition)
  628. +{
  629. +   if (!condition) {
  630. +       __ast_test_status_update(file, function, line, test, "Condition failed: %s\n", condition_str);
  631. +       return AST_TEST_FAIL;
  632. +   }
  633. +   return current_res;
  634. +}
  635. +
  636.  #endif /* TEST_FRAMEWORK */
  637.  
  638.  int ast_test_init()
  639. diff -rupN asterisk-11.18.0/makeopts.in asterisk-11.18.0-oauth/makeopts.in
  640. --- asterisk-11.18.0/makeopts.in    2015-06-04 01:44:42.000000000 +0100
  641. +++ asterisk-11.18.0-oauth/makeopts.in  2015-06-22 17:46:02.176534485 +0100
  642. @@ -172,6 +172,9 @@ IODBC_LIB=@IODBC_LIB@
  643.  JACK_INCLUDE=@JACK_INCLUDE@
  644.  JACK_LIB=@JACK_LIB@
  645.  
  646. +JANSSON_INCLUDE=@JANSSON_INCLUDE@
  647. +JANSSON_LIB=@JANSSON_LIB@
  648. +
  649.  LDAP_INCLUDE=@LDAP_INCLUDE@
  650.  LDAP_LIB=@LDAP_LIB@
  651.  
  652. diff -rupN asterisk-11.18.0/res/res_json.c asterisk-11.18.0-oauth/res/res_json.c
  653. --- asterisk-11.18.0/res/res_json.c 1970-01-01 01:00:00.000000000 +0100
  654. +++ asterisk-11.18.0-oauth/res/res_json.c   2015-06-22 17:46:02.176534485 +0100
  655. @@ -0,0 +1,512 @@
  656. +/*
  657. + * Asterisk -- An open source telephony toolkit.
  658. + *
  659. + * Copyright (C) 2012 - 2013, Digium, Inc.
  660. + *
  661. + * David M. Lee, II <dlee@digium.com>
  662. + *
  663. + * See http://www.asterisk.org for more information about
  664. + * the Asterisk project. Please do not directly contact
  665. + * any of the maintainers of this project for assistance;
  666. + * the project provides a web site, mailing lists and IRC
  667. + * channels for your use.
  668. + *
  669. + * This program is free software, distributed under the terms of
  670. + * the GNU General Public License Version 2. See the LICENSE file
  671. + * at the top of the source tree.
  672. + */
  673. +
  674. +/*! \file
  675. + *
  676. + * \brief JSON abstraction layer.
  677. + *
  678. + * This is a very thin wrapper around the Jansson API. For more details on it, see its
  679. + * docs at http://www.digip.org/jansson/doc/2.4/apiref.html.
  680. + *
  681. + * \author David M. Lee, II <dlee@digium.com>
  682. + */
  683. +
  684. +/*** MODULEINFO
  685. +   <depend>jansson</depend>
  686. +   <support_level>core</support_level>
  687. + ***/
  688. +
  689. +#include "asterisk.h"
  690. +
  691. +ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  692. +
  693. +#include "asterisk/json.h"
  694. +#include "asterisk/module.h"
  695. +#include "asterisk/utils.h"
  696. +
  697. +#include <jansson.h>
  698. +
  699. +/*!
  700. + * \brief Function wrapper around ast_malloc macro.
  701. + */
  702. +static void *json_malloc(size_t size)
  703. +{
  704. +   return ast_malloc(size);
  705. +}
  706. +
  707. +/*!
  708. + * \brief Function wrapper around ast_free macro.
  709. + */
  710. +static void json_free(void *p)
  711. +{
  712. +   ast_free(p);
  713. +}
  714. +
  715. +void ast_json_set_alloc_funcs(void *(*malloc_fn)(size_t), void (*free_fn)(void*))
  716. +{
  717. +   json_set_alloc_funcs(malloc_fn, free_fn);
  718. +}
  719. +
  720. +struct ast_json *ast_json_ref(struct ast_json *json)
  721. +{
  722. +   json_incref((json_t *)json);
  723. +   return json;
  724. +}
  725. +
  726. +void ast_json_unref(struct ast_json *json)
  727. +{
  728. +   json_decref((json_t *)json);
  729. +}
  730. +
  731. +enum ast_json_type ast_json_typeof(const struct ast_json *json)
  732. +{
  733. +   int r = json_typeof((json_t*)json);
  734. +   switch(r) {
  735. +   case JSON_OBJECT: return AST_JSON_OBJECT;
  736. +   case JSON_ARRAY: return AST_JSON_ARRAY;
  737. +   case JSON_STRING: return AST_JSON_STRING;
  738. +   case JSON_INTEGER: return AST_JSON_INTEGER;
  739. +   case JSON_REAL: return AST_JSON_REAL;
  740. +   case JSON_TRUE: return AST_JSON_TRUE;
  741. +   case JSON_FALSE: return AST_JSON_FALSE;
  742. +   case JSON_NULL: return AST_JSON_NULL;
  743. +   }
  744. +   ast_assert(0); /* Unexpect return from json_typeof */
  745. +   return r;
  746. +}
  747. +
  748. +struct ast_json *ast_json_true(void)
  749. +{
  750. +   return (struct ast_json *)json_true();
  751. +}
  752. +
  753. +struct ast_json *ast_json_false(void)
  754. +{
  755. +   return (struct ast_json *)json_false();
  756. +}
  757. +
  758. +struct ast_json *ast_json_boolean(int value)
  759. +{
  760. +#if JANSSON_VERSION_HEX >= 0x020400
  761. +   return (struct ast_json *)json_boolean(value);
  762. +#else
  763. +   return value ? ast_json_true() : ast_json_false();
  764. +#endif
  765. +}
  766. +
  767. +struct ast_json *ast_json_null(void)
  768. +{
  769. +   return (struct ast_json *)json_null();
  770. +}
  771. +
  772. +int ast_json_is_true(const struct ast_json *json)
  773. +{
  774. +   return json_is_true((const json_t *)json);
  775. +}
  776. +
  777. +int ast_json_is_false(const struct ast_json *json)
  778. +{
  779. +   return json_is_false((const json_t *)json);
  780. +}
  781. +
  782. +int ast_json_is_null(const struct ast_json *json)
  783. +{
  784. +   return json_is_null((const json_t *)json);
  785. +}
  786. +
  787. +struct ast_json *ast_json_string_create(const char *value)
  788. +{
  789. +   return (struct ast_json *)json_string(value);
  790. +}
  791. +
  792. +const char *ast_json_string_get(const struct ast_json *string)
  793. +{
  794. +   return json_string_value((json_t *)string);
  795. +}
  796. +
  797. +int ast_json_string_set(struct ast_json *string, const char *value)
  798. +{
  799. +   return json_string_set((json_t *)string, value);
  800. +}
  801. +
  802. +struct ast_json *ast_json_stringf(const char *format, ...)
  803. +{
  804. +   struct ast_json *ret;
  805. +   va_list args;
  806. +   va_start(args, format);
  807. +   ret = ast_json_vstringf(format, args);
  808. +   va_end(args);
  809. +   return ret;
  810. +}
  811. +
  812. +struct ast_json *ast_json_vstringf(const char *format, va_list args)
  813. +{
  814. +   char *str = NULL;
  815. +   json_t *ret = NULL;
  816. +
  817. +   if (format) {
  818. +       int err = vasprintf(&str, format, args);
  819. +       if (err > 0) {
  820. +           ret = json_string(str);
  821. +           free(str);
  822. +       }
  823. +   }
  824. +   return (struct ast_json *)ret;
  825. +}
  826. +
  827. +struct ast_json *ast_json_integer_create(intmax_t value)
  828. +{
  829. +   return (struct ast_json *)json_integer(value);
  830. +}
  831. +
  832. +intmax_t ast_json_integer_get(const struct ast_json *integer)
  833. +{
  834. +   return json_integer_value((json_t *)integer);
  835. +}
  836. +
  837. +int ast_json_integer_set(struct ast_json *integer, intmax_t value)
  838. +{
  839. +   return json_integer_set((json_t *)integer, value);
  840. +}
  841. +
  842. +
  843. +int ast_json_equal(const struct ast_json *lhs, const struct ast_json *rhs)
  844. +{
  845. +   return json_equal((json_t *)lhs, (json_t *)rhs);
  846. +}
  847. +
  848. +struct ast_json *ast_json_array_create(void)
  849. +{
  850. +   return (struct ast_json *)json_array();
  851. +}
  852. +size_t ast_json_array_size(const struct ast_json *array)
  853. +{
  854. +   return json_array_size((json_t *)array);
  855. +}
  856. +struct ast_json *ast_json_array_get(const struct ast_json *array, size_t index)
  857. +{
  858. +   return (struct ast_json *)json_array_get((json_t *)array, index);
  859. +}
  860. +int ast_json_array_set(struct ast_json *array, size_t index, struct ast_json *value)
  861. +{
  862. +   return json_array_set_new((json_t *)array, index, (json_t *)value);
  863. +}
  864. +int ast_json_array_append(struct ast_json *array, struct ast_json *value)
  865. +{
  866. +   return json_array_append_new((json_t *)array, (json_t *)value);
  867. +}
  868. +int ast_json_array_insert(struct ast_json *array, size_t index, struct ast_json *value)
  869. +{
  870. +   return json_array_insert_new((json_t *)array, index, (json_t *)value);
  871. +}
  872. +int ast_json_array_remove(struct ast_json *array, size_t index)
  873. +{
  874. +   return json_array_remove((json_t *)array, index);
  875. +}
  876. +int ast_json_array_clear(struct ast_json *array)
  877. +{
  878. +   return json_array_clear((json_t *)array);
  879. +}
  880. +int ast_json_array_extend(struct ast_json *array, struct ast_json *tail)
  881. +{
  882. +   return json_array_extend((json_t *)array, (json_t *)tail);
  883. +}
  884. +
  885. +struct ast_json *ast_json_object_create(void)
  886. +{
  887. +   return (struct ast_json *)json_object();
  888. +}
  889. +size_t ast_json_object_size(struct ast_json *object)
  890. +{
  891. +   return json_object_size((json_t *)object);
  892. +}
  893. +struct ast_json *ast_json_object_get(struct ast_json *object, const char *key)
  894. +{
  895. +   if (key) {
  896. +       return (struct ast_json *)json_object_get((json_t *)object, key);
  897. +   }
  898. +   return NULL;
  899. +}
  900. +int ast_json_object_set(struct ast_json *object, const char *key, struct ast_json *value)
  901. +{
  902. +   return json_object_set_new((json_t *)object, key, (json_t *)value);
  903. +}
  904. +int ast_json_object_del(struct ast_json *object, const char *key)
  905. +{
  906. +   return json_object_del((json_t *)object, key);
  907. +}
  908. +int ast_json_object_clear(struct ast_json *object)
  909. +{
  910. +   return json_object_clear((json_t *)object);
  911. +}
  912. +int ast_json_object_update(struct ast_json *object, struct ast_json *other)
  913. +{
  914. +   return json_object_update((json_t *)object, (json_t *)other);
  915. +}
  916. +int ast_json_object_update_existing(struct ast_json *object, struct ast_json *other)
  917. +{
  918. +#if JANSSON_VERSION_HEX >= 0x020300
  919. +   return json_object_update_existing((json_t *)object, (json_t *)other);
  920. +#else
  921. +   struct ast_json_iter *iter = ast_json_object_iter(other);
  922. +   int ret = 0;
  923. +
  924. +   if (object == NULL || other == NULL) {
  925. +       return -1;
  926. +   }
  927. +
  928. +   while (iter != NULL && ret == 0) {
  929. +       const char *key = ast_json_object_iter_key(iter);
  930. +       if (ast_json_object_get(object, key) != NULL) {
  931. +           ret = ast_json_object_set(object, key, ast_json_object_iter_value(iter));
  932. +       }
  933. +       iter = ast_json_object_iter_next(other, iter);
  934. +   }
  935. +   return ret;
  936. +#endif
  937. +}
  938. +int ast_json_object_update_missing(struct ast_json *object, struct ast_json *other)
  939. +{
  940. +#if JANSSON_VERSION_HEX >= 0x020300
  941. +   return json_object_update_missing((json_t *)object, (json_t *)other);
  942. +#else
  943. +   struct ast_json_iter *iter = ast_json_object_iter(other);
  944. +   int ret = 0;
  945. +
  946. +   if (object == NULL || other == NULL) {
  947. +       return -1;
  948. +   }
  949. +
  950. +   while (iter != NULL && ret == 0) {
  951. +       const char *key = ast_json_object_iter_key(iter);
  952. +       if (ast_json_object_get(object, key) == NULL) {
  953. +           ret = ast_json_object_set(object, key, ast_json_object_iter_value(iter));
  954. +       }
  955. +       iter = ast_json_object_iter_next(other, iter);
  956. +   }
  957. +   return ret;
  958. +#endif
  959. +}
  960. +
  961. +struct ast_json_iter *ast_json_object_iter(struct ast_json *object)
  962. +{
  963. +   return json_object_iter((json_t *)object);
  964. +}
  965. +struct ast_json_iter *ast_json_object_iter_at(struct ast_json *object, const char *key)
  966. +{
  967. +   return json_object_iter_at((json_t *)object, key);
  968. +}
  969. +struct ast_json_iter *ast_json_object_iter_next(struct ast_json *object, struct ast_json_iter *iter)
  970. +{
  971. +   return json_object_iter_next((json_t *)object, iter);
  972. +}
  973. +const char *ast_json_object_iter_key(struct ast_json_iter *iter)
  974. +{
  975. +   return json_object_iter_key(iter);
  976. +}
  977. +struct ast_json *ast_json_object_iter_value(struct ast_json_iter *iter)
  978. +{
  979. +   return (struct ast_json *)json_object_iter_value(iter);
  980. +}
  981. +int ast_json_object_iter_set(struct ast_json *object, struct ast_json_iter *iter, struct ast_json *value)
  982. +{
  983. +   return json_object_iter_set_new((json_t *)object, iter, (json_t *)value);
  984. +}
  985. +
  986. +/*!
  987. + * \brief Default flags for JSON encoding.
  988. + */
  989. +static size_t dump_flags(void)
  990. +{
  991. +   /* There's a chance this could become a runtime flag */
  992. +   int flags = JSON_COMPACT;
  993. +#ifdef AST_DEVMODE
  994. +   /* In dev mode, write readable JSON */
  995. +   flags = JSON_INDENT(2) | JSON_PRESERVE_ORDER;
  996. +#endif
  997. +   return flags;
  998. +}
  999. +
  1000. +char *ast_json_dump_string(struct ast_json *root)
  1001. +{
  1002. +   return json_dumps((json_t *)root, dump_flags());
  1003. +}
  1004. +
  1005. +static int write_to_ast_str(const char *buffer, size_t size, void *data)
  1006. +{
  1007. +   struct ast_str **dst = data;
  1008. +   size_t str_size = ast_str_size(*dst);
  1009. +   size_t remaining = str_size - ast_str_strlen(*dst);
  1010. +   int ret;
  1011. +
  1012. +   /* While ast_str_append will grow the ast_str, it won't report
  1013. +    * allocation errors. Fortunately, it's not that hard.
  1014. +    */
  1015. +
  1016. +   /* Remaining needs to be big enough for buffer, plus null char */
  1017. +   while (remaining < size + 1) {
  1018. +       /* doubling the size of the buffer gives us 'amortized
  1019. +        * constant' time.
  1020. +        * See http://stackoverflow.com/a/249695/115478 for info.
  1021. +        */
  1022. +       str_size *= 2;
  1023. +       remaining = str_size - ast_str_strlen(*dst);
  1024. +   }
  1025. +
  1026. +   ret = ast_str_make_space(dst, str_size);
  1027. +   if (ret == -1) {
  1028. +       /* Could not alloc; fail */
  1029. +       return -1;
  1030. +   }
  1031. +
  1032. +   ast_str_append_substr(dst, -1, buffer, size);
  1033. +   return 0;
  1034. +}
  1035. +
  1036. +int ast_json_dump_str(struct ast_json *root, struct ast_str **dst)
  1037. +{
  1038. +   return json_dump_callback((json_t *)root, write_to_ast_str, dst, dump_flags());
  1039. +}
  1040. +
  1041. +
  1042. +int ast_json_dump_file(struct ast_json *root, FILE *output)
  1043. +{
  1044. +   if (root && output) {
  1045. +       return json_dumpf((json_t *)root, output, dump_flags());
  1046. +   }
  1047. +   return -1;
  1048. +}
  1049. +int ast_json_dump_new_file(struct ast_json *root, const char *path)
  1050. +{
  1051. +   return json_dump_file((json_t *)root, path, dump_flags());
  1052. +}
  1053. +
  1054. +/*!
  1055. + * \brief Copy Jansson error struct to ours.
  1056. + */
  1057. +static void copy_error(struct ast_json_error *error, const json_error_t *jansson_error)
  1058. +{
  1059. +   if (error && jansson_error) {
  1060. +       error->line = jansson_error->line;
  1061. +       error->column = jansson_error->column;
  1062. +       error->position = jansson_error->position;
  1063. +       ast_copy_string(error->text, jansson_error->text, sizeof(error->text));
  1064. +       ast_copy_string(error->source, jansson_error->source, sizeof(error->source));
  1065. +   }
  1066. +
  1067. +}
  1068. +
  1069. +static void parse_error(struct ast_json_error *error, const char *text, const char *source)
  1070. +{
  1071. +   if (error != NULL) {
  1072. +       error->line = 0;
  1073. +       error->column = 0;
  1074. +       error->position = 0;
  1075. +       strncpy(error->text, text, sizeof(error->text));
  1076. +       strncpy(error->source, source, sizeof(error->text));
  1077. +   }
  1078. +}
  1079. +
  1080. +struct ast_json *ast_json_load_string(const char *input, struct ast_json_error *error)
  1081. +{
  1082. +   json_error_t jansson_error = {};
  1083. +   struct ast_json *r = NULL;
  1084. +   if (input != NULL) {
  1085. +       r = (struct ast_json *)json_loads(input, 0, &jansson_error);
  1086. +       copy_error(error, &jansson_error);
  1087. +   } else {
  1088. +       parse_error(error, "NULL input string", "<null>");
  1089. +   }
  1090. +   return r;
  1091. +}
  1092. +
  1093. +struct ast_json *ast_json_load_str(const struct ast_str *input, struct ast_json_error *error)
  1094. +{
  1095. +   return ast_json_load_string(ast_str_buffer(input), error);
  1096. +}
  1097. +
  1098. +struct ast_json *ast_json_load_buf(const char *buffer, size_t buflen, struct ast_json_error *error)
  1099. +{
  1100. +   json_error_t jansson_error = {};
  1101. +   struct ast_json *r = (struct ast_json *)json_loadb(buffer, buflen, 0, &jansson_error);
  1102. +   copy_error(error, &jansson_error);
  1103. +   return r;
  1104. +}
  1105. +struct ast_json *ast_json_load_file(FILE *input, struct ast_json_error *error)
  1106. +{
  1107. +   json_error_t jansson_error = {};
  1108. +   struct ast_json *r = NULL;
  1109. +   if (input != NULL) {
  1110. +       r = (struct ast_json *)json_loadf(input, 0, &jansson_error);
  1111. +       copy_error(error, &jansson_error);
  1112. +   } else {
  1113. +       parse_error(error, "NULL input file", "<null>");
  1114. +   }
  1115. +   return r;
  1116. +}
  1117. +struct ast_json *ast_json_load_new_file(const char *path, struct ast_json_error *error)
  1118. +{
  1119. +   json_error_t jansson_error = {};
  1120. +   struct ast_json *r = (struct ast_json *)json_load_file(path, 0, &jansson_error);
  1121. +   copy_error(error, &jansson_error);
  1122. +   return r;
  1123. +}
  1124. +
  1125. +struct ast_json *ast_json_pack(char const *format, ...)
  1126. +{
  1127. +   struct ast_json *ret;
  1128. +   va_list args;
  1129. +   va_start(args, format);
  1130. +   ret = ast_json_vpack(format, args);
  1131. +   va_end(args);
  1132. +   return ret;
  1133. +}
  1134. +struct ast_json *ast_json_vpack(char const *format, va_list ap)
  1135. +{
  1136. +   struct ast_json *r = NULL;
  1137. +   if (format) {
  1138. +       r = (struct ast_json *)json_vpack_ex(NULL, 0, format, ap);
  1139. +   }
  1140. +   return r;
  1141. +}
  1142. +
  1143. +struct ast_json *ast_json_copy(const struct ast_json *value)
  1144. +{
  1145. +   return (struct ast_json *)json_copy((json_t *)value);
  1146. +}
  1147. +struct ast_json *ast_json_deep_copy(const struct ast_json *value)
  1148. +{
  1149. +   return (struct ast_json *)json_deep_copy((json_t *)value);
  1150. +}
  1151. +
  1152. +static int unload_module(void)
  1153. +{
  1154. +   /* Nothing to do */
  1155. +   return 0;
  1156. +}
  1157. +
  1158. +static int load_module(void)
  1159. +{
  1160. +   /* Setup to use Asterisk custom allocators */
  1161. +   json_set_alloc_funcs(json_malloc, json_free);
  1162. +   return AST_MODULE_LOAD_SUCCESS;
  1163. +}
  1164. +
  1165. +AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "JSON library",
  1166. +       .load = load_module,
  1167. +       .unload = unload_module);
  1168. diff -rupN asterisk-11.18.0/res/res_json.exports.in asterisk-11.18.0-oauth/res/res_json.exports.in
  1169. --- asterisk-11.18.0/res/res_json.exports.in    1970-01-01 01:00:00.000000000 +0100
  1170. +++ asterisk-11.18.0-oauth/res/res_json.exports.in  2015-06-22 17:46:02.172534485 +0100
  1171. @@ -0,0 +1,6 @@
  1172. +{
  1173. +   global:
  1174. +       LINKER_SYMBOL_PREFIXast_json_*;
  1175. +   local:
  1176. +       *;
  1177. +};
  1178. diff -rupN asterisk-11.18.0/res/res_xmpp.c asterisk-11.18.0-oauth/res/res_xmpp.c
  1179. --- asterisk-11.18.0/res/res_xmpp.c 2015-06-04 01:44:42.000000000 +0100
  1180. +++ asterisk-11.18.0-oauth/res/res_xmpp.c   2015-06-22 17:46:43.916534485 +0100
  1181. @@ -53,6 +53,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revisi
  1182.  #include "asterisk/event.h"
  1183.  #include "asterisk/cli.h"
  1184.  #include "asterisk/config_options.h"
  1185. +#include "asterisk/json.h"
  1186.  
  1187.  /*** DOCUMENTATION
  1188.     <application name="JabberSend" language="en_US" module="res_xmpp">
  1189. @@ -333,6 +334,7 @@ struct ast_xmpp_client_config {
  1190.         AST_STRING_FIELD(name);        /*!< Name of the client connection */
  1191.         AST_STRING_FIELD(user);        /*!< Username to use for authentication */
  1192.         AST_STRING_FIELD(password);    /*!< Password to use for authentication */
  1193. +       AST_STRING_FIELD(refresh_token);
  1194.         AST_STRING_FIELD(server);      /*!< Server hostname */
  1195.         AST_STRING_FIELD(statusmsg);   /*!< Status message for presence */
  1196.         AST_STRING_FIELD(pubsubnode);  /*!< Pubsub node */
  1197. @@ -401,6 +403,7 @@ static ast_cond_t message_received_condi
  1198.  static ast_mutex_t messagelock;
  1199.  
  1200.  static int xmpp_client_config_post_apply(void *obj, void *arg, int flags);
  1201. +static void fetch_access_token(struct ast_xmpp_client_config *cfg);
  1202.  
  1203.  /*! \brief Destructor function for configuration */
  1204.  static void ast_xmpp_client_config_destructor(void *obj)
  1205. @@ -610,8 +613,8 @@ static int xmpp_config_prelink(void *new
  1206.     if (ast_strlen_zero(clientcfg->user)) {
  1207.         ast_log(LOG_ERROR, "No user specified on client '%s'\n", clientcfg->name);
  1208.         return -1;
  1209. -   } else if (ast_strlen_zero(clientcfg->password)) {
  1210. -       ast_log(LOG_ERROR, "No password specified on client '%s'\n", clientcfg->name);
  1211. +   } else if (ast_strlen_zero(clientcfg->refresh_token)) {
  1212. +       ast_log(LOG_ERROR, "No refresh_token specified on client '%s'\n", clientcfg->name);
  1213.         return -1;
  1214.     } else if (ast_strlen_zero(clientcfg->server)) {
  1215.         ast_log(LOG_ERROR, "No server specified on client '%s'\n", clientcfg->name);
  1216. @@ -2636,7 +2639,9 @@ static int xmpp_client_authenticate_sasl
  1217.     }
  1218.  
  1219.     iks_insert_attrib(auth, "xmlns", IKS_NS_XMPP_SASL);
  1220. -   iks_insert_attrib(auth, "mechanism", "PLAIN");
  1221. +   iks_insert_attrib(auth, "mechanism", "X-OAUTH2");
  1222. +   iks_insert_attrib(auth, "auth:service", "oauth2");
  1223. +   iks_insert_attrib(auth, "xmlns:auth", "http://www.google.com/talk/protocol/auth");
  1224.  
  1225.     if (strchr(client->jid->user, '/')) {
  1226.         char *user = ast_strdupa(client->jid->user);
  1227. @@ -3446,6 +3451,10 @@ static int xmpp_client_reconnect(struct
  1228.         return -1;
  1229.     }
  1230.  
  1231. +   ast_log(LOG_NOTICE , "Connecting to client token : %s\n" , clientcfg->refresh_token);
  1232. +
  1233. +   fetch_access_token(clientcfg);
  1234. +
  1235.     ast_xmpp_client_disconnect(client);
  1236.  
  1237.     client->timeout = 50;
  1238. @@ -3588,12 +3597,12 @@ static void *xmpp_client_thread(void *da
  1239.  
  1240.     do {
  1241.         if (client->state == XMPP_STATE_DISCONNECTING) {
  1242. -           ast_debug(1, "JABBER: Disconnecting client '%s'\n", client->name);
  1243. +           ast_debug(1, "11 JABBER: Disconnecting client '%s'\n", client->name);
  1244.             break;
  1245.         }
  1246.  
  1247.         if (res == IKS_NET_RWERR || client->timeout == 0) {
  1248. -           ast_debug(3, "Connecting client '%s'\n", client->name);
  1249. +           ast_debug(1, "111 Connecting client '%s'\n", client->name);
  1250.             if ((res = xmpp_client_reconnect(client)) != IKS_OK) {
  1251.                 sleep(4);
  1252.                 res = IKS_NET_RWERR;
  1253. @@ -3669,6 +3678,37 @@ static int xmpp_client_config_merge_budd
  1254.     return 1;
  1255.  }
  1256.  
  1257. +static void fetch_access_token(struct ast_xmpp_client_config *cfg) {
  1258. +
  1259. +   char cmd[4096];
  1260. +   char cBuf[4096];
  1261. +   const char * url = "https://www.googleapis.com/oauth2/v3/token";
  1262. +   const char * client_id = "470306186573-jtdetm9fb2b28k4gumaleamea7a14deh.apps.googleusercontent.com";
  1263. +   const char * client_secret = "zWK1W_I-4ZlfRoFnYRoGhsrB";
  1264. +
  1265. +   memset(cmd , 0 , sizeof(cmd));
  1266. +   snprintf(cmd , sizeof(cmd) - 1 , "CURL(%s,client_id=%s&client_secret=%s&refresh_token=%s&grant_type=refresh_token)" , url, client_id, client_secret, cfg->refresh_token);
  1267. +
  1268. +   ast_log(LOG_NOTICE , "Command %s\n" , cmd);
  1269. +
  1270. +   memset(cBuf , 0 , sizeof(cBuf));
  1271. +   ast_func_read(NULL, cmd , cBuf , sizeof(cBuf) - 1);
  1272. +   ast_log(LOG_NOTICE , "Command status : %s \n" , cBuf);
  1273. +
  1274. +   struct ast_json_error error;
  1275. +   struct ast_json * jobj = ast_json_load_string(cBuf, &error);
  1276. +   if(jobj != NULL) {
  1277. +       const char * token = ast_json_string_get(ast_json_object_get(jobj, "access_token"));
  1278. +       if(token != NULL) {
  1279. +           ast_string_field_set(cfg , password, token);
  1280. +       }
  1281. +       ast_log(LOG_NOTICE , "access Token : %s\n", token);
  1282. +   }
  1283. +   else {
  1284. +       ast_log(LOG_ERROR , "object is NULL\n");
  1285. +   }
  1286. +}
  1287. +
  1288.  static int xmpp_client_config_post_apply(void *obj, void *arg, int flags)
  1289.  {
  1290.     struct ast_xmpp_client_config *cfg = obj;
  1291. @@ -4430,7 +4470,7 @@ static int load_module(void)
  1292.     aco_option_register_custom(&cfg_info, "auth_policy", ACO_EXACT, global_options, "accept", global_bitfield_handler, 0);
  1293.  
  1294.     aco_option_register(&cfg_info, "username", ACO_EXACT, client_options, NULL, OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_xmpp_client_config, user));
  1295. -   aco_option_register(&cfg_info, "secret", ACO_EXACT, client_options, NULL, OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_xmpp_client_config, password));
  1296. +   aco_option_register(&cfg_info, "secret", ACO_EXACT, client_options, NULL, OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_xmpp_client_config, refresh_token));
  1297.     aco_option_register(&cfg_info, "serverhost", ACO_EXACT, client_options, NULL, OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_xmpp_client_config, server));
  1298.     aco_option_register(&cfg_info, "statusmessage", ACO_EXACT, client_options, "Online and Available", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_xmpp_client_config, statusmsg));
  1299.     aco_option_register(&cfg_info, "pubsub_node", ACO_EXACT, client_options, NULL, OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_xmpp_client_config, pubsubnode));
  1300. diff -rupN asterisk-11.18.0/tests/test_json.c asterisk-11.18.0-oauth/tests/test_json.c
  1301. --- asterisk-11.18.0/tests/test_json.c  1970-01-01 01:00:00.000000000 +0100
  1302. +++ asterisk-11.18.0-oauth/tests/test_json.c    2015-06-22 17:46:02.172534485 +0100
  1303. @@ -0,0 +1,760 @@
  1304. +/*
  1305. + * Asterisk -- An open source telephony toolkit.
  1306. + *
  1307. + * Copyright (C) 2012 - 2013, Digium, Inc.
  1308. + *
  1309. + * David M. Lee, II <dlee@digium.com>
  1310. + *
  1311. + * See http://www.asterisk.org for more information about
  1312. + * the Asterisk project. Please do not directly contact
  1313. + * any of the maintainers of this project for assistance;
  1314. + * the project provides a web site, mailing lists and IRC
  1315. + * channels for your use.
  1316. + *
  1317. + * This program is free software, distributed under the terms of
  1318. + * the GNU General Public License Version 2. See the LICENSE file
  1319. + * at the top of the source tree.
  1320. + */
  1321. +
  1322. +/*!
  1323. + * \file \brief Test JSON API.
  1324. + *
  1325. + * While some of these tests are actually testing our JSON library wrapper, the bulk of
  1326. + * them are exploratory tests to determine what the behavior of the underlying JSON
  1327. + * library is. This also gives us a good indicator if that behavior changes between
  1328. + * Jansson revisions.
  1329. + *
  1330. + * \author\verbatim David M. Lee, II <dlee@digium.com> \endverbatim
  1331. + *
  1332. + * \ingroup tests
  1333. + */
  1334. +
  1335. +/*** MODULEINFO
  1336. +   <depend>TEST_FRAMEWORK</depend>
  1337. +   <depend>res_json</depend>
  1338. +   <support_level>core</support_level>
  1339. + ***/
  1340. +
  1341. +#include "asterisk.h"
  1342. +
  1343. +ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  1344. +#include "asterisk/json.h"
  1345. +#include "asterisk/module.h"
  1346. +#include "asterisk/test.h"
  1347. +
  1348. +/*!
  1349. + * Number of allocations from JSON library that have not yet been freed.
  1350. + */
  1351. +static size_t alloc_count;
  1352. +
  1353. +/*!@{*/
  1354. +/*!
  1355. + * JSON library has its own reference counting, so we'll provide our own allocators to
  1356. + * test that everything gets freed as expected.
  1357. + */
  1358. +static void *json_debug_malloc(size_t size)
  1359. +{
  1360. +   void *p = ast_malloc(size);
  1361. +   if (p) {
  1362. +       ++alloc_count;
  1363. +   }
  1364. +   return p;
  1365. +}
  1366. +static void json_debug_free(void *p)
  1367. +{
  1368. +   if (p) {
  1369. +       --alloc_count;
  1370. +   }
  1371. +   ast_free(p);
  1372. +}
  1373. +/*!@}*/
  1374. +
  1375. +AST_TEST_DEFINE(json_test)
  1376. +{
  1377. +   enum ast_test_result_state res = AST_TEST_PASS;
  1378. +   struct ast_json *uut = NULL;
  1379. +   struct ast_json *expected = NULL;
  1380. +   struct ast_json *tail = NULL;
  1381. +   struct ast_json *merge = NULL;
  1382. +   struct ast_json *inner_child = NULL;
  1383. +   struct ast_json_iter *iter = NULL;
  1384. +   int uut_res = 0;
  1385. +   int count = 0;
  1386. +   char *str = NULL;
  1387. +   FILE *file = NULL;
  1388. +   char *filename = NULL;
  1389. +   struct ast_str *astr = NULL;
  1390. +
  1391. +   auto void clean_vars(void);
  1392. +   /*!
  1393. +    * Free all local variables and set to NULL. This is an inner function to give a
  1394. +    * decent backtrace if a free() fails.
  1395. +    */
  1396. +   void clean_vars(void) {
  1397. +       ast_json_unref(uut);
  1398. +       ast_json_unref(expected);
  1399. +       ast_json_unref(tail);
  1400. +       ast_json_unref(merge);
  1401. +       ast_json_unref(inner_child);
  1402. +       uut = expected = tail = merge = inner_child = NULL;
  1403. +       iter = NULL;
  1404. +       uut_res = count = 0;
  1405. +       json_debug_free(str);
  1406. +       str = NULL;
  1407. +       if (file) fclose(file);
  1408. +       file = NULL;
  1409. +       free(filename);
  1410. +       filename = NULL;
  1411. +       astr = NULL;
  1412. +   }
  1413. +
  1414. +   /*!
  1415. +    * Macro to free all local variables, and check for memory leaks. This is a macro
  1416. +    * so that ast_test_check() can get correct line numbers.
  1417. +    */
  1418. +#define CLEAN_VARS() do {                  \
  1419. +       clean_vars();                   \
  1420. +       ast_test_check(res, 0 == alloc_count);      \
  1421. +       /* reset alloc_count to prevent false positives */  \
  1422. +       alloc_count = 0;                \
  1423. +   } while (0)
  1424. +
  1425. +   switch (cmd) {
  1426. +   case TEST_INIT:
  1427. +       info->name = "json";
  1428. +       info->category = "/main/json/";
  1429. +       info->summary = "Testing JSON abstraction library.";
  1430. +       info->description = "Test JSON abstraction library.";
  1431. +       return AST_TEST_NOT_RUN;
  1432. +   case TEST_EXECUTE:
  1433. +       break;
  1434. +   }
  1435. +
  1436. +   /* Setup some special malloc tracking functions. */
  1437. +   ast_json_set_alloc_funcs(json_debug_malloc, json_debug_free);
  1438. +
  1439. +   /* incref and decref should be NULL safe */
  1440. +   ast_json_ref(NULL);
  1441. +   ast_json_unref(NULL);
  1442. +   CLEAN_VARS();
  1443. +   CLEAN_VARS();
  1444. +
  1445. +   /* Ridiculous looking tests for the fundamentals */
  1446. +   uut = ast_json_false();
  1447. +   ast_test_check(res, NULL != uut);
  1448. +   ast_test_check(res, AST_JSON_FALSE == ast_json_typeof(uut));
  1449. +   ast_test_check(res, !ast_json_is_null(uut));
  1450. +   ast_test_check(res, !ast_json_is_true(uut));
  1451. +   ast_test_check(res, ast_json_is_false(uut));
  1452. +   CLEAN_VARS();
  1453. +
  1454. +   uut = ast_json_true();
  1455. +   ast_test_check(res, NULL != uut);
  1456. +   ast_test_check(res, AST_JSON_TRUE == ast_json_typeof(uut));
  1457. +   ast_test_check(res, !ast_json_is_null(uut));
  1458. +   ast_test_check(res, ast_json_is_true(uut));
  1459. +   ast_test_check(res, !ast_json_is_false(uut));
  1460. +   CLEAN_VARS();
  1461. +
  1462. +   uut = ast_json_boolean(0);
  1463. +   ast_test_check(res, NULL != uut);
  1464. +   ast_test_check(res, AST_JSON_FALSE == ast_json_typeof(uut));
  1465. +   ast_test_check(res, !ast_json_is_null(uut));
  1466. +   ast_test_check(res, !ast_json_is_true(uut));
  1467. +   ast_test_check(res, ast_json_is_false(uut));
  1468. +   ast_test_check(res, ast_json_equal(uut, ast_json_false()));
  1469. +   ast_test_check(res, !ast_json_equal(uut, ast_json_true()));
  1470. +   CLEAN_VARS();
  1471. +
  1472. +   uut = ast_json_boolean(1);
  1473. +   ast_test_check(res, NULL != uut);
  1474. +   ast_test_check(res, AST_JSON_TRUE == ast_json_typeof(uut));
  1475. +   ast_test_check(res, !ast_json_is_null(uut));
  1476. +   ast_test_check(res, ast_json_is_true(uut));
  1477. +   ast_test_check(res, !ast_json_is_false(uut));
  1478. +   ast_test_check(res, !ast_json_equal(uut, ast_json_false()));
  1479. +   ast_test_check(res, ast_json_equal(uut, ast_json_true()));
  1480. +   CLEAN_VARS();
  1481. +
  1482. +   uut = ast_json_null();
  1483. +   ast_test_check(res, NULL != uut);
  1484. +   ast_test_check(res, AST_JSON_NULL == ast_json_typeof(uut));
  1485. +   ast_test_check(res, ast_json_is_null(uut));
  1486. +   ast_test_check(res, !ast_json_is_true(uut));
  1487. +   ast_test_check(res, !ast_json_is_false(uut));
  1488. +   CLEAN_VARS();
  1489. +
  1490. +   /* NULL isn't null, true or false */
  1491. +   ast_test_check(res, !ast_json_is_null(NULL));
  1492. +   ast_test_check(res, !ast_json_is_false(NULL));
  1493. +   ast_test_check(res, !ast_json_is_true(NULL));
  1494. +
  1495. +   /* Basic string tests */
  1496. +   uut = ast_json_string_create("Hello, json");
  1497. +   ast_test_check(res, NULL != uut);
  1498. +   ast_test_check(res, AST_JSON_STRING == ast_json_typeof(uut));
  1499. +   ast_test_check(res, 0 == strcmp("Hello, json", ast_json_string_get(uut)));
  1500. +
  1501. +   uut_res = ast_json_string_set(uut, NULL);
  1502. +   ast_test_check(res, -1 == uut_res);
  1503. +   ast_test_check(res, 0 == strcmp("Hello, json", ast_json_string_get(uut)));
  1504. +
  1505. +   uut_res = ast_json_string_set(uut, "Not UTF-8 - \xff");
  1506. +   ast_test_check(res, -1 == uut_res);
  1507. +   ast_test_check(res, 0 == strcmp("Hello, json", ast_json_string_get(uut)));
  1508. +
  1509. +   uut_res = ast_json_string_set(uut, "Is UTF-8 - \xE2\x98\xBA");
  1510. +   ast_test_check(res, 0 == uut_res);
  1511. +   ast_test_check(res, 0 == strcmp("Is UTF-8 - \xE2\x98\xBA", ast_json_string_get(uut)));
  1512. +
  1513. +   uut_res = ast_json_string_set(uut, "Goodbye, json");
  1514. +   ast_test_check(res, 0 == uut_res);
  1515. +   ast_test_check(res, 0 == strcmp("Goodbye, json", ast_json_string_get(uut)));
  1516. +   CLEAN_VARS();
  1517. +
  1518. +   /* formatted string */
  1519. +   uut = ast_json_stringf("Hello, %s", "json");
  1520. +   expected = ast_json_string_create("Hello, json");
  1521. +   ast_test_check(res, NULL != uut);
  1522. +   ast_test_check(res, ast_json_equal(expected, uut));
  1523. +   CLEAN_VARS();
  1524. +
  1525. +   /* Non-UTF-8 strings are invalid */
  1526. +   uut = ast_json_stringf("Not UTF-8 - %s", "\xff");
  1527. +   ast_test_check(res, NULL == uut);
  1528. +   CLEAN_VARS();
  1529. +
  1530. +   /* NULL string */
  1531. +   uut = ast_json_string_create(NULL);
  1532. +   ast_test_check(res, NULL == uut);
  1533. +   CLEAN_VARS();
  1534. +
  1535. +   /* NULL format string */
  1536. +   uut = ast_json_stringf(NULL);
  1537. +   ast_test_check(res, NULL == uut);
  1538. +   CLEAN_VARS();
  1539. +
  1540. +   /* NULL JSON strings */
  1541. +   ast_test_check(res, NULL == ast_json_string_create(NULL));
  1542. +   ast_test_check(res, NULL == ast_json_string_get(NULL));
  1543. +   ast_test_check(res, -1 == ast_json_string_set(NULL, "not null"));
  1544. +
  1545. +   /* string_value from non-string elements should return NULL */
  1546. +   ast_test_check(res, NULL == ast_json_string_get(ast_json_null()));
  1547. +   ast_test_check(res, NULL == ast_json_string_get(ast_json_false()));
  1548. +   ast_test_check(res, NULL == ast_json_string_get(ast_json_true()));
  1549. +
  1550. +   /* Integer tests */
  1551. +   uut = ast_json_integer_create(0);
  1552. +   ast_test_check(res, NULL != uut);
  1553. +   ast_test_check(res, AST_JSON_INTEGER == ast_json_typeof(uut));
  1554. +   ast_test_check(res, 0 == ast_json_integer_get(uut));
  1555. +
  1556. +   uut_res = ast_json_integer_set(uut, 1);
  1557. +   ast_test_check(res, 0 == uut_res);
  1558. +   ast_test_check(res, 1 == ast_json_integer_get(uut));
  1559. +
  1560. +   uut_res = ast_json_integer_set(uut, -1);
  1561. +   ast_test_check(res, 0 == uut_res);
  1562. +   ast_test_check(res, -1 == ast_json_integer_get(uut));
  1563. +
  1564. +   uut_res = ast_json_integer_set(uut, LLONG_MAX);
  1565. +   ast_test_check(res, 0 == uut_res);
  1566. +   ast_test_check(res, LLONG_MAX == ast_json_integer_get(uut));
  1567. +
  1568. +   uut_res = ast_json_integer_set(uut, LLONG_MIN);
  1569. +   ast_test_check(res, 0 == uut_res);
  1570. +   ast_test_check(res, LLONG_MIN == ast_json_integer_get(uut));
  1571. +   CLEAN_VARS();
  1572. +
  1573. +   /* Non-ints return 0 integer value */
  1574. +   ast_test_check(res, 0 == ast_json_integer_get(ast_json_null()));
  1575. +   ast_test_check(res, 0 == ast_json_integer_get(ast_json_true()));
  1576. +   ast_test_check(res, 0 == ast_json_integer_get(ast_json_false()));
  1577. +
  1578. +   /* JSON NULL integers */
  1579. +   ast_test_check(res, 0 == ast_json_integer_get(NULL));
  1580. +   ast_test_check(res, -1 == ast_json_integer_set(NULL, 911));
  1581. +   ast_test_check(res, 0 == ast_json_array_size(NULL));
  1582. +
  1583. +   /* No magical parsing of strings into ints */
  1584. +   uut = ast_json_string_create("314");
  1585. +   ast_test_check(res, 0 == ast_json_integer_get(uut));
  1586. +   CLEAN_VARS();
  1587. +
  1588. +   /* Or vice-versa */
  1589. +   uut = ast_json_integer_create(314);
  1590. +   ast_test_check(res, NULL == ast_json_string_get(uut));
  1591. +   CLEAN_VARS();
  1592. +
  1593. +   /* array creation */
  1594. +   uut = ast_json_array_create();
  1595. +   ast_test_check(res, NULL != uut);
  1596. +   ast_test_check(res, AST_JSON_ARRAY == ast_json_typeof(uut));
  1597. +   ast_test_check(res, 0 == ast_json_array_size(uut));
  1598. +   CLEAN_VARS();
  1599. +
  1600. +   /* array append */
  1601. +   uut = ast_json_array_create();
  1602. +   uut_res = ast_json_array_append(uut, ast_json_string_create("one"));
  1603. +   ast_test_check(res, 0 == uut_res);
  1604. +   ast_test_check(res, 1 == ast_json_array_size(uut));
  1605. +   ast_test_check(res, 0 == strcmp("one", ast_json_string_get(ast_json_array_get(uut, 0))));
  1606. +   /* index out of range */
  1607. +   ast_test_check(res, NULL == ast_json_array_get(uut, 1));
  1608. +   ast_test_check(res, NULL == ast_json_array_get(uut, -1));
  1609. +   CLEAN_VARS();
  1610. +
  1611. +   /* array insert */
  1612. +   uut = ast_json_pack("[s]", "one");
  1613. +   uut_res = ast_json_array_insert(uut, 0, ast_json_string_create("zero"));
  1614. +   ast_test_check(res, 0 == uut_res);
  1615. +   ast_test_check(res, 2 == ast_json_array_size(uut));
  1616. +   ast_test_check(res, 0 == strcmp("zero", ast_json_string_get(ast_json_array_get(uut, 0))));
  1617. +   ast_test_check(res, 0 == strcmp("one", ast_json_string_get(ast_json_array_get(uut, 1))));
  1618. +   CLEAN_VARS();
  1619. +
  1620. +   /* array set */
  1621. +   uut = ast_json_pack("[s, s]", "zero", "one");
  1622. +   uut_res = ast_json_array_set(uut, 1, ast_json_integer_create(1));
  1623. +   ast_test_check(res, 0 == uut_res);
  1624. +   ast_test_check(res, 2 == ast_json_array_size(uut));
  1625. +   ast_test_check(res, 0 == strcmp("zero", ast_json_string_get(ast_json_array_get(uut, 0))));
  1626. +   ast_test_check(res, 1 == ast_json_integer_get(ast_json_array_get(uut, 1)));
  1627. +   CLEAN_VARS();
  1628. +
  1629. +   /* array remove */
  1630. +   uut = ast_json_array_create();
  1631. +   uut_res = ast_json_array_append(uut, ast_json_string_create("zero"));
  1632. +   uut_res = ast_json_array_append(uut, ast_json_integer_create(1));
  1633. +   uut_res = ast_json_array_remove(uut, 0);
  1634. +   ast_test_check(res, 0 == uut_res);
  1635. +   ast_test_check(res, 1 == ast_json_array_size(uut));
  1636. +   ast_test_check(res, 1 == ast_json_integer_get(ast_json_array_get(uut, 0)));
  1637. +   CLEAN_VARS();
  1638. +
  1639. +   /* array clear */
  1640. +   uut = ast_json_array_create();
  1641. +   uut_res = ast_json_array_append(uut, ast_json_string_create("zero"));
  1642. +   uut_res = ast_json_array_append(uut, ast_json_integer_create(1));
  1643. +   uut_res = ast_json_array_clear(uut);
  1644. +   ast_test_check(res, 0 == uut_res);
  1645. +   ast_test_check(res, 0 == ast_json_array_size(uut));
  1646. +   CLEAN_VARS();
  1647. +
  1648. +   /* array extending */
  1649. +   expected = ast_json_array_create();
  1650. +   ast_json_array_append(expected, ast_json_string_create("a"));
  1651. +   ast_json_array_append(expected, ast_json_string_create("b"));
  1652. +   ast_json_array_append(expected, ast_json_string_create("c"));
  1653. +   ast_json_array_append(expected, ast_json_integer_create(1));
  1654. +   ast_json_array_append(expected, ast_json_integer_create(2));
  1655. +   ast_json_array_append(expected, ast_json_integer_create(3));
  1656. +
  1657. +   uut = ast_json_array_create();
  1658. +   ast_json_array_append(uut, ast_json_string_create("a"));
  1659. +   ast_json_array_append(uut, ast_json_string_create("b"));
  1660. +   ast_json_array_append(uut, ast_json_string_create("c"));
  1661. +
  1662. +   tail = ast_json_array_create();
  1663. +   ast_json_array_append(tail, ast_json_integer_create(1));
  1664. +   ast_json_array_append(tail, ast_json_integer_create(2));
  1665. +   ast_json_array_append(tail, ast_json_integer_create(3));
  1666. +
  1667. +   uut_res = ast_json_array_extend(uut, tail);
  1668. +   ast_test_check(res, 0 == uut_res);
  1669. +   ast_test_check(res, ast_json_equal(expected, uut));
  1670. +   /* tail is preserved */
  1671. +   ast_test_check(res, 3 == ast_json_array_size(tail));
  1672. +   CLEAN_VARS();
  1673. +
  1674. +   /* array NULL checks */
  1675. +   ast_test_check(res, 0 == ast_json_array_size(NULL));
  1676. +   ast_test_check(res, NULL == ast_json_array_get(NULL, 0));
  1677. +   ast_test_check(res, -1 == ast_json_array_set(NULL, 0, ast_json_null()));
  1678. +   ast_test_check(res, -1 == ast_json_array_append(NULL, ast_json_null()));
  1679. +   ast_test_check(res, -1 == ast_json_array_insert(NULL, 0, ast_json_null()));
  1680. +   ast_test_check(res, -1 == ast_json_array_remove(NULL, 0));
  1681. +   ast_test_check(res, -1 == ast_json_array_clear(NULL));
  1682. +   uut = ast_json_array_create();
  1683. +   ast_test_check(res, -1 == ast_json_array_extend(uut, NULL));
  1684. +   ast_test_check(res, -1 == ast_json_array_extend(NULL, uut));
  1685. +   ast_test_check(res, -1 == ast_json_array_extend(NULL, NULL));
  1686. +   CLEAN_VARS();
  1687. +
  1688. +   /* object allocation */
  1689. +   uut = ast_json_object_create();
  1690. +   ast_test_check(res, NULL != uut);
  1691. +   ast_test_check(res, AST_JSON_OBJECT == ast_json_typeof(uut));
  1692. +   ast_test_check(res, 0 == ast_json_object_size(uut));
  1693. +   CLEAN_VARS();
  1694. +
  1695. +   /* object set */
  1696. +   expected = ast_json_pack("{s: i, s: i, s: i}", "one", 1, "two", 2, "three", 3);
  1697. +   uut = ast_json_object_create();
  1698. +   uut_res = ast_json_object_set(uut, "one", ast_json_integer_create(1));
  1699. +   ast_test_check(res, 0 == uut_res);
  1700. +   uut_res = ast_json_object_set(uut, "two", ast_json_integer_create(2));
  1701. +   ast_test_check(res, 0 == uut_res);
  1702. +   uut_res = ast_json_object_set(uut, "three", ast_json_integer_create(3));
  1703. +   ast_test_check(res, 0 == uut_res);
  1704. +   ast_test_check(res, ast_json_equal(expected, uut));
  1705. +   ast_test_check(res, NULL == ast_json_object_get(uut, "dne"));
  1706. +   CLEAN_VARS();
  1707. +
  1708. +   /* object set existing */
  1709. +   uut = ast_json_pack("{s: i, s: i, s: i}", "one", 1, "two", 2, "three", 3);
  1710. +   uut_res = ast_json_object_set(uut, "two", ast_json_integer_create(-2));
  1711. +   ast_test_check(res, 0 == uut_res);
  1712. +   ast_test_check(res, -2 == ast_json_integer_get(ast_json_object_get(uut, "two")));
  1713. +   CLEAN_VARS();
  1714. +
  1715. +   /* object get */
  1716. +   uut = ast_json_pack("{s: i, s: i, s: i}", "one", 1, "two", 2, "three", 3);
  1717. +   ast_test_check(res, 2 == ast_json_integer_get(ast_json_object_get(uut, "two")));
  1718. +   ast_test_check(res, NULL == ast_json_object_get(uut, "dne"));
  1719. +   ast_test_check(res, NULL == ast_json_object_get(uut, NULL));
  1720. +   CLEAN_VARS();
  1721. +
  1722. +   /* object del */
  1723. +   expected = ast_json_object_create();
  1724. +   uut = ast_json_object_create();
  1725. +   uut_res = ast_json_object_set(uut, "one", ast_json_integer_create(1));
  1726. +   uut_res = ast_json_object_del(uut, "one");
  1727. +   ast_test_check(res, 0 == uut_res);
  1728. +   ast_test_check(res, ast_json_equal(expected, uut));
  1729. +   uut_res = ast_json_object_del(uut, "dne");
  1730. +   ast_test_check(res, -1 == uut_res);
  1731. +   CLEAN_VARS();
  1732. +
  1733. +   /* object clear */
  1734. +   uut = ast_json_object_create();
  1735. +   ast_json_object_set(uut, "one", ast_json_integer_create(1));
  1736. +   ast_json_object_set(uut, "two", ast_json_integer_create(2));
  1737. +   ast_json_object_set(uut, "three", ast_json_integer_create(3));
  1738. +   uut_res = ast_json_object_clear(uut);
  1739. +   ast_test_check(res, 0 == uut_res);
  1740. +   ast_test_check(res, 0 == ast_json_object_size(uut));
  1741. +   CLEAN_VARS();
  1742. +
  1743. +   /* object merging - all */
  1744. +   uut = ast_json_object_create();
  1745. +   ast_json_object_set(uut, "one", ast_json_integer_create(1));
  1746. +   ast_json_object_set(uut, "two", ast_json_integer_create(2));
  1747. +   ast_json_object_set(uut, "three", ast_json_integer_create(3));
  1748. +
  1749. +   merge = ast_json_object_create();
  1750. +   ast_json_object_set(merge, "three", ast_json_integer_create(-3));
  1751. +   ast_json_object_set(merge, "four", ast_json_integer_create(-4));
  1752. +   ast_json_object_set(merge, "five", ast_json_integer_create(-5));
  1753. +
  1754. +   expected = ast_json_object_create();
  1755. +   ast_json_object_set(expected, "one", ast_json_integer_create(1));
  1756. +   ast_json_object_set(expected, "two", ast_json_integer_create(2));
  1757. +   ast_json_object_set(expected, "three", ast_json_integer_create(-3));
  1758. +   ast_json_object_set(expected, "four", ast_json_integer_create(-4));
  1759. +   ast_json_object_set(expected, "five", ast_json_integer_create(-5));
  1760. +
  1761. +   uut_res = ast_json_object_update(uut, merge);
  1762. +   ast_test_check(res, 0 == uut_res);
  1763. +   ast_test_check(res, ast_json_equal(expected, uut));
  1764. +   /* merge object is untouched */
  1765. +   ast_test_check(res, 3 == ast_json_object_size(merge));
  1766. +   CLEAN_VARS();
  1767. +
  1768. +   /* object merging - existing */
  1769. +   uut = ast_json_object_create();
  1770. +   ast_json_object_set(uut, "one", ast_json_integer_create(1));
  1771. +   ast_json_object_set(uut, "two", ast_json_integer_create(2));
  1772. +   ast_json_object_set(uut, "three", ast_json_integer_create(3));
  1773. +
  1774. +   merge = ast_json_object_create();
  1775. +   ast_json_object_set(merge, "three", ast_json_integer_create(-3));
  1776. +   ast_json_object_set(merge, "four", ast_json_integer_create(-4));
  1777. +   ast_json_object_set(merge, "five", ast_json_integer_create(-5));
  1778. +
  1779. +   expected = ast_json_object_create();
  1780. +   ast_json_object_set(expected, "one", ast_json_integer_create(1));
  1781. +   ast_json_object_set(expected, "two", ast_json_integer_create(2));
  1782. +   ast_json_object_set(expected, "three", ast_json_integer_create(-3));
  1783. +
  1784. +   uut_res = ast_json_object_update_existing(uut, merge);
  1785. +   ast_test_check(res, 0 == uut_res);
  1786. +   ast_test_check(res, ast_json_equal(expected, uut));
  1787. +   /* merge object is untouched */
  1788. +   ast_test_check(res, 3 == ast_json_object_size(merge));
  1789. +   CLEAN_VARS();
  1790. +
  1791. +   /* object merging - missing */
  1792. +   uut = ast_json_object_create();
  1793. +   ast_json_object_set(uut, "one", ast_json_integer_create(1));
  1794. +   ast_json_object_set(uut, "two", ast_json_integer_create(2));
  1795. +   ast_json_object_set(uut, "three", ast_json_integer_create(3));
  1796. +
  1797. +   merge = ast_json_object_create();
  1798. +   ast_json_object_set(merge, "three", ast_json_integer_create(-3));
  1799. +   ast_json_object_set(merge, "four", ast_json_integer_create(-4));
  1800. +   ast_json_object_set(merge, "five", ast_json_integer_create(-5));
  1801. +
  1802. +   expected = ast_json_object_create();
  1803. +   ast_json_object_set(expected, "one", ast_json_integer_create(1));
  1804. +   ast_json_object_set(expected, "two", ast_json_integer_create(2));
  1805. +   ast_json_object_set(expected, "three", ast_json_integer_create(3));
  1806. +   ast_json_object_set(expected, "four", ast_json_integer_create(-4));
  1807. +   ast_json_object_set(expected, "five", ast_json_integer_create(-5));
  1808. +
  1809. +   uut_res = ast_json_object_update_missing(uut, merge);
  1810. +   ast_test_check(res, 0 == uut_res);
  1811. +   ast_test_check(res, ast_json_equal(expected, uut));
  1812. +   /* merge object is untouched */
  1813. +   ast_test_check(res, 3 == ast_json_object_size(merge));
  1814. +   CLEAN_VARS();
  1815. +
  1816. +   /* Object NULL testing */
  1817. +   ast_test_check(res, 0 == ast_json_object_size(NULL));
  1818. +   ast_test_check(res, NULL == ast_json_object_get(NULL, "not null"));
  1819. +   ast_test_check(res, -1 == ast_json_object_set(NULL, "not null", ast_json_null()));
  1820. +   ast_test_check(res, -1 == ast_json_object_del(NULL, "not null"));
  1821. +   ast_test_check(res, -1 == ast_json_object_clear(NULL));
  1822. +   uut = ast_json_object_create();
  1823. +   ast_test_check(res, -1 == ast_json_object_update(NULL, uut));
  1824. +   ast_test_check(res, -1 == ast_json_object_update(uut, NULL));
  1825. +   ast_test_check(res, -1 == ast_json_object_update(NULL, NULL));
  1826. +   ast_test_check(res, -1 == ast_json_object_update_existing(NULL, uut));
  1827. +   ast_test_check(res, -1 == ast_json_object_update_existing(uut, NULL));
  1828. +   ast_test_check(res, -1 == ast_json_object_update_existing(NULL, NULL));
  1829. +   ast_test_check(res, -1 == ast_json_object_update_missing(NULL, uut));
  1830. +   ast_test_check(res, -1 == ast_json_object_update_missing(uut, NULL));
  1831. +   ast_test_check(res, -1 == ast_json_object_update_missing(NULL, NULL));
  1832. +   CLEAN_VARS();
  1833. +
  1834. +   /* Object iterator testing */
  1835. +   uut = ast_json_object_create();
  1836. +   ast_json_object_set(uut, "one", ast_json_integer_create(1));
  1837. +   ast_json_object_set(uut, "two", ast_json_integer_create(2));
  1838. +   ast_json_object_set(uut, "three", ast_json_integer_create(3));
  1839. +   ast_json_object_set(uut, "four", ast_json_integer_create(4));
  1840. +   ast_json_object_set(uut, "five", ast_json_integer_create(5));
  1841. +
  1842. +   /* Iterate through the object; be aware that order isn't specified */
  1843. +   iter = ast_json_object_iter(uut);
  1844. +   ast_test_check(res, NULL != iter);
  1845. +   while (NULL != iter) {
  1846. +       if (0 == strcmp("one", ast_json_object_iter_key(iter))) {
  1847. +           ast_test_check(res, 1 == ast_json_integer_get(ast_json_object_iter_value(iter)));
  1848. +       } else if (0 == strcmp("two", ast_json_object_iter_key(iter))) {
  1849. +           ast_test_check(res, 2 == ast_json_integer_get(ast_json_object_iter_value(iter)));
  1850. +       } else if (0 == strcmp("three", ast_json_object_iter_key(iter))) {
  1851. +           ast_test_check(res, 3 == ast_json_integer_get(ast_json_object_iter_value(iter)));
  1852. +       } else if (0 == strcmp("four", ast_json_object_iter_key(iter))) {
  1853. +           ast_test_check(res, 4 == ast_json_integer_get(ast_json_object_iter_value(iter)));
  1854. +       } else if (0 == strcmp("five", ast_json_object_iter_key(iter))) {
  1855. +           ast_test_check(res, 5 == ast_json_integer_get(ast_json_object_iter_value(iter)));
  1856. +       } else {
  1857. +           /* Unexpected key */
  1858. +           ast_test_check(res, 0);
  1859. +       }
  1860. +       iter = ast_json_object_iter_next(uut, iter);
  1861. +       ++count;
  1862. +   }
  1863. +   ast_test_check(res, 5 == count);
  1864. +
  1865. +   /* iterator non-existing key */
  1866. +   iter = ast_json_object_iter_at(uut, "dne");
  1867. +   ast_test_check(res, NULL == iter);
  1868. +
  1869. +   /* iterator specific key */
  1870. +   iter = ast_json_object_iter_at(uut, "three");
  1871. +   ast_test_check(res, NULL != iter);
  1872. +   ast_test_check(res, 3 == ast_json_integer_get(ast_json_object_iter_value(iter)));
  1873. +
  1874. +   /* set via iter */
  1875. +   iter = ast_json_object_iter_at(uut, "three");
  1876. +   uut_res = ast_json_object_iter_set(uut, iter, ast_json_integer_create(-3));
  1877. +   ast_test_check(res, 0 == uut_res);
  1878. +   ast_test_check(res, -3 == ast_json_integer_get(ast_json_object_get(uut, "three")));
  1879. +   CLEAN_VARS();
  1880. +
  1881. +   /* iterator NULL tests */
  1882. +   uut = ast_json_object_create();
  1883. +   ast_test_check(res, NULL == ast_json_object_iter(NULL));
  1884. +   ast_test_check(res, NULL == ast_json_object_iter_at(NULL, "not null"));
  1885. +   ast_test_check(res, NULL == ast_json_object_iter_next(NULL, NULL));
  1886. +   ast_test_check(res, NULL == ast_json_object_iter_next(uut, NULL));
  1887. +   ast_test_check(res, NULL == ast_json_object_iter_key(NULL));
  1888. +   ast_test_check(res, NULL == ast_json_object_iter_value(NULL));
  1889. +   ast_test_check(res, -1 == ast_json_object_iter_set(NULL, NULL, ast_json_null()));
  1890. +   ast_test_check(res, -1 == ast_json_object_iter_set(uut, NULL, ast_json_null()));
  1891. +   CLEAN_VARS();
  1892. +
  1893. +   /* dump/load string */
  1894. +   expected = ast_json_pack("{ s: i }", "one", 1);
  1895. +   str = ast_json_dump_string(expected);
  1896. +   ast_test_check(res, NULL != str);
  1897. +   uut = ast_json_load_string(str, NULL);
  1898. +   ast_test_check(res, NULL != uut);
  1899. +   ast_test_check(res, ast_json_equal(expected, uut));
  1900. +   CLEAN_VARS();
  1901. +
  1902. +   /* dump_string NULL */
  1903. +   ast_test_check(res, NULL == ast_json_dump_string(NULL));
  1904. +
  1905. +   /* dump/load ast_str */
  1906. +   expected = ast_json_pack("{ s: i }", "one", 1);
  1907. +   astr = ast_str_create(1); /* should expand to hold output */
  1908. +   uut_res = ast_json_dump_str(expected, &astr);
  1909. +   ast_test_check(res, 0 == uut_res);
  1910. +   uut = ast_json_load_str(astr, NULL);
  1911. +   ast_test_check(res, NULL != uut);
  1912. +   ast_test_check(res, ast_json_equal(expected, uut));
  1913. +   ast_free(astr);
  1914. +   CLEAN_VARS();
  1915. +
  1916. +   /* dump ast_str growth failure */
  1917. +   expected = ast_json_pack("{ s: i }", "one", 1);
  1918. +   astr = ast_str_alloca(1); /* cannot grow */
  1919. +   uut_res = ast_json_dump_str(expected, &astr);
  1920. +   ast_test_check(res, 0 != uut_res);
  1921. +   CLEAN_VARS();
  1922. +
  1923. +   /* load buffer */
  1924. +   str = "{ \"one\": 1 } trailing garbage";
  1925. +   uut = ast_json_load_string(str, NULL);
  1926. +   ast_test_check(res, NULL == uut);
  1927. +   uut = ast_json_load_buf(str, strlen("{ \"one\": 1 }"), NULL);
  1928. +   ast_test_check(res, NULL != uut);
  1929. +   str = NULL;
  1930. +   CLEAN_VARS();
  1931. +
  1932. +   /* dump/load file */
  1933. +   expected = ast_json_pack("{ s: i }", "one", 1);
  1934. +   filename = tempnam(NULL, "ast-json");
  1935. +   file = fopen(filename, "w");
  1936. +   uut_res = ast_json_dump_file(expected, file);
  1937. +   ast_test_check(res, 0 == uut_res);
  1938. +   fclose(file);
  1939. +   file = fopen(filename, "r");
  1940. +   uut = ast_json_load_file(file, NULL);
  1941. +   ast_test_check(res, ast_json_equal(expected, uut));
  1942. +   CLEAN_VARS();
  1943. +
  1944. +   /* dump/load filename */
  1945. +   expected = ast_json_pack("{ s: i }", "one", 1);
  1946. +   filename = tempnam(NULL, "ast-json");
  1947. +   uut_res = ast_json_dump_new_file(expected, filename);
  1948. +   ast_test_check(res, 0 == uut_res);
  1949. +   uut = ast_json_load_new_file(filename, NULL);
  1950. +   ast_test_check(res, ast_json_equal(expected, uut));
  1951. +   CLEAN_VARS();
  1952. +
  1953. +   /* dump/load NULL tests */
  1954. +   uut = ast_json_load_string("{ \"one\": 1 }", NULL);
  1955. +   ast_test_check(res, NULL != uut);
  1956. +   filename = tempnam(NULL, "ast-json");
  1957. +   file = fopen(filename, "w");
  1958. +   ast_test_check(res, NULL == ast_json_dump_string(NULL));
  1959. +   ast_test_check(res, -1 == ast_json_dump_file(NULL, file));
  1960. +   ast_test_check(res, -1 == ast_json_dump_file(uut, NULL));
  1961. +   ast_test_check(res, -1 == ast_json_dump_file(NULL, NULL));
  1962. +   ast_test_check(res, -1 == ast_json_dump_new_file(uut, NULL));
  1963. +   ast_test_check(res, -1 == ast_json_dump_new_file(NULL, filename));
  1964. +   ast_test_check(res, -1 == ast_json_dump_new_file(NULL, NULL));
  1965. +   ast_test_check(res, NULL == ast_json_load_string(NULL, NULL));
  1966. +   ast_test_check(res, NULL == ast_json_load_buf(NULL, 0, NULL));
  1967. +   ast_test_check(res, NULL == ast_json_load_file(NULL, NULL));
  1968. +   ast_test_check(res, NULL == ast_json_load_new_file(NULL, NULL));
  1969. +   CLEAN_VARS();
  1970. +
  1971. +   /* parse errors */
  1972. +   ast_test_check(res, NULL == ast_json_load_string("'singleton'", NULL));
  1973. +   ast_test_check(res, NULL == ast_json_load_string("{ no value }", NULL));
  1974. +   ast_test_check(res, NULL == ast_json_load_string("{ 'no': 'curly' ", NULL));
  1975. +   ast_test_check(res, NULL == ast_json_load_string("[ 'no', 'square'", NULL));
  1976. +   ast_test_check(res, NULL == ast_json_load_string("{ 1: 'int key' }", NULL));
  1977. +   ast_test_check(res, NULL == ast_json_load_string("", NULL));
  1978. +   ast_test_check(res, NULL == ast_json_load_string("{ 'missing' 'colon' }", NULL));
  1979. +   ast_test_check(res, NULL == ast_json_load_string("[ 'missing' 'comma' ]", NULL));
  1980. +
  1981. +   /* pack test */
  1982. +   expected = ast_json_array_create();
  1983. +   ast_json_array_append(expected, ast_json_array_create());
  1984. +   ast_json_array_append(expected, ast_json_object_create());
  1985. +   ast_json_array_append(ast_json_array_get(expected, 0), ast_json_integer_create(1));
  1986. +   ast_json_array_append(ast_json_array_get(expected, 0), ast_json_integer_create(2));
  1987. +   ast_json_object_set(ast_json_array_get(expected, 1), "cool", ast_json_true());
  1988. +   uut = ast_json_pack("[[i,i],{s:b}]", 1, 2, "cool", 1);
  1989. +   ast_test_check(res, NULL != uut);
  1990. +   ast_test_check(res, ast_json_equal(expected, uut));
  1991. +   CLEAN_VARS();
  1992. +
  1993. +   /* pack errors */
  1994. +   ast_test_check(res, NULL == ast_json_pack(NULL));
  1995. +   ast_test_check(res, NULL == ast_json_pack("{s:i", "no curly", 911));
  1996. +   ast_test_check(res, NULL == ast_json_pack("[s, s", "no", "square"));
  1997. +
  1998. +   /* copy test */
  1999. +   expected = ast_json_pack("{s: {s: i}}", "outer", "inner", 8675309);
  2000. +   uut = ast_json_copy(expected);
  2001. +   ast_test_check(res, NULL != uut);
  2002. +   ast_test_check(res, ast_json_equal(expected, uut));
  2003. +   ast_test_check(res, ast_json_object_get(expected, "outer") == ast_json_object_get(uut, "outer"));
  2004. +   CLEAN_VARS();
  2005. +
  2006. +   /* deep copy test */
  2007. +   expected = ast_json_pack("{s: {s: i}}", "outer", "inner", 8675309);
  2008. +   uut = ast_json_deep_copy(expected);
  2009. +   ast_test_check(res, NULL != uut);
  2010. +   ast_test_check(res, ast_json_equal(expected, uut));
  2011. +   ast_test_check(res, ast_json_object_get(expected, "outer") != ast_json_object_get(uut, "outer"));
  2012. +   /* Changing the inner value of one should not change the other */
  2013. +   ast_json_integer_set(ast_json_object_get(ast_json_object_get(uut, "outer"), "inner"), 411);
  2014. +   ast_test_check(res, !ast_json_equal(expected, uut));
  2015. +   CLEAN_VARS();
  2016. +
  2017. +   /* copy NULL */
  2018. +   ast_test_check(res, NULL == ast_json_copy(NULL));
  2019. +   ast_test_check(res, NULL == ast_json_deep_copy(NULL));
  2020. +
  2021. +   /* circular reference testing */
  2022. +   /* Cannot add self */
  2023. +   uut = ast_json_object_create();
  2024. +   uut_res = ast_json_object_set(uut, "myself", uut);
  2025. +   ast_test_check(res, -1 == uut_res);
  2026. +   ast_test_check(res, 0 == ast_json_object_size(uut));
  2027. +   CLEAN_VARS();
  2028. +
  2029. +   uut = ast_json_array_create();
  2030. +   uut_res = ast_json_object_set(uut, "myself", uut);
  2031. +   ast_test_check(res, -1 == uut_res);
  2032. +   ast_test_check(res, 0 == ast_json_array_size(uut));
  2033. +   CLEAN_VARS();
  2034. +
  2035. +   /* can add to self if you're clever enough, but it should not encode */
  2036. +   uut = ast_json_object_create();
  2037. +   inner_child = ast_json_object_create();
  2038. +   uut_res = ast_json_object_set(uut, "inner_child", ast_json_ref(inner_child));   /* incref to keep a reference */
  2039. +   ast_test_check(res, 0 == uut_res);
  2040. +   uut_res = ast_json_object_set(inner_child, "parent", ast_json_ref(uut));   /* incref to keep a reference */
  2041. +   ast_test_check(res, 0 == uut_res);
  2042. +   str = ast_json_dump_string(uut);
  2043. +   ast_test_check(res, NULL == str);
  2044. +   /* Circular refs screw up reference counting, so break the cycle */
  2045. +   ast_json_object_clear(inner_child);
  2046. +   CLEAN_VARS();
  2047. +
  2048. +   return res;
  2049. +}
  2050. +
  2051. +static int unload_module(void)
  2052. +{
  2053. +   AST_TEST_UNREGISTER(json_test);
  2054. +   return 0;
  2055. +}
  2056. +
  2057. +static int load_module(void)
  2058. +{
  2059. +   AST_TEST_REGISTER(json_test);
  2060. +   return AST_MODULE_LOAD_SUCCESS;
  2061. +}
  2062. +
  2063. +AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "JSON testing.");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement