ffi-hydrogen 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,137 @@
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_bjavascript.h"
11
+
12
+ /**
13
+ * Tests input where no escaping happens
14
+ */
15
+ static char* testNoEscape(void)
16
+ {
17
+ char buf[100];
18
+ const char* s1 = "this is a string";
19
+ const size_t len1 = strlen(s1);
20
+ size_t d = modp_bjavascript_encode(buf, s1, len1);
21
+
22
+ mu_assert_int_equals(len1, d);
23
+ mu_assert_str_equals(buf, s1);
24
+
25
+ size_t sz = modp_bjavascript_encode_strlen(s1, len1);
26
+ mu_assert_int_equals(sz, len1);
27
+
28
+ return 0;
29
+ }
30
+
31
+ static char* testSimpleEscape(void)
32
+ {
33
+ char buf[100];
34
+ const char* s1 = "\\this\nis a string\n";
35
+ const char* s2 = "\\\\this\\nis a string\\n";
36
+ const size_t len1 = strlen(s1);
37
+ const size_t len2 = strlen(s2);
38
+ size_t d = modp_bjavascript_encode(buf, s1, len1);
39
+
40
+ mu_assert_int_equals(len2, d);
41
+ mu_assert_str_equals(buf, s2);
42
+
43
+ size_t sz = modp_bjavascript_encode_strlen(s1, len1);
44
+ mu_assert_int_equals(sz, len2);
45
+
46
+ /*
47
+ * Test the Raw escape '\' --> '\\'
48
+ */
49
+ char ibuf[] = { '\\', '\0' };
50
+ memset(buf, 0, sizeof(buf));
51
+ d = modp_bjavascript_encode(buf, ibuf, (size_t)1);
52
+ mu_assert_int_equals(d, 2);
53
+ mu_assert_int_equals(buf[0], '\\');
54
+ mu_assert_int_equals(buf[1], '\\');
55
+ mu_assert_int_equals(buf[2], 0);
56
+
57
+ return 0;
58
+ }
59
+
60
+ static char* testSQuoteEscape(void)
61
+ {
62
+ char buf[100];
63
+ const char* s1 = "this is a 'string'\n";
64
+ const char* s2 = "this is a \\'string\\'\\n";
65
+ const size_t len1 = strlen(s1);
66
+ const size_t len2 = strlen(s2);
67
+ size_t d = modp_bjavascript_encode(buf, s1, len1);
68
+
69
+ mu_assert_int_equals(len2, d);
70
+ mu_assert_str_equals(buf, s2);
71
+
72
+ size_t sz = modp_bjavascript_encode_strlen(s1, len1);
73
+ mu_assert_int_equals(sz, len2);
74
+
75
+ char ibuf[] = { '\'', '\0' };
76
+ memset(buf, 0, sizeof(buf));
77
+ d = modp_bjavascript_encode(buf, ibuf, (size_t)1);
78
+ mu_assert_int_equals(d, 2);
79
+ mu_assert_int_equals(buf[0], '\\');
80
+ mu_assert_int_equals(buf[1], '\'');
81
+ mu_assert_int_equals(buf[2], '\0');
82
+
83
+ return 0;
84
+ }
85
+
86
+ static char* testDQuoteEscape(void)
87
+ {
88
+ char buf[100];
89
+ const char* s1 = "this is a \"string\"\n";
90
+ const char* s2 = "this is a \\\"string\\\"\\n";
91
+ const size_t len1 = strlen(s1);
92
+ const size_t len2 = strlen(s2);
93
+ size_t d = modp_bjavascript_encode(buf, s1, len1);
94
+
95
+ mu_assert_int_equals(len2, d);
96
+ mu_assert_str_equals(buf, s2);
97
+
98
+ size_t sz = modp_bjavascript_encode_strlen(s1, len1);
99
+ mu_assert_int_equals(sz, len2);
100
+ char ibuf[] = { '\"', '\0' };
101
+ memset(buf, 0, sizeof(buf));
102
+ d = modp_bjavascript_encode(buf, ibuf, (size_t)1);
103
+ mu_assert_int_equals(d, 2);
104
+ mu_assert_int_equals(buf[0], '\\');
105
+ mu_assert_int_equals(buf[1], '\"');
106
+ mu_assert_int_equals(buf[2], '\0');
107
+ return 0;
108
+ }
109
+
110
+ static char* testBinaryEscape(void)
111
+ {
112
+ char buf[100];
113
+ const char s1[] = { 1, 2, 3, 4, 0 };
114
+ const char* s2 = "\\x01\\x02\\x03\\x04";
115
+ const size_t len1 = strlen(s1);
116
+ const size_t len2 = strlen(s2);
117
+ size_t d = modp_bjavascript_encode(buf, s1, len1);
118
+
119
+ mu_assert_int_equals(len2, d);
120
+ mu_assert_str_equals(buf, s2);
121
+
122
+ size_t sz = modp_bjavascript_encode_strlen(s1, len1);
123
+ mu_assert_int_equals(sz, len2);
124
+ return 0;
125
+ }
126
+
127
+ static char* all_tests(void)
128
+ {
129
+ mu_run_test(testNoEscape);
130
+ mu_run_test(testSimpleEscape);
131
+ mu_run_test(testBinaryEscape);
132
+ mu_run_test(testSQuoteEscape);
133
+ mu_run_test(testDQuoteEscape);
134
+ return 0;
135
+ }
136
+
137
+ UNITTESTS
@@ -0,0 +1,423 @@
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_burl.h"
11
+
12
+ /**
13
+ * Test empty input to encode and decode
14
+ */
15
+ static char* testUrlEmpty(void)
16
+ {
17
+ size_t d;
18
+ char buf[1000];
19
+ buf[0] = 1;
20
+ d = modp_burl_encode(buf, "", (size_t)0);
21
+ mu_assert_int_equals(d, 0);
22
+ mu_assert(buf[0] == 0);
23
+
24
+ buf[0] = 1;
25
+ d = modp_burl_decode(buf, "", (size_t)0);
26
+ mu_assert_int_equals(d, 0);
27
+ mu_assert(buf[0] == 0);
28
+
29
+ return 0;
30
+ }
31
+
32
+ /**
33
+ * Handle gracefully null inputs, with 0 length.
34
+ */
35
+ static char* testUrlEmptyNull(void)
36
+ {
37
+ size_t d;
38
+ char buf[1000];
39
+ buf[0] = 1;
40
+ d = modp_burl_encode(buf, NULL, (size_t)0);
41
+ mu_assert_int_equals(d, 0);
42
+ mu_assert(buf[0] == 0);
43
+
44
+ buf[0] = 1;
45
+ d = modp_burl_decode(buf, NULL, (size_t)0);
46
+ mu_assert_int_equals(d, 0);
47
+ mu_assert(buf[0] == 0);
48
+
49
+ return 0;
50
+ }
51
+
52
+ /**
53
+ * test space <--> plus conversion
54
+ */
55
+ static char* testUrlSpaces(void)
56
+ {
57
+ size_t d = 0;
58
+ char buf[1000];
59
+ const char* input = " ";
60
+ const char* output = "+++";
61
+
62
+ d = modp_burl_encode(buf, input, strlen(input));
63
+ mu_assert_int_equals(d, strlen(output));
64
+ mu_assert_str_equals(buf, output);
65
+
66
+ d = modp_burl_decode(buf, output, strlen(output));
67
+ mu_assert_int_equals(d, strlen(input));
68
+ mu_assert_str_equals(buf, input);
69
+
70
+ return 0;
71
+ }
72
+
73
+ /**
74
+ * Test charactes that should be unchanged
75
+ */
76
+ static char* testUrlUntouched(void)
77
+ {
78
+ const char* lower = "abcdefghijklmnopqrstuvwxyz";
79
+ const char* upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
80
+ const char* digits = "0123456789";
81
+ const char* special = ".-_";
82
+ char buf[1000];
83
+ size_t d = 0;
84
+
85
+ memset(buf, 0, sizeof(buf));
86
+ d = modp_burl_encode(buf, lower, strlen(lower));
87
+ mu_assert_int_equals(d, strlen(lower));
88
+ mu_assert_str_equals(buf, lower);
89
+ memset(buf, 0, sizeof(buf));
90
+ d = modp_burl_decode(buf, lower, strlen(lower));
91
+ mu_assert_int_equals(d, strlen(lower));
92
+ mu_assert_str_equals(buf, lower);
93
+
94
+ memset(buf, 0, sizeof(buf));
95
+ d = modp_burl_encode(buf, upper, strlen(upper));
96
+ mu_assert_int_equals(d, strlen(upper));
97
+ mu_assert_str_equals(buf, upper);
98
+ memset(buf, 0, sizeof(buf));
99
+ d = modp_burl_decode(buf, upper, strlen(upper));
100
+ mu_assert_int_equals(d, strlen(upper));
101
+ mu_assert_str_equals(buf, upper);
102
+
103
+ memset(buf, 0, sizeof(buf));
104
+ d = modp_burl_encode(buf, digits, strlen(digits));
105
+ mu_assert_int_equals(d, strlen(digits));
106
+ mu_assert_str_equals(buf, digits);
107
+ memset(buf, 0, sizeof(buf));
108
+ d = modp_burl_decode(buf, digits, strlen(digits));
109
+ mu_assert_int_equals(d, strlen(digits));
110
+ mu_assert_str_equals(buf, digits);
111
+
112
+ memset(buf, 0, sizeof(buf));
113
+ d = modp_burl_encode(buf, special, strlen(special));
114
+ mu_assert_int_equals(d, strlen(special));
115
+ mu_assert_str_equals(buf, special);
116
+ memset(buf, 0, sizeof(buf));
117
+ d = modp_burl_decode(buf, special, strlen(special));
118
+ mu_assert_int_equals(d, strlen(special));
119
+ mu_assert_str_equals(buf, special);
120
+
121
+ return 0;
122
+ }
123
+
124
+ /**
125
+ * Test charactes that should be unchanged
126
+ */
127
+ static char* testUrlMinUntouched(void)
128
+ {
129
+ const char* lower = "abcdefghijklmnopqrstuvwxyz";
130
+ const char* upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
131
+ const char* digits = "0123456789";
132
+ const char* special = ".-_";
133
+ const char* extra = "~!$()*,;:@/?";
134
+ char buf[1000];
135
+ size_t d = 0;
136
+
137
+ memset(buf, 0, sizeof(buf));
138
+ d = modp_burl_min_encode(buf, lower, strlen(lower));
139
+ mu_assert_int_equals(d, strlen(lower));
140
+ mu_assert_str_equals(buf, lower);
141
+ memset(buf, 0, sizeof(buf));
142
+ d = modp_burl_decode(buf, lower, strlen(lower));
143
+ mu_assert_int_equals(d, strlen(lower));
144
+ mu_assert_str_equals(buf, lower);
145
+
146
+ memset(buf, 0, sizeof(buf));
147
+ d = modp_burl_min_encode(buf, upper, strlen(upper));
148
+ mu_assert_int_equals(d, strlen(upper));
149
+ mu_assert_str_equals(buf, upper);
150
+ memset(buf, 0, sizeof(buf));
151
+ d = modp_burl_decode(buf, upper, strlen(upper));
152
+ mu_assert_int_equals(d, strlen(upper));
153
+ mu_assert_str_equals(buf, upper);
154
+
155
+ memset(buf, 0, sizeof(buf));
156
+ d = modp_burl_min_encode(buf, digits, strlen(digits));
157
+ mu_assert_int_equals(d, strlen(digits));
158
+ mu_assert_str_equals(buf, digits);
159
+ memset(buf, 0, sizeof(buf));
160
+ d = modp_burl_decode(buf, digits, strlen(digits));
161
+ mu_assert_int_equals(d, strlen(digits));
162
+ mu_assert_str_equals(buf, digits);
163
+
164
+ memset(buf, 0, sizeof(buf));
165
+ d = modp_burl_min_encode(buf, special, strlen(special));
166
+ mu_assert_int_equals(d, strlen(special));
167
+ mu_assert_str_equals(buf, special);
168
+ memset(buf, 0, sizeof(buf));
169
+ d = modp_burl_decode(buf, special, strlen(special));
170
+ mu_assert_int_equals(d, strlen(special));
171
+ mu_assert_str_equals(buf, special);
172
+
173
+ memset(buf, 0, sizeof(buf));
174
+ d = modp_burl_min_encode(buf, extra, strlen(extra));
175
+ mu_assert_int_equals(d, strlen(extra));
176
+ mu_assert_str_equals(buf, extra);
177
+ memset(buf, 0, sizeof(buf));
178
+ d = modp_burl_decode(buf, extra, strlen(extra));
179
+ mu_assert_int_equals(d, strlen(extra));
180
+ mu_assert_str_equals(buf, extra);
181
+
182
+ return 0;
183
+ }
184
+
185
+ /** \brief make sure min encoding actually does hex encoding
186
+ *
187
+ */
188
+ static char* testUrlMinEncodeHex(void)
189
+ {
190
+ char buf[1000];
191
+ size_t d = 0;
192
+
193
+ memset(buf, 0, sizeof(buf));
194
+ const char* str1 = "a b";
195
+ d = modp_burl_min_encode(buf, str1, strlen(str1));
196
+ mu_assert_int_equals(3, d);
197
+ mu_assert_str_equals("a+b", buf);
198
+
199
+ memset(buf, 0, sizeof(buf));
200
+ const char* str2 = "ab\n";
201
+ d = modp_burl_min_encode(buf, str2, strlen(str2));
202
+ mu_assert_int_equals(5, d);
203
+ mu_assert_str_equals("ab%0A", buf);
204
+
205
+ return 0;
206
+ }
207
+
208
+ static char* testUrlDecodeHexBad(void)
209
+ {
210
+
211
+ const char* bad1 = "%0X"; /* bad trailing char */
212
+ const char* bad2 = "%X0"; /* bad leading char */
213
+ const char* bad3 = "%XX"; /* bad chars */
214
+ const char* bad4 = "%2"; /* not enough room, good char */
215
+ const char* bad5 = "%X"; /* not enought room, bad char */
216
+ const char* bad6 = "%"; /* test oddball */
217
+ const char* bad7 = "AA%"; /* test end of line */
218
+ char bad8[4]; /* %XX where X is high bit (test sign char vs. uint8_t*) */
219
+ bad8[0] = '%';
220
+ bad8[1] = (char)0x81;
221
+ bad8[2] = (char)0x82;
222
+ bad8[3] = (char)0;
223
+
224
+ size_t d = 0;
225
+ char buf[1000];
226
+
227
+ memset(buf, 0, sizeof(buf));
228
+ d = modp_burl_decode(buf, bad1, strlen(bad1));
229
+ mu_assert_int_equals(d, strlen(bad1));
230
+ mu_assert_str_equals(buf, bad1);
231
+
232
+ memset(buf, 0, sizeof(buf));
233
+ d = modp_burl_decode(buf, bad2, strlen(bad2));
234
+ mu_assert_int_equals(d, strlen(bad2));
235
+ mu_assert_str_equals(bad2, buf);
236
+
237
+ memset(buf, 0, sizeof(buf));
238
+ d = modp_burl_decode(buf, bad3, strlen(bad3));
239
+ mu_assert_int_equals(d, strlen(bad3));
240
+ mu_assert_str_equals(buf, bad3);
241
+
242
+ memset(buf, 0, sizeof(buf));
243
+ d = modp_burl_decode(buf, bad4, strlen(bad4));
244
+ mu_assert_int_equals(d, strlen(bad4));
245
+ mu_assert_str_equals(buf, bad4);
246
+
247
+ memset(buf, 0, sizeof(buf));
248
+ d = modp_burl_decode(buf, bad5, strlen(bad5));
249
+ mu_assert_int_equals(d, strlen(bad5));
250
+ mu_assert_str_equals(buf, bad5);
251
+
252
+ memset(buf, 0, sizeof(buf));
253
+ d = modp_burl_decode(buf, bad6, strlen(bad6));
254
+ mu_assert_int_equals(d, strlen(bad6));
255
+ mu_assert_str_equals(buf, bad6);
256
+
257
+ memset(buf, 0, sizeof(buf));
258
+ d = modp_burl_decode(buf, bad7, strlen(bad7));
259
+ mu_assert_int_equals(d, strlen(bad7));
260
+ mu_assert_str_equals(buf, bad7);
261
+
262
+ memset(buf, 0, sizeof(buf));
263
+ d = modp_burl_decode(buf, bad8, strlen(bad8));
264
+ mu_assert_int_equals(d, strlen(bad8));
265
+ mu_assert_str_equals(buf, bad8);
266
+ return 0;
267
+ }
268
+
269
+ static char* testUrlDecodeHex(void)
270
+ {
271
+ size_t d; /* size of output */
272
+ int i, j; /* loops */
273
+ int k = 0; /* position in inputbuf; */
274
+ char inputbuf[3 * 256 + 1];
275
+ char output[257];
276
+ char msg[1000];
277
+
278
+ /* make input string contain every possible "%XX" */
279
+ static const char* hexdigits1 = "0123456789ABCDEF";
280
+ memset(inputbuf, 0, sizeof(inputbuf));
281
+ memset(output, 1, sizeof(output));
282
+ k = 0;
283
+ for (i = 0; i < 16; ++i) {
284
+ for (j = 0; j < 16; ++j) {
285
+ inputbuf[k++] = '%';
286
+ inputbuf[k++] = hexdigits1[i];
287
+ inputbuf[k++] = hexdigits1[j];
288
+ }
289
+ }
290
+
291
+ d = modp_burl_decode(output, inputbuf, sizeof(inputbuf) - 1);
292
+ mu_assert_int_equals(d, 256);
293
+ for (i = 0; i < 256; ++i) {
294
+ sprintf(msg, "Loop at %d", i);
295
+ mu_assert_int_equals_msg(msg, i, (unsigned char)output[i]);
296
+ }
297
+
298
+ /* make input string contain every possible "%XX" */
299
+ static const char* hexdigits2 = "0123456789abcdef";
300
+ memset(inputbuf, 0, sizeof(inputbuf));
301
+ memset(output, 1, sizeof(output));
302
+
303
+ k = 0;
304
+ for (i = 0; i < 16; ++i) {
305
+ for (j = 0; j < 16; ++j) {
306
+ inputbuf[k++] = '%';
307
+ inputbuf[k++] = hexdigits2[i];
308
+ inputbuf[k++] = hexdigits2[j];
309
+ }
310
+ }
311
+
312
+ d = modp_burl_decode(output, inputbuf, sizeof(inputbuf) - 1);
313
+ mu_assert_int_equals(256, d);
314
+ for (i = 0; i < 256; ++i) {
315
+ sprintf(msg, "Loop at %d", i);
316
+ mu_assert_int_equals_msg(msg, i, (unsigned char)output[i]);
317
+ }
318
+ return 0;
319
+ }
320
+
321
+ /**
322
+ * test hex encoding.. to be done after hex decoding
323
+ * is tested.
324
+ */
325
+ static char* testHexEncoding(void)
326
+ {
327
+ int i = 0;
328
+ size_t d = 0;
329
+ char msg[1000];
330
+ char input[257];
331
+ memset(input, 0, sizeof(input));
332
+ char output[257 * 3];
333
+ memset(output, 0, sizeof(output));
334
+ char buf[1000];
335
+ memset(buf, 0, sizeof(buf));
336
+ d = modp_burl_encode(output, input, (size_t)256);
337
+ d = modp_burl_decode(buf, output, d);
338
+ mu_assert_int_equals(256, d);
339
+ for (i = 0; i < 256; ++i) {
340
+ sprintf(msg, "Loop at %d failed", i);
341
+ mu_assert_int_equals_msg(msg, input[i], buf[i]);
342
+ }
343
+ return 0;
344
+ }
345
+
346
+ static char* testEncodeStrlen(void)
347
+ {
348
+ char ibuf[100];
349
+ char obuf[100];
350
+ memset(ibuf, 0, sizeof(ibuf));
351
+ memset(obuf, 0, sizeof(obuf));
352
+
353
+ /* Empty. should be 0 */
354
+ ibuf[0] = 0;
355
+ mu_assert_int_equals(strlen(ibuf), (size_t)modp_burl_encode_strlen(ibuf, strlen(ibuf)));
356
+
357
+ /* Plain, should be same size */
358
+ strcpy(ibuf, "abcdefg");
359
+ mu_assert_int_equals(strlen(ibuf), (size_t)modp_burl_encode_strlen(ibuf, strlen(ibuf)));
360
+
361
+ /* Plain and spaces, should be same size */
362
+ strcpy(ibuf, "a b c d e f g");
363
+ mu_assert_int_equals(strlen(ibuf), (size_t)modp_burl_encode_strlen(ibuf, strlen(ibuf)));
364
+
365
+ /* one bad char, adds two bytes */
366
+ strcpy(ibuf, "abcdefg\n");
367
+ mu_assert_int_equals(strlen(ibuf) + 2, (size_t)modp_burl_encode_strlen(ibuf, strlen(ibuf)));
368
+
369
+ /* 2 bad chars, adds 4 bytes */
370
+ strcpy(ibuf, "\nabcdefg\n");
371
+ mu_assert_int_equals(strlen(ibuf) + 4, (size_t)modp_burl_encode_strlen(ibuf, strlen(ibuf)));
372
+ return 0;
373
+ }
374
+
375
+ /** \brief test "modp_burl_min_encode_strlen"
376
+ *
377
+ */
378
+ static char* testEncodeMinStrlen(void)
379
+ {
380
+ char ibuf[100];
381
+ char obuf[100];
382
+ memset(ibuf, 0, sizeof(ibuf));
383
+ memset(obuf, 0, sizeof(obuf));
384
+
385
+ /* Empty. should be 0 */
386
+ ibuf[0] = 0;
387
+ mu_assert_int_equals(strlen(ibuf), (size_t)modp_burl_min_encode_strlen(ibuf, strlen(ibuf)));
388
+
389
+ /* Plain, should be same size */
390
+ strcpy(ibuf, "abcdefg");
391
+ mu_assert_int_equals(strlen(ibuf), (size_t)modp_burl_min_encode_strlen(ibuf, strlen(ibuf)));
392
+
393
+ /* Plain and spaces, should be same size */
394
+ strcpy(ibuf, "a b c d e f g");
395
+ mu_assert_int_equals(strlen(ibuf), (size_t)modp_burl_min_encode_strlen(ibuf, strlen(ibuf)));
396
+
397
+ /* one bad char, adds two bytes */
398
+ strcpy(ibuf, "abcdefg\n");
399
+ mu_assert_int_equals(strlen(ibuf) + 2, (size_t)modp_burl_min_encode_strlen(ibuf, strlen(ibuf)));
400
+
401
+ /* 2 bad chars, adds 4 bytes */
402
+ strcpy(ibuf, "\nabcdefg\n");
403
+ mu_assert_int_equals(strlen(ibuf) + 4, (size_t)modp_burl_min_encode_strlen(ibuf, strlen(ibuf)));
404
+ return 0;
405
+ }
406
+
407
+ static char* all_tests(void)
408
+ {
409
+ mu_run_test(testUrlUntouched);
410
+ mu_run_test(testUrlEmpty);
411
+ mu_run_test(testUrlEmptyNull);
412
+ mu_run_test(testUrlSpaces);
413
+ mu_run_test(testUrlDecodeHex);
414
+ mu_run_test(testUrlDecodeHexBad);
415
+ mu_run_test(testHexEncoding);
416
+ mu_run_test(testEncodeStrlen);
417
+ mu_run_test(testUrlMinUntouched);
418
+ mu_run_test(testUrlMinEncodeHex);
419
+ mu_run_test(testEncodeMinStrlen);
420
+ return 0;
421
+ }
422
+
423
+ UNITTESTS