blob: 5a791fe9eeec69b97750fce01cd8fba7d043d511 [file] [log] [blame]
hkuang9c04b8d2020-01-22 10:03:21 -08001/*
2 * Copyright (C) 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// Unit Test for MediaTranscoding Service.
18
19//#define LOG_NDEBUG 0
20#define LOG_TAG "MediaTranscodingServiceTest"
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-base/unique_fd.h>
27#include <android/binder_ibinder_jni.h>
28#include <android/binder_manager.h>
29#include <android/binder_process.h>
30#include <cutils/ashmem.h>
31#include <gtest/gtest.h>
32#include <stdlib.h>
33#include <sys/mman.h>
34#include <utils/Log.h>
35
36namespace android {
37
38namespace media {
39
40using Status = ::ndk::ScopedAStatus;
41using aidl::android::media::BnTranscodingServiceClient;
42using aidl::android::media::IMediaTranscodingService;
43using aidl::android::media::ITranscodingServiceClient;
44
45constexpr int32_t kInvalidClientId = -5;
46
47// Note that -1 is valid and means using calling pid/uid for the service. But only privilege caller could
48// use them. This test is not a privilege caller.
49constexpr int32_t kInvalidClientPid = -5;
50constexpr int32_t kInvalidClientUid = -5;
51constexpr const char* kInvalidClientOpPackageName = "";
52
53constexpr int32_t kClientUseCallingPid = -1;
54constexpr int32_t kClientUseCallingUid = -1;
55constexpr const char* kClientOpPackageName = "TestClient";
56
57class MediaTranscodingServiceTest : public ::testing::Test {
58public:
59 MediaTranscodingServiceTest() { ALOGD("MediaTranscodingServiceTest created"); }
60
61 void SetUp() override {
62 ::ndk::SpAIBinder binder(AServiceManager_getService("media.transcoding"));
63 mService = IMediaTranscodingService::fromBinder(binder);
64 if (mService == nullptr) {
65 ALOGE("Failed to connect to the media.trascoding service.");
66 return;
67 }
68 }
69
70 ~MediaTranscodingServiceTest() { ALOGD("MediaTranscodingingServiceTest destroyed"); }
71
72 std::shared_ptr<IMediaTranscodingService> mService = nullptr;
73};
74
75struct TestClient : public BnTranscodingServiceClient {
76 TestClient(const std::shared_ptr<IMediaTranscodingService>& service) : mService(service) {
77 ALOGD("TestClient Created");
78 }
79
80 Status getName(std::string* _aidl_return) override {
81 *_aidl_return = "test_client";
82 return Status::ok();
83 }
84
85 Status onTranscodingFinished(
86 int32_t /* in_jobId */,
87 const ::aidl::android::media::TranscodingResultParcel& /* in_result */) override {
88 return Status::ok();
89 }
90
91 Status onTranscodingFailed(
92 int32_t /* in_jobId */,
93 ::aidl::android::media::TranscodingErrorCode /*in_errorCode */) override {
94 return Status::ok();
95 }
96
97 Status onAwaitNumberOfJobsChanged(int32_t /* in_jobId */, int32_t /* in_oldAwaitNumber */,
98 int32_t /* in_newAwaitNumber */) override {
99 return Status::ok();
100 }
101
102 Status onProgressUpdate(int32_t /* in_jobId */, int32_t /* in_progress */) override {
103 return Status::ok();
104 }
105
106 virtual ~TestClient() { ALOGI("TestClient destroyed"); };
107
108private:
109 std::shared_ptr<IMediaTranscodingService> mService;
110};
111
112TEST_F(MediaTranscodingServiceTest, TestRegisterNullClient) {
113 std::shared_ptr<ITranscodingServiceClient> client = nullptr;
114 int32_t clientId = 0;
115 Status status = mService->registerClient(client, kClientOpPackageName, kClientUseCallingUid,
116 kClientUseCallingPid, &clientId);
117 EXPECT_FALSE(status.isOk());
118}
119
120TEST_F(MediaTranscodingServiceTest, TestRegisterClientWithInvalidClientPid) {
121 std::shared_ptr<ITranscodingServiceClient> client =
122 ::ndk::SharedRefBase::make<TestClient>(mService);
123 EXPECT_TRUE(client != nullptr);
124
125 // Register the client with the service.
126 int32_t clientId = 0;
127 Status status = mService->registerClient(client, kClientOpPackageName, kClientUseCallingUid,
128 kInvalidClientPid, &clientId);
129 EXPECT_FALSE(status.isOk());
130}
131
132TEST_F(MediaTranscodingServiceTest, TestRegisterClientWithInvalidClientUid) {
133 std::shared_ptr<ITranscodingServiceClient> client =
134 ::ndk::SharedRefBase::make<TestClient>(mService);
135 EXPECT_TRUE(client != nullptr);
136
137 // Register the client with the service.
138 int32_t clientId = 0;
139 Status status = mService->registerClient(client, kClientOpPackageName, kInvalidClientUid,
140 kClientUseCallingPid, &clientId);
141 EXPECT_FALSE(status.isOk());
142}
143
144TEST_F(MediaTranscodingServiceTest, TestRegisterClientWithInvalidClientPackageName) {
145 std::shared_ptr<ITranscodingServiceClient> client =
146 ::ndk::SharedRefBase::make<TestClient>(mService);
147 EXPECT_TRUE(client != nullptr);
148
149 // Register the client with the service.
150 int32_t clientId = 0;
151 Status status = mService->registerClient(client, kInvalidClientOpPackageName,
152 kClientUseCallingUid, kClientUseCallingPid, &clientId);
153 EXPECT_FALSE(status.isOk());
154}
155
156TEST_F(MediaTranscodingServiceTest, TestRegisterOneClient) {
157 std::shared_ptr<ITranscodingServiceClient> client =
158 ::ndk::SharedRefBase::make<TestClient>(mService);
159 EXPECT_TRUE(client != nullptr);
160
161 // Register the client with the service.
162 int32_t clientId = 0;
163 Status status = mService->registerClient(client, kClientOpPackageName, kClientUseCallingPid,
164 kClientUseCallingUid, &clientId);
165 ALOGD("client id is %d", clientId);
166 EXPECT_TRUE(status.isOk());
167
168 // Validate the clientId.
169 EXPECT_TRUE(clientId > 0);
170
171 // Check the number of Clients.
172 int32_t numOfClients;
173 status = mService->getNumOfClients(&numOfClients);
174 EXPECT_TRUE(status.isOk());
175 EXPECT_EQ(1, numOfClients);
176
177 // Unregister the client.
178 bool res;
179 status = mService->unregisterClient(clientId, &res);
180 EXPECT_TRUE(status.isOk());
181 EXPECT_TRUE(res);
182}
183
184TEST_F(MediaTranscodingServiceTest, TestUnRegisterClientWithInvalidClientId) {
185 std::shared_ptr<ITranscodingServiceClient> client =
186 ::ndk::SharedRefBase::make<TestClient>(mService);
187 EXPECT_TRUE(client != nullptr);
188
189 // Register the client with the service.
190 int32_t clientId = 0;
191 Status status = mService->registerClient(client, kClientOpPackageName, kClientUseCallingUid,
192 kClientUseCallingPid, &clientId);
193 ALOGD("client id is %d", clientId);
194 EXPECT_TRUE(status.isOk());
195
196 // Validate the clientId.
197 EXPECT_TRUE(clientId > 0);
198
199 // Check the number of Clients.
200 int32_t numOfClients;
201 status = mService->getNumOfClients(&numOfClients);
202 EXPECT_TRUE(status.isOk());
203 EXPECT_EQ(1, numOfClients);
204
205 // Unregister the client with invalid ID
206 bool res;
207 mService->unregisterClient(kInvalidClientId, &res);
208 EXPECT_FALSE(res);
209
210 // Unregister the valid client.
211 mService->unregisterClient(clientId, &res);
212}
213
214TEST_F(MediaTranscodingServiceTest, TestRegisterClientTwice) {
215 std::shared_ptr<ITranscodingServiceClient> client =
216 ::ndk::SharedRefBase::make<TestClient>(mService);
217 EXPECT_TRUE(client != nullptr);
218
219 // Register the client with the service.
220 int32_t clientId = 0;
221 Status status = mService->registerClient(client, kClientOpPackageName, kClientUseCallingUid,
222 kClientUseCallingPid, &clientId);
223 EXPECT_TRUE(status.isOk());
224
225 // Validate the clientId.
226 EXPECT_TRUE(clientId > 0);
227
228 // Register the client again and expects failure.
229 status = mService->registerClient(client, kClientOpPackageName, kClientUseCallingUid,
230 kClientUseCallingPid, &clientId);
231 EXPECT_FALSE(status.isOk());
232
233 // Unregister the valid client.
234 bool res;
235 mService->unregisterClient(clientId, &res);
236}
237
238} // namespace media
239} // namespace android