• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "test-common.h"
18 #include <gtest/gtest.h>
19 #include "jerryscript-port.h"
20 #include "jerryscript-port-default.h"
21 
22 static bool
test_syntax_error(const char * script_p)23 test_syntax_error (const char *script_p) /**< script */
24 {
25   jerry_value_t parse_result = jerry_parse (NULL,
26                                             0,
27                                             (const jerry_char_t *) script_p,
28                                             strlen (script_p),
29                                             JERRY_PARSE_NO_OPTS);
30 
31   bool result = false;
32 
33   if (jerry_value_is_error (parse_result))
34   {
35     result = true;
36     TEST_ASSERT (jerry_get_error_type (parse_result) == JERRY_ERROR_SYNTAX);
37   }
38 
39   jerry_release_value (parse_result);
40   return result;
41 } /* test_syntax_error */
42 
43 class UnicodeTest : public testing::Test{
44 public:
SetUpTestCase()45     static void SetUpTestCase()
46     {
47         GTEST_LOG_(INFO) << "UnicodeTest SetUpTestCase";
48     }
49 
TearDownTestCase()50     static void TearDownTestCase()
51     {
52         GTEST_LOG_(INFO) << "UnicodeTest TearDownTestCase";
53     }
54 
SetUp()55     void SetUp() override {}
TearDown()56     void TearDown() override {}
57 
58 };
59 static constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024;
context_alloc_fn(size_t size,void * cb_data)60 static void* context_alloc_fn(size_t size, void* cb_data)
61 {
62     (void)cb_data;
63     size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size;
64     return malloc(newSize);
65 }
66 HWTEST_F(UnicodeTest, Test001, testing::ext::TestSize.Level1)
67 {
68   jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL);
69   jerry_port_default_set_current_context (ctx_p);
70   jerry_init (JERRY_INIT_EMPTY);
71 
72   if (!test_syntax_error ("\\u{61}"))
73   {
74     TEST_ASSERT (!test_syntax_error ("\xF0\x90\xB2\x80: break \\u{10C80}"));
75     /* The \u surrogate pairs are ignored. The \u{hex} form must be used. */
76     TEST_ASSERT (test_syntax_error ("\xF0\x90\xB2\x80: break \\ud803\\udc80"));
77     /* The utf8 code point and the cesu8 surrogate pair must match. */
78     TEST_ASSERT (!test_syntax_error ("\xF0\x90\xB2\x80: break \xed\xa0\x83\xed\xb2\x80"));
79 
80     TEST_ASSERT (!test_syntax_error ("$\xF0\x90\xB2\x80$: break $\\u{10C80}$"));
81     TEST_ASSERT (test_syntax_error ("$\xF0\x90\xB2\x80$: break $\\ud803\\udc80$"));
82     TEST_ASSERT (!test_syntax_error ("$\xF0\x90\xB2\x80$: break $\xed\xa0\x83\xed\xb2\x80$"));
83   }
84 
85   jerry_cleanup ();
86   free(ctx_p);
87 }
88