1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "dex/proto_reference.h"
#include <vector>
#include "dex/dex_file_types.h"
#include "dex/test_dex_file_builder.h"
#include "gtest/gtest.h"
namespace art {
TEST(ProtoReference, WithinOneDexFile) {
TestDexFileBuilder builder;
builder.AddMethod("LClass", "()I", "sideEffect2");
builder.AddMethod("LClass", "(I)Ljava/lang/String;", "toString");
builder.AddMethod("LClass", "(II)Ljava/lang/String;", "toString");
builder.AddMethod("LClass", "(IJ)Ljava/lang/String;", "toString");
builder.AddMethod("LClass", "(JJ)Ljava/lang/String;", "toString");
builder.AddMethod("LClass", "()V", "sideEffect1");
std::unique_ptr<const DexFile> dex_file = builder.Build("fake location");
const size_t num_protos = 6u;
EXPECT_EQ(num_protos, dex_file->NumProtoIds());
std::vector<ProtoReference> protos;
for (size_t i = 0; i < num_protos; ++i) {
protos.emplace_back(ProtoReference(dex_file.get(), dex::ProtoIndex(i)));
}
ProtoReferenceValueComparator cmp;
for (size_t i = 0; i < num_protos; ++i) {
for (size_t j = 0; j < num_protos; ++j) {
EXPECT_EQ(cmp(protos[i], protos[j]), i < j)
<< "Inconsistent at i=" << i << " and j=" << j;
}
}
}
TEST(ProtoReference, AcrossDifferentDexFiles) {
TestDexFileBuilder builder1;
builder1.AddMethod("LClass", "()I", "sideEffect2");
builder1.AddMethod("LClass", "(I)Ljava/lang/String;", "toString");
builder1.AddMethod("LClass", "(II)Ljava/lang/String;", "toString");
builder1.AddMethod("LClass", "(IJ)Ljava/lang/String;", "toString");
builder1.AddMethod("LClass", "(IJZ)Ljava/lang/String;", "toString");
builder1.AddMethod("LClass", "()V", "sideEffect1");
std::unique_ptr<const DexFile> dex_file1 = builder1.Build("fake location");
EXPECT_EQ(6u, dex_file1->NumProtoIds());
TestDexFileBuilder builder2;
builder2.AddMethod("LClass2", "(IJ)Ljava/lang/String;", "toString");
builder2.AddMethod("LClass2", "()V", "sideEffect1");
builder2.AddMethod("LClass2", "(I)V", "sideEffect2");
std::unique_ptr<const DexFile> dex_file2 = builder2.Build("fake location 2");
EXPECT_EQ(3u, dex_file2->NumProtoIds());
ProtoReference V_dex1 = ProtoReference(dex_file1.get(), dex::ProtoIndex(5));
ProtoReference V_dex2 = ProtoReference(dex_file2.get(), dex::ProtoIndex(1));
ProtoReferenceValueComparator cmp;
EXPECT_FALSE(cmp(V_dex1, V_dex2));
EXPECT_FALSE(cmp(V_dex2, V_dex1));
ProtoReference IString_dex1 = ProtoReference(dex_file1.get(), dex::ProtoIndex(1));
ProtoReference IIString_dex1 = ProtoReference(dex_file1.get(), dex::ProtoIndex(2));
ProtoReference IJString_dex1 = ProtoReference(dex_file1.get(), dex::ProtoIndex(3));
ProtoReference IJZString_dex1 = ProtoReference(dex_file1.get(), dex::ProtoIndex(4));
ProtoReference IJString_dex2 = ProtoReference(dex_file2.get(), dex::ProtoIndex(0));
EXPECT_TRUE(cmp(IString_dex1, V_dex2));
EXPECT_TRUE(cmp(IString_dex1, IJString_dex2));
EXPECT_TRUE(cmp(IIString_dex1, IJString_dex2));
EXPECT_FALSE(cmp(IJString_dex1, IJString_dex2));
EXPECT_FALSE(cmp(IJString_dex2, IJString_dex1));
EXPECT_FALSE(cmp(IJZString_dex1, IJString_dex2));
EXPECT_TRUE(cmp(IJString_dex2, IJZString_dex1));
}
} // namespace art
|