Santiago Seifert | 353df12 | 2021-02-17 11:07:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 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 | #include <binder/IServiceManager.h> |
| 18 | #include <binder/Parcel.h> |
| 19 | #include <gtest/gtest.h> |
| 20 | #include <media/stagefright/foundation/ABuffer.h> |
| 21 | #include <media/IMediaPlayer.h> |
| 22 | #include <media/IMediaPlayerService.h> |
| 23 | #include <media/mediaplayer.h> |
| 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | constexpr uint8_t kMockByteArray[16] = {}; |
| 28 | |
| 29 | class IMediaPlayerTest : public testing::Test { |
| 30 | protected: |
| 31 | static constexpr uint32_t PREPARE_DRM = IMediaPlayer::PREPARE_DRM; |
| 32 | |
| 33 | void SetUp() override { |
| 34 | mediaPlayer_ = new MediaPlayer(); |
| 35 | sp<IServiceManager> serviceManager = defaultServiceManager(); |
| 36 | sp<IBinder> mediaPlayerService = serviceManager->getService(String16("media.player")); |
| 37 | sp<IMediaPlayerService> iMediaPlayerService = |
| 38 | IMediaPlayerService::asInterface(mediaPlayerService); |
| 39 | iMediaPlayer_ = iMediaPlayerService->create(mediaPlayer_); |
| 40 | } |
| 41 | |
| 42 | sp<MediaPlayer> mediaPlayer_; |
| 43 | sp<IMediaPlayer> iMediaPlayer_; |
| 44 | }; |
| 45 | |
| 46 | TEST_F(IMediaPlayerTest, PrepareDrmInvalidTransaction) { |
| 47 | Parcel data, reply; |
| 48 | data.writeInterfaceToken(iMediaPlayer_->getInterfaceDescriptor()); |
| 49 | data.write(kMockByteArray, 16); |
| 50 | |
| 51 | // We write a length greater than the following session id array. Should be discarded. |
| 52 | data.writeUint32(2); |
| 53 | data.writeUnpadded(kMockByteArray, 1); |
| 54 | |
| 55 | status_t result = IMediaPlayer::asBinder(iMediaPlayer_) |
| 56 | ->transact(PREPARE_DRM, data, &reply); |
| 57 | ASSERT_EQ(result, BAD_VALUE); |
| 58 | } |
| 59 | |
| 60 | TEST_F(IMediaPlayerTest, PrepareDrmValidTransaction) { |
| 61 | Parcel data, reply; |
| 62 | data.writeInterfaceToken(iMediaPlayer_->getInterfaceDescriptor()); |
| 63 | data.write(kMockByteArray, 16); |
| 64 | |
| 65 | // We write a length equal to the length of the following data. The transaction should be valid. |
| 66 | data.writeUint32(1); |
| 67 | data.write(kMockByteArray, 1); |
| 68 | |
| 69 | status_t result = IMediaPlayer::asBinder(iMediaPlayer_) |
| 70 | ->transact(PREPARE_DRM, data, &reply); |
| 71 | ASSERT_EQ(result, OK); |
| 72 | } |
| 73 | } // namespace android |