diff --git a/framework/js/napi/common/include/napi_utils.h b/framework/js/napi/common/include/napi_utils.h index 5c70749f28d5dbba406c251b826c20d2e7aba74b..cc5601c339f0058f5d6c2171a219474ebd1b4379 100644 --- a/framework/js/napi/common/include/napi_utils.h +++ b/framework/js/napi/common/include/napi_utils.h @@ -93,6 +93,7 @@ constexpr int32_t DESTROY_MAX_PARA = 2; constexpr int32_t MAX_TIME_ZONE_ID = 1024; constexpr int32_t INVALID_TIME = -1; +constexpr int32_t OS_ERROR = 77856781; enum JsErrorCode : int32_t { ERROR_OK = 0, diff --git a/framework/js/napi/system_date_time/include/napi_system_date_time.h b/framework/js/napi/system_date_time/include/napi_system_date_time.h index d0aa8f98e21033bff3b563042fc4d281b5ea4233..b3a93a12f68e23b039eb2f3954601bd2e846c481 100644 --- a/framework/js/napi/system_date_time/include/napi_system_date_time.h +++ b/framework/js/napi/system_date_time/include/napi_system_date_time.h @@ -41,10 +41,13 @@ private: static napi_value GetTime(napi_env env, napi_callback_info info); static napi_value GetUptime(napi_env env, napi_callback_info info); static napi_value GetTimezoneSync(napi_env env, napi_callback_info info); + static napi_value GetAutoTimeStatus(napi_env env, napi_callback_info info); + static napi_value SetAutoTimeStatus(napi_env env, napi_callback_info info); static napi_value UpdateNtpTime(napi_env env, napi_callback_info info); static napi_value GetNtpTime(napi_env env, napi_callback_info info); static int32_t GetTimezone(std::string &timezone); + static int32_t GetAutoTime(bool &autoTime); static int32_t GetDeviceTime(clockid_t clockId, bool isNano, int64_t &time); static int32_t GetDeviceTime(bool isNano, int32_t timeType, int64_t &time); }; diff --git a/framework/js/napi/system_date_time/src/napi_system_date_time.cpp b/framework/js/napi/system_date_time/src/napi_system_date_time.cpp index bbb6e1b5a46afe2d5ef4087ed16fc75f114f8cde..4ed03f8b05c843b4ff44f2a466a07ab1a1fda22b 100644 --- a/framework/js/napi/system_date_time/src/napi_system_date_time.cpp +++ b/framework/js/napi/system_date_time/src/napi_system_date_time.cpp @@ -18,6 +18,7 @@ #include "parameters.h" #include "napi_work.h" #include "napi_utils.h" +#include "time_common.h" #include "time_service_client.h" using namespace OHOS::MiscServices; @@ -32,6 +33,7 @@ constexpr int64_t NANO_TO_MILLI = SECONDS_TO_NANO / SECONDS_TO_MILLI; constexpr int32_t STARTUP = 0; constexpr int32_t ACTIVE = 1; constexpr const char *TIMEZONE_KEY = "persist.time.timezone"; +constexpr const char *AUTOTIME_KEY = "persist.time.auto_time"; napi_value NapiSystemDateTime::SystemDateTimeInit(napi_env env, napi_value exports) { @@ -58,6 +60,8 @@ napi_value NapiSystemDateTime::SystemDateTimeInit(napi_env env, napi_value expor DECLARE_NAPI_STATIC_FUNCTION("getTimezone", GetTimezone), DECLARE_NAPI_STATIC_FUNCTION("getUptime", GetUptime), DECLARE_NAPI_STATIC_FUNCTION("getTimezoneSync", GetTimezoneSync), + DECLARE_NAPI_STATIC_FUNCTION("getAutoTimeStatus", GetAutoTimeStatus), + DECLARE_NAPI_STATIC_FUNCTION("setAutoTimeStatus", SetAutoTimeStatus), DECLARE_NAPI_STATIC_PROPERTY("TimeType", timeType), }; @@ -449,6 +453,62 @@ napi_value NapiSystemDateTime::GetTimezoneSync(napi_env env, napi_callback_info return NapiWork::SyncEnqueue(env, getTimezoneContext, "GetTimezone", executor, complete); } +napi_value NapiSystemDateTime::GetAutoTimeStatus(napi_env env, napi_callback_info info) +{ + struct GetAutoTimeContext : public ContextBase { + bool autotime; + }; + auto *getAutoTimeContext = new GetAutoTimeContext(); + auto inputParser = [env, getAutoTimeContext](size_t argc, napi_value *argv) { + getAutoTimeContext->status = napi_ok; + }; + getAutoTimeContext->GetCbInfo(env, info, inputParser, true); + auto executor = [getAutoTimeContext]() { + auto innerCode = GetAutoTime(getAutoTimeContext->autotime); + if (innerCode != JsErrorCode::ERROR_OK) { + getAutoTimeContext->errCode = innerCode; + getAutoTimeContext->status = napi_generic_failure; + } + }; + auto complete = [env, getAutoTimeContext](napi_value &output) { + getAutoTimeContext->status = + napi_get_boolean(env, getAutoTimeContext->autotime, &output); + CHECK_STATUS_RETURN_VOID(TIME_MODULE_JS_NAPI, getAutoTimeContext, + "convert native object to javascript object failed", JsErrorCode::ERROR); + }; + return NapiWork::SyncEnqueue(env, getAutoTimeContext, "GetAutoTimeStatus", executor, complete); +} + +napi_value NapiSystemDateTime::SetAutoTimeStatus(napi_env env, napi_callback_info info) +{ + struct SetAutoTimeContext : public ContextBase { + bool autotime; + }; + auto *setAutoTimeContext = new SetAutoTimeContext(); + auto inputParser = [env, setAutoTimeContext](size_t argc, napi_value *argv) { + CHECK_ARGS_RETURN_VOID(TIME_MODULE_JS_NAPI, setAutoTimeContext, argc >= ARGC_ONE, + "Mandatory parameters are left unspecified", JsErrorCode::PARAMETER_ERROR); + setAutoTimeContext->status = napi_get_value_bool(env, argv[ARGV_FIRST], &setAutoTimeContext->autotime); + CHECK_ARGS_RETURN_VOID(TIME_MODULE_JS_NAPI, setAutoTimeContext, setAutoTimeContext->status == napi_ok, + "The type of 'time' must be bool", JsErrorCode::PARAMETER_ERROR); + setAutoTimeContext->status = napi_ok; + }; + setAutoTimeContext->GetCbInfo(env, info, inputParser); + auto executor = [setAutoTimeContext]() { + auto innerCode = TimeServiceClient::GetInstance()->SetAutoTime(setAutoTimeContext->autotime); + if (innerCode != JsErrorCode::ERROR_OK) { + if (innerCode != E_TIME_NOT_SYSTEM_APP && innerCode != E_TIME_NO_PERMISSION) { + setAutoTimeContext->errCode = E_TIME_NTP_UPDATE_FAILED; + } else { + setAutoTimeContext->errCode = innerCode; + } + setAutoTimeContext->status = napi_generic_failure; + } + }; + auto complete = [env](napi_value &output) { output = NapiUtils::GetUndefinedValue(env); }; + return NapiWork::AsyncEnqueue(env, setAutoTimeContext, "SetAutoTimeStatus", executor, complete); +} + napi_value NapiSystemDateTime::UpdateNtpTime(napi_env env, napi_callback_info info) { struct UpdateNtpTime : public ContextBase { @@ -525,6 +585,13 @@ int32_t NapiSystemDateTime::GetTimezone(std::string &timezone) return ERROR_OK; } +int32_t NapiSystemDateTime::GetAutoTime(bool &autoTime) +{ + auto res = system::GetParameter(AUTOTIME_KEY, "ON"); + autoTime = (res == "ON"); + return ERROR_OK; +} + int32_t NapiSystemDateTime::GetDeviceTime(bool isNano, int32_t timeType, int64_t &time) { #ifdef IOS_PLATFORM diff --git a/interfaces/inner_api/include/time_service_client.h b/interfaces/inner_api/include/time_service_client.h index 3c352c0b18cc8bbbe9be9a380fc59c8406de3b3a..c8ed35e3feaaad6342722ed19e365fa77c118d13 100644 --- a/interfaces/inner_api/include/time_service_client.h +++ b/interfaces/inner_api/include/time_service_client.h @@ -62,6 +62,16 @@ public: */ TIME_API int32_t SetTimeV9(int64_t time); + /** + * @brief Set autotime status + * + * This api is used to set autotime status. + * + * @param status status of autotime. + * @return error code. + */ + TIME_API int32_t SetAutoTime(bool autoTime); + /** * @brief Set Timezone * diff --git a/interfaces/inner_api/src/time_service_client.cpp b/interfaces/inner_api/src/time_service_client.cpp index ea018cefcbf7366d7b1084a2923bc96da358c707..a3951c1a8e58ac5ae1bb8d715cc58399c37a1791 100644 --- a/interfaces/inner_api/src/time_service_client.cpp +++ b/interfaces/inner_api/src/time_service_client.cpp @@ -220,6 +220,20 @@ int32_t TimeServiceClient::SetTimeV9(int64_t time) return code; } +int32_t TimeServiceClient::SetAutoTime(bool autoTime) +{ + if (!ConnectService()) { + return E_TIME_SA_DIED; + } + auto proxy = GetProxy(); + if (proxy == nullptr) { + return E_TIME_NULLPTR; + } + int32_t code = proxy->SetAutoTime(autoTime); + code = ConvertErrCode(code); + return code; +} + bool TimeServiceClient::SetTimeZone(const std::string &timezoneId) { if (!ConnectService()) { diff --git a/services/ITimeService.idl b/services/ITimeService.idl index 2f9f05e0e57a9a819803a975e53f675f128f97f3..0c94a5db5b3eb33de6992b7228ef500554e70c0b 100644 --- a/services/ITimeService.idl +++ b/services/ITimeService.idl @@ -18,6 +18,7 @@ sequenceable OHOS.IRemoteObject; sequenceable OHOS.AbilityRuntime.WantAgent.WantAgent; interface OHOS.MiscServices.ITimeService { void SetTime([in] long time, [in] byte apiVersion); + void SetAutoTime([in] boolean autoTime); void SetTimeZone([in] String timezoneId, [in] byte apiVersion); void GetTimeZone([out] String timezoneId); void GetThreadTimeMs([out] long time); diff --git a/services/time_system_ability.cpp b/services/time_system_ability.cpp index 1fcb9f5e520ca3306fa5e817d22c4cc4239b56a4..0790c185f0706173d2dd8c23205505a82585f32c 100644 --- a/services/time_system_ability.cpp +++ b/services/time_system_ability.cpp @@ -59,10 +59,12 @@ static constexpr int32_t STR_MAX_LENGTH = 64; constexpr int32_t MILLI_TO_MICR = MICR_TO_BASE / MILLI_TO_BASE; constexpr int32_t NANO_TO_MILLI = NANO_TO_BASE / MILLI_TO_BASE; constexpr int32_t ONE_MILLI = 1000; +constexpr int AUTOTIME_OK = 0; static const std::vector ALL_DATA = { "timerId", "type", "flag", "windowLength", "interval", \ "uid", "bundleName", "wantAgent", "state", "triggerTime", \ "pid", "name"}; constexpr const char* BOOTEVENT_PARAMETER = "bootevent.boot.completed"; +constexpr const char* AUTOTIME_KEY = "persist.time.auto_time"; static constexpr int MAX_PID_LIST_SIZE = 1024; static constexpr uint32_t MAX_EXEMPTION_SIZE = 1000; @@ -821,6 +823,26 @@ int TimeSystemAbility::GetWallClockRtcId() return -1; } +int32_t TimeSystemAbility::SetAutoTime(bool autoTime) +{ + TimeXCollie timeXCollie("TimeService::SetAutoTime"); + if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) { + TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications"); + return E_TIME_NOT_SYSTEM_APP; + } + if (!TimePermission::CheckCallingPermission(TimePermission::setTime)) { + TIME_HILOGE(TIME_MODULE_SERVICE, "permission check setTime failed"); + return E_TIME_NO_PERMISSION; + } + auto errNo = SetParameter(AUTOTIME_KEY, autoTime ? "ON" : "OFF"); + if (errNo != AUTOTIME_OK) { + TIME_HILOGE(TIME_MODULE_SERVICE, "SetAutoTime FAIL,errNo: %{public}d", errNo); + return E_TIME_DEAL_FAILED; + } else { + return E_TIME_OK; + } +} + int32_t TimeSystemAbility::SetTimeZone(const std::string &timeZoneId, int8_t apiVersion) { TimeXCollie timeXCollie("TimeService::SetTimeZone"); diff --git a/services/time_system_ability.h b/services/time_system_ability.h index 542454700b7f915921b56028c8239ec6eaa15d7f..0049b5d05725d414ad68cffbeaddd751ab51d2c6 100644 --- a/services/time_system_ability.h +++ b/services/time_system_ability.h @@ -59,6 +59,7 @@ public: int32_t SetTime(int64_t time, int8_t apiVersion = APIVersion::API_VERSION_7) override; int32_t SetTimeInner(int64_t time, int8_t apiVersion = APIVersion::API_VERSION_7); bool SetRealTime(int64_t time); + int32_t SetAutoTime(bool autoTime) override; int32_t SetTimeZone(const std::string &timeZoneId, int8_t apiVersion = APIVersion::API_VERSION_7) override; int32_t SetTimeZoneInner(const std::string &timeZoneId, int8_t apiVersion = APIVersion::API_VERSION_7); int32_t GetTimeZone(std::string &timeZoneId) override; diff --git a/test/unittest/js_test/nopermission/acl/SystemDateTime.test.js b/test/unittest/js_test/nopermission/acl/SystemDateTime.test.js index 4800d66e1276cc166cab52b946e7073e913052d3..7b0c22c53a17e9668c2de658a0a6c8ddf893147b 100644 --- a/test/unittest/js_test/nopermission/acl/SystemDateTime.test.js +++ b/test/unittest/js_test/nopermission/acl/SystemDateTime.test.js @@ -110,4 +110,25 @@ describe('SystemDateTimeTest', function () { }) console.log('testSetTimezoneNoPermission002 end'); }) + + /** + * @tc.number: TestSetAutoTimeNoPermission001 + * @tc.name: TestSetAutoTimeNoPermission001 + * @tc.desc: Test setAutoTimeStatus without permission + * @tc.size: MediumTest + * @tc.type: Function + * @tc.level: Level 1 + * @tc.require: + */ + it('TestSetAutoTimeNoPermission001', 0, async function (done) { + systemDateTime.setAutoTimeStatus(true, (err) => { + if (err) { + expect(err.code).assertEqual(ACL_ERROR); + } else { + expect(false).assertTrue(); + } + done(); + }) + console.log('TestSetAutoTimeNoPermission001 end'); + }) }) \ No newline at end of file diff --git a/test/unittest/js_test/nopermission/systemapi/SystemDateTime.test.js b/test/unittest/js_test/nopermission/systemapi/SystemDateTime.test.js index a1b173cd1e2e920ede7c75a245f00853b4a3a97d..372d9f1eaab2616c9abc636e445737538756b740 100644 --- a/test/unittest/js_test/nopermission/systemapi/SystemDateTime.test.js +++ b/test/unittest/js_test/nopermission/systemapi/SystemDateTime.test.js @@ -152,4 +152,25 @@ describe('SystemDateTimeTest', function () { console.log('TestgetNtpTimeNoPermission001 end'); }) + /** + * @tc.number:TestSetAutoTimeNoPermission001 + * @tc.name: TestSetAutoTimeNoPermission001 + * @tc.desc: Test getNtpTime no permission + * @tc.size: MediumTest + * @tc.type: Function + * @tc.level: Level 1 + * @tc.require: + */ + it('TestSetAutoTimeNoPermission001', 0, async function (done) { + console.log("TestSetAutoTimeNoPermission001 start"); + systemDateTime.setAutoTimeStatus(true, (err) => { + if (err) { + expect(err.code).assertEqual(202); + } else { + expect(false).assertTrue(); + } + done(); + }) + console.log('TestSetAutoTimeNoPermission001 end'); + }) }) \ No newline at end of file diff --git a/test/unittest/js_test/permission/SystemDateTimeGet.test.js b/test/unittest/js_test/permission/SystemDateTimeGet.test.js index 823de5e4f5e720c8751adf4c95a5acb7700c21a5..faff69ab22aafaf8e93599c9dacf13259f5ebee5 100644 --- a/test/unittest/js_test/permission/SystemDateTimeGet.test.js +++ b/test/unittest/js_test/permission/SystemDateTimeGet.test.js @@ -853,4 +853,40 @@ describe('SystemDateTimeGetTest', function () { console.log('testUpdateAndGetNtpTime001 end'); done(); }) + + /** + * @tc.number: testGetAutoTime001 + * @tc.name: testGetAutoTime001 + * @tc.desc: test setAutoTimeStatus and getAutoTimeStatus. + * @tc.size: MediumTest + * @tc.type: Function + * @tc.level: Level 1 + * @tc.require: + */ + it('testGetAutoTime001', 0, async function (done) { + console.log("testGetAutoTime001 start"); + await systemDateTime.setAutoTimeStatus(false); + const autoTimeStatus = systemDateTime.getAutoTimeStatus(); + expect(typeof (autoTimeStatus) == 'boolean' && autoTimeStatus === false).assertTrue(); + done(); + console.log('testGetAutoTime001 end'); + }) + + /** + * @tc.number: testGetAutoTime001 + * @tc.name: testGetAutoTime001 + * @tc.desc: test setAutoTimeStatus and getAutoTimeStatus. + * @tc.size: MediumTest + * @tc.type: Function + * @tc.level: Level 1 + * @tc.require: + */ + it('testGetAutoTime002', 0, async function (done) { + console.log("testGetAutoTime002 start"); + await systemDateTime.setAutoTimeStatus(true); + const autoTimeStatus = systemDateTime.getAutoTimeStatus(); + expect(typeof (autoTimeStatus) == 'boolean' && autoTimeStatus === true).assertTrue(); + done(); + console.log('testGetAutoTime002 end'); + }) }) \ No newline at end of file diff --git a/test/unittest/js_test/permission/SystemDateTimeSet.test.js b/test/unittest/js_test/permission/SystemDateTimeSet.test.js index b67d61d82b9813d83f390414a772b2a17d3b0a5c..6bb3f8095473eb179806f900cda50b92653903e4 100644 --- a/test/unittest/js_test/permission/SystemDateTimeSet.test.js +++ b/test/unittest/js_test/permission/SystemDateTimeSet.test.js @@ -417,49 +417,111 @@ describe("SystemDateTimeSetTest", function () { * @tc.level: Level 1 * @tc.require: */ - it("TestSetTimezonenonsupport005", 0, async function (done) { - console.log("TestSetTimezonenonsupport005 start"); - try { - systemDateTime.setTimezone('Asia/Hangzhou').then(() => { - expect(false).assertTrue(); - done(); - }).catch((err) => { - expect(true).assertTrue(); - done(); - }) - } catch (err) { + it("TestSetTimezonenonsupport005", 0, async function (done) { + console.log("TestSetTimezonenonsupport005 start"); + try { + systemDateTime.setTimezone('Asia/Hangzhou').then(() => { + expect(false).assertTrue(); + done(); + }).catch((err) => { expect(true).assertTrue(); done(); - } - console.log("TestSetTimezonenonsupport005 end"); - }); + }) + } catch (err) { + expect(true).assertTrue(); + done(); + } + console.log("TestSetTimezonenonsupport005 end"); + }); - /** - * @tc.number: TestSetTimezonenonsupport006 - * @tc.name: TestSetTimezonenonsupport006 - * @tc.desc: Test setDate for callback with nonsupport zone. - * @tc.size: MediumTest - * @tc.type: Function - * @tc.level: Level 1 - * @tc.require: - */ - it("TestSetTimezonenonsupport006", 0, async function (done) { - console.log("TestSetTimezonenonsupport006 start"); - try { - systemDateTime.setTimezone('Asia/Hangzhou', (err) => { - if (err) { - expect(true).assertTrue(); - console.log(err.code); - } else { - expect(false).assertTrue(); - } - done(); - }); - } catch (err) { - expect(true).assertTrue(); + /** + * @tc.number: TestSetTimezonenonsupport006 + * @tc.name: TestSetTimezonenonsupport006 + * @tc.desc: Test setDate for callback with nonsupport zone. + * @tc.size: MediumTest + * @tc.type: Function + * @tc.level: Level 1 + * @tc.require: + */ + it("TestSetTimezonenonsupport006", 0, async function (done) { + console.log("TestSetTimezonenonsupport006 start"); + try { + systemDateTime.setTimezone('Asia/Hangzhou', (err) => { + if (err) { + expect(true).assertTrue(); + console.log(err.code); + } else { + expect(false).assertTrue(); + } done(); - } - console.log("TestSetTimezonenonsupport006 end"); - }); + }); + } catch (err) { + expect(true).assertTrue(); + done(); + } + console.log("TestSetTimezonenonsupport006 end"); + }); + /** + * @tc.number: testSetAutoTime001 + * @tc.name: testSetAutoTime001 + * @tc.desc: test setAutoTimeStatus and getAutoTimeStatus. + * @tc.size: MediumTest + * @tc.type: Function + * @tc.level: Level 1 + * @tc.require: + */ + it('testSetAutoTime001', 0, async function (done) { + console.log("testSetAutoTime001 start"); + await systemDateTime.setAutoTimeStatus(false); + const autoTimeStatus = systemDateTime.getAutoTimeStatus(); + expect(!autoTimeStatus).assertTrue(); + done(); + console.log('testGetAutoTime001 end'); + }) + + /** + * @tc.number: testSetAutoTime002 + * @tc.name: testSetAutoTime002 + * @tc.desc: test setAutoTimeStatus and getAutoTimeStatus. + * @tc.size: MediumTest + * @tc.type: Function + * @tc.level: Level 1 + * @tc.require: + */ + it('testSetAutoTime002', 0, async function (done) { + console.log("testSetAutoTime002 start"); + await systemDateTime.setAutoTimeStatus(true); + const autoTimeStatus = systemDateTime.getAutoTimeStatus(); + expect(autoTimeStatus).assertTrue(); + done(); + console.log('testSetAutoTime002 end'); + }) + + /** + * @tc.number: testSetAutoTime003 + * @tc.name: testSetAutoTime003 + * @tc.desc: test setAutoTimeStatus. + * @tc.size: MediumTest + * @tc.type: Function + * @tc.level: Level 1 + * @tc.require: + */ + it("testSetAutoTime003", 0, async function (done) { + console.log("testSetAutoTime003 start"); + try { + systemDateTime.setAutoTimeStatus(null, (err) => { + if (err) { + expect(err.code).assertEqual(401); + } else { + expect(false).assertTrue(); + } + done(); + }); + } catch (err) { + expect(err.code).assertEqual(401); + done(); + } + console.log('testSetAutoTime003 end') + }) }) \ No newline at end of file diff --git a/test/unittest/service_test/src/time_client_test.cpp b/test/unittest/service_test/src/time_client_test.cpp index 2b9ec9ac6ecf40881db7f1bc9c783cc4fb373a28..d3e194c9659c7a6509edfe7a4eadd7b4d7db7e58 100644 --- a/test/unittest/service_test/src/time_client_test.cpp +++ b/test/unittest/service_test/src/time_client_test.cpp @@ -1213,4 +1213,17 @@ HWTEST_F(TimeClientTest, ReBatchAllTimers001, TestSize.Level1) EXPECT_EQ(g_data1, 0); TimeServiceClient::GetInstance()->DestroyTimerV9(timerId); } + +/** +* @tc.name: SetAutoTime001 +* @tc.desc: test SetAutoTime. +* @tc.type: FUNC +*/ +HWTEST_F(TimeClientTest, SetAutoTime001, TestSize.Level1) +{ + auto res = TimeServiceClient::GetInstance()->SetAutoTime(true); + EXPECT_EQ(res, 0); + res = TimeServiceClient::GetInstance()->SetAutoTime(false); + EXPECT_EQ(res, 0); +} } // namespace \ No newline at end of file diff --git a/test/unittest/service_test/src/time_service_time_test.cpp b/test/unittest/service_test/src/time_service_time_test.cpp index 8c7d44280d91d5d484e35f6b342257906dffc495..67573d18885fbeccbce6f42549b9af06de4851f9 100644 --- a/test/unittest/service_test/src/time_service_time_test.cpp +++ b/test/unittest/service_test/src/time_service_time_test.cpp @@ -121,6 +121,49 @@ static HapPolicyParams g_policyA = { } }; +static HapPolicyParams g_policyC = { + .apl = APL_SYSTEM_CORE, + .domain = "test.domain", + .permList = { + { + .permissionName = "ohos.permission.SET_TIME_ZONE", + .bundleName = "ohos.permission_test.demoB", + .grantMode = 1, + .availableLevel = APL_NORMAL, + .label = "label", + .labelId = 1, + .description = "test", + .descriptionId = 1 + }, + { + .permissionName = "ohos.permission.MANAGE_LOCAL_ACCOUNTS", + .bundleName = "ohos.permission_test.demoB", + .grantMode = 1, + .availableLevel = APL_NORMAL, + .label = "label", + .labelId = 1, + .description = "test", + .descriptionId = 1 + } + }, + .permStateList = { + { + .permissionName = "ohos.permission.SET_TIME_ZONE", + .isGeneral = true, + .resDeviceID = { "local" }, + .grantStatus = { PermissionState::PERMISSION_GRANTED }, + .grantFlags = { 1 } + }, + { + .permissionName = "ohos.permission.MANAGE_LOCAL_ACCOUNTS", + .isGeneral = true, + .resDeviceID = { "local" }, + .grantStatus = { PermissionState::PERMISSION_GRANTED }, + .grantFlags = { 1 } + } + } +}; + static HapInfoParams g_systemInfoParams = { .userID = 1, .bundleName = "timer", @@ -806,6 +849,20 @@ HWTEST_F(TimeServiceTimeTest, NtpUpdateTime002, TestSize.Level0) EXPECT_EQ(NtpUpdateTime::ntpRetryInterval_, MAX_NTP_RETRY_INTERVAL); } +/** +* @tc.name: SetAutoTime001 +* @tc.desc: test SetAutoTime. +* @tc.type: FUNC +*/ +HWTEST_F(TimeServiceTimeTest, SetAutoTime001, TestSize.Level0) +{ + AddPermission(); + auto res = TimeSystemAbility::GetInstance()->SetAutoTime(true); + EXPECT_EQ(res, E_TIME_OK); + res = TimeSystemAbility::GetInstance()->SetAutoTime(false); + EXPECT_EQ(res, E_TIME_OK); +} + /** * @tc.name: GetNtpTimeMsWithNoPermission001 * @tc.desc: test GetNtpTimeMs with no permission. @@ -831,4 +888,32 @@ HWTEST_F(TimeServiceTimeTest, GetRealTimeMsWithNoPermission001, TestSize.Level0) auto res = TimeSystemAbility::GetInstance()->GetRealTimeMs(time); EXPECT_EQ(res, E_TIME_NOT_SYSTEM_APP); } + +/** +* @tc.name: SetAutoTimeWithNoPermission001 +* @tc.desc: test SetAutoTime with no permission. +* @tc.type: FUNC +*/ +HWTEST_F(TimeServiceTimeTest, SetAutoTimeWithNoPermission001, TestSize.Level0) +{ + DeletePermission(); + auto res = TimeSystemAbility::GetInstance()->SetAutoTime(true); + EXPECT_EQ(res, E_TIME_NOT_SYSTEM_APP); +} + +/** +* @tc.name: SetAutoTimeWithNoPermission002 +* @tc.desc: test SetAutoTime with no permission. +* @tc.type: FUNC +*/ +HWTEST_F(TimeServiceTimeTest, SetAutoTimeWithNoPermission002, TestSize.Level0) +{ + DeletePermission(); + AccessTokenIDEx tokenIdEx = { 0 }; + tokenIdEx = AccessTokenKit::AllocHapToken(g_systemInfoParams, g_policyC); + SetSelfTokenID(tokenIdEx.tokenIDEx); + auto res = TimeSystemAbility::GetInstance()->SetAutoTime(true); + EXPECT_EQ(res, E_TIME_NO_PERMISSION); +} + } // namespace \ No newline at end of file