1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <mutex>
17 #include <string>
18 #include "cstdio"
19 #include "cstdlib"
20 #include "drm_dfx.h"
21 #include "drm_dfx_utils.h"
22 #include <securec.h>
23 #include "native_drm_common.h"
24 #include "native_drm_err.h"
25 #include "gmock/gmock.h"
26 #include "native_drm_base.h"
27 #include "native_drm_object.h"
28 #include "native_mediakeysession.h"
29 #include "native_mediakeysystem.h"
30 #include "drm_framework_unittest.h"
31 #include "http.h"
32 
33 #define DRM_SAMPLE_CHECK_AND_RETURN_RET_LOG(cond, ret, fmt, ...) \
34     do {                                                         \
35         if (!(cond)) {                                            \
36             printf(fmt, ##__VA_ARGS__);                          \
37             return ret;                                          \
38         }                                                        \
39     } while (0)
40 
41 #define OFFRESPONSE                                                                                            \
42     {                                                                                                          \
43         0x31, 0x64, 0x6E, 0x5A, 0x32, 0x4E, 0x57, 0x74, 0x76, 0x4D, 0x47,                                      \
44             0x34, 0x34, 0x4E, 0x6A, 0x42, 0x30, 0x4D, 0x32, 0x77, 0x33,                                        \
45             0x4E, 0x67, 0x3D, 0x3D, 0x3A, 0x59, 0x7A, 0x56, 0x78, 0x63,                                        \
46             0x48, 0x64, 0x70, 0x61, 0x6D, 0x30, 0x34, 0x59, 0x57, 0x45,                                        \
47             0x34, 0x5A, 0x48, 0x6B, 0x79, 0x4D, 0x67, 0x3D, 0x3D                                               \
48     }
49 #define REQUESTINFODATA                                                                                        \
50     {                                                                                                          \
51         0x00, 0x00, 0x00, 0x8B, 0x70, 0x73, 0x73, 0x68, 0x00, 0x00, 0x00,                                      \
52             0x00, 0x3D, 0x5E, 0x6D, 0x35, 0x9B, 0x9A, 0x41, 0xE8, 0xB8,                                        \
53             0x43, 0xDD, 0x3C, 0x6E, 0x72, 0xC4, 0x2C, 0x00, 0x00, 0x00,                                        \
54             0x6B, 0x7B, 0x22, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E,                                        \
55             0x22, 0x3A, 0x22, 0x56, 0x31, 0x2E, 0x30, 0x22, 0x2C, 0x22,                                        \
56             0x63, 0x6F, 0x6E, 0x74, 0x65, 0x6E, 0x74, 0x49, 0x44, 0x22,                                        \
57             0x3A, 0x22, 0x64, 0x48, 0x4D, 0x74, 0x4D, 0x6A, 0x59, 0x30,                                        \
58             0x4C, 0x54, 0x45, 0x77, 0x4F, 0x44, 0x41, 0x74, 0x59, 0x57,                                        \
59             0x56, 0x7A, 0x22, 0x2C, 0x22, 0x6B, 0x69, 0x64, 0x73, 0x22,                                        \
60             0x3A, 0x5B, 0x22, 0x47, 0x2B, 0x45, 0x6B, 0x2F, 0x2B, 0x58,                                        \
61             0x6D, 0x55, 0x6B, 0x70, 0x42, 0x48, 0x51, 0x67, 0x58, 0x59,                                        \
62             0x57, 0x51, 0x51, 0x49, 0x67, 0x3D, 0x3D, 0x22, 0x5D, 0x2C,                                        \
63             0x22, 0x65, 0x6E, 0x73, 0x63, 0x68, 0x65, 0x6D, 0x61, 0x22,                                        \
64             0x3A, 0x22, 0x63, 0x62, 0x63, 0x31, 0x22, 0x7D                                                     \
65     }
66 #define LICENSE_URL "http://192.168.50.59:9528/getLicense"
67 #define PROVISION_URL "http://192.168.50.59:9528/getProvision"
68 
69 
70 using namespace testing::ext;
71 using namespace std;
72 
73 
74 namespace OHOS {
75 namespace DrmStandard {
76 
77 HWTEST_F(DrmFrameworkUnitTest, Drm_Unittest_GetMediaKeySystems, TestSize.Level0)
78 {
79     uint32_t count = 10;
80     DRM_MediaKeySystemDescription infos[10];
81     memset_s(infos, sizeof(infos), 0, sizeof(infos));
82     Drm_ErrCode errNo = OH_MediaKeySystem_GetMediaKeySystems(infos, &count);
83     EXPECT_EQ(errNo, 0);
84 }
85 
86 HWTEST_F(DrmFrameworkUnitTest, Drm_Unittest_GetMediaKeySystemsAbnorm, TestSize.Level0)
87 {
88     uint32_t count = 10;
89     Drm_ErrCode errNo = OH_MediaKeySystem_GetMediaKeySystems(nullptr, &count);
90     EXPECT_NE(errNo, 0);
91 }
92 
93 HWTEST_F(DrmFrameworkUnitTest, Drm_Unittest_GetMediaKeySystemsAbnorm1, TestSize.Level0)
94 {
95     DRM_MediaKeySystemDescription infos[10];
96     memset_s(infos, sizeof(infos), 0, sizeof(infos));
97     Drm_ErrCode errNo = OH_MediaKeySystem_GetMediaKeySystems(infos, nullptr);
98     EXPECT_NE(errNo, 0);
99 }
100 
101 HWTEST_F(DrmFrameworkUnitTest, Drm_Unittest_GetMediaKeySystemsAbnorm2, TestSize.Level0)
102 {
103     uint32_t count = 0;
104     DRM_MediaKeySystemDescription infos[10];
105     memset_s(infos, sizeof(infos), 0, sizeof(infos));
106     Drm_ErrCode errNo = OH_MediaKeySystem_GetMediaKeySystems(infos, &count);
107     EXPECT_NE(errNo, 0);
108 }
109 
110 HWTEST_F(DrmFrameworkUnitTest, Drm_unittest_ReportFaultEvent, TestSize.Level0)
111 {
112     uint32_t errorCode = 0;
113     std::string errorMesg = "GenerateMediaKeyRequest failed";
114     std::string extraMesg = "";
115     Drm_ErrCode errNo = (Drm_ErrCode)ReportFaultEvent(errorCode, errorMesg, extraMesg);
116     EXPECT_EQ(errNo, 0);
117 }
118 
119 HWTEST_F(DrmFrameworkUnitTest, Drm_unittest_ReportDecryptionFaultEvent, TestSize.Level0)
120 {
121     uint32_t errorCode = 0;
122     std::string errorMesg = "Decryption failed";
123     std::string decryptAlgo = "decryptAlgo";
124     std::string decryptKeyid = "decryptKeyid";
125     std::string decryptIv = "decryptIv";
126     Drm_ErrCode errNo = (Drm_ErrCode)ReportDecryptionFaultEvent(errorCode, errorMesg, decryptAlgo, decryptKeyid,
127         decryptIv);
128     EXPECT_EQ(errNo, 0);
129 }
130 
131 HWTEST_F(DrmFrameworkUnitTest, Drm_unittest_ReportServiceBehaviorEvent, TestSize.Level0)
132 {
133     std::string serviceName = "DRM_OEM_SERVICE";
134     std::string action = "start";
135     Drm_ErrCode errNo = (Drm_ErrCode)ReportServiceBehaviorEvent(serviceName, action);
136     EXPECT_EQ(errNo, 0);
137 }
138 
139 HWTEST_F(DrmFrameworkUnitTest, Drm_unittest_ReportLicenseBehaviorEvent, TestSize.Level0)
140 {
141     std::string mediaKeyType = "keytype";
142     struct DownLoadInfo downLoadInfo = InitDownLoadInfo(10, "success", 1, "success");
143     struct StatisticsInfo statisticsInfo = {
144         "123123",
145         "clearplay",
146         "vendor",
147         "clearplay_v1",
148         "bundleName",
149     };
150     Drm_ErrCode errNo = (Drm_ErrCode)ReportLicenseBehaviorEvent(statisticsInfo, mediaKeyType, downLoadInfo);
151     EXPECT_EQ(errNo, 0);
152 }
153 
154 HWTEST_F(DrmFrameworkUnitTest, Drm_unittest_ReportCertificateBehaviorEvent, TestSize.Level0)
155 {
156     std::string mediaKeyType = "keytype";
157     uint32_t callServerTime = 1;
158     uint32_t serverCostDuration = 19;
159     std::string serverResult = "serverResult";
160     struct DownLoadInfo downLoadInfo = InitDownLoadInfo(10, "success", 1, "success");
161     struct StatisticsInfo statisticsInfo = {
162         "123123",
163         "clearplay",
164         "vendor",
165         "clearplay_v1",
166         "bundleName",
167     };
168     Drm_ErrCode errNo = (Drm_ErrCode)ReportCertificateBehaviorEvent(statisticsInfo, downLoadInfo, callServerTime,
169         serverCostDuration,
170         serverResult);
171     EXPECT_EQ(errNo, 0);
172 }
173 
174 } // DrmStandard
175 } // OHOS