1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "descriptors_names.h"
18
19 #include "gtest/gtest.h"
20
21 namespace art {
22
23 class DescriptorsNamesTest : public testing::Test {};
24
TEST_F(DescriptorsNamesTest,PrettyDescriptor_ArrayReferences)25 TEST_F(DescriptorsNamesTest, PrettyDescriptor_ArrayReferences) {
26 EXPECT_EQ("java.lang.Class[]", PrettyDescriptor("[Ljava/lang/Class;"));
27 EXPECT_EQ("java.lang.Class[][]", PrettyDescriptor("[[Ljava/lang/Class;"));
28 }
29
TEST_F(DescriptorsNamesTest,PrettyDescriptor_ScalarReferences)30 TEST_F(DescriptorsNamesTest, PrettyDescriptor_ScalarReferences) {
31 EXPECT_EQ("java.lang.String", PrettyDescriptor("Ljava.lang.String;"));
32 EXPECT_EQ("java.lang.String", PrettyDescriptor("Ljava/lang/String;"));
33 }
34
TEST_F(DescriptorsNamesTest,PrettyDescriptor_Primitive)35 TEST_F(DescriptorsNamesTest, PrettyDescriptor_Primitive) {
36 EXPECT_EQ("boolean", PrettyDescriptor(Primitive::kPrimBoolean));
37 EXPECT_EQ("byte", PrettyDescriptor(Primitive::kPrimByte));
38 EXPECT_EQ("char", PrettyDescriptor(Primitive::kPrimChar));
39 EXPECT_EQ("short", PrettyDescriptor(Primitive::kPrimShort));
40 EXPECT_EQ("int", PrettyDescriptor(Primitive::kPrimInt));
41 EXPECT_EQ("float", PrettyDescriptor(Primitive::kPrimFloat));
42 EXPECT_EQ("long", PrettyDescriptor(Primitive::kPrimLong));
43 EXPECT_EQ("double", PrettyDescriptor(Primitive::kPrimDouble));
44 EXPECT_EQ("void", PrettyDescriptor(Primitive::kPrimVoid));
45 }
46
TEST_F(DescriptorsNamesTest,PrettyDescriptor_PrimitiveArrays)47 TEST_F(DescriptorsNamesTest, PrettyDescriptor_PrimitiveArrays) {
48 EXPECT_EQ("boolean[]", PrettyDescriptor("[Z"));
49 EXPECT_EQ("boolean[][]", PrettyDescriptor("[[Z"));
50 EXPECT_EQ("byte[]", PrettyDescriptor("[B"));
51 EXPECT_EQ("byte[][]", PrettyDescriptor("[[B"));
52 EXPECT_EQ("char[]", PrettyDescriptor("[C"));
53 EXPECT_EQ("char[][]", PrettyDescriptor("[[C"));
54 EXPECT_EQ("double[]", PrettyDescriptor("[D"));
55 EXPECT_EQ("double[][]", PrettyDescriptor("[[D"));
56 EXPECT_EQ("float[]", PrettyDescriptor("[F"));
57 EXPECT_EQ("float[][]", PrettyDescriptor("[[F"));
58 EXPECT_EQ("int[]", PrettyDescriptor("[I"));
59 EXPECT_EQ("int[][]", PrettyDescriptor("[[I"));
60 EXPECT_EQ("long[]", PrettyDescriptor("[J"));
61 EXPECT_EQ("long[][]", PrettyDescriptor("[[J"));
62 EXPECT_EQ("short[]", PrettyDescriptor("[S"));
63 EXPECT_EQ("short[][]", PrettyDescriptor("[[S"));
64 }
65
TEST_F(DescriptorsNamesTest,PrettyDescriptor_PrimitiveScalars)66 TEST_F(DescriptorsNamesTest, PrettyDescriptor_PrimitiveScalars) {
67 EXPECT_EQ("boolean", PrettyDescriptor("Z"));
68 EXPECT_EQ("byte", PrettyDescriptor("B"));
69 EXPECT_EQ("char", PrettyDescriptor("C"));
70 EXPECT_EQ("double", PrettyDescriptor("D"));
71 EXPECT_EQ("float", PrettyDescriptor("F"));
72 EXPECT_EQ("int", PrettyDescriptor("I"));
73 EXPECT_EQ("long", PrettyDescriptor("J"));
74 EXPECT_EQ("short", PrettyDescriptor("S"));
75 }
76
TEST_F(DescriptorsNamesTest,InversePrettyDescriptor_ArrayReferences)77 TEST_F(DescriptorsNamesTest, InversePrettyDescriptor_ArrayReferences) {
78 EXPECT_EQ("[Ljava/lang/Class;", InversePrettyDescriptor("java.lang.Class[]"));
79 EXPECT_EQ("[[Ljava/lang/Class;", InversePrettyDescriptor("java.lang.Class[][]"));
80 }
81
TEST_F(DescriptorsNamesTest,InversePrettyDescriptor_ScalarReferences)82 TEST_F(DescriptorsNamesTest, InversePrettyDescriptor_ScalarReferences) {
83 EXPECT_EQ("Ljava/lang/String;", InversePrettyDescriptor("java.lang.String"));
84 }
85
TEST_F(DescriptorsNamesTest,InversePrettyDescriptor_PrimitiveArrays)86 TEST_F(DescriptorsNamesTest, InversePrettyDescriptor_PrimitiveArrays) {
87 EXPECT_EQ("[B", InversePrettyDescriptor("byte[]"));
88 EXPECT_EQ("[[B", InversePrettyDescriptor("byte[][]"));
89 EXPECT_EQ("[C", InversePrettyDescriptor("char[]"));
90 EXPECT_EQ("[[C", InversePrettyDescriptor("char[][]"));
91 EXPECT_EQ("[D", InversePrettyDescriptor("double[]"));
92 EXPECT_EQ("[[D", InversePrettyDescriptor("double[][]"));
93 EXPECT_EQ("[F", InversePrettyDescriptor("float[]"));
94 EXPECT_EQ("[[F", InversePrettyDescriptor("float[][]"));
95 EXPECT_EQ("[I", InversePrettyDescriptor("int[]"));
96 EXPECT_EQ("[[I", InversePrettyDescriptor("int[][]"));
97 EXPECT_EQ("[J", InversePrettyDescriptor("long[]"));
98 EXPECT_EQ("[[J", InversePrettyDescriptor("long[][]"));
99 EXPECT_EQ("[S", InversePrettyDescriptor("short[]"));
100 EXPECT_EQ("[[S", InversePrettyDescriptor("short[][]"));
101 EXPECT_EQ("[Z", InversePrettyDescriptor("boolean[]"));
102 EXPECT_EQ("[[Z", InversePrettyDescriptor("boolean[][]"));
103 }
104
TEST_F(DescriptorsNamesTest,InversePrettyDescriptor_PrimitiveScalars)105 TEST_F(DescriptorsNamesTest, InversePrettyDescriptor_PrimitiveScalars) {
106 EXPECT_EQ("B", InversePrettyDescriptor("byte"));
107 EXPECT_EQ("C", InversePrettyDescriptor("char"));
108 EXPECT_EQ("D", InversePrettyDescriptor("double"));
109 EXPECT_EQ("F", InversePrettyDescriptor("float"));
110 EXPECT_EQ("I", InversePrettyDescriptor("int"));
111 EXPECT_EQ("J", InversePrettyDescriptor("long"));
112 EXPECT_EQ("S", InversePrettyDescriptor("short"));
113 EXPECT_EQ("Z", InversePrettyDescriptor("boolean"));
114 }
115
TEST_F(DescriptorsNamesTest,MangleForJni)116 TEST_F(DescriptorsNamesTest, MangleForJni) {
117 EXPECT_EQ("hello_00024world", MangleForJni("hello$world"));
118 EXPECT_EQ("hello_000a9world", MangleForJni("hello\xc2\xa9world"));
119 EXPECT_EQ("hello_1world", MangleForJni("hello_world"));
120 EXPECT_EQ("Ljava_lang_String_2", MangleForJni("Ljava/lang/String;"));
121 EXPECT_EQ("_3C", MangleForJni("[C"));
122 }
123
TEST_F(DescriptorsNamesTest,IsValidDescriptor)124 TEST_F(DescriptorsNamesTest, IsValidDescriptor) {
125 std::vector<uint8_t> descriptor(
126 { 'L', 'a', '/', 'b', '$', 0xed, 0xa0, 0x80, 0xed, 0xb0, 0x80, ';', 0x00 });
127 EXPECT_TRUE(IsValidDescriptor(reinterpret_cast<char*>(&descriptor[0])));
128
129 std::vector<uint8_t> unpaired_surrogate(
130 { 'L', 'a', '/', 'b', '$', 0xed, 0xa0, 0x80, ';', 0x00 });
131 EXPECT_FALSE(IsValidDescriptor(reinterpret_cast<char*>(&unpaired_surrogate[0])));
132
133 std::vector<uint8_t> unpaired_surrogate_at_end(
134 { 'L', 'a', '/', 'b', '$', 0xed, 0xa0, 0x80, 0x00 });
135 EXPECT_FALSE(IsValidDescriptor(reinterpret_cast<char*>(&unpaired_surrogate_at_end[0])));
136
137 std::vector<uint8_t> invalid_surrogate(
138 { 'L', 'a', '/', 'b', '$', 0xed, 0xb0, 0x80, ';', 0x00 });
139 EXPECT_FALSE(IsValidDescriptor(reinterpret_cast<char*>(&invalid_surrogate[0])));
140
141 std::vector<uint8_t> unpaired_surrogate_with_multibyte_sequence(
142 { 'L', 'a', '/', 'b', '$', 0xed, 0xb0, 0x80, 0xf0, 0x9f, 0x8f, 0xa0, ';', 0x00 });
143 EXPECT_FALSE(
144 IsValidDescriptor(reinterpret_cast<char*>(&unpaired_surrogate_with_multibyte_sequence[0])));
145 }
146
147 } // namespace art
148