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 #include <chrono>
16 #include <cstdio>
17 #include <cstdlib>
18 #include <iostream>
19 #include <memory>
20 #include <string>
21 #include <unistd.h>
22 #include "accesstoken_kit.h"
23 
24 using namespace std;
25 using namespace OHOS::Security::AccessToken;
26 
PrintCurrentTime()27 void PrintCurrentTime()
28 {
29     std::chrono::milliseconds ms = std::chrono::duration_cast<std::chrono::milliseconds>(
30         std::chrono::system_clock::now().time_since_epoch()
31     );
32 
33     int64_t timestampMs = ms.count();
34     time_t timestampS = static_cast<time_t>(timestampMs / 1000);
35     struct tm t = {0};
36     // localtime is not thread safe, localtime_r first param unit is second, timestamp unit is ms, so divided by 1000
37     localtime_r(&timestampS, &t);
38 
39     std::cout << "[" << t.tm_hour << ":" << t.tm_min << ":" << t.tm_sec << "] ";
40 }
41 
main(int argc, char *argv[])42 int32_t main(int argc, char *argv[])
43 {
44     if (argc < 4) { // 4: size
45         std::cout << "Help: ./VerifyAccessToken tokenid permisisionName\n" << std::endl;
46         return 0;
47     }
48 
49     uint32_t tokenId = static_cast<uint32_t>(atoi(argv[1])); // 1: index
50     std::string permisisionName = argv[2]; // 2: index
51     uint32_t count = static_cast<uint32_t>(atoi(argv[3])); // 3: index
52     uint32_t i = 0;
53     while (i < count) {
54         int32_t status = AccessTokenKit::VerifyAccessToken(tokenId, permisisionName);
55         PrintCurrentTime();
56         std::cout << "tokenId: " << tokenId << ", perm: " << permisisionName << ", status: " << status << std::endl;
57         i++;
58         sleep(1);
59     }
60     return 0;
61 }
62