1 /**
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
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 "ast_verifier_test.h"
17 #include "checker/ETSchecker.h"
18
19 using ark::es2panda::compiler::ast_verifier::ASTVerifier;
20 using ark::es2panda::compiler::ast_verifier::InvariantNameSet;
21 using ark::es2panda::ir::ETSScript;
22
TEST_F(ASTVerifierTest,PrivateAccessTestNegative1)23 TEST_F(ASTVerifierTest, PrivateAccessTestNegative1)
24 {
25 ASTVerifier verifier {Allocator()};
26
27 char const *text = R"(
28 class Base {
29 public a: int = 1;
30 }
31 class Derived extends Base {
32 public b: int = this.a;
33 }
34 )";
35 es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
36 impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
37 ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
38
39 auto *ast = reinterpret_cast<ETSScript *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
40
41 ast->AsETSScript()
42 ->Statements()[1]
43 ->AsClassDeclaration()
44 ->Definition()
45 ->AsClassDefinition()
46 ->Body()[0]
47 ->AsClassProperty()
48 ->AddModifier(ark::es2panda::ir::ModifierFlags::PRIVATE);
49
50 InvariantNameSet checks;
51 checks.insert("ModifierAccessValidForAll");
52 const auto &messages = verifier.Verify(ast, checks);
53 ASSERT_EQ(messages.size(), 1);
54
55 ASSERT_NE(checks.find(messages[0].Invariant()), checks.end());
56
57 impl_->DestroyContext(ctx);
58 }
59
TEST_F(ASTVerifierTest,PrivateAccessTestNegative2)60 TEST_F(ASTVerifierTest, PrivateAccessTestNegative2)
61 {
62 ASTVerifier verifier {Allocator()};
63
64 char const *text = R"(
65 class Base {
66 public a: int = 1;
67 }
68 function main(): void {
69 let base: Base = new Base();
70 let a = base.a;
71 }
72 )";
73 es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
74 impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
75 ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
76
77 auto *ast = reinterpret_cast<ETSScript *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
78
79 ast->AsETSScript()
80 ->Statements()[1]
81 ->AsClassDeclaration()
82 ->Definition()
83 ->AsClassDefinition()
84 ->Body()[0]
85 ->AsClassProperty()
86 ->AddModifier(ark::es2panda::ir::ModifierFlags::PRIVATE);
87
88 InvariantNameSet checks;
89 checks.insert("ModifierAccessValidForAll");
90 const auto &messages = verifier.Verify(ast, checks);
91 ASSERT_EQ(messages.size(), 1);
92
93 ASSERT_NE(checks.find(messages[0].Invariant()), checks.end());
94
95 impl_->DestroyContext(ctx);
96 }
97
TEST_F(ASTVerifierTest,PrivateAccessTestNegative3)98 TEST_F(ASTVerifierTest, PrivateAccessTestNegative3)
99 {
100 ASTVerifier verifier {Allocator()};
101
102 char const *text = R"(
103 class Base {
104 public a: int = 1;
105 }
106 class Derived extends Base {}
107 function main(): void {
108 let derived: Derived = new Derived();
109 let a = derived.a;
110 }
111 )";
112 es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
113 impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
114 ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
115
116 auto *ast = reinterpret_cast<ETSScript *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
117
118 ast->AsETSScript()
119 ->Statements()[1]
120 ->AsClassDeclaration()
121 ->Definition()
122 ->AsClassDefinition()
123 ->Body()[0]
124 ->AsClassProperty()
125 ->AddModifier(ark::es2panda::ir::ModifierFlags::PRIVATE);
126
127 InvariantNameSet checks;
128 checks.insert("ModifierAccessValidForAll");
129 const auto &messages = verifier.Verify(ast, checks);
130 ASSERT_EQ(messages.size(), 1);
131
132 ASSERT_NE(checks.find(messages[0].Invariant()), checks.end());
133
134 impl_->DestroyContext(ctx);
135 }
136
TEST_F(ASTVerifierTest,PrivateAccessTestNegative4)137 TEST_F(ASTVerifierTest, PrivateAccessTestNegative4)
138 {
139 ASTVerifier verifier {Allocator()};
140
141 char const *text = R"(
142 class Base {
143 public a: int = 1;
144 }
145 class Derived extends Base {}
146 function main(): void {
147 let derived: Base = new Derived();
148 let a = derived.a;
149 }
150 )";
151 es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
152 impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
153 ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
154
155 auto *ast = reinterpret_cast<ETSScript *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
156
157 ast->AsETSScript()
158 ->Statements()[1]
159 ->AsClassDeclaration()
160 ->Definition()
161 ->AsClassDefinition()
162 ->Body()[0]
163 ->AsClassProperty()
164 ->AddModifier(ark::es2panda::ir::ModifierFlags::PRIVATE);
165
166 InvariantNameSet checks;
167 checks.insert("ModifierAccessValidForAll");
168 const auto &messages = verifier.Verify(ast, checks);
169 ASSERT_EQ(messages.size(), 1);
170
171 ASSERT_NE(checks.find(messages[0].Invariant()), checks.end());
172
173 impl_->DestroyContext(ctx);
174 }
175