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,ProtectedAccessTestNegative1)23 TEST_F(ASTVerifierTest, ProtectedAccessTestNegative1)
24 {
25 ASTVerifier verifier {Allocator()};
26
27 char const *text = R"(
28 class Base {
29 public a: int = 1;
30 }
31 function main(): void {
32 let base: Base = new Base();
33 let a = base.a;
34 }
35 )";
36 es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
37 impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
38 ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
39
40 auto *ast = reinterpret_cast<ETSScript *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
41
42 ast->AsETSScript()
43 ->Statements()[1]
44 ->AsClassDeclaration()
45 ->Definition()
46 ->AsClassDefinition()
47 ->Body()[0]
48 ->AsClassProperty()
49 ->AddModifier(ark::es2panda::ir::ModifierFlags::PROTECTED);
50
51 InvariantNameSet checks;
52 checks.insert("ModifierAccessValidForAll");
53 const auto &messages = verifier.Verify(ast, checks);
54 ASSERT_EQ(messages.size(), 1);
55
56 ASSERT_NE(checks.find(messages[0].Invariant()), checks.end());
57
58 impl_->DestroyContext(ctx);
59 }
60
TEST_F(ASTVerifierTest,ProtectedAccessTestNegative2)61 TEST_F(ASTVerifierTest, ProtectedAccessTestNegative2)
62 {
63 ASTVerifier verifier {Allocator()};
64
65 char const *text = R"(
66 class Base {
67 public a: int = 1;
68 }
69 class Derived extends Base {}
70 function main(): void {
71 let derived: Derived = new Derived();
72 let a = derived.a;
73 }
74 )";
75 es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
76 impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
77 ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
78
79 auto *ast = reinterpret_cast<ETSScript *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
80
81 ast->AsETSScript()
82 ->Statements()[1]
83 ->AsClassDeclaration()
84 ->Definition()
85 ->AsClassDefinition()
86 ->Body()[0]
87 ->AsClassProperty()
88 ->AddModifier(ark::es2panda::ir::ModifierFlags::PROTECTED);
89
90 InvariantNameSet checks;
91 checks.insert("ModifierAccessValidForAll");
92 const auto &messages = verifier.Verify(ast, checks);
93 ASSERT_EQ(messages.size(), 1);
94
95 ASSERT_NE(checks.find(messages[0].Invariant()), checks.end());
96
97 impl_->DestroyContext(ctx);
98 }
99
TEST_F(ASTVerifierTest,ProtectedAccessTestNegative3)100 TEST_F(ASTVerifierTest, ProtectedAccessTestNegative3)
101 {
102 ASTVerifier verifier {Allocator()};
103
104 char const *text = R"(
105 class Base {
106 public a: int = 1;
107 }
108 class Derived extends Base {}
109 function main(): void {
110 let derived: Base = new Derived();
111 let a = derived.a;
112 }
113 )";
114 es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
115 impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
116 ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
117
118 auto *ast = reinterpret_cast<ETSScript *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
119
120 ast->AsETSScript()
121 ->Statements()[1]
122 ->AsClassDeclaration()
123 ->Definition()
124 ->AsClassDefinition()
125 ->Body()[0]
126 ->AsClassProperty()
127 ->AddModifier(ark::es2panda::ir::ModifierFlags::PROTECTED);
128
129 InvariantNameSet checks;
130 checks.insert("ModifierAccessValidForAll");
131 const auto &messages = verifier.Verify(ast, checks);
132 ASSERT_EQ(messages.size(), 1);
133
134 ASSERT_NE(checks.find(messages[0].Invariant()), checks.end());
135
136 impl_->DestroyContext(ctx);
137 }
138