1 /*
2 * Copyright (C) 2016 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 #define LOG_TAG "libhidl-gen-utils"
18
19 #include <hidl-util/FqInstance.h>
20
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23 #include <vector>
24
25 using ::android::FqInstance;
26 using ::android::FQName;
27 using ::testing::Optional;
28 using ::testing::Property;
29
30 static const std::vector<std::string> kValidFqNames = {
31 "android.hardware.foo@1.0::IFoo.Type",
32 "@1.0::IFoo.Type",
33 "android.hardware.foo@1.0",
34 "IFoo.Type",
35 "Type",
36 "f",
37 "_",
38 "_9",
39 "_a",
40 "android.hardware.foo@1.0::IFoo.Type:MY_ENUM_VALUE",
41 "@1.0::IFoo.Type:MY_ENUM_VALUE",
42 "IFoo.Type:MY_ENUM_VALUE",
43 "foo@1.0::IFoo",
44 "android.hardware.foo@1.0::IFoo"};
45
46 static const std::vector<std::string> kInvalidFqNames = {
47 "",
48 "@",
49 ":",
50 "@foo",
51 "@1.0:Foo",
52 "foo::IFoo",
53 "foo.bar::IFoo",
54 "*",
55 "&",
56 "aa;sdf",
57 "foo@1.0:FOO_BAR",
58 "9foo@1.0",
59 "foo.9foo@1.0",
60 "@01.0::IFoo.Type",
61 "@1.00::IFoo.Type",
62 "@1.01::IFoo.Type",
63 "88.foo@1.0",
64 "9Foo",
65 "08Foo",
66 "@:",
67 "foo:",
68 "foo@",
69 "8foo@",
70 "@foo8",
71 ":foo8",
72 "8:foo",
73 "8@foo",
74 "foo@bar",
75 };
76
TEST(LibHidlGenUtilsTest,FqName)77 TEST(LibHidlGenUtilsTest, FqName) {
78 FQName e;
79 for (const std::string& testString : kValidFqNames) {
80 ASSERT_TRUE(e.setTo(testString)) << testString;
81 EXPECT_EQ(testString, e.string());
82 };
83 }
84
TEST(LibHidlGenUtilsTest,FqNameIdentifier)85 TEST(LibHidlGenUtilsTest, FqNameIdentifier) {
86 FQName e;
87 ASSERT_TRUE(e.setTo("IFoo"));
88 EXPECT_TRUE(e.isIdentifier());
89
90 ASSERT_TRUE(e.setTo("foo.IFoo"));
91 EXPECT_FALSE(e.isIdentifier());
92 ASSERT_TRUE(e.setTo("@1.0::IFoo"));
93 EXPECT_FALSE(e.isIdentifier());
94 }
95
TEST(LibHidlGenUtilsTest,InvalidFqName)96 TEST(LibHidlGenUtilsTest, InvalidFqName) {
97 FQName e;
98 for (const std::string& testString : kInvalidFqNames) {
99 EXPECT_FALSE(e.setTo(testString)) << testString;
100 };
101 }
102
TEST(LibHidlGenUtilsTest,FqInstance1)103 TEST(LibHidlGenUtilsTest, FqInstance1) {
104 FqInstance e;
105 ASSERT_TRUE(e.setTo("android.hardware.foo@1.0::IFoo/instance"));
106 EXPECT_EQ("android.hardware.foo@1.0::IFoo/instance", e.string());
107 ASSERT_TRUE(e.hasPackage());
108 EXPECT_EQ("android.hardware.foo", e.getPackage());
109 ASSERT_TRUE(e.hasVersion());
110 EXPECT_EQ(1u, e.getMajorVersion());
111 EXPECT_EQ(0u, e.getMinorVersion());
112 EXPECT_EQ((std::make_pair<size_t, size_t>(1u, 0u)), e.getVersion());
113 ASSERT_TRUE(e.hasInterface());
114 EXPECT_EQ("IFoo", e.getInterface());
115 ASSERT_TRUE(e.hasInstance());
116 EXPECT_EQ("instance", e.getInstance());
117 }
118
TEST(LibHidlGenUtilsTest,FqInstance2)119 TEST(LibHidlGenUtilsTest, FqInstance2) {
120 FqInstance e;
121 ASSERT_TRUE(e.setTo("@1.0::IFoo/instance"));
122 EXPECT_EQ("@1.0::IFoo/instance", e.string());
123 ASSERT_FALSE(e.hasPackage());
124 ASSERT_TRUE(e.hasVersion());
125 EXPECT_EQ((std::make_pair<size_t, size_t>(1u, 0u)), e.getVersion());
126 ASSERT_TRUE(e.hasInterface());
127 EXPECT_EQ("IFoo", e.getInterface());
128 ASSERT_TRUE(e.hasInstance());
129 EXPECT_EQ("instance", e.getInstance());
130 }
131
TEST(LibHidlGenUtilsTest,FqInstance3)132 TEST(LibHidlGenUtilsTest, FqInstance3) {
133 FqInstance e;
134 ASSERT_TRUE(e.setTo("IFoo/instance"));
135 EXPECT_EQ("IFoo/instance", e.string());
136 ASSERT_FALSE(e.hasPackage());
137 ASSERT_FALSE(e.hasVersion());
138 ASSERT_TRUE(e.hasInterface());
139 EXPECT_EQ("IFoo", e.getInterface());
140 ASSERT_TRUE(e.hasInstance());
141 EXPECT_EQ("instance", e.getInstance());
142 }
143
TEST(LibHidlGenUtilsTest,FqInstanceFqNameOnly)144 TEST(LibHidlGenUtilsTest, FqInstanceFqNameOnly) {
145 FqInstance e;
146 for (const std::string& testString : kValidFqNames) {
147 ASSERT_TRUE(e.setTo(testString)) << testString;
148 EXPECT_EQ(testString, e.string());
149 ASSERT_FALSE(e.hasInstance());
150 };
151 }
152
TEST(LibHidlGenUtilsTest,FqInstanceInvalidFqNameOnly)153 TEST(LibHidlGenUtilsTest, FqInstanceInvalidFqNameOnly) {
154 FqInstance e;
155 for (const std::string& testString : kInvalidFqNames) {
156 EXPECT_FALSE(e.setTo(testString)) << testString;
157 };
158 }
159
TEST(LibHidlGenUtilsTest,FqInstanceIdentifier)160 TEST(LibHidlGenUtilsTest, FqInstanceIdentifier) {
161 FqInstance e;
162 ASSERT_TRUE(e.setTo("Type"));
163 EXPECT_EQ("Type", e.string());
164 ASSERT_FALSE(e.hasInstance());
165 }
166
TEST(LibHidlGenUtilsTest,FqInstanceSetToByComponent)167 TEST(LibHidlGenUtilsTest, FqInstanceSetToByComponent) {
168 FqInstance e;
169 ASSERT_TRUE(e.setTo("android.hardware.foo", 1, 0, "IFoo", "default"));
170 EXPECT_EQ("android.hardware.foo@1.0::IFoo/default", e.string());
171 ASSERT_TRUE(e.setTo("android.hardware.foo", 1, 0, "IFoo"));
172 EXPECT_EQ("android.hardware.foo@1.0::IFoo", e.string());
173 ASSERT_TRUE(e.setTo("android.hardware.foo", 1, 0));
174 EXPECT_EQ("android.hardware.foo@1.0", e.string());
175 ASSERT_TRUE(e.setTo(1, 0, "IFoo", "default"));
176 EXPECT_EQ("@1.0::IFoo/default", e.string());
177 ASSERT_TRUE(e.setTo(1, 0, "IFoo"));
178 EXPECT_EQ("@1.0::IFoo", e.string());
179 ASSERT_TRUE(e.setTo("IFoo", "default"));
180 EXPECT_EQ("IFoo/default", e.string());
181 }
182
TEST(LibHidlGenUtilsTest,FqDefaultVersion)183 TEST(LibHidlGenUtilsTest, FqDefaultVersion) {
184 FQName n;
185 FqInstance i;
186
187 ASSERT_TRUE(FQName::parse("IFoo.test", &n));
188 EXPECT_EQ((std::make_pair<size_t, size_t>(0u, 0u)), n.getVersion());
189 ASSERT_TRUE(i.setTo("IFoo.test"));
190 EXPECT_EQ((std::make_pair<size_t, size_t>(0u, 0u)), i.getVersion());
191 ASSERT_TRUE(FQName::parse("package@1.2::IFoo", &n));
192 EXPECT_EQ((std::make_pair<size_t, size_t>(1u, 2u)), n.getVersion());
193 ASSERT_TRUE(i.setTo("package@1.2::IFoo"));
194 EXPECT_EQ((std::make_pair<size_t, size_t>(1u, 2u)), i.getVersion());
195 }
196
TEST(LibHidlGenUtilsTest,FqInstanceFrom)197 TEST(LibHidlGenUtilsTest, FqInstanceFrom) {
198 EXPECT_THAT(FqInstance::from("android.hardware.foo", 1, 0, "IFoo", "default"),
199 Optional(Property(&FqInstance::string, "android.hardware.foo@1.0::IFoo/default")));
200 EXPECT_THAT(FqInstance::from("android.hardware.foo", 1, 0, "IFoo"),
201 Optional(Property(&FqInstance::string, "android.hardware.foo@1.0::IFoo")));
202 EXPECT_THAT(FqInstance::from("android.hardware.foo", 1, 0),
203 Optional(Property(&FqInstance::string, "android.hardware.foo@1.0")));
204 EXPECT_THAT(FqInstance::from(1, 0, "IFoo", "default"),
205 Optional(Property(&FqInstance::string, "@1.0::IFoo/default")));
206 EXPECT_THAT(FqInstance::from(1, 0, "IFoo"),
207 Optional(Property(&FqInstance::string, "@1.0::IFoo")));
208 EXPECT_THAT(FqInstance::from("IFoo", "default"),
209 Optional(Property(&FqInstance::string, "IFoo/default")));
210 }
211
main(int argc,char ** argv)212 int main(int argc, char **argv) {
213 ::testing::InitGoogleTest(&argc, argv);
214 return RUN_ALL_TESTS();
215 }
216