1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation
2425bb815Sopenharmony_ci *
3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License.
5425bb815Sopenharmony_ci * You may obtain a copy of the License at
6425bb815Sopenharmony_ci *
7425bb815Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8425bb815Sopenharmony_ci *
9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS
11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and
13425bb815Sopenharmony_ci * limitations under the License.
14425bb815Sopenharmony_ci */
15425bb815Sopenharmony_ci
16425bb815Sopenharmony_ci#include "jerryscript.h"
17425bb815Sopenharmony_ci#include "test-common.h"
18425bb815Sopenharmony_ci#include <gtest/gtest.h>
19425bb815Sopenharmony_ci#include "jerryscript-port.h"
20425bb815Sopenharmony_ci#include "jerryscript-port-default.h"
21425bb815Sopenharmony_ci
22425bb815Sopenharmony_cistatic bool
23425bb815Sopenharmony_citest_syntax_error (const char *script_p) /**< script */
24425bb815Sopenharmony_ci{
25425bb815Sopenharmony_ci  jerry_value_t parse_result = jerry_parse (NULL,
26425bb815Sopenharmony_ci                                            0,
27425bb815Sopenharmony_ci                                            (const jerry_char_t *) script_p,
28425bb815Sopenharmony_ci                                            strlen (script_p),
29425bb815Sopenharmony_ci                                            JERRY_PARSE_NO_OPTS);
30425bb815Sopenharmony_ci
31425bb815Sopenharmony_ci  bool result = false;
32425bb815Sopenharmony_ci
33425bb815Sopenharmony_ci  if (jerry_value_is_error (parse_result))
34425bb815Sopenharmony_ci  {
35425bb815Sopenharmony_ci    result = true;
36425bb815Sopenharmony_ci    TEST_ASSERT (jerry_get_error_type (parse_result) == JERRY_ERROR_SYNTAX);
37425bb815Sopenharmony_ci  }
38425bb815Sopenharmony_ci
39425bb815Sopenharmony_ci  jerry_release_value (parse_result);
40425bb815Sopenharmony_ci  return result;
41425bb815Sopenharmony_ci} /* test_syntax_error */
42425bb815Sopenharmony_ci
43425bb815Sopenharmony_ciclass UnicodeTest : public testing::Test{
44425bb815Sopenharmony_cipublic:
45425bb815Sopenharmony_ci    static void SetUpTestCase()
46425bb815Sopenharmony_ci    {
47425bb815Sopenharmony_ci        GTEST_LOG_(INFO) << "UnicodeTest SetUpTestCase";
48425bb815Sopenharmony_ci    }
49425bb815Sopenharmony_ci
50425bb815Sopenharmony_ci    static void TearDownTestCase()
51425bb815Sopenharmony_ci    {
52425bb815Sopenharmony_ci        GTEST_LOG_(INFO) << "UnicodeTest TearDownTestCase";
53425bb815Sopenharmony_ci    }
54425bb815Sopenharmony_ci
55425bb815Sopenharmony_ci    void SetUp() override {}
56425bb815Sopenharmony_ci    void TearDown() override {}
57425bb815Sopenharmony_ci
58425bb815Sopenharmony_ci};
59425bb815Sopenharmony_cistatic constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024;
60425bb815Sopenharmony_cistatic void* context_alloc_fn(size_t size, void* cb_data)
61425bb815Sopenharmony_ci{
62425bb815Sopenharmony_ci    (void)cb_data;
63425bb815Sopenharmony_ci    size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size;
64425bb815Sopenharmony_ci    return malloc(newSize);
65425bb815Sopenharmony_ci}
66425bb815Sopenharmony_ciHWTEST_F(UnicodeTest, Test001, testing::ext::TestSize.Level1)
67425bb815Sopenharmony_ci{
68425bb815Sopenharmony_ci  jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL);
69425bb815Sopenharmony_ci  jerry_port_default_set_current_context (ctx_p);
70425bb815Sopenharmony_ci  jerry_init (JERRY_INIT_EMPTY);
71425bb815Sopenharmony_ci
72425bb815Sopenharmony_ci  if (!test_syntax_error ("\\u{61}"))
73425bb815Sopenharmony_ci  {
74425bb815Sopenharmony_ci    TEST_ASSERT (!test_syntax_error ("\xF0\x90\xB2\x80: break \\u{10C80}"));
75425bb815Sopenharmony_ci    /* The \u surrogate pairs are ignored. The \u{hex} form must be used. */
76425bb815Sopenharmony_ci    TEST_ASSERT (test_syntax_error ("\xF0\x90\xB2\x80: break \\ud803\\udc80"));
77425bb815Sopenharmony_ci    /* The utf8 code point and the cesu8 surrogate pair must match. */
78425bb815Sopenharmony_ci    TEST_ASSERT (!test_syntax_error ("\xF0\x90\xB2\x80: break \xed\xa0\x83\xed\xb2\x80"));
79425bb815Sopenharmony_ci
80425bb815Sopenharmony_ci    TEST_ASSERT (!test_syntax_error ("$\xF0\x90\xB2\x80$: break $\\u{10C80}$"));
81425bb815Sopenharmony_ci    TEST_ASSERT (test_syntax_error ("$\xF0\x90\xB2\x80$: break $\\ud803\\udc80$"));
82425bb815Sopenharmony_ci    TEST_ASSERT (!test_syntax_error ("$\xF0\x90\xB2\x80$: break $\xed\xa0\x83\xed\xb2\x80$"));
83425bb815Sopenharmony_ci  }
84425bb815Sopenharmony_ci
85425bb815Sopenharmony_ci  jerry_cleanup ();
86425bb815Sopenharmony_ci  free(ctx_p);
87425bb815Sopenharmony_ci}
88