• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,ProtectedAccessTestNegative4)23 TEST_F(ASTVerifierTest, ProtectedAccessTestNegative4)
24 {
25     ASTVerifier verifier {Allocator()};
26 
27     char const *text = R"(
28         class Base {
29             public a: int = 1;
30             public protectedMethod() {
31                 this.a = 2;
32             }
33         }
34         function main(): void {
35             let base: Base = new Base();
36             base.protectedMethod();
37         }
38     )";
39     es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
40     impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
41     ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
42 
43     auto *ast = reinterpret_cast<ETSScript *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
44 
45     ast->AsETSScript()
46         ->Statements()[0]
47         ->AsClassDeclaration()
48         ->Definition()
49         ->AsClassDefinition()
50         ->Body()[1]
51         ->AsClassElement()
52         ->Value()
53         ->AsFunctionExpression()
54         ->Function()
55         ->AsScriptFunction()
56         ->Body()
57         ->AsBlockStatement()
58         ->Statements()[1]
59         ->AsExpressionStatement()
60         ->GetExpression()
61         ->AsCallExpression()
62         ->Signature()
63         ->AddSignatureFlag(ark::es2panda::checker::SignatureFlags::PROTECTED);
64 
65     InvariantNameSet checks;
66     checks.insert("ModifierAccessValidForAll");
67     const auto &messages = verifier.Verify(ast, checks);
68     ASSERT_EQ(messages.size(), 1);
69 
70     ASSERT_NE(checks.find(messages[0].Invariant()), checks.end());
71 
72     impl_->DestroyContext(ctx);
73 }
74 
TEST_F(ASTVerifierTest,ProtectedAccessTestNegative5)75 TEST_F(ASTVerifierTest, ProtectedAccessTestNegative5)
76 {
77     ASTVerifier verifier {Allocator()};
78 
79     char const *text = R"(
80         class Base {
81             public a: int = 1;
82             public protectedMethod() {
83                 this.a = 2;
84             }
85         }
86         class Derived extends Base {}
87         function main(): void {
88             let derived: Derived = new Derived();
89             derived.protectedMethod();
90         }
91     )";
92     es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
93     impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
94     ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
95 
96     auto *ast = reinterpret_cast<ETSScript *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
97 
98     ast->AsETSScript()
99         ->Statements()[0]
100         ->AsClassDeclaration()
101         ->Definition()
102         ->AsClassDefinition()
103         ->Body()[1]
104         ->AsClassElement()
105         ->Value()
106         ->AsFunctionExpression()
107         ->Function()
108         ->AsScriptFunction()
109         ->Body()
110         ->AsBlockStatement()
111         ->Statements()[1]
112         ->AsExpressionStatement()
113         ->GetExpression()
114         ->AsCallExpression()
115         ->Signature()
116         ->AddSignatureFlag(ark::es2panda::checker::SignatureFlags::PROTECTED);
117 
118     InvariantNameSet checks;
119     checks.insert("ModifierAccessValidForAll");
120     const auto &messages = verifier.Verify(ast, checks);
121     ASSERT_EQ(messages.size(), 1);
122 
123     ASSERT_NE(checks.find(messages[0].Invariant()), checks.end());
124 
125     impl_->DestroyContext(ctx);
126 }
127 
TEST_F(ASTVerifierTest,ProtectedAccessTestNegative6)128 TEST_F(ASTVerifierTest, ProtectedAccessTestNegative6)
129 {
130     ASTVerifier verifier {Allocator()};
131 
132     char const *text = R"(
133         class Base {
134             public a: int = 1;
135             public protectedMethod() {
136                 this.a = 2;
137             }
138         }
139         class Derived extends Base {}
140         function main(): void {
141             let derived: Base = new Derived();
142             derived.protectedMethod();
143         }
144     )";
145     es2panda_Context *ctx = impl_->CreateContextFromString(cfg_, text, "dummy.sts");
146     impl_->ProceedToState(ctx, ES2PANDA_STATE_CHECKED);
147     ASSERT_EQ(impl_->ContextState(ctx), ES2PANDA_STATE_CHECKED);
148 
149     auto *ast = reinterpret_cast<ETSScript *>(impl_->ProgramAst(impl_->ContextProgram(ctx)));
150 
151     ast->AsETSScript()
152         ->Statements()[0]
153         ->AsClassDeclaration()
154         ->Definition()
155         ->AsClassDefinition()
156         ->Body()[1]
157         ->AsClassElement()
158         ->Value()
159         ->AsFunctionExpression()
160         ->Function()
161         ->AsScriptFunction()
162         ->Body()
163         ->AsBlockStatement()
164         ->Statements()[1]
165         ->AsExpressionStatement()
166         ->GetExpression()
167         ->AsCallExpression()
168         ->Signature()
169         ->AddSignatureFlag(ark::es2panda::checker::SignatureFlags::PROTECTED);
170 
171     InvariantNameSet checks;
172     checks.insert("ModifierAccessValidForAll");
173 
174     const auto &messages = verifier.Verify(ast, checks);
175     ASSERT_EQ(messages.size(), 1);
176 
177     ASSERT_NE(checks.find(messages[0].Invariant()), checks.end());
178 
179     impl_->DestroyContext(ctx);
180 }
181