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 <gtest/gtest.h>
20 
21 #include <ConstantExpression.h>
22 #include <Coordinator.h>
23 #include <hidl-util/FQName.h>
24 
25 #define EXPECT_EQ_OK(expectResult, call, ...)        \
26     do {                                             \
27         std::string result;                          \
28         status_t err = (call)(__VA_ARGS__, &result); \
29         EXPECT_EQ(err, ::android::OK);               \
30         EXPECT_EQ(expectResult, result);             \
31     } while (false)
32 
33 namespace android {
34 
35 class HidlGenHostTest : public ::testing::Test {};
36 
TEST_F(HidlGenHostTest,CoordinatorTest)37 TEST_F(HidlGenHostTest, CoordinatorTest) {
38     Coordinator coordinator;
39 
40     std::string error;
41     EXPECT_EQ(OK, coordinator.addPackagePath("a.b", "a1/b1", &error));
42     EXPECT_TRUE(error.empty());
43     EXPECT_NE(OK, coordinator.addPackagePath("a.b", "a2/b2/", &error));
44     EXPECT_FALSE(error.empty());
45 
46     coordinator.addDefaultPackagePath("a.b", "a3/b3/"); // should take path above
47     coordinator.addDefaultPackagePath("a.c", "a4/b4/"); // should succeed
48 
49     EXPECT_EQ_OK("a.b", coordinator.getPackageRoot, FQName("a.b.foo@1.0"));
50     EXPECT_EQ_OK("a.c", coordinator.getPackageRoot, FQName("a.c.foo.bar@1.0::IFoo"));
51 
52     // getPackagePath(fqname, relative, sanitized, ...)
53     EXPECT_EQ_OK("a1/b1/foo/1.0/", coordinator.getPackagePath, FQName("a.b.foo@1.0"), false, false);
54     EXPECT_EQ_OK("a4/b4/foo/bar/1.0/", coordinator.getPackagePath, FQName("a.c.foo.bar@1.0::IFoo"),
55                  false, false);
56     EXPECT_EQ_OK("a1/b1/foo/V1_0/", coordinator.getPackagePath, FQName("a.b.foo@1.0"), false, true);
57     EXPECT_EQ_OK("a4/b4/foo/bar/V1_0/", coordinator.getPackagePath, FQName("a.c.foo.bar@1.0::IFoo"),
58                  false, true);
59     EXPECT_EQ_OK("foo/1.0/", coordinator.getPackagePath, FQName("a.b.foo@1.0"), true, false);
60     EXPECT_EQ_OK("foo/bar/1.0/", coordinator.getPackagePath, FQName("a.c.foo.bar@1.0::IFoo"), true,
61                  false);
62     EXPECT_EQ_OK("foo/V1_0/", coordinator.getPackagePath, FQName("a.b.foo@1.0"), true, true);
63     EXPECT_EQ_OK("foo/bar/V1_0/", coordinator.getPackagePath, FQName("a.c.foo.bar@1.0::IFoo"), true,
64                  true);
65 }
66 
TEST_F(HidlGenHostTest,CoordinatorFilepathTest)67 TEST_F(HidlGenHostTest, CoordinatorFilepathTest) {
68     using Location = Coordinator::Location;
69 
70     Coordinator coordinator;
71     coordinator.setOutputPath("foo/");
72     coordinator.setRootPath("bar/");
73 
74     std::string error;
75     EXPECT_EQ(OK, coordinator.addPackagePath("a.b", "a1/b1", &error));
76     EXPECT_TRUE(error.empty());
77 
78     const static FQName kName = FQName("a.b.c@1.2");
79 
80     // get file names
81     EXPECT_EQ_OK("foo/x.y", coordinator.getFilepath, kName, Location::DIRECT, "x.y");
82     EXPECT_EQ_OK("foo/a1/b1/c/1.2/x.y", coordinator.getFilepath, kName, Location::PACKAGE_ROOT,
83                  "x.y");
84     EXPECT_EQ_OK("foo/a/b/c/1.2/x.y", coordinator.getFilepath, kName, Location::GEN_OUTPUT, "x.y");
85     EXPECT_EQ_OK("foo/a/b/c/V1_2/x.y", coordinator.getFilepath, kName, Location::GEN_SANITIZED,
86                  "x.y");
87 
88     // get directories
89     EXPECT_EQ_OK("foo/", coordinator.getFilepath, kName, Location::DIRECT, "");
90     EXPECT_EQ_OK("foo/a1/b1/c/1.2/", coordinator.getFilepath, kName, Location::PACKAGE_ROOT, "");
91     EXPECT_EQ_OK("foo/a/b/c/1.2/", coordinator.getFilepath, kName, Location::GEN_OUTPUT, "");
92     EXPECT_EQ_OK("foo/a/b/c/V1_2/", coordinator.getFilepath, kName, Location::GEN_SANITIZED, "");
93 }
94 
TEST_F(HidlGenHostTest,LocationTest)95 TEST_F(HidlGenHostTest, LocationTest) {
96     Location a{{"file", 3, 4}, {"file", 3, 5}};
97     Location b{{"file", 3, 6}, {"file", 3, 7}};
98     Location c{{"file", 4, 4}, {"file", 4, 5}};
99 
100     Location other{{"other", 0, 0}, {"other", 0, 1}};
101 
102     EXPECT_LT(a, b);
103     EXPECT_LT(b, c);
104     EXPECT_LT(a, c);
105     EXPECT_FALSE(Location::inSameFile(a, other));
106 }
107 
main(int argc,char ** argv)108 int main(int argc, char **argv) {
109     ::testing::InitGoogleTest(&argc, argv);
110     return RUN_ALL_TESTS();
111 }
112 
113 }  // namespace android