blob: accfd033997045771acf082379d47681d68349a7 [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
Chong Zhang8e062632020-03-31 10:56:37 -070022#include <aidl/android/media/BnTranscodingClientListener.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>
25#include <aidl/android/media/ITranscodingClientListener.h>
hkuang9c04b8d2020-01-22 10:03:21 -080026#include <android-base/logging.h>
27#include <android-base/unique_fd.h>
28#include <android/binder_ibinder_jni.h>
29#include <android/binder_manager.h>
30#include <android/binder_process.h>
31#include <cutils/ashmem.h>
32#include <gtest/gtest.h>
33#include <stdlib.h>
34#include <sys/mman.h>
35#include <utils/Log.h>
36
37namespace android {
38
39namespace media {
40
41using Status = ::ndk::ScopedAStatus;
Chong Zhang8e062632020-03-31 10:56:37 -070042using aidl::android::media::BnTranscodingClientListener;
43using aidl::android::media::ITranscodingClient;
44using aidl::android::media::ITranscodingClientListener;
hkuang9c04b8d2020-01-22 10:03:21 -080045using aidl::android::media::IMediaTranscodingService;
hkuang9c04b8d2020-01-22 10:03:21 -080046
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;
Chong Zhang8e062632020-03-31 10:56:37 -070051constexpr const char* kInvalidClientName = "";
hkuang9c04b8d2020-01-22 10:03:21 -080052constexpr const char* kInvalidClientOpPackageName = "";
53
54constexpr int32_t kClientUseCallingPid = -1;
55constexpr int32_t kClientUseCallingUid = -1;
Chong Zhang8e062632020-03-31 10:56:37 -070056constexpr const char* kClientName = "TestClient";
57constexpr const char* kClientOpPackageName = "TestClientPackage";
hkuang9c04b8d2020-01-22 10:03:21 -080058
Chong Zhang8e062632020-03-31 10:56:37 -070059struct TestClient : public BnTranscodingClientListener {
60 TestClient() {
hkuang9c04b8d2020-01-22 10:03:21 -080061 ALOGD("TestClient Created");
62 }
63
Chong Zhang8e062632020-03-31 10:56:37 -070064 virtual ~TestClient() {
65 ALOGI("TestClient destroyed");
hkuang9c04b8d2020-01-22 10:03:21 -080066 }
67
68 Status onTranscodingFinished(
69 int32_t /* in_jobId */,
70 const ::aidl::android::media::TranscodingResultParcel& /* in_result */) override {
71 return Status::ok();
72 }
73
74 Status onTranscodingFailed(
75 int32_t /* in_jobId */,
76 ::aidl::android::media::TranscodingErrorCode /*in_errorCode */) override {
77 return Status::ok();
78 }
79
80 Status onAwaitNumberOfJobsChanged(int32_t /* in_jobId */, int32_t /* in_oldAwaitNumber */,
81 int32_t /* in_newAwaitNumber */) override {
82 return Status::ok();
83 }
84
85 Status onProgressUpdate(int32_t /* in_jobId */, int32_t /* in_progress */) override {
86 return Status::ok();
87 }
hkuang9c04b8d2020-01-22 10:03:21 -080088};
89
Chong Zhang8e062632020-03-31 10:56:37 -070090class MediaTranscodingServiceTest : public ::testing::Test {
91public:
92 MediaTranscodingServiceTest() {
93 ALOGD("MediaTranscodingServiceTest created");
94 }
95
96 ~MediaTranscodingServiceTest() {
97 ALOGD("MediaTranscodingingServiceTest destroyed");
98 }
99
100 void SetUp() override {
101 ::ndk::SpAIBinder binder(AServiceManager_getService("media.transcoding"));
102 mService = IMediaTranscodingService::fromBinder(binder);
103 if (mService == nullptr) {
104 ALOGE("Failed to connect to the media.trascoding service.");
105 return;
106 }
107 mClientListener = ::ndk::SharedRefBase::make<TestClient>();
108 mClientListener2 = ::ndk::SharedRefBase::make<TestClient>();
109 mClientListener3 = ::ndk::SharedRefBase::make<TestClient>();
110 }
111
112 std::shared_ptr<IMediaTranscodingService> mService;
113 std::shared_ptr<ITranscodingClientListener> mClientListener;
114 std::shared_ptr<ITranscodingClientListener> mClientListener2;
115 std::shared_ptr<ITranscodingClientListener> mClientListener3;
116};
117
118
hkuang9c04b8d2020-01-22 10:03:21 -0800119TEST_F(MediaTranscodingServiceTest, TestRegisterNullClient) {
Chong Zhang8e062632020-03-31 10:56:37 -0700120 std::shared_ptr<ITranscodingClient> client;
121
122 // Register the client with null listener
123 Status status = mService->registerClient(
124 nullptr, kClientName, kClientOpPackageName,
125 kClientUseCallingUid, kClientUseCallingPid, &client);
hkuang9c04b8d2020-01-22 10:03:21 -0800126 EXPECT_FALSE(status.isOk());
127}
128
129TEST_F(MediaTranscodingServiceTest, TestRegisterClientWithInvalidClientPid) {
Chong Zhang8e062632020-03-31 10:56:37 -0700130 std::shared_ptr<ITranscodingClient> client;
hkuang9c04b8d2020-01-22 10:03:21 -0800131
132 // Register the client with the service.
Chong Zhang8e062632020-03-31 10:56:37 -0700133 Status status = mService->registerClient(
134 mClientListener, kClientName, kClientOpPackageName,
135 kClientUseCallingUid, kInvalidClientPid, &client);
hkuang9c04b8d2020-01-22 10:03:21 -0800136 EXPECT_FALSE(status.isOk());
137}
138
139TEST_F(MediaTranscodingServiceTest, TestRegisterClientWithInvalidClientUid) {
Chong Zhang8e062632020-03-31 10:56:37 -0700140 std::shared_ptr<ITranscodingClient> client;
hkuang9c04b8d2020-01-22 10:03:21 -0800141
142 // Register the client with the service.
Chong Zhang8e062632020-03-31 10:56:37 -0700143 Status status = mService->registerClient(
144 mClientListener, kClientName, kClientOpPackageName,
145 kInvalidClientUid, kClientUseCallingPid, &client);
146 EXPECT_FALSE(status.isOk());
147}
148
149TEST_F(MediaTranscodingServiceTest, TestRegisterClientWithInvalidClientName) {
150 std::shared_ptr<ITranscodingClient> client;
151
152 // Register the client with the service.
153 Status status = mService->registerClient(
154 mClientListener, kInvalidClientName, kInvalidClientOpPackageName,
155 kClientUseCallingUid, kClientUseCallingPid, &client);
hkuang9c04b8d2020-01-22 10:03:21 -0800156 EXPECT_FALSE(status.isOk());
157}
158
159TEST_F(MediaTranscodingServiceTest, TestRegisterClientWithInvalidClientPackageName) {
Chong Zhang8e062632020-03-31 10:56:37 -0700160 std::shared_ptr<ITranscodingClient> client;
hkuang9c04b8d2020-01-22 10:03:21 -0800161
162 // Register the client with the service.
Chong Zhang8e062632020-03-31 10:56:37 -0700163 Status status = mService->registerClient(
164 mClientListener, kClientName, kInvalidClientOpPackageName,
165 kClientUseCallingUid, kClientUseCallingPid, &client);
hkuang9c04b8d2020-01-22 10:03:21 -0800166 EXPECT_FALSE(status.isOk());
167}
168
169TEST_F(MediaTranscodingServiceTest, TestRegisterOneClient) {
Chong Zhang8e062632020-03-31 10:56:37 -0700170 std::shared_ptr<ITranscodingClient> client;
hkuang9c04b8d2020-01-22 10:03:21 -0800171
Chong Zhang8e062632020-03-31 10:56:37 -0700172 Status status = mService->registerClient(
173 mClientListener, kClientName, kClientOpPackageName,
174 kClientUseCallingUid, kClientUseCallingPid, &client);
hkuang9c04b8d2020-01-22 10:03:21 -0800175 EXPECT_TRUE(status.isOk());
176
Chong Zhang8e062632020-03-31 10:56:37 -0700177 // Validate the client.
178 EXPECT_TRUE(client != nullptr);
hkuang9c04b8d2020-01-22 10:03:21 -0800179
180 // Check the number of Clients.
181 int32_t numOfClients;
182 status = mService->getNumOfClients(&numOfClients);
183 EXPECT_TRUE(status.isOk());
184 EXPECT_EQ(1, numOfClients);
185
186 // Unregister the client.
Chong Zhang8e062632020-03-31 10:56:37 -0700187 status = client->unregister();
hkuang9c04b8d2020-01-22 10:03:21 -0800188 EXPECT_TRUE(status.isOk());
hkuang9c04b8d2020-01-22 10:03:21 -0800189
190 // Check the number of Clients.
hkuang9c04b8d2020-01-22 10:03:21 -0800191 status = mService->getNumOfClients(&numOfClients);
192 EXPECT_TRUE(status.isOk());
Chong Zhang8e062632020-03-31 10:56:37 -0700193 EXPECT_EQ(0, numOfClients);
hkuang9c04b8d2020-01-22 10:03:21 -0800194}
195
196TEST_F(MediaTranscodingServiceTest, TestRegisterClientTwice) {
Chong Zhang8e062632020-03-31 10:56:37 -0700197 std::shared_ptr<ITranscodingClient> client;
hkuang9c04b8d2020-01-22 10:03:21 -0800198
Chong Zhang8e062632020-03-31 10:56:37 -0700199 Status status = mService->registerClient(
200 mClientListener, 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 // Register the client again and expects failure.
Chong Zhang8e062632020-03-31 10:56:37 -0700208 std::shared_ptr<ITranscodingClient> client1;
209 status = mService->registerClient(
210 mClientListener, kClientName, kClientOpPackageName,
211 kClientUseCallingUid, kClientUseCallingPid, &client1);
hkuang9c04b8d2020-01-22 10:03:21 -0800212 EXPECT_FALSE(status.isOk());
213
Chong Zhang8e062632020-03-31 10:56:37 -0700214 // Unregister the client.
215 status = client->unregister();
216 EXPECT_TRUE(status.isOk());
hkuang9c04b8d2020-01-22 10:03:21 -0800217}
218
Chong Zhang8e062632020-03-31 10:56:37 -0700219TEST_F(MediaTranscodingServiceTest, TestRegisterMultipleClients) {
220 std::shared_ptr<ITranscodingClient> client1;
221 std::shared_ptr<ITranscodingClient> client2;
222 std::shared_ptr<ITranscodingClient> client3;
223
224 // Register 3 clients.
225 Status status = mService->registerClient(
226 mClientListener, kClientName, kClientOpPackageName,
227 kClientUseCallingUid, kClientUseCallingPid, &client1);
228 EXPECT_TRUE(status.isOk());
229 EXPECT_TRUE(client1 != nullptr);
230
231 status = mService->registerClient(
232 mClientListener2, kClientName, kClientOpPackageName,
233 kClientUseCallingUid, kClientUseCallingPid, &client2);
234 EXPECT_TRUE(status.isOk());
235 EXPECT_TRUE(client2 != nullptr);
236
237 status = mService->registerClient(
238 mClientListener3, kClientName, kClientOpPackageName,
239 kClientUseCallingUid, kClientUseCallingPid, &client3);
240 EXPECT_TRUE(status.isOk());
241 EXPECT_TRUE(client3 != nullptr);
242
243 // Check the number of clients.
244 int32_t numOfClients;
245 status = mService->getNumOfClients(&numOfClients);
246 EXPECT_TRUE(status.isOk());
247 EXPECT_EQ(3, numOfClients);
248
249 // Unregister the clients.
250 status = client1->unregister();
251 EXPECT_TRUE(status.isOk());
252
253 status = client2->unregister();
254 EXPECT_TRUE(status.isOk());
255
256 status = client3->unregister();
257 EXPECT_TRUE(status.isOk());
258
259 // Check the number of clients.
260 status = mService->getNumOfClients(&numOfClients);
261 EXPECT_TRUE(status.isOk());
262 EXPECT_EQ(0, numOfClients);
263}
hkuang9c04b8d2020-01-22 10:03:21 -0800264} // namespace media
265} // namespace android