1 /*
2  * Copyright 2019 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 <aidl/android/automotive/computepipe/registry/BnClientInfo.h>
18 #include <binder/IInterface.h>
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
22 #include <list>
23 #include <memory>
24 #include <string>
25 #include <utility>
26 
27 #include "FakeRunner.h"
28 #include "PipeClient.h"
29 #include "PipeQuery.h"
30 #include "PipeRunner.h"
31 
32 using namespace android::automotive::computepipe::router;
33 using namespace android::automotive::computepipe::router::V1_0::implementation;
34 using namespace android::automotive::computepipe::tests;
35 using namespace aidl::android::automotive::computepipe::runner;
36 using namespace aidl::android::automotive::computepipe::registry;
37 using namespace ::testing;
38 
39 /**
40  * Fakeclass to instantiate client info for query purposes
41  */
42 class FakeClientInfo : public BnClientInfo {
43   public:
getClientName(std::string * name)44     ndk::ScopedAStatus getClientName(std::string* name) override {
45         *name = "FakeClient";
46         return ndk::ScopedAStatus::ok();
47     }
48 };
49 
50 /**
51  * Class that exposes protected interfaces of PipeRegistry
52  * a) Used to retrieve entries without client ref counts
53  * b) Used to remove entries
54  */
55 class FakeRegistry : public PipeRegistry<PipeRunner> {
56   public:
getDebuggerPipeHandle(const std::string & name)57     std ::unique_ptr<PipeHandle<PipeRunner>> getDebuggerPipeHandle(const std::string& name) {
58         return getPipeHandle(name, nullptr);
59     }
RemoveEntry(const std::string & name)60     Error RemoveEntry(const std::string& name) {
61         return DeletePipeHandle(name);
62     }
63 };
64 
65 /**
66  * Test Fixture class that is responsible for maintaining a registry.
67  * The registry object is used to test the query interfaces
68  */
69 class PipeQueryTest : public ::testing::Test {
70   protected:
71     /**
72      * Setup for the test fixture to initialize a registry to be used in all
73      * tests
74      */
SetUp()75     void SetUp() override {
76         mRegistry = std::make_shared<FakeRegistry>();
77     }
78     /**
79      * Utility to generate fake runners
80      */
addFakeRunner(const std::string & name,const std::shared_ptr<IPipeRunner> & runnerIface)81     void addFakeRunner(const std::string& name, const std::shared_ptr<IPipeRunner>& runnerIface) {
82         std::unique_ptr<PipeHandle<PipeRunner>> handle = std::make_unique<RunnerHandle>(runnerIface);
83         Error status = mRegistry->RegisterPipe(std::move(handle), name);
84         ASSERT_THAT(status, testing::Eq(Error::OK));
85     }
86     /**
87      * Utility to remove runners from the registry
88      */
removeRunner(const std::string & name)89     void removeRunner(const std::string& name) {
90         ASSERT_THAT(mRegistry->RemoveEntry(name), testing::Eq(Error::OK));
91     }
92     /**
93      * Tear down to cleanup registry resources
94      */
TearDown()95     void TearDown() override {
96         mRegistry = nullptr;
97     }
98     std::shared_ptr<FakeRegistry> mRegistry;
99 };
100 
101 // Check retrieval of inserted entries
TEST_F(PipeQueryTest,GetGraphListTest)102 TEST_F(PipeQueryTest, GetGraphListTest) {
103     std::shared_ptr<IPipeRunner> stub1 = ndk::SharedRefBase::make<FakeRunner>();
104     addFakeRunner("stub1", stub1);
105     std::shared_ptr<IPipeRunner> stub2 = ndk::SharedRefBase::make<FakeRunner>();
106     addFakeRunner("stub2", stub2);
107 
108     std::vector<std::string>* outNames = new std::vector<std::string>();
109     std::shared_ptr<PipeQuery> qIface = ndk::SharedRefBase::make<PipeQuery>(mRegistry);
110     ASSERT_TRUE(qIface->getGraphList(outNames).isOk());
111 
112     ASSERT_NE(outNames->size(), 0);
113     EXPECT_THAT(std::find(outNames->begin(), outNames->end(), "stub1"),
114                 testing::Ne(outNames->end()));
115     EXPECT_THAT(std::find(outNames->begin(), outNames->end(), "stub2"),
116                 testing::Ne(outNames->end()));
117 }
118 
119 // Check successful retrieval of runner
TEST_F(PipeQueryTest,GetRunnerTest)120 TEST_F(PipeQueryTest, GetRunnerTest) {
121     std::shared_ptr<IPipeRunner> stub1 = ndk::SharedRefBase::make<FakeRunner>();
122     addFakeRunner("stub1", stub1);
123 
124     std::shared_ptr<PipeQuery> qIface = ndk::SharedRefBase::make<PipeQuery>(mRegistry);
125     std::shared_ptr<IClientInfo> info = ndk::SharedRefBase::make<FakeClientInfo>();
126     std::shared_ptr<IPipeRunner> runner;
127     ASSERT_TRUE(qIface->getPipeRunner("stub1", info, &runner).isOk());
128     EXPECT_THAT(runner, testing::NotNull());
129 }
130