1 /*
2  * Copyright (C) 2020 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 // Unit Test for TranscodingClientManager
18 
19 // #define LOG_NDEBUG 0
20 #define LOG_TAG "TranscodingClientManagerTest"
21 
22 #include <aidl/android/media/BnTranscodingServiceClient.h>
23 #include <aidl/android/media/IMediaTranscodingService.h>
24 #include <aidl/android/media/ITranscodingServiceClient.h>
25 #include <android-base/logging.h>
26 #include <android/binder_manager.h>
27 #include <android/binder_process.h>
28 #include <gtest/gtest.h>
29 #include <media/TranscodingClientManager.h>
30 #include <utils/Log.h>
31 
32 namespace android {
33 
34 using Status = ::ndk::ScopedAStatus;
35 using aidl::android::media::BnTranscodingServiceClient;
36 using aidl::android::media::IMediaTranscodingService;
37 using aidl::android::media::ITranscodingServiceClient;
38 
39 constexpr int32_t kInvalidClientId = -1;
40 constexpr int32_t kInvalidClientPid = -1;
41 constexpr int32_t kInvalidClientUid = -1;
42 constexpr const char* kInvalidClientOpPackageName = "";
43 
44 constexpr int32_t kClientId = 1;
45 constexpr int32_t kClientPid = 2;
46 constexpr int32_t kClientUid = 3;
47 constexpr const char* kClientOpPackageName = "TestClient";
48 
49 struct TestClient : public BnTranscodingServiceClient {
TestClientandroid::TestClient50     TestClient(const std::shared_ptr<IMediaTranscodingService>& service) : mService(service) {
51         ALOGD("TestClient Created");
52     }
53 
getNameandroid::TestClient54     Status getName(std::string* _aidl_return) override {
55         *_aidl_return = "test_client";
56         return Status::ok();
57     }
58 
onTranscodingFinishedandroid::TestClient59     Status onTranscodingFinished(
60             int32_t /* in_jobId */,
61             const ::aidl::android::media::TranscodingResultParcel& /* in_result */) override {
62         return Status::ok();
63     }
64 
onTranscodingFailedandroid::TestClient65     Status onTranscodingFailed(
66             int32_t /* in_jobId */,
67             ::aidl::android::media::TranscodingErrorCode /*in_errorCode */) override {
68         return Status::ok();
69     }
70 
onAwaitNumberOfJobsChangedandroid::TestClient71     Status onAwaitNumberOfJobsChanged(int32_t /* in_jobId */, int32_t /* in_oldAwaitNumber */,
72                                       int32_t /* in_newAwaitNumber */) override {
73         return Status::ok();
74     }
75 
onProgressUpdateandroid::TestClient76     Status onProgressUpdate(int32_t /* in_jobId */, int32_t /* in_progress */) override {
77         return Status::ok();
78     }
79 
~TestClientandroid::TestClient80     virtual ~TestClient() { ALOGI("TestClient destroyed"); };
81 
82    private:
83     std::shared_ptr<IMediaTranscodingService> mService;
84     TestClient(const TestClient&) = delete;
85     TestClient& operator=(const TestClient&) = delete;
86 };
87 
88 class TranscodingClientManagerTest : public ::testing::Test {
89    public:
TranscodingClientManagerTest()90     TranscodingClientManagerTest() : mClientManager(TranscodingClientManager::getInstance()) {
91         ALOGD("TranscodingClientManagerTest created");
92     }
93 
SetUp()94     void SetUp() override {
95         ::ndk::SpAIBinder binder(AServiceManager_getService("media.transcoding"));
96         mService = IMediaTranscodingService::fromBinder(binder);
97         if (mService == nullptr) {
98             ALOGE("Failed to connect to the media.trascoding service.");
99             return;
100         }
101 
102         mTestClient = ::ndk::SharedRefBase::make<TestClient>(mService);
103     }
104 
TearDown()105     void TearDown() override {
106         ALOGI("TranscodingClientManagerTest tear down");
107         mService = nullptr;
108     }
109 
~TranscodingClientManagerTest()110     ~TranscodingClientManagerTest() { ALOGD("TranscodingClientManagerTest destroyed"); }
111 
112     TranscodingClientManager& mClientManager;
113     std::shared_ptr<ITranscodingServiceClient> mTestClient = nullptr;
114     std::shared_ptr<IMediaTranscodingService> mService = nullptr;
115 };
116 
TEST_F(TranscodingClientManagerTest,TestAddingWithInvalidClientId)117 TEST_F(TranscodingClientManagerTest, TestAddingWithInvalidClientId) {
118     std::shared_ptr<ITranscodingServiceClient> client =
119             ::ndk::SharedRefBase::make<TestClient>(mService);
120 
121     // Create a client with invalid client id.
122     std::unique_ptr<TranscodingClientManager::ClientInfo> clientInfo =
123             std::make_unique<TranscodingClientManager::ClientInfo>(
124                     client, kInvalidClientId, kClientPid, kClientUid, kClientOpPackageName);
125 
126     // Add the client to the manager and expect failure.
127     status_t err = mClientManager.addClient(std::move(clientInfo));
128     EXPECT_TRUE(err != OK);
129 }
130 
TEST_F(TranscodingClientManagerTest,TestAddingWithInvalidClientPid)131 TEST_F(TranscodingClientManagerTest, TestAddingWithInvalidClientPid) {
132     std::shared_ptr<ITranscodingServiceClient> client =
133             ::ndk::SharedRefBase::make<TestClient>(mService);
134 
135     // Create a client with invalid Pid.
136     std::unique_ptr<TranscodingClientManager::ClientInfo> clientInfo =
137             std::make_unique<TranscodingClientManager::ClientInfo>(
138                     client, kClientId, kInvalidClientPid, kClientUid, kClientOpPackageName);
139 
140     // Add the client to the manager and expect failure.
141     status_t err = mClientManager.addClient(std::move(clientInfo));
142     EXPECT_TRUE(err != OK);
143 }
144 
TEST_F(TranscodingClientManagerTest,TestAddingWithInvalidClientUid)145 TEST_F(TranscodingClientManagerTest, TestAddingWithInvalidClientUid) {
146     std::shared_ptr<ITranscodingServiceClient> client =
147             ::ndk::SharedRefBase::make<TestClient>(mService);
148 
149     // Create a client with invalid Uid.
150     std::unique_ptr<TranscodingClientManager::ClientInfo> clientInfo =
151             std::make_unique<TranscodingClientManager::ClientInfo>(
152                     client, kClientId, kClientPid, kInvalidClientUid, kClientOpPackageName);
153 
154     // Add the client to the manager and expect failure.
155     status_t err = mClientManager.addClient(std::move(clientInfo));
156     EXPECT_TRUE(err != OK);
157 }
158 
TEST_F(TranscodingClientManagerTest,TestAddingWithInvalidClientPackageName)159 TEST_F(TranscodingClientManagerTest, TestAddingWithInvalidClientPackageName) {
160     std::shared_ptr<ITranscodingServiceClient> client =
161             ::ndk::SharedRefBase::make<TestClient>(mService);
162 
163     // Create a client with invalid packagename.
164     std::unique_ptr<TranscodingClientManager::ClientInfo> clientInfo =
165             std::make_unique<TranscodingClientManager::ClientInfo>(
166                     client, kClientId, kClientPid, kClientUid, kInvalidClientOpPackageName);
167 
168     // Add the client to the manager and expect failure.
169     status_t err = mClientManager.addClient(std::move(clientInfo));
170     EXPECT_TRUE(err != OK);
171 }
172 
TEST_F(TranscodingClientManagerTest,TestAddingValidClient)173 TEST_F(TranscodingClientManagerTest, TestAddingValidClient) {
174     std::shared_ptr<ITranscodingServiceClient> client1 =
175             ::ndk::SharedRefBase::make<TestClient>(mService);
176 
177     std::unique_ptr<TranscodingClientManager::ClientInfo> clientInfo =
178             std::make_unique<TranscodingClientManager::ClientInfo>(
179                     client1, kClientId, kClientPid, kClientUid, kClientOpPackageName);
180 
181     status_t err = mClientManager.addClient(std::move(clientInfo));
182     EXPECT_TRUE(err == OK);
183 
184     size_t numOfClients = mClientManager.getNumOfClients();
185     EXPECT_EQ(numOfClients, 1);
186 
187     err = mClientManager.removeClient(kClientId);
188     EXPECT_TRUE(err == OK);
189 }
190 
TEST_F(TranscodingClientManagerTest,TestAddingDupliacteClient)191 TEST_F(TranscodingClientManagerTest, TestAddingDupliacteClient) {
192     std::shared_ptr<ITranscodingServiceClient> client1 =
193             ::ndk::SharedRefBase::make<TestClient>(mService);
194 
195     std::unique_ptr<TranscodingClientManager::ClientInfo> clientInfo =
196             std::make_unique<TranscodingClientManager::ClientInfo>(
197                     client1, kClientId, kClientPid, kClientUid, kClientOpPackageName);
198 
199     status_t err = mClientManager.addClient(std::move(clientInfo));
200     EXPECT_TRUE(err == OK);
201 
202     err = mClientManager.addClient(std::move(clientInfo));
203     EXPECT_TRUE(err != OK);
204 
205     err = mClientManager.removeClient(kClientId);
206     EXPECT_TRUE(err == OK);
207 }
208 
TEST_F(TranscodingClientManagerTest,TestAddingMultipleClient)209 TEST_F(TranscodingClientManagerTest, TestAddingMultipleClient) {
210     std::shared_ptr<ITranscodingServiceClient> client1 =
211             ::ndk::SharedRefBase::make<TestClient>(mService);
212 
213     std::unique_ptr<TranscodingClientManager::ClientInfo> clientInfo1 =
214             std::make_unique<TranscodingClientManager::ClientInfo>(
215                     client1, kClientId, kClientPid, kClientUid, kClientOpPackageName);
216 
217     status_t err = mClientManager.addClient(std::move(clientInfo1));
218     EXPECT_TRUE(err == OK);
219 
220     std::shared_ptr<ITranscodingServiceClient> client2 =
221             ::ndk::SharedRefBase::make<TestClient>(mService);
222 
223     std::unique_ptr<TranscodingClientManager::ClientInfo> clientInfo2 =
224             std::make_unique<TranscodingClientManager::ClientInfo>(
225                     client2, kClientId + 1, kClientPid, kClientUid, kClientOpPackageName);
226 
227     err = mClientManager.addClient(std::move(clientInfo2));
228     EXPECT_TRUE(err == OK);
229 
230     std::shared_ptr<ITranscodingServiceClient> client3 =
231             ::ndk::SharedRefBase::make<TestClient>(mService);
232 
233     // Create a client with invalid packagename.
234     std::unique_ptr<TranscodingClientManager::ClientInfo> clientInfo3 =
235             std::make_unique<TranscodingClientManager::ClientInfo>(
236                     client3, kClientId + 2, kClientPid, kClientUid, kClientOpPackageName);
237 
238     err = mClientManager.addClient(std::move(clientInfo3));
239     EXPECT_TRUE(err == OK);
240 
241     size_t numOfClients = mClientManager.getNumOfClients();
242     EXPECT_EQ(numOfClients, 3);
243 
244     err = mClientManager.removeClient(kClientId);
245     EXPECT_TRUE(err == OK);
246 
247     err = mClientManager.removeClient(kClientId + 1);
248     EXPECT_TRUE(err == OK);
249 
250     err = mClientManager.removeClient(kClientId + 2);
251     EXPECT_TRUE(err == OK);
252 }
253 
TEST_F(TranscodingClientManagerTest,TestRemovingNonExistClient)254 TEST_F(TranscodingClientManagerTest, TestRemovingNonExistClient) {
255     status_t err = mClientManager.removeClient(kInvalidClientId);
256     EXPECT_TRUE(err != OK);
257 
258     err = mClientManager.removeClient(1000 /* clientId */);
259     EXPECT_TRUE(err != OK);
260 }
261 
TEST_F(TranscodingClientManagerTest,TestCheckClientWithClientId)262 TEST_F(TranscodingClientManagerTest, TestCheckClientWithClientId) {
263     std::shared_ptr<ITranscodingServiceClient> client =
264             ::ndk::SharedRefBase::make<TestClient>(mService);
265 
266     std::unique_ptr<TranscodingClientManager::ClientInfo> clientInfo =
267             std::make_unique<TranscodingClientManager::ClientInfo>(
268                     client, kClientId, kClientPid, kClientUid, kClientOpPackageName);
269 
270     status_t err = mClientManager.addClient(std::move(clientInfo));
271     EXPECT_TRUE(err == OK);
272 
273     bool res = mClientManager.isClientIdRegistered(kClientId);
274     EXPECT_TRUE(res);
275 
276     res = mClientManager.isClientIdRegistered(kInvalidClientId);
277     EXPECT_FALSE(res);
278 }
279 
280 }  // namespace android