ffi-hydrogen 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +30 -0
  5. data/.travis.yml +10 -0
  6. data/Gemfile +6 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +72 -0
  9. data/Rakefile +46 -0
  10. data/bench/both.rb +86 -0
  11. data/bench/encode.rb +57 -0
  12. data/bench/encrypt.rb +80 -0
  13. data/bench/init.rb +5 -0
  14. data/bin/console +14 -0
  15. data/bin/setup +8 -0
  16. data/ffi-hydrogen.gemspec +31 -0
  17. data/lib/ffi/hydrogen.rb +216 -0
  18. data/vendor/.clang-format +2 -0
  19. data/vendor/.gitignore +3 -0
  20. data/vendor/README.md +2 -0
  21. data/vendor/libhydrogen/.clang-format +95 -0
  22. data/vendor/libhydrogen/.gitignore +32 -0
  23. data/vendor/libhydrogen/.travis.yml +22 -0
  24. data/vendor/libhydrogen/LICENSE +18 -0
  25. data/vendor/libhydrogen/Makefile +61 -0
  26. data/vendor/libhydrogen/Makefile.arduino +51 -0
  27. data/vendor/libhydrogen/README.md +29 -0
  28. data/vendor/libhydrogen/hydrogen.c +18 -0
  29. data/vendor/libhydrogen/hydrogen.h +317 -0
  30. data/vendor/libhydrogen/impl/common.h +316 -0
  31. data/vendor/libhydrogen/impl/core.h +220 -0
  32. data/vendor/libhydrogen/impl/gimli-core/portable.h +39 -0
  33. data/vendor/libhydrogen/impl/gimli-core/sse2.h +97 -0
  34. data/vendor/libhydrogen/impl/gimli-core.h +25 -0
  35. data/vendor/libhydrogen/impl/hash.h +138 -0
  36. data/vendor/libhydrogen/impl/hydrogen_p.h +83 -0
  37. data/vendor/libhydrogen/impl/kdf.h +20 -0
  38. data/vendor/libhydrogen/impl/kx.h +441 -0
  39. data/vendor/libhydrogen/impl/pwhash.h +281 -0
  40. data/vendor/libhydrogen/impl/random.h +376 -0
  41. data/vendor/libhydrogen/impl/secretbox.h +236 -0
  42. data/vendor/libhydrogen/impl/sign.h +207 -0
  43. data/vendor/libhydrogen/impl/x25519.h +383 -0
  44. data/vendor/libhydrogen/library.properties +10 -0
  45. data/vendor/libhydrogen/logo.png +0 -0
  46. data/vendor/libhydrogen/tests/tests.c +431 -0
  47. data/vendor/main.c +140 -0
  48. data/vendor/stringencoders/.gitignore +25 -0
  49. data/vendor/stringencoders/.travis.yml +13 -0
  50. data/vendor/stringencoders/AUTHORS +1 -0
  51. data/vendor/stringencoders/COPYING +2 -0
  52. data/vendor/stringencoders/ChangeLog +170 -0
  53. data/vendor/stringencoders/Doxyfile +276 -0
  54. data/vendor/stringencoders/INSTALL +119 -0
  55. data/vendor/stringencoders/LICENSE +22 -0
  56. data/vendor/stringencoders/Makefile.am +3 -0
  57. data/vendor/stringencoders/NEWS +3 -0
  58. data/vendor/stringencoders/README +2 -0
  59. data/vendor/stringencoders/README.md +32 -0
  60. data/vendor/stringencoders/bootstrap.sh +3 -0
  61. data/vendor/stringencoders/configure-gcc-hardened.sh +16 -0
  62. data/vendor/stringencoders/configure.ac +44 -0
  63. data/vendor/stringencoders/doxy/footer.html +34 -0
  64. data/vendor/stringencoders/doxy/header.html +85 -0
  65. data/vendor/stringencoders/indent.sh +9 -0
  66. data/vendor/stringencoders/javascript/base64-speed.html +43 -0
  67. data/vendor/stringencoders/javascript/base64-test.html +209 -0
  68. data/vendor/stringencoders/javascript/base64.html +18 -0
  69. data/vendor/stringencoders/javascript/base64.js +176 -0
  70. data/vendor/stringencoders/javascript/qunit.css +119 -0
  71. data/vendor/stringencoders/javascript/qunit.js +1062 -0
  72. data/vendor/stringencoders/javascript/urlparse-test.html +367 -0
  73. data/vendor/stringencoders/javascript/urlparse.js +328 -0
  74. data/vendor/stringencoders/make-ci.sh +13 -0
  75. data/vendor/stringencoders/makerelease.sh +16 -0
  76. data/vendor/stringencoders/python/b85.py +176 -0
  77. data/vendor/stringencoders/src/Makefile.am +134 -0
  78. data/vendor/stringencoders/src/arraytoc.c +85 -0
  79. data/vendor/stringencoders/src/arraytoc.h +43 -0
  80. data/vendor/stringencoders/src/extern_c_begin.h +3 -0
  81. data/vendor/stringencoders/src/extern_c_end.h +3 -0
  82. data/vendor/stringencoders/src/html_named_entities_generator.py +203 -0
  83. data/vendor/stringencoders/src/modp_ascii.c +159 -0
  84. data/vendor/stringencoders/src/modp_ascii.h +162 -0
  85. data/vendor/stringencoders/src/modp_ascii_data.h +84 -0
  86. data/vendor/stringencoders/src/modp_ascii_gen.c +55 -0
  87. data/vendor/stringencoders/src/modp_b16.c +125 -0
  88. data/vendor/stringencoders/src/modp_b16.h +148 -0
  89. data/vendor/stringencoders/src/modp_b16_data.h +104 -0
  90. data/vendor/stringencoders/src/modp_b16_gen.c +65 -0
  91. data/vendor/stringencoders/src/modp_b2.c +69 -0
  92. data/vendor/stringencoders/src/modp_b2.h +130 -0
  93. data/vendor/stringencoders/src/modp_b2_data.h +44 -0
  94. data/vendor/stringencoders/src/modp_b2_gen.c +36 -0
  95. data/vendor/stringencoders/src/modp_b36.c +108 -0
  96. data/vendor/stringencoders/src/modp_b36.h +170 -0
  97. data/vendor/stringencoders/src/modp_b64.c +254 -0
  98. data/vendor/stringencoders/src/modp_b64.h +236 -0
  99. data/vendor/stringencoders/src/modp_b64_data.h +477 -0
  100. data/vendor/stringencoders/src/modp_b64_gen.c +168 -0
  101. data/vendor/stringencoders/src/modp_b64r.c +254 -0
  102. data/vendor/stringencoders/src/modp_b64r.h +242 -0
  103. data/vendor/stringencoders/src/modp_b64r_data.h +477 -0
  104. data/vendor/stringencoders/src/modp_b64w.c +254 -0
  105. data/vendor/stringencoders/src/modp_b64w.h +231 -0
  106. data/vendor/stringencoders/src/modp_b64w_data.h +477 -0
  107. data/vendor/stringencoders/src/modp_b85.c +109 -0
  108. data/vendor/stringencoders/src/modp_b85.h +171 -0
  109. data/vendor/stringencoders/src/modp_b85_data.h +36 -0
  110. data/vendor/stringencoders/src/modp_b85_gen.c +65 -0
  111. data/vendor/stringencoders/src/modp_bjavascript.c +65 -0
  112. data/vendor/stringencoders/src/modp_bjavascript.h +105 -0
  113. data/vendor/stringencoders/src/modp_bjavascript_data.h +84 -0
  114. data/vendor/stringencoders/src/modp_bjavascript_gen.c +58 -0
  115. data/vendor/stringencoders/src/modp_burl.c +228 -0
  116. data/vendor/stringencoders/src/modp_burl.h +259 -0
  117. data/vendor/stringencoders/src/modp_burl_data.h +136 -0
  118. data/vendor/stringencoders/src/modp_burl_gen.c +121 -0
  119. data/vendor/stringencoders/src/modp_html.c +128 -0
  120. data/vendor/stringencoders/src/modp_html.h +53 -0
  121. data/vendor/stringencoders/src/modp_html_named_entities.h +9910 -0
  122. data/vendor/stringencoders/src/modp_json.c +315 -0
  123. data/vendor/stringencoders/src/modp_json.h +103 -0
  124. data/vendor/stringencoders/src/modp_json_data.h +57 -0
  125. data/vendor/stringencoders/src/modp_json_gen.py +60 -0
  126. data/vendor/stringencoders/src/modp_mainpage.h +120 -0
  127. data/vendor/stringencoders/src/modp_numtoa.c +350 -0
  128. data/vendor/stringencoders/src/modp_numtoa.h +100 -0
  129. data/vendor/stringencoders/src/modp_qsiter.c +76 -0
  130. data/vendor/stringencoders/src/modp_qsiter.h +71 -0
  131. data/vendor/stringencoders/src/modp_stdint.h +43 -0
  132. data/vendor/stringencoders/src/modp_utf8.c +88 -0
  133. data/vendor/stringencoders/src/modp_utf8.h +38 -0
  134. data/vendor/stringencoders/src/modp_xml.c +311 -0
  135. data/vendor/stringencoders/src/modp_xml.h +166 -0
  136. data/vendor/stringencoders/src/stringencoders.pc +10 -0
  137. data/vendor/stringencoders/src/stringencoders.pc.in +10 -0
  138. data/vendor/stringencoders/test/Makefile.am +113 -0
  139. data/vendor/stringencoders/test/apr_base64.c +262 -0
  140. data/vendor/stringencoders/test/apr_base64.h +120 -0
  141. data/vendor/stringencoders/test/cxx_test.cc +482 -0
  142. data/vendor/stringencoders/test/minunit.h +82 -0
  143. data/vendor/stringencoders/test/modp_ascii_test.c +281 -0
  144. data/vendor/stringencoders/test/modp_b16_test.c +288 -0
  145. data/vendor/stringencoders/test/modp_b2_test.c +250 -0
  146. data/vendor/stringencoders/test/modp_b64_test.c +266 -0
  147. data/vendor/stringencoders/test/modp_b85_test.c +130 -0
  148. data/vendor/stringencoders/test/modp_bjavascript_test.c +137 -0
  149. data/vendor/stringencoders/test/modp_burl_test.c +423 -0
  150. data/vendor/stringencoders/test/modp_html_test.c +296 -0
  151. data/vendor/stringencoders/test/modp_json_test.c +336 -0
  152. data/vendor/stringencoders/test/modp_numtoa_test.c +545 -0
  153. data/vendor/stringencoders/test/modp_qsiter_test.c +280 -0
  154. data/vendor/stringencoders/test/modp_utf8_test.c +188 -0
  155. data/vendor/stringencoders/test/modp_xml_test.c +339 -0
  156. data/vendor/stringencoders/test/speedtest.c +241 -0
  157. data/vendor/stringencoders/test/speedtest_ascii.c +345 -0
  158. data/vendor/stringencoders/test/speedtest_msg.c +78 -0
  159. data/vendor/stringencoders/test/speedtest_numtoa.c +276 -0
  160. metadata +314 -0
@@ -0,0 +1,296 @@
1
+ /* we compile as C90 but use snprintf */
2
+ #define _ISOC99_SOURCE
3
+
4
+ #include <stdio.h>
5
+ #include <stdlib.h>
6
+ #include <string.h>
7
+
8
+ #include "minunit.h"
9
+
10
+ #include "modp_html.h"
11
+
12
+ /**
13
+ * Test empty input to decode
14
+ */
15
+ static char* testHtmlDecodeEmpty(void)
16
+ {
17
+ const char* junk = "junk";
18
+ int d;
19
+ size_t consumed;
20
+ d = modp_html_decode_char_at(NULL, 10, &consumed);
21
+ mu_assert_int_equals(d, -1);
22
+
23
+ d = modp_html_decode_char_at(junk, 0, &consumed);
24
+ mu_assert_int_equals(d, -1);
25
+
26
+ return 0;
27
+ }
28
+
29
+ /**
30
+ * Test characters that should be unchanged
31
+ * Mostly a copy from URL encoding
32
+ */
33
+ static char* testHtmlDecodeUntouched(void)
34
+ {
35
+ const char* digits = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_~!$()*,;:@/?";
36
+
37
+ int i;
38
+ int ch;
39
+ size_t consumed;
40
+
41
+ for (i = 0; i < (int)strlen(digits); ++i) {
42
+ consumed = 0;
43
+ ch = modp_html_decode_char_at(digits + i, 1, &consumed);
44
+ mu_assert_int_equals(ch, digits[i]);
45
+ mu_assert_int_equals(consumed, 1);
46
+ }
47
+ return 0;
48
+ }
49
+
50
+ static char* testHtmlDecodeDigits(void)
51
+ {
52
+ size_t consumed;
53
+ int ch;
54
+ const char* s1 = "&#98;";
55
+ const char* s2 = "&#00;";
56
+ const char* s3 = "&#001;";
57
+ const char* s4 = "&#1;";
58
+ const char* s5 = "&#01";
59
+ const char* s6 = "&#1";
60
+ const char* s7 = "&#";
61
+ const char* s8 = "&#01X";
62
+
63
+ consumed = 0;
64
+ ch = modp_html_decode_char_at(s1, strlen(s1), &consumed);
65
+ mu_assert_int_equals(ch, 98);
66
+ mu_assert_int_equals(consumed, 5);
67
+
68
+ consumed = 0;
69
+ ch = modp_html_decode_char_at(s2, strlen(s2), &consumed);
70
+ mu_assert_int_equals(ch, 0);
71
+ mu_assert_int_equals(consumed, 5);
72
+
73
+ consumed = 0;
74
+ ch = modp_html_decode_char_at(s3, strlen(s3), &consumed);
75
+ mu_assert_int_equals(ch, 1);
76
+ mu_assert_int_equals(consumed, 6);
77
+
78
+ consumed = 0;
79
+ ch = modp_html_decode_char_at(s4, strlen(s4), &consumed);
80
+ mu_assert_int_equals(ch, 1);
81
+ mu_assert_int_equals(consumed, 4);
82
+
83
+ consumed = 0;
84
+ ch = modp_html_decode_char_at(s5, strlen(s5), &consumed);
85
+ mu_assert_int_equals(ch, 1);
86
+ mu_assert_int_equals(consumed, 4);
87
+
88
+ consumed = 0;
89
+ ch = modp_html_decode_char_at(s6, strlen(s6), &consumed);
90
+ mu_assert_int_equals(ch, 1);
91
+ mu_assert_int_equals(consumed, 3);
92
+
93
+ consumed = 0;
94
+ ch = modp_html_decode_char_at(s7, strlen(s7), &consumed);
95
+ mu_assert_int_equals(ch, '&');
96
+ mu_assert_int_equals(consumed, 1);
97
+
98
+ consumed = 0;
99
+ ch = modp_html_decode_char_at(s8, strlen(s8), &consumed);
100
+ mu_assert_int_equals(ch, 1);
101
+ mu_assert_int_equals(consumed, 4);
102
+
103
+ return 0;
104
+ }
105
+
106
+ static char* testHtmlDecodeHexDigits(void)
107
+ {
108
+ size_t consumed;
109
+ int ch;
110
+
111
+ {
112
+ const char* s1 = "&#x98;";
113
+ consumed = 0;
114
+ ch = modp_html_decode_char_at(s1, strlen(s1), &consumed);
115
+ mu_assert_int_equals(ch, 0x98);
116
+ mu_assert_int_equals(consumed, 6);
117
+ }
118
+
119
+ {
120
+ const char* s1 = "&#x00;";
121
+ consumed = 0;
122
+ ch = modp_html_decode_char_at(s1, strlen(s1), &consumed);
123
+ mu_assert_int_equals(ch, 0);
124
+ mu_assert_int_equals(consumed, 6);
125
+ }
126
+
127
+ {
128
+ const char* s1 = "&#x001;";
129
+ consumed = 0;
130
+ ch = modp_html_decode_char_at(s1, strlen(s1), &consumed);
131
+ mu_assert_int_equals(ch, 1);
132
+ mu_assert_int_equals(consumed, 7);
133
+ }
134
+
135
+ {
136
+ const char* s1 = "&#x1;";
137
+ consumed = 0;
138
+ ch = modp_html_decode_char_at(s1, strlen(s1), &consumed);
139
+ mu_assert_int_equals(ch, 1);
140
+ mu_assert_int_equals(consumed, 5);
141
+ }
142
+
143
+ {
144
+ const char* s1 = "&#x01";
145
+ consumed = 0;
146
+ ch = modp_html_decode_char_at(s1, strlen(s1), &consumed);
147
+ mu_assert_int_equals(ch, 1);
148
+ mu_assert_int_equals(consumed, 5);
149
+ }
150
+
151
+ {
152
+ const char* s1 = "&#x1";
153
+ consumed = 0;
154
+ ch = modp_html_decode_char_at(s1, strlen(s1), &consumed);
155
+ mu_assert_int_equals(ch, 1);
156
+ mu_assert_int_equals(consumed, 4);
157
+ }
158
+
159
+ {
160
+ const char* s1 = "&#x";
161
+ consumed = 0;
162
+ ch = modp_html_decode_char_at(s1, strlen(s1), &consumed);
163
+ mu_assert_int_equals(ch, '&');
164
+ mu_assert_int_equals(consumed, 1);
165
+ }
166
+
167
+ {
168
+ const char* s1 = "&#x01X";
169
+ consumed = 0;
170
+ ch = modp_html_decode_char_at(s1, strlen(s1), &consumed);
171
+ mu_assert_int_equals(ch, 1);
172
+ mu_assert_int_equals(consumed, 5);
173
+ }
174
+
175
+ {
176
+ const char* s1 = "&#X";
177
+ consumed = 0;
178
+ ch = modp_html_decode_char_at(s1, strlen(s1), &consumed);
179
+ mu_assert_int_equals(ch, '&');
180
+ mu_assert_int_equals(consumed, 1);
181
+ }
182
+
183
+ {
184
+ const char* s1 = "&#X1";
185
+ consumed = 0;
186
+ ch = modp_html_decode_char_at(s1, strlen(s1), &consumed);
187
+ mu_assert_int_equals(ch, 1);
188
+ mu_assert_int_equals(consumed, 4);
189
+ }
190
+
191
+ {
192
+ /* overflow */
193
+ const char* s1 = "&#xFFFFFF";
194
+ consumed = 0;
195
+ ch = modp_html_decode_char_at(s1, strlen(s1), &consumed);
196
+ mu_assert_int_equals(ch, '&');
197
+ mu_assert_int_equals(consumed, 1);
198
+ }
199
+
200
+ {
201
+ /* overflow */
202
+ const char* s1 = "&#99999999999999999999999";
203
+ consumed = 0;
204
+ ch = modp_html_decode_char_at(s1, strlen(s1), &consumed);
205
+ mu_assert_int_equals(ch, '&');
206
+ mu_assert_int_equals(consumed, 1);
207
+ }
208
+
209
+ return 0;
210
+ }
211
+
212
+ /**
213
+ * http://www.w3.org/html/wg/drafts/html/master/text-level-semantics.html#character-references
214
+ * http://www.w3.org/html/wg/drafts/html/master/text-level-semantics.html#named-character-references
215
+ * http://www.w3.org/html/wg/drafts/html/master/text-level-semantics.html#consume-a-character-reference
216
+ */
217
+
218
+ static char* testHtmlDecodeNamedEntities(void)
219
+ {
220
+ size_t consumed;
221
+ int cp;
222
+
223
+ {
224
+ consumed = 0;
225
+ const char* s1 = "&amp;";
226
+ cp = modp_html_decode_char_at(s1, strlen(s1), &consumed);
227
+ mu_assert_int_equals(cp, 38);
228
+ mu_assert_int_equals(consumed, 5);
229
+ }
230
+
231
+ {
232
+ consumed = 0;
233
+ const char* s1 = "&AMP;";
234
+ cp = modp_html_decode_char_at(s1, strlen(s1), &consumed);
235
+ mu_assert_int_equals(cp, 38);
236
+ mu_assert_int_equals(consumed, 5);
237
+ }
238
+
239
+ {
240
+ consumed = 0;
241
+ const char* s1 = "&amp";
242
+ cp = modp_html_decode_char_at(s1, strlen(s1), &consumed);
243
+ mu_assert_int_equals(cp, 38);
244
+ mu_assert_int_equals(consumed, 4);
245
+ }
246
+
247
+ {
248
+ consumed = 0;
249
+ const char* s1 = "&AMP";
250
+ cp = modp_html_decode_char_at(s1, strlen(s1), &consumed);
251
+ mu_assert_int_equals(cp, 38);
252
+ mu_assert_int_equals(consumed, 4);
253
+ }
254
+
255
+ {
256
+ /* &not is a special one where '&not' and '&not;' are defined */
257
+ consumed = 0;
258
+ const char* s1 = "&notit";
259
+ cp = modp_html_decode_char_at(s1, strlen(s1), &consumed);
260
+ mu_assert_int_equals(cp, 172);
261
+ mu_assert_int_equals(consumed, 4);
262
+ }
263
+
264
+ {
265
+ /* &not is a special one where '&not' and '&not;' are defined */
266
+ consumed = 0;
267
+ const char* s1 = "&notin;";
268
+ cp = modp_html_decode_char_at(s1, strlen(s1), &consumed);
269
+ mu_assert_int_equals(cp, 8713);
270
+ mu_assert_int_equals(consumed, 7);
271
+ }
272
+
273
+ {
274
+ /* off the edge */
275
+ consumed = 0;
276
+ const char* s1 = "&NotHumpEqua";
277
+ cp = modp_html_decode_char_at(s1, strlen(s1), &consumed);
278
+ mu_assert_int_equals(cp, 38);
279
+ mu_assert_int_equals(consumed, 1);
280
+ }
281
+
282
+ return 0;
283
+ }
284
+
285
+ static char* all_tests(void)
286
+ {
287
+ mu_run_test(testHtmlDecodeEmpty);
288
+ mu_run_test(testHtmlDecodeUntouched);
289
+ mu_run_test(testHtmlDecodeDigits);
290
+ mu_run_test(testHtmlDecodeHexDigits);
291
+ mu_run_test(testHtmlDecodeNamedEntities);
292
+
293
+ return 0;
294
+ }
295
+
296
+ UNITTESTS
@@ -0,0 +1,336 @@
1
+ /* we compile as C90 but use snprintf */
2
+ #define _ISOC99_SOURCE
3
+
4
+ #include <stdio.h>
5
+ #include <stdlib.h>
6
+ #include <string.h>
7
+
8
+ #include "minunit.h"
9
+
10
+ #include "modp_json.h"
11
+
12
+ /**
13
+ * Smoke test
14
+ */
15
+ static char* test_json_init()
16
+ {
17
+ char buf[100];
18
+ modp_json_ctx ctx;
19
+
20
+ modp_json_init(&ctx, NULL);
21
+ mu_assert(ctx.dest == NULL);
22
+
23
+ modp_json_init(&ctx, buf);
24
+ mu_assert(ctx.dest != NULL);
25
+
26
+ modp_json_init(&ctx, NULL);
27
+ mu_assert(ctx.dest == NULL);
28
+
29
+ return 0;
30
+ }
31
+
32
+ static char* test_json_map_empty()
33
+ {
34
+ size_t len;
35
+ char buf[100];
36
+ modp_json_ctx ctx;
37
+ const int expected = 2;
38
+
39
+ /* do count */
40
+ modp_json_init(&ctx, buf);
41
+ modp_json_map_open(&ctx);
42
+ modp_json_map_close(&ctx);
43
+ len = modp_json_end(&ctx);
44
+ mu_assert_int_equals(len, expected);
45
+
46
+ /* do real thing */
47
+ modp_json_init(&ctx, buf);
48
+ modp_json_map_open(&ctx);
49
+ modp_json_map_close(&ctx);
50
+ len = modp_json_end(&ctx);
51
+ mu_assert_int_equals(len, expected);
52
+ mu_assert_int_equals(strlen(buf), expected);
53
+ mu_assert_str_equals("{}", buf);
54
+ return 0;
55
+ }
56
+
57
+ static char* test_json_map_1()
58
+ {
59
+ size_t len;
60
+ char buf[100];
61
+ modp_json_ctx ctx;
62
+ const int expected = 13;
63
+
64
+ /* do count */
65
+ modp_json_init(&ctx, buf);
66
+ modp_json_map_open(&ctx);
67
+ modp_json_add_string(&ctx, "foo", 3);
68
+ modp_json_add_string(&ctx, "bar", 3);
69
+ modp_json_map_close(&ctx);
70
+ len = modp_json_end(&ctx);
71
+ mu_assert_int_equals(len, expected);
72
+
73
+ /* do real thing */
74
+ modp_json_init(&ctx, buf);
75
+ modp_json_map_open(&ctx);
76
+ modp_json_add_string(&ctx, "foo", 3);
77
+ modp_json_add_string(&ctx, "bar", 3);
78
+ modp_json_map_close(&ctx);
79
+ len = modp_json_end(&ctx);
80
+
81
+ mu_assert_int_equals(strlen(buf), expected);
82
+ mu_assert_str_equals("{\"foo\":\"bar\"}", buf);
83
+ mu_assert_int_equals(len, expected);
84
+ return 0;
85
+ }
86
+
87
+ static char* test_json_map_2()
88
+ {
89
+ size_t len;
90
+ char buf[100];
91
+ modp_json_ctx ctx;
92
+ const int expected = 21;
93
+
94
+ /* do count */
95
+ modp_json_init(&ctx, buf);
96
+ modp_json_map_open(&ctx);
97
+ modp_json_add_string(&ctx, "foo", 3);
98
+ modp_json_add_string(&ctx, "bar", 3);
99
+ modp_json_add_string(&ctx, "x", 1);
100
+ modp_json_add_string(&ctx, "y", 1);
101
+ modp_json_map_close(&ctx);
102
+ len = modp_json_end(&ctx);
103
+ mu_assert_int_equals(len, expected);
104
+
105
+ /* do real thing */
106
+ modp_json_init(&ctx, buf);
107
+ modp_json_map_open(&ctx);
108
+ modp_json_add_string(&ctx, "foo", 3);
109
+ modp_json_add_string(&ctx, "bar", 3);
110
+ modp_json_add_string(&ctx, "x", 1);
111
+ modp_json_add_string(&ctx, "y", 1);
112
+ modp_json_map_close(&ctx);
113
+ len = modp_json_end(&ctx);
114
+
115
+ mu_assert_int_equals(strlen(buf), expected);
116
+ mu_assert_str_equals("{\"foo\":\"bar\",\"x\":\"y\"}", buf);
117
+ mu_assert_int_equals(len, expected);
118
+ return 0;
119
+ }
120
+
121
+ static char* test_json_nest_1()
122
+ {
123
+ size_t len;
124
+ char buf[100];
125
+ modp_json_ctx ctx;
126
+ const int expected = 17;
127
+
128
+ /* do count */
129
+ modp_json_init(&ctx, buf);
130
+ modp_json_map_open(&ctx);
131
+ modp_json_add_string(&ctx, "foo", 3);
132
+ modp_json_ary_open(&ctx);
133
+ modp_json_add_string(&ctx, "x", 1);
134
+ modp_json_add_string(&ctx, "y", 1);
135
+ modp_json_ary_close(&ctx);
136
+ modp_json_map_close(&ctx);
137
+ len = modp_json_end(&ctx);
138
+ mu_assert_int_equals(len, expected);
139
+
140
+ /* do real thing */
141
+ modp_json_init(&ctx, buf);
142
+ modp_json_map_open(&ctx);
143
+ modp_json_add_string(&ctx, "foo", 3);
144
+ modp_json_ary_open(&ctx);
145
+ modp_json_add_string(&ctx, "x", 1);
146
+ modp_json_add_string(&ctx, "y", 1);
147
+ modp_json_ary_close(&ctx);
148
+ modp_json_map_close(&ctx);
149
+ len = modp_json_end(&ctx);
150
+
151
+ mu_assert_int_equals(len, expected);
152
+ mu_assert_int_equals(strlen(buf), expected);
153
+ mu_assert_str_equals(buf, "{\"foo\":[\"x\",\"y\"]}");
154
+
155
+ return 0;
156
+ }
157
+
158
+ static char* test_json_ary_empty()
159
+ {
160
+ size_t len;
161
+ char buf[100];
162
+ modp_json_ctx ctx;
163
+ const int expected = 2;
164
+
165
+ /* do count */
166
+ modp_json_init(&ctx, buf);
167
+ modp_json_ary_open(&ctx);
168
+ modp_json_ary_close(&ctx);
169
+ len = modp_json_end(&ctx);
170
+ mu_assert_int_equals(len, expected);
171
+
172
+ /* do real thing */
173
+ modp_json_init(&ctx, buf);
174
+ modp_json_ary_open(&ctx);
175
+ modp_json_ary_close(&ctx);
176
+ len = modp_json_end(&ctx);
177
+ mu_assert_int_equals(len, expected);
178
+ mu_assert_int_equals(strlen(buf), expected);
179
+ mu_assert_str_equals("[]", buf);
180
+ return 0;
181
+ }
182
+
183
+ static char* test_json_ary_1()
184
+ {
185
+ size_t len;
186
+ char buf[100];
187
+ modp_json_ctx ctx;
188
+ const int expected = 7;
189
+
190
+ /* do count */
191
+ modp_json_init(&ctx, buf);
192
+ modp_json_ary_open(&ctx);
193
+ modp_json_add_string(&ctx, "foo", 3);
194
+ modp_json_ary_close(&ctx);
195
+ len = modp_json_end(&ctx);
196
+ mu_assert_int_equals(len, expected);
197
+
198
+ /* do real thing */
199
+ modp_json_init(&ctx, buf);
200
+ modp_json_ary_open(&ctx);
201
+ modp_json_add_string(&ctx, "foo", 3);
202
+ modp_json_ary_close(&ctx);
203
+ len = modp_json_end(&ctx);
204
+
205
+ mu_assert_int_equals(strlen(buf), expected);
206
+ mu_assert_str_equals("[\"foo\"]", buf);
207
+ mu_assert_int_equals(len, expected);
208
+ return 0;
209
+ }
210
+
211
+ static char* test_json_ary_2()
212
+ {
213
+ size_t len;
214
+ char buf[100];
215
+ modp_json_ctx ctx;
216
+ const int expected = 13;
217
+
218
+ /* do count */
219
+ modp_json_init(&ctx, buf);
220
+ modp_json_ary_open(&ctx);
221
+ modp_json_add_string(&ctx, "foo", 3);
222
+ modp_json_add_string(&ctx, "bar", 3);
223
+ modp_json_ary_close(&ctx);
224
+ len = modp_json_end(&ctx);
225
+ mu_assert_int_equals(len, expected);
226
+
227
+ /* do real thing */
228
+ modp_json_init(&ctx, buf);
229
+ modp_json_ary_open(&ctx);
230
+ modp_json_add_string(&ctx, "foo", 3);
231
+ modp_json_add_string(&ctx, "bar", 3);
232
+ modp_json_ary_close(&ctx);
233
+ len = modp_json_end(&ctx);
234
+
235
+ mu_assert_int_equals(strlen(buf), expected);
236
+ mu_assert_str_equals("[\"foo\",\"bar\"]", buf);
237
+ mu_assert_int_equals(len, expected);
238
+ return 0;
239
+ }
240
+
241
+ static char* test_json_int32()
242
+ {
243
+ size_t len;
244
+ char buf[100];
245
+ modp_json_ctx ctx;
246
+
247
+ /* do count */
248
+ modp_json_init(&ctx, NULL);
249
+ modp_json_add_int32(&ctx, 123);
250
+ len = modp_json_end(&ctx);
251
+ mu_assert_int_equals(len, 3);
252
+
253
+ modp_json_init(&ctx, buf);
254
+ modp_json_add_int32(&ctx, -123);
255
+ len = modp_json_end(&ctx);
256
+ mu_assert_int_equals(len, 4);
257
+ mu_assert_str_equals("-123", buf);
258
+
259
+ modp_json_init(&ctx, NULL);
260
+ modp_json_add_int32(&ctx, -123);
261
+ len = modp_json_end(&ctx);
262
+ mu_assert_int_equals(len, 4);
263
+
264
+ modp_json_init(&ctx, buf);
265
+ modp_json_add_int32(&ctx, -123);
266
+ len = modp_json_end(&ctx);
267
+ mu_assert_int_equals(len, 4);
268
+ mu_assert_str_equals("-123", buf);
269
+
270
+ return 0;
271
+ }
272
+
273
+ static char* test_json_uint64()
274
+ {
275
+ size_t len;
276
+ char buf[100];
277
+ unsigned long long val = (1ULL << 53);
278
+ modp_json_ctx ctx;
279
+
280
+ /* do count */
281
+ modp_json_init(&ctx, NULL);
282
+ modp_json_add_uint64(&ctx, val, 0);
283
+ len = modp_json_end(&ctx);
284
+ mu_assert_int_equals(len, 16);
285
+
286
+ modp_json_init(&ctx, buf);
287
+ modp_json_add_uint64(&ctx, val, 0);
288
+ len = modp_json_end(&ctx);
289
+ mu_assert_int_equals(len, 16);
290
+ mu_assert_str_equals("9007199254740992", buf);
291
+
292
+ /* force string-only */
293
+ modp_json_init(&ctx, NULL);
294
+ modp_json_add_uint64(&ctx, val, 1);
295
+ len = modp_json_end(&ctx);
296
+ mu_assert_int_equals(len, 18);
297
+
298
+ modp_json_init(&ctx, buf);
299
+ modp_json_add_uint64(&ctx, val, 1);
300
+ len = modp_json_end(&ctx);
301
+ mu_assert_int_equals(len, 18);
302
+ mu_assert_str_equals("\"9007199254740992\"", buf);
303
+
304
+ /* automatic stringonly mode */
305
+ val = (1ULL << 54);
306
+
307
+ modp_json_init(&ctx, NULL);
308
+ modp_json_add_uint64(&ctx, val, 0);
309
+ len = modp_json_end(&ctx);
310
+ mu_assert_int_equals(len, 19);
311
+
312
+ modp_json_init(&ctx, buf);
313
+ modp_json_add_uint64(&ctx, val, 0);
314
+ len = modp_json_end(&ctx);
315
+ mu_assert_int_equals(len, 19);
316
+ mu_assert_str_equals("\"18014398509481984\"", buf);
317
+
318
+ return 0;
319
+ }
320
+
321
+ static char* all_tests()
322
+ {
323
+ mu_run_test(test_json_init);
324
+ mu_run_test(test_json_map_empty);
325
+ mu_run_test(test_json_map_1);
326
+ mu_run_test(test_json_map_2);
327
+ mu_run_test(test_json_ary_empty);
328
+ mu_run_test(test_json_ary_1);
329
+ mu_run_test(test_json_ary_2);
330
+ mu_run_test(test_json_nest_1);
331
+ mu_run_test(test_json_int32);
332
+ mu_run_test(test_json_uint64);
333
+ return 0;
334
+ }
335
+
336
+ UNITTESTS