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 "gtest/gtest.h"
17 
18 #define private public
19 #define protected public
20 #include "base/memory/ace_type.h"
21 #include "frameworks/bridge/common/utils/source_map.h"
22 
23 #undef private
24 #undef protected
25 
26 using namespace testing;
27 using namespace testing::ext;
28 
29 namespace OHOS::Ace::Framework {
30 class SourceMapTest : public testing::Test {
31 public:
SetUpTestSuite()32     static void SetUpTestSuite() {}
TearDownTestSuite()33     static void TearDownTestSuite() {}
34 };
35 
36 /**
37  * @tc.name: SourceMapTest001
38  * @tc.desc: Find()
39  * @tc.type: FUNC
40  */
41 HWTEST_F(SourceMapTest, SourceMapTest001, TestSize.Level1)
42 {
43     /**
44      * @tc.steps: step1. Create a revSourceMap.
45      */
46     RevSourceMap revSourceMap;
47     int32_t row = 0;
48     int32_t col = 0;
49     bool isColPrecise = false;
50     revSourceMap.files_.emplace_back("files");
51 
52     /**
53      * @tc.steps: step2. Testing values.
54      * @tc.expected: return mappingInfo.
55      */
56     auto mappingInfo = revSourceMap.Find(row, col, isColPrecise); // 47line if 1、1、1
57     EXPECT_EQ(mappingInfo.col, 0);
58     row = 5;
59     mappingInfo = revSourceMap.Find(row, col, isColPrecise); // 47line if 0、1、1
60     EXPECT_EQ(mappingInfo.col, 0);
61     col = 1;
62     mappingInfo = revSourceMap.Find(row, col, isColPrecise); // 47line if 0、0、1
63     EXPECT_EQ(mappingInfo.col, 0);
64     SourceMapInfo sourceMapInfo;
65     revSourceMap.afterPos_.emplace_back(sourceMapInfo);
66     mappingInfo = revSourceMap.Find(row, col, isColPrecise); // 47line if 0、0、0 57line if 1
67     EXPECT_EQ(mappingInfo.col, 1);
68     revSourceMap.afterPos_.emplace_back(SourceMapInfo { 0, 0, 5, 0, 0, 0 });
69     mappingInfo = revSourceMap.Find(row, col, isColPrecise); // 47line if 0、0、0 57line if 0 62line 0、0、0
70     EXPECT_EQ(mappingInfo.col, 0);
71     revSourceMap.afterPos_.clear();
72     revSourceMap.afterPos_.emplace_back(SourceMapInfo { 0, 0, 5, 1, 0, 0 }); // 62line 0、1、1
73     mappingInfo = revSourceMap.Find(row, col, isColPrecise);
74     EXPECT_EQ(mappingInfo.col, 0);
75     revSourceMap.afterPos_.clear();
76     revSourceMap.afterPos_.emplace_back(SourceMapInfo { 0, 0, 4, 1, 0, 0 }); // 62line 1、1、0
77     mappingInfo = revSourceMap.Find(row, col, isColPrecise);
78     EXPECT_EQ(mappingInfo.col, 0);
79 }
80 
81 /**
82  * @tc.name: SourceMapTest002
83  * @tc.desc: Find()
84  * @tc.type: FUNC
85  */
86 HWTEST_F(SourceMapTest, SourceMapTest002, TestSize.Level1)
87 {
88     /**
89      * @tc.steps: step1. Create a revSourceMap.
90      */
91     RevSourceMap revSourceMap;
92     int32_t row = 0;
93     int32_t col = 0;
94     bool isColPrecise = true;
95     revSourceMap.files_.emplace_back("files");
96 
97     /**
98      * @tc.steps: step2. Testing values.
99      * @tc.expected: return mappingInfo.
100      */
101     row = 5;
102     col = 5;
103     revSourceMap.afterPos_.emplace_back(SourceMapInfo { 0, 0, 5, 1, 0, 0 });
104     revSourceMap.afterPos_.emplace_back(SourceMapInfo { 0, 0, 3, 1, 0, 0 });
105     revSourceMap.afterPos_.emplace_back(SourceMapInfo { 0, 0, 4, 1, 0, 0 });
106     auto mappingInfo = revSourceMap.Find(row, col, isColPrecise); // 79line 0、0、 1、0
107     EXPECT_EQ(mappingInfo.col, 0);
108     isColPrecise = false;
109     mappingInfo = revSourceMap.Find(row, col, isColPrecise); // 79line 1、0、 1、0
110     EXPECT_EQ(mappingInfo.col, 0);
111     col = 1;
112     mappingInfo = revSourceMap.Find(row, col, isColPrecise); // 79line 1、1、 1、 1 83line 0、1
113     EXPECT_EQ(mappingInfo.col, 0);
114     revSourceMap.afterPos_[2].sourcesVal = -1;
115     mappingInfo = revSourceMap.Find(row, col, isColPrecise); // 83line 1、0
116     EXPECT_EQ(mappingInfo.col, 0);
117     revSourceMap.afterPos_[2].sourcesVal = 0;
118     revSourceMap.sources_.emplace_back("0");
119     revSourceMap.sources_.emplace_back("1");
120     mappingInfo = revSourceMap.Find(row, col, isColPrecise); // 89line 0
121     EXPECT_EQ(mappingInfo.col, 1);
122     revSourceMap.sources_[0] = ("webpack:///111");
123     mappingInfo = revSourceMap.Find(row, col, isColPrecise); // 89line 1
124     EXPECT_EQ(mappingInfo.col, 1);
125 }
126 
127 /**
128  * @tc.name: SourceMapTest003
129  * @tc.desc: GetOriginalNames()
130  * @tc.type: FUNC
131  */
132 HWTEST_F(SourceMapTest, SourceMapTest003, TestSize.Level1)
133 {
134     /**
135      * @tc.steps: step1. Create a revSourceMap.
136      */
137     RevSourceMap revSourceMap;
138     std::string sourceCode;
139     uint32_t errorPos = 0;
140     /**
141      * @tc.steps: step2. Testing values.
142      * @tc.expected: return sourceCode.
143      */
144     EXPECT_EQ(revSourceMap.GetOriginalNames(sourceCode, errorPos), sourceCode);
145     sourceCode = "test";
146     EXPECT_EQ(revSourceMap.GetOriginalNames(sourceCode, errorPos), sourceCode);
147     sourceCode = "SourceCode:\n test";
148     EXPECT_EQ(revSourceMap.GetOriginalNames(sourceCode, errorPos), sourceCode);
149     revSourceMap.nameMap_.emplace_back("0");
150     EXPECT_EQ(revSourceMap.GetOriginalNames(sourceCode, errorPos), sourceCode);
151     revSourceMap.nameMap_.emplace_back("test");
152     revSourceMap.nameMap_.emplace_back("SourceCode");
153     revSourceMap.nameMap_.emplace_back("Code");
154     EXPECT_EQ(revSourceMap.GetOriginalNames(sourceCode, errorPos), "Code:\n test");
155     errorPos = 10;
156     EXPECT_EQ(revSourceMap.GetOriginalNames(sourceCode, errorPos), "Code:\n test");
157     sourceCode = "SourceCode:\n";
158     EXPECT_EQ(revSourceMap.GetOriginalNames(sourceCode, errorPos), "Code:\n");
159 }
160 
161 /**
162  * @tc.name: SourceMapTest004
163  * @tc.desc: ExtractKeyInfo() and Init()
164  * @tc.type: FUNC
165  */
166 HWTEST_F(SourceMapTest, SourceMapTest004, TestSize.Level1)
167 {
168     /**
169      * @tc.steps: step1. Create a revSourceMap.
170      */
171     RevSourceMap revSourceMap;
172     std::string sourceMap;
173     revSourceMap.Init(sourceMap);
174     EXPECT_TRUE(revSourceMap.mappings_.empty());
175     /**
176      * @tc.steps: step2. Testing values.
177      * @tc.expected: return revSourceMap.mappings_.size() = 0.
178      */
179     sourceMap = "\"test\"\"sources\"\"./pag\\es\""
180                 "\"names\"\"dfxtest\""
181                 "\"mappings\"\"test123AB+B,B;test2BBBB,test3B\""
182                 "\"file\"\"dfxtest.js\""
183                 "\"nameMap\"\"testMap\""
184                 "\"sourceContent\"\"conent\""
185                 "\"sourceRoot\"\"root\"";
186     revSourceMap.Init(sourceMap);
187     EXPECT_EQ(revSourceMap.mappings_.size(), 0);
188 }
189 
190 /**
191  * @tc.name: SourceMapTest005
192  * @tc.desc: ExtractKeyInfo() and Init()
193  * @tc.type: FUNC
194  */
195 HWTEST_F(SourceMapTest, SourceMapTest005, TestSize.Level1)
196 {
197     /**
198      * @tc.steps: step1. Create a revSourceMap.
199      */
200     RevSourceMap revSourceMap;
201     std::string sourceMap;
202     revSourceMap.Init(sourceMap);
203     EXPECT_TRUE(revSourceMap.mappings_.empty());
204     /**
205      * @tc.steps: step2. Testing values.
206      * @tc.expected: return revSourceMap.mappings_.size() = 1.
207      */
208     sourceMap = "\"test\"\"sources\"\"./pag\\es\""
209                 "\"names\"\"dfxtest\""
210                 "\"mappings\"\"test123AB+\""
211                 "\"file\"\"dfxtest.js\""
212                 "\"nameMap\"\"testMap\""
213                 "\"sourceContent\"\"conent\""
214                 "\"sourceRoot\"\"root\"";
215     revSourceMap.Init(sourceMap);
216     EXPECT_EQ(revSourceMap.mappings_.size(), 1);
217 }
218 
219 /**
220  * @tc.name: SourceMapTest006
221  * @tc.desc: ExtractKeyInfo() and MergeInit()
222  * @tc.type: FUNC
223  */
224 HWTEST_F(SourceMapTest, SourceMapTest006, TestSize.Level1)
225 {
226     /**
227      * @tc.steps: step1. Create a revSourceMap.
228      */
229     RevSourceMap revSourceMap;
230     std::string sourceMap;
231     RefPtr<RevSourceMap> curMapData = AceType::MakeRefPtr<RevSourceMap>();
232     revSourceMap.MergeInit(sourceMap, curMapData);
233     EXPECT_TRUE(curMapData->mappings_.empty());
234     /**
235      * @tc.steps: step2. Testing values.
236      * @tc.expected: return curMapData.mappings_.size() = 1.
237      */
238     sourceMap = "\"test\"\"sources\"\"./pag\\es\""
239                 "\"names\"\"dfxtest\""
240                 "\"mappings\"\"test123AB+B,B;test2BBBB,test3B\""
241                 "\"file\"\"dfxtest.js\""
242                 "\"nameMap\"\"testMap\""
243                 "\"sourceContent\"\"conent\""
244                 "\"sourceRoot\"\"root\"";
245     revSourceMap.MergeInit(sourceMap, curMapData);
246     EXPECT_EQ(curMapData->mappings_.size(), 0);
247 }
248 
249 /**
250  * @tc.name: SourceMapTest007
251  * @tc.desc: ExtractKeyInfo() and MergeInit()
252  * @tc.type: FUNC
253  */
254 HWTEST_F(SourceMapTest, SourceMapTest007, TestSize.Level1)
255 {
256     /**
257      * @tc.steps: step1. Create a revSourceMap.
258      */
259     RevSourceMap revSourceMap;
260     std::string sourceMap;
261     RefPtr<RevSourceMap> curMapData = AceType::MakeRefPtr<RevSourceMap>();
262     /**
263      * @tc.steps: step2. Testing values.
264      * @tc.expected: return curMapData.mappings_.size() = 1.
265      */
266     sourceMap = "\"test\"\"sources\"\"./pag\\es\""
267                 "\"names\"\"dfxtest\""
268                 "\"mappings\"\"test123AB+\""
269                 "\"file\"\"dfxtest.js\""
270                 "\"nameMap\"\"testMap\""
271                 "\"sourceContent\"\"conent\""
272                 "\"sourceRoot\"\"root\"";
273     revSourceMap.MergeInit(sourceMap, curMapData);
274     EXPECT_EQ(curMapData->mappings_.size(), 1);
275 }
276 
277 /**
278  * @tc.name: SourceMapTest008
279  * @tc.desc: StageModeSourceMapSplit()
280  * @tc.type: FUNC
281  */
282 HWTEST_F(SourceMapTest, SourceMapTest008, TestSize.Level1)
283 {
284     /**
285      * @tc.steps: step1. Create a revSourceMap.
286      */
287     RevSourceMap revSourceMap;
288     std::string sourceMap;
289     std::unordered_map<std::string, RefPtr<RevSourceMap>> sourceMaps;
290     /**
291      * @tc.steps: step2. Testing values.
292      * @tc.expected: return curMapData.mappings_.size() = 1.
293      */
294     sourceMap = "\"test\": {\"sources\": [test],[test1]}\"./pag\\es\""
295                 "\"names\"\"dfxtest\""
296                 "\"mappings\"\"test123AB+\""
297                 "\"file\"\"dfxtest.js\""
298                 "\"nameMap\"\"testMap\""
299                 "\"sourceContent\"\"conent\""
300                 "\"sourceRoot\"\"root\"";
301     revSourceMap.StageModeSourceMapSplit(sourceMap, sourceMaps);
302     EXPECT_EQ(sourceMaps.size(), 1);
303 }
304 
305 /**
306  * @tc.name: SourceMapTest009
307  * @tc.desc: GetOriginalNames()
308  * @tc.type: FUNC
309  */
310 HWTEST_F(SourceMapTest, SourceMapTest009, TestSize.Level1)
311 {
312     /**
313      * @tc.steps: step1. construct posDiff > 0,lineBreakPos + 1 < jsCode.length() - 1 is true
314      */
315     RevSourceMap revSourceMap;
316     revSourceMap.nameMap_.push_back("param1");
317     revSourceMap.nameMap_.push_back("param111");
318     revSourceMap.nameMap_.push_back("param2");
319     revSourceMap.nameMap_.push_back("param222");
320     std::string sourceCode = "SourceCode:\nvar param1 = 1;\nvar param2 = 2;\n";
321     uint32_t errorPos = 20;
322     std::string result = revSourceMap.GetOriginalNames(sourceCode, errorPos);
323     EXPECT_EQ(result, "SourceCode:\nvar param111 = 1;\n  var param222 = 2;\n");
324 
325     /**
326      * @tc.steps: step2. construct posDiff < 0,flagPos < jsCode.length() false
327      */
328     sourceCode = "SourceCode:\nvar param111 = 1;\nvar param222 = 2;\n";
329     revSourceMap.nameMap_.clear();
330     revSourceMap.nameMap_.push_back("param111");
331     revSourceMap.nameMap_.push_back("param1");
332     revSourceMap.nameMap_.push_back("param222");
333     revSourceMap.nameMap_.push_back("param2");
334     std::string result2 = revSourceMap.GetOriginalNames(sourceCode, errorPos);
335     EXPECT_EQ(result2, "SourceCode:\nvar param1 = 1;\nvar param2 = 2;\n");
336     // flagPos < jsCode.length() true, false
337     sourceCode = "SourceCode:\nvar param111 = 1;var param222 = 2;\n";
338     std::string result3 = revSourceMap.GetOriginalNames(sourceCode, errorPos);
339     EXPECT_EQ(result3, "SourceCode:\nvar param1 = 1;var param2 = 2;\n");
340     // flagPos < jsCode.length() true, true,true
341     sourceCode = "SourceCode:\nvar param111 = 1;var^param222 = 2;\n";
342     uint32_t errorPos2 = 19;
343     std::string result4 = revSourceMap.GetOriginalNames(sourceCode, errorPos2);
344     EXPECT_EQ(result4, "SourceCode:\nvar param1 = 1;r^param2 = 2;\n");
345 }
346 
347 /**
348  * @tc.name: SourceMapTest010
349  * @tc.desc: VlqRevCode()
350  * @tc.type: FUNC
351  */
352 HWTEST_F(SourceMapTest, SourceMapTest010, TestSize.Level1)
353 {
354     /**
355      * @tc.steps: step1. VlqRevCode vStr.size() == 0
356      */
357     RevSourceMap revSourceMap;
358     std::string vStr = "";
359     std::vector<int32_t> ans;
360     bool ret = revSourceMap.VlqRevCode(vStr, ans);
361     EXPECT_FALSE(ret);
362 
363     /**
364      * @tc.steps: step2. VlqRevCode vStr.size() != 0,include invalid char
365      */
366     std::string vStr2 = "abc/&";
367     bool ret2 = revSourceMap.VlqRevCode(vStr2, ans);
368     EXPECT_FALSE(ret2);
369 
370     /**
371      * @tc.steps: step3. HandleMappings mapping = ""
372      */
373     std::string mapping;
374     std::vector<std::string> ret3 = revSourceMap.HandleMappings(mapping);
375     EXPECT_EQ(ret3.size(), 0);
376 
377     /**
378      * @tc.steps: step4. HandleMappings mapping contain ';'
379      */
380     std::string mapping2 = ";";
381     std::vector<std::string> ret4 = revSourceMap.HandleMappings(mapping2);
382     EXPECT_EQ(ret4.size(), 1);
383 }
384 } // namespace OHOS::Ace::Framework
385