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,MangleForJni)77 TEST_F(DescriptorsNamesTest, MangleForJni) {
78   EXPECT_EQ("hello_00024world", MangleForJni("hello$world"));
79   EXPECT_EQ("hello_000a9world", MangleForJni("hello\xc2\xa9world"));
80   EXPECT_EQ("hello_1world", MangleForJni("hello_world"));
81   EXPECT_EQ("Ljava_lang_String_2", MangleForJni("Ljava/lang/String;"));
82   EXPECT_EQ("_3C", MangleForJni("[C"));
83 }
84 
TEST_F(DescriptorsNamesTest,IsValidDescriptor)85 TEST_F(DescriptorsNamesTest, IsValidDescriptor) {
86   std::vector<uint8_t> descriptor(
87       { 'L', 'a', '/', 'b', '$', 0xed, 0xa0, 0x80, 0xed, 0xb0, 0x80, ';', 0x00 });
88   EXPECT_TRUE(IsValidDescriptor(reinterpret_cast<char*>(&descriptor[0])));
89 
90   std::vector<uint8_t> unpaired_surrogate(
91       { 'L', 'a', '/', 'b', '$', 0xed, 0xa0, 0x80, ';', 0x00 });
92   EXPECT_FALSE(IsValidDescriptor(reinterpret_cast<char*>(&unpaired_surrogate[0])));
93 
94   std::vector<uint8_t> unpaired_surrogate_at_end(
95       { 'L', 'a', '/', 'b', '$', 0xed, 0xa0, 0x80, 0x00 });
96   EXPECT_FALSE(IsValidDescriptor(reinterpret_cast<char*>(&unpaired_surrogate_at_end[0])));
97 
98   std::vector<uint8_t> invalid_surrogate(
99       { 'L', 'a', '/', 'b', '$', 0xed, 0xb0, 0x80, ';', 0x00 });
100   EXPECT_FALSE(IsValidDescriptor(reinterpret_cast<char*>(&invalid_surrogate[0])));
101 
102   std::vector<uint8_t> unpaired_surrogate_with_multibyte_sequence(
103       { 'L', 'a', '/', 'b', '$', 0xed, 0xb0, 0x80, 0xf0, 0x9f, 0x8f, 0xa0, ';', 0x00 });
104   EXPECT_FALSE(
105       IsValidDescriptor(reinterpret_cast<char*>(&unpaired_surrogate_with_multibyte_sequence[0])));
106 }
107 
108 }  // namespace art
109