1 /*
2  * Copyright 2020 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 <android-base/logging.h>
18 #include <android/hardware/cas/1.0/types.h>
19 #include <android/hardware/cas/1.2/ICas.h>
20 #include <android/hardware/cas/1.2/ICasListener.h>
21 #include <android/hardware/cas/1.2/IMediaCasService.h>
22 #include <android/hardware/cas/1.2/types.h>
23 #include <android/hardware/tv/tuner/1.0/IDescrambler.h>
24 #include <android/hardware/tv/tuner/1.0/IDvr.h>
25 #include <android/hardware/tv/tuner/1.0/IDvrCallback.h>
26 #include <android/hardware/tv/tuner/1.0/ITuner.h>
27 #include <android/hardware/tv/tuner/1.0/types.h>
28 #include <fmq/MessageQueue.h>
29 #include <gtest/gtest.h>
30 #include <hidl/HidlSupport.h>
31 #include <hidl/Status.h>
32 #include <utils/Condition.h>
33 #include <utils/Mutex.h>
34 #include <fstream>
35 #include <iostream>
36 #include <map>
37 
38 using android::Condition;
39 using android::Mutex;
40 using android::sp;
41 using android::hardware::EventFlag;
42 using android::hardware::hidl_handle;
43 using android::hardware::hidl_string;
44 using android::hardware::hidl_vec;
45 using android::hardware::kSynchronizedReadWrite;
46 using android::hardware::MessageQueue;
47 using android::hardware::MQDescriptorSync;
48 using android::hardware::Return;
49 using android::hardware::Void;
50 using android::hardware::cas::V1_2::ICas;
51 using android::hardware::cas::V1_2::ICasListener;
52 using android::hardware::cas::V1_2::IMediaCasService;
53 using android::hardware::cas::V1_2::ScramblingMode;
54 using android::hardware::cas::V1_2::SessionIntent;
55 using android::hardware::cas::V1_2::Status;
56 using android::hardware::cas::V1_2::StatusEvent;
57 using android::hardware::tv::tuner::V1_0::DemuxFilterMainType;
58 using android::hardware::tv::tuner::V1_0::DemuxFilterSettings;
59 using android::hardware::tv::tuner::V1_0::DemuxFilterStatus;
60 using android::hardware::tv::tuner::V1_0::DemuxFilterType;
61 using android::hardware::tv::tuner::V1_0::DemuxMmtpFilterType;
62 using android::hardware::tv::tuner::V1_0::DemuxPid;
63 using android::hardware::tv::tuner::V1_0::DemuxTsFilterType;
64 using android::hardware::tv::tuner::V1_0::IDescrambler;
65 using android::hardware::tv::tuner::V1_0::IFilter;
66 using android::hardware::tv::tuner::V1_0::ITuner;
67 using android::hardware::tv::tuner::V1_0::Result;
68 using android::hardware::tv::tuner::V1_0::TunerKeyToken;
69 
70 using ::testing::AssertionResult;
71 
72 using namespace std;
73 
74 class MediaCasListener : public ICasListener {
75   public:
onEvent(int32_t,int32_t,const hidl_vec<uint8_t> &)76     virtual Return<void> onEvent(int32_t /*event*/, int32_t /*arg*/,
77                                  const hidl_vec<uint8_t>& /*data*/) override {
78         return Void();
79     }
80 
onSessionEvent(const hidl_vec<uint8_t> &,int32_t,int32_t,const hidl_vec<uint8_t> &)81     virtual Return<void> onSessionEvent(const hidl_vec<uint8_t>& /*sessionId*/, int32_t /*event*/,
82                                         int32_t /*arg*/,
83                                         const hidl_vec<uint8_t>& /*data*/) override {
84         return Void();
85     }
86 
onStatusUpdate(StatusEvent,int32_t)87     virtual Return<void> onStatusUpdate(StatusEvent /*event*/, int32_t /*arg*/) override {
88         return Void();
89     }
90 };
91 
92 class DescramblerTests {
93   public:
setService(sp<ITuner> tuner)94     void setService(sp<ITuner> tuner) { mService = tuner; }
setCasService(sp<IMediaCasService> casService)95     void setCasService(sp<IMediaCasService> casService) { mMediaCasService = casService; }
96 
97     AssertionResult setKeyToken(TunerKeyToken token);
98     AssertionResult openDescrambler(uint32_t demuxId);
99     AssertionResult addPid(DemuxPid pid, sp<IFilter> optionalSourceFilter);
100     AssertionResult removePid(DemuxPid pid, sp<IFilter> optionalSourceFilter);
101     AssertionResult closeDescrambler();
102     AssertionResult getKeyToken(int32_t caSystemId, string provisonStr,
103                                 hidl_vec<uint8_t> hidlPvtData, TunerKeyToken& token);
104     AssertionResult getDemuxPidFromFilterSettings(DemuxFilterType type,
105                                                   DemuxFilterSettings settings, DemuxPid& pid);
106 
107   protected:
failure()108     static AssertionResult failure() { return ::testing::AssertionFailure(); }
109 
success()110     static AssertionResult success() { return ::testing::AssertionSuccess(); }
111 
112     sp<ITuner> mService;
113     sp<ICas> mCas;
114     sp<IMediaCasService> mMediaCasService;
115     sp<MediaCasListener> mCasListener;
116     sp<IDescrambler> mDescrambler;
117 
118   private:
119     AssertionResult openCasSession(TunerKeyToken& sessionId, vector<uint8_t> hidlPvtData);
120     AssertionResult createCasPlugin(int32_t caSystemId);
121 };
122