• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "gtest/gtest.h"
17 #include "calendar_log.h"
18 #include "get_self_permissions.h"
19 #include "data_share_helper_manager.h"
20 #include "calendar_env.h"
21 #include "accesstoken_kit.h"
22 #include "datashare_helper.h"
23 #include "iservice_registry.h"
24 #include "system_ability_definition.h"
25 #include "token_setproc.h"
26 
27 using namespace testing::ext;
28 using namespace OHOS::Security::AccessToken;
29 using namespace OHOS::DataShare;
30 namespace {
31     std::string DATA_SHARE_URI = "datashare:///calendardata";
32 }
33 
34 namespace OHOS::CalendarApi {
CreateDataShareHelper(int32_t systemAbilityId,const std::string & uri)35 std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper(int32_t systemAbilityId, const std::string& uri)
36 {
37     LOG_INFO("CreateDataShareHelper start");
38     auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
39     if (saManager == nullptr) {
40         LOG_ERROR("GetSystemAbilityManager get samgr failed.");
41         return nullptr;
42     }
43     auto remoteObj = saManager->GetSystemAbility(systemAbilityId);
44     while (remoteObj == nullptr) {
45         LOG_ERROR("GetSystemAbility service failed.");
46         return nullptr;
47     }
48     LOG_INFO("CreateDataShareHelper %{public}s", uri.c_str());
49     return DataShare::DataShareHelper::Creator(remoteObj, uri);
50 }
51 
InitEnv(void)52 void InitEnv(void)
53 {
54     constexpr int storageManagerId = 5003;
55     vector<string> perms;
56     perms.push_back("ohos.permission.READ_CALENDAR");
57     perms.push_back("ohos.permission.WRITE_CALENDAR");
58     uint64_t tokenId = 0;
59     SetAccessTokenPermission("calendar_manager_test", perms, tokenId);
60     ASSERT_TRUE(tokenId != 0);
61     CalendarEnv::GetInstance().Init("ohos.calendar_manager_test.demo", tokenId);
62     auto helper = CreateDataShareHelper(storageManagerId, DATA_SHARE_URI);
63     ASSERT_TRUE(helper != nullptr);
64     DataShareHelperManager::GetInstance().SetDataShareHelper(helper);
65     LOG_INFO("SetUpTestCase end");
66 }
67 
UnInitEnv(void)68 void UnInitEnv(void)
69 {
70     auto tokenId = AccessTokenKit::GetHapTokenID(100, "ohos.datashareclienttest.demo", 0);
71     AccessTokenKit::DeleteToken(tokenId);
72 }
73 } // namespace OHOS::CalendarApi
74 
75 class GlobalEnvironment : public testing::Environment {
76 public:
SetUp()77     virtual void SetUp()
78     {
79         OHOS::CalendarApi::InitEnv();
80     }
TearDown()81     virtual void TearDown()
82     {
83         OHOS::CalendarApi::UnInitEnv();
84     }
85 };
86 
87 
main(int argc,char ** argv)88 int main(int argc, char **argv)
89 {
90     testing::GTEST_FLAG(output) = "xml:./";
91     ::testing::InitGoogleTest(&argc, argv);
92     testing::AddGlobalTestEnvironment(new GlobalEnvironment);
93     return RUN_ALL_TESTS();
94 }