1 /* Copyright JS Foundation and other contributors, http://js.foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "jerryscript.h"
17 #include "jerryscript-port.h"
18 #include "jerryscript-port-default.h"
19 #include "test-common.h"
20 #include <gtest/gtest.h>
21
22 static bool
strict_equals(jerry_value_t a,jerry_value_t b)23 strict_equals (jerry_value_t a, /**< the first string to compare */
24 jerry_value_t b) /**< the second string to compare */
25 {
26 const jerry_char_t is_equal_src[] = "var isEqual = function(a, b) { return (a === b); }; isEqual";
27 jerry_value_t is_equal_fn_val = jerry_eval (is_equal_src, sizeof (is_equal_src) - 1, JERRY_PARSE_NO_OPTS);
28 TEST_ASSERT (!jerry_value_is_error (is_equal_fn_val));
29 jerry_value_t args[2] = {a, b};
30 jerry_value_t res = jerry_call_function (is_equal_fn_val, jerry_create_undefined (), args, 2);
31 TEST_ASSERT (!jerry_value_is_error (res));
32 TEST_ASSERT (jerry_value_is_boolean (res));
33 bool is_strict_equal = jerry_get_boolean_value (res);
34 jerry_release_value (res);
35 jerry_release_value (is_equal_fn_val);
36 return is_strict_equal;
37 } /* strict_equals */
38 class ApiStringsTest : public testing::Test{
39 public:
SetUpTestCase()40 static void SetUpTestCase()
41 {
42 GTEST_LOG_(INFO) << "ApiStringsTest SetUpTestCase";
43 }
44
TearDownTestCase()45 static void TearDownTestCase()
46 {
47 GTEST_LOG_(INFO) << "ApiStringsTest TearDownTestCase";
48 }
49
SetUp()50 void SetUp() override {}
TearDown()51 void TearDown() override {}
52
53 };
54
55 static constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024;
context_alloc_fn(size_t size,void * cb_data)56 static void* context_alloc_fn(size_t size, void* cb_data)
57 {
58 (void)cb_data;
59 size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size;
60 return malloc(newSize);
61 }
62
63 HWTEST_F(ApiStringsTest, Test001, testing::ext::TestSize.Level1)
64 {
65 jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL);
66 jerry_port_default_set_current_context (ctx_p);
67 jerry_size_t sz, utf8_sz, cesu8_sz;
68 jerry_length_t cesu8_length, utf8_length;
69 jerry_value_t args[2];
70
71 TEST_INIT ();
72 jerry_init (JERRY_INIT_EMPTY);
73
74 /* Test corner case for jerry_string_to_char_buffer */
75 args[0] = jerry_create_string ((jerry_char_t *) "");
76 sz = jerry_get_string_size (args[0]);
77 TEST_ASSERT (sz == 0);
78 jerry_release_value (args[0]);
79
80 /* Test create_jerry_string_from_utf8 with 4-byte long unicode sequences,
81 * test string: 'str: {DESERET CAPITAL LETTER LONG I}'
82 */
83 args[0] = jerry_create_string_from_utf8 ((jerry_char_t *) "\x73\x74\x72\x3a \xf0\x90\x90\x80");
84 args[1] = jerry_create_string ((jerry_char_t *) "\x73\x74\x72\x3a \xed\xa0\x81\xed\xb0\x80");
85
86 /* These sizes must be equal */
87 utf8_sz = jerry_get_string_size (args[0]);
88 cesu8_sz = jerry_get_string_size (args[1]);
89
90 JERRY_VLA (char, string_from_utf8, utf8_sz);
91 JERRY_VLA (char, string_from_cesu8, cesu8_sz);
92
93 jerry_string_to_char_buffer (args[0], (jerry_char_t *) string_from_utf8, utf8_sz);
94 jerry_string_to_char_buffer (args[1], (jerry_char_t *) string_from_cesu8, cesu8_sz);
95
96 TEST_ASSERT (utf8_sz == cesu8_sz);
97
98 TEST_ASSERT (!strncmp (string_from_utf8, string_from_cesu8, utf8_sz));
99 jerry_release_value (args[0]);
100 jerry_release_value (args[1]);
101
102 /* Test jerry_string_to_utf8_char_buffer, test string: 'str: {DESERET CAPITAL LETTER LONG I}' */
103 args[0] = jerry_create_string_from_utf8 ((jerry_char_t *) "\x73\x74\x72\x3a \xf0\x90\x90\x80");
104 args[1] = jerry_create_string ((jerry_char_t *) "\x73\x74\x72\x3a \xed\xa0\x81\xed\xb0\x80");
105
106 /* Test that the strings are equal / ensure hashes are equal */
107 TEST_ASSERT (strict_equals (args[0], args[1]));
108
109 /* These sizes must be equal */
110 utf8_sz = jerry_get_utf8_string_size (args[0]);
111 cesu8_sz = jerry_get_utf8_string_size (args[1]);
112
113 TEST_ASSERT (utf8_sz == cesu8_sz && utf8_sz > 0);
114
115 JERRY_VLA (char, string_from_utf8_string, utf8_sz);
116 JERRY_VLA (char, string_from_cesu8_string, cesu8_sz);
117
118 jerry_string_to_utf8_char_buffer (args[0], (jerry_char_t *) string_from_utf8_string, utf8_sz);
119 jerry_string_to_utf8_char_buffer (args[1], (jerry_char_t *) string_from_cesu8_string, cesu8_sz);
120
121 TEST_ASSERT (!strncmp (string_from_utf8_string, string_from_cesu8_string, utf8_sz));
122 jerry_release_value (args[0]);
123 jerry_release_value (args[1]);
124
125 /* Test string: 'str: {MATHEMATICAL FRAKTUR SMALL F}{MATHEMATICAL FRAKTUR SMALL G}' */
126 args[0] = jerry_create_string_from_utf8 ((jerry_char_t *) "\x73\x74\x72\x3a \xf0\x9d\x94\xa3 \xf0\x9d\x94\xa4");
127
128 cesu8_length = jerry_get_string_length (args[0]);
129 utf8_length = jerry_get_utf8_string_length (args[0]);
130
131 cesu8_sz = jerry_get_string_size (args[0]);
132 utf8_sz = jerry_get_utf8_string_size (args[0]);
133
134 TEST_ASSERT (cesu8_length == 10 && utf8_length == 8);
135 TEST_ASSERT (cesu8_sz != utf8_sz);
136 TEST_ASSERT (utf8_sz == 14 && cesu8_sz == 18);
137
138 JERRY_VLA (char, test_string, utf8_sz);
139
140 TEST_ASSERT (jerry_string_to_utf8_char_buffer (args[0], (jerry_char_t *) test_string, utf8_sz) == 14);
141 TEST_ASSERT (!strncmp (test_string, "\x73\x74\x72\x3a \xf0\x9d\x94\xa3 \xf0\x9d\x94\xa4", utf8_sz));
142
143 sz = jerry_substring_to_utf8_char_buffer (args[0], 0, utf8_length, (jerry_char_t *) test_string, utf8_sz);
144 TEST_ASSERT (sz == 14);
145 TEST_ASSERT (!strncmp (test_string, "\x73\x74\x72\x3a \xf0\x9d\x94\xa3 \xf0\x9d\x94\xa4", sz));
146
147 sz = jerry_substring_to_utf8_char_buffer (args[0], 0, utf8_length + 1, (jerry_char_t *) test_string, utf8_sz);
148 TEST_ASSERT (sz == 14);
149 TEST_ASSERT (!strncmp (test_string, "\x73\x74\x72\x3a \xf0\x9d\x94\xa3 \xf0\x9d\x94\xa4", sz));
150
151 sz = jerry_substring_to_utf8_char_buffer (args[0], utf8_length, 0, (jerry_char_t *) test_string, utf8_sz);
152 TEST_ASSERT (sz == 0);
153
154 sz = jerry_substring_to_utf8_char_buffer (args[0], 0, utf8_length, (jerry_char_t *) test_string, utf8_sz - 1);
155 TEST_ASSERT (sz == 10);
156 TEST_ASSERT (!strncmp (test_string, "\x73\x74\x72\x3a \xf0\x9d\x94\xa3 ", sz));
157
158 sz = jerry_substring_to_utf8_char_buffer (args[0], 0, utf8_length - 1, (jerry_char_t *) test_string, utf8_sz);
159 TEST_ASSERT (sz == 10);
160 TEST_ASSERT (!strncmp (test_string, "\x73\x74\x72\x3a \xf0\x9d\x94\xa3 ", sz));
161
162 sz = jerry_substring_to_utf8_char_buffer (args[0],
163 utf8_length - 2,
164 utf8_length - 1,
165 (jerry_char_t *) test_string,
166 utf8_sz);
167 TEST_ASSERT (sz == 1);
168 TEST_ASSERT (!strncmp (test_string, " ", sz));
169
170 sz = jerry_substring_to_utf8_char_buffer (args[0],
171 utf8_length - 3,
172 utf8_length - 2,
173 (jerry_char_t *) test_string,
174 utf8_sz);
175 TEST_ASSERT (sz == 4);
176 TEST_ASSERT (!strncmp (test_string, "\xf0\x9d\x94\xa3", sz));
177
178 jerry_release_value (args[0]);
179
180 /* Test string: 'str: {DESERET CAPITAL LETTER LONG I}' */
181 args[0] = jerry_create_string ((jerry_char_t *) "\x73\x74\x72\x3a \xed\xa0\x81\xed\xb0\x80");
182
183 cesu8_length = jerry_get_string_length (args[0]);
184 utf8_length = jerry_get_utf8_string_length (args[0]);
185
186 cesu8_sz = jerry_get_string_size (args[0]);
187 utf8_sz = jerry_get_utf8_string_size (args[0]);
188
189 TEST_ASSERT (cesu8_length == 7 && utf8_length == 6);
190 TEST_ASSERT (cesu8_sz != utf8_sz);
191 TEST_ASSERT (utf8_sz == 9 && cesu8_sz == 11);
192
193 jerry_release_value (args[0]);
194
195 /* Test string: 'price: 10{EURO SIGN}' */
196 args[0] = jerry_create_string_from_utf8 ((jerry_char_t *) "\x70\x72\x69\x63\x65\x3a \x31\x30\xe2\x82\xac");
197
198 cesu8_length = jerry_get_string_length (args[0]);
199 utf8_length = jerry_get_utf8_string_length (args[0]);
200
201 cesu8_sz = jerry_get_string_size (args[0]);
202 utf8_sz = jerry_get_utf8_string_size (args[0]);
203
204 TEST_ASSERT (cesu8_length == utf8_length);
205 TEST_ASSERT (cesu8_length == 10);
206 TEST_ASSERT (cesu8_sz == utf8_sz);
207 TEST_ASSERT (utf8_sz == 12);
208 jerry_release_value (args[0]);
209
210 /* Test string: '3' */
211 {
212 jerry_value_t test_str = jerry_create_string ((const jerry_char_t *) "3");
213 char result_string[1] = { 'E' };
214 jerry_size_t copied_utf8 = jerry_substring_to_utf8_char_buffer (test_str,
215 0,
216 1,
217 (jerry_char_t *) result_string,
218 sizeof (result_string));
219 TEST_ASSERT (copied_utf8 == 1);
220 TEST_ASSERT (result_string[0] == '3');
221
222 result_string[0] = 'E';
223 jerry_size_t copied = jerry_substring_to_char_buffer (test_str,
224 0,
225 1,
226 (jerry_char_t *) result_string,
227 sizeof (result_string));
228 TEST_ASSERT (copied == 1);
229 TEST_ASSERT (result_string[0] == '3');
230
231 jerry_release_value (test_str);
232 }
233
234 /* Test jerry_substring_to_char_buffer */
235 args[0] = jerry_create_string ((jerry_char_t *) "an ascii string");
236
237 /* Buffer size */
238 cesu8_sz = 5;
239
240 JERRY_VLA (char, substring, cesu8_sz);
241 sz = jerry_substring_to_char_buffer (args[0], 3, 8, (jerry_char_t *) substring, cesu8_sz);
242 TEST_ASSERT (sz == 5);
243 TEST_ASSERT (!strncmp (substring, "ascii", sz));
244
245 /* Buffer size is 5, substring length is 11 => copied only the first 5 char */
246 sz = jerry_substring_to_char_buffer (args[0], 0, 11, (jerry_char_t *) substring, cesu8_sz);
247
248 TEST_ASSERT (sz == 5);
249 TEST_ASSERT (!strncmp (substring, "an as", sz));
250
251 /* Position of the first character is greater than the string length */
252 sz = jerry_substring_to_char_buffer (args[0], 16, 21, (jerry_char_t *) substring, cesu8_sz);
253 TEST_ASSERT (sz == 0);
254
255 sz = jerry_substring_to_char_buffer (args[0], 14, 15, (jerry_char_t *) substring, cesu8_sz);
256 TEST_ASSERT (sz == 1);
257 TEST_ASSERT (!strncmp (substring, "g", sz));
258
259 sz = jerry_substring_to_char_buffer (args[0], 0, 1, (jerry_char_t *) substring, cesu8_sz);
260 TEST_ASSERT (sz == 1);
261 TEST_ASSERT (!strncmp (substring, "a", sz));
262
263 cesu8_length = jerry_get_string_length (args[0]);
264 cesu8_sz = jerry_get_string_size (args[0]);
265 TEST_ASSERT (cesu8_length == 15);
266 TEST_ASSERT (cesu8_length == cesu8_sz);
267
268 JERRY_VLA (char, fullstring, cesu8_sz);
269 sz = jerry_substring_to_char_buffer (args[0], 0, cesu8_length, (jerry_char_t *) fullstring, cesu8_sz);
270 TEST_ASSERT (sz == 15);
271 TEST_ASSERT (!strncmp (fullstring, "an ascii string", sz));
272
273 jerry_release_value (args[0]);
274
275 /* Test jerry_substring_to_char_buffer: '0101' */
276 args[0] = jerry_create_string ((jerry_char_t *) "0101");
277 cesu8_sz = jerry_get_string_size (args[0]);
278
279 JERRY_VLA (char, number_substring, cesu8_sz);
280
281 sz = jerry_substring_to_char_buffer (args[0], 1, 3, (jerry_char_t *) number_substring, cesu8_sz);
282 TEST_ASSERT (sz == 2);
283 TEST_ASSERT (!strncmp (number_substring, "10", sz));
284
285 jerry_release_value (args[0]);
286
287 /* Test jerry_substring_to_char_buffer: 'str: {greek zero sign}' */
288 args[0] = jerry_create_string ((jerry_char_t *) "\x73\x74\x72\x3a \xed\xa0\x80\xed\xb6\x8a");
289 cesu8_sz = jerry_get_string_size (args[0]);
290 cesu8_length = jerry_get_string_length (args[0]);
291 TEST_ASSERT (cesu8_sz == 11);
292 TEST_ASSERT (cesu8_length = 7);
293
294 JERRY_VLA (char, supl_substring, cesu8_sz);
295
296 sz = jerry_substring_to_char_buffer (args[0], 0, cesu8_length, (jerry_char_t *) supl_substring, cesu8_sz);
297 TEST_ASSERT (sz == 11);
298 TEST_ASSERT (!strncmp (supl_substring, "\x73\x74\x72\x3a \xed\xa0\x80\xed\xb6\x8a", sz));
299
300 /* Decrease the buffer size => the low surrogate char will not fit into the buffer */
301 cesu8_sz -= 1;
302 sz = jerry_substring_to_char_buffer (args[0], 0, cesu8_length, (jerry_char_t *) supl_substring, cesu8_sz);
303 TEST_ASSERT (sz == 8);
304 TEST_ASSERT (!strncmp (supl_substring, "\x73\x74\x72\x3a \xed\xa0\x80", sz));
305
306 sz = jerry_substring_to_char_buffer (args[0],
307 cesu8_length - 1,
308 cesu8_length,
309 (jerry_char_t *) supl_substring,
310 cesu8_sz);
311 TEST_ASSERT (sz == 3);
312 TEST_ASSERT (!strncmp (supl_substring, "\xed\xb6\x8a", sz));
313
314 sz = jerry_substring_to_char_buffer (args[0],
315 cesu8_length - 2,
316 cesu8_length - 1,
317 (jerry_char_t *) supl_substring,
318 cesu8_sz);
319 TEST_ASSERT (sz == 3);
320 TEST_ASSERT (!strncmp (supl_substring, "\xed\xa0\x80", sz));
321
322 jerry_release_value (args[0]);
323
324 jerry_cleanup ();
325 free (ctx_p);
326 }
327