• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (C) 2022 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 <ctime>
17 #include <gtest/gtest.h>
18 
19 #include "accesstoken_kit.h"
20 #include "directory_ex.h"
21 #include "hilog_wrapper.h"
22 #include "image_packer.h"
23 #include "nativetoken_kit.h"
24 #include "pixel_map.h"
25 #include "token_setproc.h"
26 #include "wallpaper_manager.h"
27 #include "wallpaper_manager_kits.h"
28 
29 constexpr int LOCKSCREEN = 1;
30 constexpr int HUNDRED = 100;
31 using namespace testing::ext;
32 using namespace testing;
33 using namespace OHOS::Media;
34 using namespace OHOS::HiviewDFX;
35 using namespace OHOS::MiscServices;
36 using namespace OHOS::Security::AccessToken;
37 
38 namespace OHOS {
39 namespace WallpaperMgrService {
40 constexpr const char *URL = "/data/test/theme/wallpaper/wallpaper_test.JPG";
41 
42 class WallpaperPermissionTest : public testing::Test {
43 public:
44     static void SetUpTestCase(void);
45     static void TearDownTestCase(void);
46     void SetUp();
47     void TearDown();
48     static void CreateTempImage();
49     static std::shared_ptr<PixelMap> CreateTempPixelMap();
50 };
51 const std::string VALID_SCHEMA_STRICT_DEFINE = "{\"SCHEMA_VERSION\":\"1.0\","
52                                                "\"SCHEMA_MODE\":\"STRICT\","
53                                                "\"SCHEMA_SKIPSIZE\":0,"
54                                                "\"SCHEMA_DEFINE\":{"
55                                                "\"age\":\"INTEGER, NOT NULL\""
56                                                "},"
57                                                "\"SCHEMA_INDEXES\":[\"$.age\"]}";
58 
SetUpTestCase(void)59 void WallpaperPermissionTest::SetUpTestCase(void)
60 {
61     CreateTempImage();
62     HILOG_INFO("SetUpPermissionTestCase");
63 }
64 
TearDownTestCase(void)65 void WallpaperPermissionTest::TearDownTestCase(void)
66 {
67     HILOG_INFO("PermissionTestTearDownTestCase");
68 }
69 
SetUp(void)70 void WallpaperPermissionTest::SetUp(void)
71 {
72 }
73 
TearDown(void)74 void WallpaperPermissionTest::TearDown(void)
75 {
76 }
77 
CreateTempImage()78 void WallpaperPermissionTest::CreateTempImage()
79 {
80     std::shared_ptr<PixelMap> pixelMap = CreateTempPixelMap();
81     ImagePacker imagePacker;
82     PackOption option;
83     option.format = "image/jpeg";
84     option.quality = HUNDRED;
85     option.numberHint = 1;
86     std::set<std::string> formats;
87     imagePacker.GetSupportedFormats(formats);
88     imagePacker.StartPacking(URL, option);
89     HILOG_INFO("AddImage start");
90     imagePacker.AddImage(*pixelMap);
91     int64_t packedSize = 0;
92     HILOG_INFO("FinalizePacking start");
93     imagePacker.FinalizePacking(packedSize);
94     if (packedSize == 0) {
95         HILOG_INFO("FinalizePacking error");
96     }
97 }
98 
CreateTempPixelMap()99 std::shared_ptr<PixelMap> WallpaperPermissionTest::CreateTempPixelMap()
100 {
101     uint32_t color[100] = { 3, 7, 9, 9, 7, 6 };
102     InitializationOptions opts = { { 5, 7 }, OHOS::Media::PixelFormat::ARGB_8888 };
103     std::unique_ptr<PixelMap> uniquePixelMap = PixelMap::Create(color, sizeof(color) / sizeof(color[0]), opts);
104     std::shared_ptr<PixelMap> pixelMap = std::move(uniquePixelMap);
105     return pixelMap;
106 }
107 
108 /*********************   ResetWallpaper   *********************/
109 /**
110 * @tc.name:    ResetPermission001
111 * @tc.desc:    Reset wallpaper throw permission error.
112 * @tc.type:    FUNC
113 * @tc.require: issueI60MT1
114 * @tc.author:  lvbai
115 */
116 HWTEST_F(WallpaperPermissionTest, ResetPermission001, TestSize.Level1)
117 {
118     HILOG_INFO("ResetPermission001 begin");
119     ApiInfo apiInfo{ false, false };
120     int32_t wallpaperErrorCode =
121         OHOS::WallpaperMgrService::WallpaperManagerkits::GetInstance().ResetWallpaper(LOCKSCREEN, apiInfo);
122     EXPECT_EQ(wallpaperErrorCode, static_cast<int32_t>(E_NO_PERMISSION)) << "throw permission error successfully";
123 }
124 /*********************   ResetWallpaper   *********************/
125 
126 /*********************   GetFile   *********************/
127 /**
128 * @tc.name:    GetFilePermission001
129 * @tc.desc:    GetFile with wallpaperType[1] throw permission error.
130 * @tc.type:    FUNC
131 * @tc.require: issueI60MT1
132 * @tc.author:  lvbai
133 */
134 HWTEST_F(WallpaperPermissionTest, GetFilePermission001, TestSize.Level0)
135 {
136     HILOG_INFO("GetFilePermission001 begin");
137     int32_t wallpaperFd = 0;
138     int wallpaperErrorCode =
139         OHOS::WallpaperMgrService::WallpaperManagerkits::GetInstance().GetFile(LOCKSCREEN, wallpaperFd);
140     EXPECT_EQ(wallpaperErrorCode, static_cast<int32_t>(E_NO_PERMISSION)) << "throw permission error successfully";
141 }
142 /*********************   GetFile   *********************/
143 
144 /*********************   GetPiexlMap   *********************/
145 /**
146 * @tc.name:    GetPiexlMapPermission001
147 * @tc.desc:    GetPixelMap with wallpaperType[1] throw permission error.
148 * @tc.type:    FUNC
149 * @tc.require: issueI60MT1
150 * @tc.author:  lvbai
151 */
152 HWTEST_F(WallpaperPermissionTest, GetPiexlMapPermission001, TestSize.Level0)
153 {
154     HILOG_INFO("GetPixelMapPermission001  begin");
155     ApiInfo apiInfo{ false, false };
156     std::shared_ptr<OHOS::Media::PixelMap> pixelMap;
157     int32_t wallpaperErrorCode =
158         OHOS::WallpaperMgrService::WallpaperManagerkits::GetInstance().GetPixelMap(LOCKSCREEN, apiInfo, pixelMap);
159     EXPECT_EQ(wallpaperErrorCode, static_cast<int32_t>(E_NO_PERMISSION)) << "throw permission error successfully";
160 }
161 /*********************   GetPiexlMap   *********************/
162 
163 /*********************   SetWallpaperByMap   *********************/
164 /**
165 * @tc.name:    SetWallpaperByMapPermission001
166 * @tc.desc:    SetWallpaperByMap with wallpaperType[1] throw permission error.
167 * @tc.type:    FUNC
168 * @tc.require: issueI60MT1
169 * @tc.author:  lvbai
170 */
171 HWTEST_F(WallpaperPermissionTest, SetWallpaperByMapPermission001, TestSize.Level0)
172 {
173     HILOG_INFO("SetWallpaperByMapPermission001  begin");
174     std::shared_ptr<PixelMap> pixelMap = WallpaperPermissionTest::CreateTempPixelMap();
175 
176     ApiInfo apiInfo{ false, false };
177     int32_t wallpaperErrorCode =
178         OHOS::WallpaperMgrService::WallpaperManagerkits::GetInstance().SetWallpaper(pixelMap, 2, apiInfo);
179     EXPECT_EQ(wallpaperErrorCode, static_cast<int32_t>(E_NO_PERMISSION)) << "throw permission error successfully";
180 }
181 /*********************   SetWallpaperByMap   *********************/
182 
183 /*********************   SetWallpaperByUrl   *********************/
184 /**
185 * @tc.name:    SetWallpaperByUrlPermission001
186 * @tc.desc:    SetWallpaperByUrl with wallpaperType[1] throw permission error.
187 * @tc.type:    FUNC
188 * @tc.require: issueI60MT1
189 * @tc.author:  lvbai
190 */
191 HWTEST_F(WallpaperPermissionTest, SetWallpaperByUrlPermission001, TestSize.Level0)
192 {
193     HILOG_INFO("SetWallpaperByUriPermission001  begin");
194     ApiInfo apiInfo{ false, false };
195     int32_t wallpaperErrorCode =
196         OHOS::WallpaperMgrService::WallpaperManagerkits::GetInstance().SetWallpaper(URL, LOCKSCREEN, apiInfo);
197     EXPECT_EQ(wallpaperErrorCode, static_cast<int32_t>(E_NO_PERMISSION)) << "throw permission error successfully";
198     HILOG_INFO("SetWallpaperByUriPermission001  end");
199 }
200 /*********************   SetWallpaperByUrl   *********************/
201 } // namespace WallpaperMgrService
202 } // namespace OHOS