• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
18 
19 #include <gtest/gtest.h>
20 #include <log/log.h>
21 #include <utils/Condition.h>
22 #include <utils/Mutex.h>
23 #include <fstream>
24 #include <iostream>
25 #include <map>
26 
27 #include <android/hardware/cas/1.0/types.h>
28 #include <android/hardware/cas/1.2/ICas.h>
29 #include <android/hardware/cas/1.2/ICasListener.h>
30 #include <android/hardware/cas/1.2/IMediaCasService.h>
31 #include <android/hardware/cas/1.2/types.h>
32 
33 #include <aidl/android/hardware/cas/BnCasListener.h>
34 #include <aidl/android/hardware/cas/ICas.h>
35 #include <aidl/android/hardware/cas/IMediaCasService.h>
36 #include <aidl/android/hardware/cas/ScramblingMode.h>
37 #include <aidl/android/hardware/cas/SessionIntent.h>
38 #include <aidl/android/hardware/tv/tuner/IDescrambler.h>
39 #include <aidl/android/hardware/tv/tuner/IDvr.h>
40 #include <aidl/android/hardware/tv/tuner/IDvrCallback.h>
41 #include <aidl/android/hardware/tv/tuner/ITuner.h>
42 
43 using ::aidl::android::hardware::cas::BnCasListener;
44 using android::Condition;
45 using android::Mutex;
46 using android::sp;
47 using android::hardware::hidl_string;
48 using android::hardware::hidl_vec;
49 using android::hardware::Return;
50 using android::hardware::Void;
51 using android::hardware::cas::V1_2::Status;
52 using android::hardware::cas::V1_2::StatusEvent;
53 using ICasAidl = ::aidl::android::hardware::cas::ICas;
54 using ICasHidl = android::hardware::cas::V1_2::ICas;
55 using ICasListenerHidl = android::hardware::cas::V1_2::ICasListener;
56 using IMediaCasServiceAidl = ::aidl::android::hardware::cas::IMediaCasService;
57 using IMediaCasServiceHidl = android::hardware::cas::V1_2::IMediaCasService;
58 using ScramblingModeAidl = ::aidl::android::hardware::cas::ScramblingMode;
59 using ScramblingModeHidl = android::hardware::cas::V1_2::ScramblingMode;
60 using SessionIntentAidl = ::aidl::android::hardware::cas::SessionIntent;
61 using SessionIntentHidl = android::hardware::cas::V1_2::SessionIntent;
62 
63 using ::ndk::ScopedAStatus;
64 using ::testing::AssertionResult;
65 
66 using namespace aidl::android::hardware::tv::tuner;
67 
68 const std::string MEDIA_CAS_AIDL_SERVICE_NAME = "android.hardware.cas.IMediaCasService/default";
69 
70 class MediaCasListener : public ICasListenerHidl, public BnCasListener {
71   public:
onEvent(int32_t,int32_t,const hidl_vec<uint8_t> &)72     virtual Return<void> onEvent(int32_t /*event*/, int32_t /*arg*/,
73                                  const hidl_vec<uint8_t>& /*data*/) override {
74         return Void();
75     }
76 
onSessionEvent(const hidl_vec<uint8_t> &,int32_t,int32_t,const hidl_vec<uint8_t> &)77     virtual Return<void> onSessionEvent(const hidl_vec<uint8_t>& /*sessionId*/, int32_t /*event*/,
78                                         int32_t /*arg*/,
79                                         const hidl_vec<uint8_t>& /*data*/) override {
80         return Void();
81     }
82 
onStatusUpdate(StatusEvent,int32_t)83     virtual Return<void> onStatusUpdate(StatusEvent /*event*/, int32_t /*arg*/) override {
84         return Void();
85     }
86 
onEvent(int32_t,int32_t,const std::vector<uint8_t> &)87     ScopedAStatus onEvent(int32_t /*in_event*/, int32_t /*in_arg*/,
88                           const std::vector<uint8_t>& /*in_data*/) override {
89         return ScopedAStatus::ok();
90     }
91 
onSessionEvent(const std::vector<uint8_t> &,int32_t,int32_t,const std::vector<uint8_t> &)92     ScopedAStatus onSessionEvent(const std::vector<uint8_t>& /*in_sessionId*/, int32_t /*in_event*/,
93                                  int32_t /*in_arg*/,
94                                  const std::vector<uint8_t>& /*in_data*/) override {
95         return ScopedAStatus::ok();
96     }
97 
onStatusUpdate(::aidl::android::hardware::cas::StatusEvent,int32_t)98     ScopedAStatus onStatusUpdate(::aidl::android::hardware::cas::StatusEvent /*in_event*/,
99                                  int32_t /*in_number*/) override {
100         return ScopedAStatus::ok();
101     }
102 };
103 
104 class DescramblerTests {
105   public:
setService(std::shared_ptr<ITuner> tuner)106     void setService(std::shared_ptr<ITuner> tuner) { mService = tuner; }
setCasServiceHidl(sp<IMediaCasServiceHidl> casService)107     void setCasServiceHidl(sp<IMediaCasServiceHidl> casService) {
108         mMediaCasServiceHidl = casService;
109     }
setCasServiceAidl(std::shared_ptr<IMediaCasServiceAidl> casService)110     void setCasServiceAidl(std::shared_ptr<IMediaCasServiceAidl> casService) {
111         mMediaCasServiceAidl = casService;
112     }
113 
114     AssertionResult setKeyToken(std::vector<uint8_t>& token);
115     AssertionResult openDescrambler(int32_t demuxId);
116     AssertionResult addPid(DemuxPid pid, std::shared_ptr<IFilter> optionalSourceFilter);
117     AssertionResult removePid(DemuxPid pid, std::shared_ptr<IFilter> optionalSourceFilter);
118     AssertionResult closeDescrambler();
119     AssertionResult getKeyToken(int32_t caSystemId, std::string& provisonStr,
120                                 std::vector<uint8_t>& hidlPvtData, std::vector<uint8_t>& token);
121     AssertionResult getDemuxPidFromFilterSettings(DemuxFilterType type,
122                                                   const DemuxFilterSettings& settings,
123                                                   DemuxPid& pid);
124 
125   protected:
failure()126     static AssertionResult failure() { return ::testing::AssertionFailure(); }
127 
success()128     static AssertionResult success() { return ::testing::AssertionSuccess(); }
129 
130     std::shared_ptr<ITuner> mService;
131     std::shared_ptr<IDescrambler> mDescrambler;
132     std::shared_ptr<ICasAidl> mCasAidl;
133     android::sp<ICasHidl> mCasHidl;
134     std::shared_ptr<IMediaCasServiceAidl> mMediaCasServiceAidl;
135     android::sp<IMediaCasServiceHidl> mMediaCasServiceHidl;
136     std::shared_ptr<MediaCasListener> mCasListener;
137 
138   private:
139     AssertionResult openCasSession(std::vector<uint8_t>& sessionId, std::vector<uint8_t>& pvtData);
140     AssertionResult createCasPlugin(int32_t caSystemId);
141 };
142