blob: 351f830e5569a8894a60ac04d0d24da15bb551ed [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
Chong Zhang182b06a2020-04-09 14:38:05 -070017// Unit Test for MediaTranscodingService.
hkuang9c04b8d2020-01-22 10:03:21 -080018
19//#define LOG_NDEBUG 0
20#define LOG_TAG "MediaTranscodingServiceTest"
21
Chong Zhang182b06a2020-04-09 14:38:05 -070022#include <aidl/android/media/BnTranscodingClientCallback.h>
hkuang9c04b8d2020-01-22 10:03:21 -080023#include <aidl/android/media/IMediaTranscodingService.h>
Chong Zhang8e062632020-03-31 10:56:37 -070024#include <aidl/android/media/ITranscodingClient.h>
Chong Zhang182b06a2020-04-09 14:38:05 -070025#include <aidl/android/media/ITranscodingClientCallback.h>
26#include <aidl/android/media/TranscodingJobParcel.h>
27#include <aidl/android/media/TranscodingRequestParcel.h>
hkuang9c04b8d2020-01-22 10:03:21 -080028#include <android-base/logging.h>
29#include <android-base/unique_fd.h>
30#include <android/binder_ibinder_jni.h>
31#include <android/binder_manager.h>
32#include <android/binder_process.h>
33#include <cutils/ashmem.h>
34#include <gtest/gtest.h>
35#include <stdlib.h>
36#include <sys/mman.h>
37#include <utils/Log.h>
38
39namespace android {
40
41namespace media {
42
43using Status = ::ndk::ScopedAStatus;
Chong Zhang182b06a2020-04-09 14:38:05 -070044using aidl::android::media::BnTranscodingClientCallback;
hkuang9c04b8d2020-01-22 10:03:21 -080045using aidl::android::media::IMediaTranscodingService;
Chong Zhang182b06a2020-04-09 14:38:05 -070046using aidl::android::media::ITranscodingClient;
47using aidl::android::media::ITranscodingClientCallback;
48using aidl::android::media::TranscodingJobParcel;
49using aidl::android::media::TranscodingRequestParcel;
hkuang9c04b8d2020-01-22 10:03:21 -080050
51// Note that -1 is valid and means using calling pid/uid for the service. But only privilege caller could
52// use them. This test is not a privilege caller.
53constexpr int32_t kInvalidClientPid = -5;
Chong Zhang8e062632020-03-31 10:56:37 -070054constexpr const char* kInvalidClientName = "";
hkuang9c04b8d2020-01-22 10:03:21 -080055constexpr const char* kInvalidClientOpPackageName = "";
56
Chong Zhang182b06a2020-04-09 14:38:05 -070057constexpr int32_t kClientUseCallingPid = IMediaTranscodingService::USE_CALLING_PID;
58constexpr int32_t kClientUseCallingUid = IMediaTranscodingService::USE_CALLING_UID;
Chong Zhang8e062632020-03-31 10:56:37 -070059constexpr const char* kClientName = "TestClient";
60constexpr const char* kClientOpPackageName = "TestClientPackage";
hkuang9c04b8d2020-01-22 10:03:21 -080061
Chong Zhang182b06a2020-04-09 14:38:05 -070062struct TestClient : public BnTranscodingClientCallback {
63 TestClient() { ALOGD("TestClient Created"); }
hkuang9c04b8d2020-01-22 10:03:21 -080064
Chong Zhang182b06a2020-04-09 14:38:05 -070065 virtual ~TestClient() { ALOGI("TestClient destroyed"); }
hkuang9c04b8d2020-01-22 10:03:21 -080066
67 Status onTranscodingFinished(
68 int32_t /* in_jobId */,
69 const ::aidl::android::media::TranscodingResultParcel& /* in_result */) override {
70 return Status::ok();
71 }
72
73 Status onTranscodingFailed(
74 int32_t /* in_jobId */,
75 ::aidl::android::media::TranscodingErrorCode /*in_errorCode */) override {
76 return Status::ok();
77 }
78
79 Status onAwaitNumberOfJobsChanged(int32_t /* in_jobId */, int32_t /* in_oldAwaitNumber */,
80 int32_t /* in_newAwaitNumber */) override {
81 return Status::ok();
82 }
83
84 Status onProgressUpdate(int32_t /* in_jobId */, int32_t /* in_progress */) override {
85 return Status::ok();
86 }
hkuang9c04b8d2020-01-22 10:03:21 -080087};
88
Chong Zhang8e062632020-03-31 10:56:37 -070089class MediaTranscodingServiceTest : public ::testing::Test {
90public:
Chong Zhang182b06a2020-04-09 14:38:05 -070091 MediaTranscodingServiceTest() { ALOGD("MediaTranscodingServiceTest created"); }
Chong Zhang8e062632020-03-31 10:56:37 -070092
Chong Zhang182b06a2020-04-09 14:38:05 -070093 ~MediaTranscodingServiceTest() { ALOGD("MediaTranscodingingServiceTest destroyed"); }
Chong Zhang8e062632020-03-31 10:56:37 -070094
95 void SetUp() override {
96 ::ndk::SpAIBinder binder(AServiceManager_getService("media.transcoding"));
97 mService = IMediaTranscodingService::fromBinder(binder);
98 if (mService == nullptr) {
99 ALOGE("Failed to connect to the media.trascoding service.");
100 return;
101 }
Chong Zhang182b06a2020-04-09 14:38:05 -0700102 mClientCallback = ::ndk::SharedRefBase::make<TestClient>();
103 mClientCallback2 = ::ndk::SharedRefBase::make<TestClient>();
104 mClientCallback3 = ::ndk::SharedRefBase::make<TestClient>();
105 }
106
107 void registerMultipleClients() {
108 // Register 3 clients.
109 Status status =
110 mService->registerClient(mClientCallback, kClientName, kClientOpPackageName,
111 kClientUseCallingUid, kClientUseCallingPid, &mClient1);
112 EXPECT_TRUE(status.isOk());
113 EXPECT_TRUE(mClient1 != nullptr);
114
115 status = mService->registerClient(mClientCallback2, kClientName, kClientOpPackageName,
116 kClientUseCallingUid, kClientUseCallingPid, &mClient2);
117 EXPECT_TRUE(status.isOk());
118 EXPECT_TRUE(mClient2 != nullptr);
119
120 status = mService->registerClient(mClientCallback3, kClientName, kClientOpPackageName,
121 kClientUseCallingUid, kClientUseCallingPid, &mClient3);
122 EXPECT_TRUE(status.isOk());
123 EXPECT_TRUE(mClient3 != nullptr);
124
125 // Check the number of clients.
126 int32_t numOfClients;
127 status = mService->getNumOfClients(&numOfClients);
128 EXPECT_TRUE(status.isOk());
129 EXPECT_EQ(3, numOfClients);
130 }
131
132 void unregisterMultipleClients() {
133 // Unregister the clients.
134 Status status = mClient1->unregister();
135 EXPECT_TRUE(status.isOk());
136
137 status = mClient2->unregister();
138 EXPECT_TRUE(status.isOk());
139
140 status = mClient3->unregister();
141 EXPECT_TRUE(status.isOk());
142
143 // Check the number of clients.
144 int32_t numOfClients;
145 status = mService->getNumOfClients(&numOfClients);
146 EXPECT_TRUE(status.isOk());
147 EXPECT_EQ(0, numOfClients);
Chong Zhang8e062632020-03-31 10:56:37 -0700148 }
149
150 std::shared_ptr<IMediaTranscodingService> mService;
Chong Zhang182b06a2020-04-09 14:38:05 -0700151 std::shared_ptr<ITranscodingClientCallback> mClientCallback;
152 std::shared_ptr<ITranscodingClientCallback> mClientCallback2;
153 std::shared_ptr<ITranscodingClientCallback> mClientCallback3;
154 std::shared_ptr<ITranscodingClient> mClient1;
155 std::shared_ptr<ITranscodingClient> mClient2;
156 std::shared_ptr<ITranscodingClient> mClient3;
Chong Zhang8e062632020-03-31 10:56:37 -0700157};
158
hkuang9c04b8d2020-01-22 10:03:21 -0800159TEST_F(MediaTranscodingServiceTest, TestRegisterNullClient) {
Chong Zhang8e062632020-03-31 10:56:37 -0700160 std::shared_ptr<ITranscodingClient> client;
161
Chong Zhang182b06a2020-04-09 14:38:05 -0700162 // Register the client with null callback.
163 Status status = mService->registerClient(nullptr, kClientName, kClientOpPackageName,
164 kClientUseCallingUid, kClientUseCallingPid, &client);
hkuang9c04b8d2020-01-22 10:03:21 -0800165 EXPECT_FALSE(status.isOk());
166}
167
168TEST_F(MediaTranscodingServiceTest, TestRegisterClientWithInvalidClientPid) {
Chong Zhang8e062632020-03-31 10:56:37 -0700169 std::shared_ptr<ITranscodingClient> client;
hkuang9c04b8d2020-01-22 10:03:21 -0800170
171 // Register the client with the service.
Chong Zhang182b06a2020-04-09 14:38:05 -0700172 Status status = mService->registerClient(mClientCallback, kClientName, kClientOpPackageName,
173 kClientUseCallingUid, kInvalidClientPid, &client);
Chong Zhang8e062632020-03-31 10:56:37 -0700174 EXPECT_FALSE(status.isOk());
175}
176
177TEST_F(MediaTranscodingServiceTest, TestRegisterClientWithInvalidClientName) {
178 std::shared_ptr<ITranscodingClient> client;
179
180 // Register the client with the service.
Chong Zhang182b06a2020-04-09 14:38:05 -0700181 Status status = mService->registerClient(mClientCallback, kInvalidClientName,
182 kInvalidClientOpPackageName, kClientUseCallingUid,
183 kClientUseCallingPid, &client);
hkuang9c04b8d2020-01-22 10:03:21 -0800184 EXPECT_FALSE(status.isOk());
185}
186
187TEST_F(MediaTranscodingServiceTest, TestRegisterClientWithInvalidClientPackageName) {
Chong Zhang8e062632020-03-31 10:56:37 -0700188 std::shared_ptr<ITranscodingClient> client;
hkuang9c04b8d2020-01-22 10:03:21 -0800189
190 // Register the client with the service.
Chong Zhang182b06a2020-04-09 14:38:05 -0700191 Status status =
192 mService->registerClient(mClientCallback, kClientName, kInvalidClientOpPackageName,
193 kClientUseCallingUid, kClientUseCallingPid, &client);
hkuang9c04b8d2020-01-22 10:03:21 -0800194 EXPECT_FALSE(status.isOk());
195}
196
197TEST_F(MediaTranscodingServiceTest, TestRegisterOneClient) {
Chong Zhang8e062632020-03-31 10:56:37 -0700198 std::shared_ptr<ITranscodingClient> client;
hkuang9c04b8d2020-01-22 10:03:21 -0800199
Chong Zhang182b06a2020-04-09 14:38:05 -0700200 Status status = mService->registerClient(mClientCallback, kClientName, kClientOpPackageName,
201 kClientUseCallingUid, kClientUseCallingPid, &client);
hkuang9c04b8d2020-01-22 10:03:21 -0800202 EXPECT_TRUE(status.isOk());
203
Chong Zhang8e062632020-03-31 10:56:37 -0700204 // Validate the client.
205 EXPECT_TRUE(client != nullptr);
hkuang9c04b8d2020-01-22 10:03:21 -0800206
207 // Check the number of Clients.
208 int32_t numOfClients;
209 status = mService->getNumOfClients(&numOfClients);
210 EXPECT_TRUE(status.isOk());
211 EXPECT_EQ(1, numOfClients);
212
213 // Unregister the client.
Chong Zhang8e062632020-03-31 10:56:37 -0700214 status = client->unregister();
hkuang9c04b8d2020-01-22 10:03:21 -0800215 EXPECT_TRUE(status.isOk());
hkuang9c04b8d2020-01-22 10:03:21 -0800216
217 // Check the number of Clients.
hkuang9c04b8d2020-01-22 10:03:21 -0800218 status = mService->getNumOfClients(&numOfClients);
219 EXPECT_TRUE(status.isOk());
Chong Zhang8e062632020-03-31 10:56:37 -0700220 EXPECT_EQ(0, numOfClients);
hkuang9c04b8d2020-01-22 10:03:21 -0800221}
222
223TEST_F(MediaTranscodingServiceTest, TestRegisterClientTwice) {
Chong Zhang8e062632020-03-31 10:56:37 -0700224 std::shared_ptr<ITranscodingClient> client;
hkuang9c04b8d2020-01-22 10:03:21 -0800225
Chong Zhang182b06a2020-04-09 14:38:05 -0700226 Status status = mService->registerClient(mClientCallback, kClientName, kClientOpPackageName,
227 kClientUseCallingUid, kClientUseCallingPid, &client);
hkuang9c04b8d2020-01-22 10:03:21 -0800228 EXPECT_TRUE(status.isOk());
229
Chong Zhang8e062632020-03-31 10:56:37 -0700230 // Validate the client.
231 EXPECT_TRUE(client != nullptr);
hkuang9c04b8d2020-01-22 10:03:21 -0800232
233 // Register the client again and expects failure.
Chong Zhang8e062632020-03-31 10:56:37 -0700234 std::shared_ptr<ITranscodingClient> client1;
Chong Zhang182b06a2020-04-09 14:38:05 -0700235 status = mService->registerClient(mClientCallback, kClientName, kClientOpPackageName,
236 kClientUseCallingUid, kClientUseCallingPid, &client1);
hkuang9c04b8d2020-01-22 10:03:21 -0800237 EXPECT_FALSE(status.isOk());
238
Chong Zhang8e062632020-03-31 10:56:37 -0700239 // Unregister the client.
240 status = client->unregister();
241 EXPECT_TRUE(status.isOk());
hkuang9c04b8d2020-01-22 10:03:21 -0800242}
243
Chong Zhang8e062632020-03-31 10:56:37 -0700244TEST_F(MediaTranscodingServiceTest, TestRegisterMultipleClients) {
Chong Zhang182b06a2020-04-09 14:38:05 -0700245 registerMultipleClients();
246 unregisterMultipleClients();
Chong Zhang8e062632020-03-31 10:56:37 -0700247}
Chong Zhang182b06a2020-04-09 14:38:05 -0700248
249TEST_F(MediaTranscodingServiceTest, TestSubmitCancelGetJobs) {
250 registerMultipleClients();
251
252 // Test jobId assignment.
253 TranscodingRequestParcel request;
254 request.fileName = "test_file_0";
255 TranscodingJobParcel job;
256 bool result;
257 EXPECT_TRUE(mClient1->submitRequest(request, &job, &result).isOk());
258 EXPECT_TRUE(result);
259 EXPECT_EQ(job.jobId, 0);
260
261 request.fileName = "test_file_1";
262 EXPECT_TRUE(mClient1->submitRequest(request, &job, &result).isOk());
263 EXPECT_TRUE(result);
264 EXPECT_EQ(job.jobId, 1);
265
266 request.fileName = "test_file_2";
267 EXPECT_TRUE(mClient1->submitRequest(request, &job, &result).isOk());
268 EXPECT_TRUE(result);
269 EXPECT_EQ(job.jobId, 2);
270
271 // Test submit bad request (no valid fileName) fails.
272 TranscodingRequestParcel badRequest;
273 EXPECT_TRUE(mClient1->submitRequest(badRequest, &job, &result).isOk());
274 EXPECT_FALSE(result);
275
276 // Test get jobs by id.
277 EXPECT_TRUE(mClient1->getJobWithId(2, &job, &result).isOk());
278 EXPECT_EQ(job.jobId, 2);
279 EXPECT_EQ(job.request.fileName, "test_file_2");
280 EXPECT_TRUE(result);
281
282 // Test get jobs by invalid id fails.
283 EXPECT_TRUE(mClient1->getJobWithId(100, &job, &result).isOk());
284 EXPECT_FALSE(result);
285
286 // Test cancel non-existent job fail.
287 EXPECT_TRUE(mClient2->cancelJob(100, &result).isOk());
288 EXPECT_FALSE(result);
289
290 // Test cancel valid jobId in arbitrary order.
291 EXPECT_TRUE(mClient1->cancelJob(2, &result).isOk());
292 EXPECT_TRUE(result);
293
294 EXPECT_TRUE(mClient1->cancelJob(0, &result).isOk());
295 EXPECT_TRUE(result);
296
297 EXPECT_TRUE(mClient1->cancelJob(1, &result).isOk());
298 EXPECT_TRUE(result);
299
300 // Test cancel job again fails.
301 EXPECT_TRUE(mClient1->cancelJob(1, &result).isOk());
302 EXPECT_FALSE(result);
303
304 // Test get job after cancel fails.
305 EXPECT_TRUE(mClient1->getJobWithId(2, &job, &result).isOk());
306 EXPECT_FALSE(result);
307
308 // Test jobId independence for each client.
309 EXPECT_TRUE(mClient2->submitRequest(request, &job, &result).isOk());
310 EXPECT_TRUE(result);
311 EXPECT_EQ(job.jobId, 0);
312
313 EXPECT_TRUE(mClient2->submitRequest(request, &job, &result).isOk());
314 EXPECT_TRUE(result);
315 EXPECT_EQ(job.jobId, 1);
316
317 unregisterMultipleClients();
318}
319
hkuang9c04b8d2020-01-22 10:03:21 -0800320} // namespace media
321} // namespace android