1 /*
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 
36 namespace android {
37 
38 namespace media {
39 
40 using Status = ::ndk::ScopedAStatus;
41 using aidl::android::media::BnTranscodingServiceClient;
42 using aidl::android::media::IMediaTranscodingService;
43 using aidl::android::media::ITranscodingServiceClient;
44 
45 constexpr 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.
49 constexpr int32_t kInvalidClientPid = -5;
50 constexpr int32_t kInvalidClientUid = -5;
51 constexpr const char* kInvalidClientOpPackageName = "";
52 
53 constexpr int32_t kClientUseCallingPid = -1;
54 constexpr int32_t kClientUseCallingUid = -1;
55 constexpr const char* kClientOpPackageName = "TestClient";
56 
57 class MediaTranscodingServiceTest : public ::testing::Test {
58 public:
MediaTranscodingServiceTest()59     MediaTranscodingServiceTest() { ALOGD("MediaTranscodingServiceTest created"); }
60 
SetUp()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 
~MediaTranscodingServiceTest()70     ~MediaTranscodingServiceTest() { ALOGD("MediaTranscodingingServiceTest destroyed"); }
71 
72     std::shared_ptr<IMediaTranscodingService> mService = nullptr;
73 };
74 
75 struct TestClient : public BnTranscodingServiceClient {
TestClientandroid::media::TestClient76     TestClient(const std::shared_ptr<IMediaTranscodingService>& service) : mService(service) {
77         ALOGD("TestClient Created");
78     }
79 
getNameandroid::media::TestClient80     Status getName(std::string* _aidl_return) override {
81         *_aidl_return = "test_client";
82         return Status::ok();
83     }
84 
onTranscodingFinishedandroid::media::TestClient85     Status onTranscodingFinished(
86             int32_t /* in_jobId */,
87             const ::aidl::android::media::TranscodingResultParcel& /* in_result */) override {
88         return Status::ok();
89     }
90 
onTranscodingFailedandroid::media::TestClient91     Status onTranscodingFailed(
92             int32_t /* in_jobId */,
93             ::aidl::android::media::TranscodingErrorCode /*in_errorCode */) override {
94         return Status::ok();
95     }
96 
onAwaitNumberOfJobsChangedandroid::media::TestClient97     Status onAwaitNumberOfJobsChanged(int32_t /* in_jobId */, int32_t /* in_oldAwaitNumber */,
98                                       int32_t /* in_newAwaitNumber */) override {
99         return Status::ok();
100     }
101 
onProgressUpdateandroid::media::TestClient102     Status onProgressUpdate(int32_t /* in_jobId */, int32_t /* in_progress */) override {
103         return Status::ok();
104     }
105 
~TestClientandroid::media::TestClient106     virtual ~TestClient() { ALOGI("TestClient destroyed"); };
107 
108 private:
109     std::shared_ptr<IMediaTranscodingService> mService;
110 };
111 
TEST_F(MediaTranscodingServiceTest,TestRegisterNullClient)112 TEST_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 
TEST_F(MediaTranscodingServiceTest,TestRegisterClientWithInvalidClientPid)120 TEST_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 
TEST_F(MediaTranscodingServiceTest,TestRegisterClientWithInvalidClientUid)132 TEST_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 
TEST_F(MediaTranscodingServiceTest,TestRegisterClientWithInvalidClientPackageName)144 TEST_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 
TEST_F(MediaTranscodingServiceTest,TestRegisterOneClient)156 TEST_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 
TEST_F(MediaTranscodingServiceTest,TestUnRegisterClientWithInvalidClientId)184 TEST_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 
TEST_F(MediaTranscodingServiceTest,TestRegisterClientTwice)214 TEST_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
240