13298bea7Sopenharmony_ci/* 23298bea7Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 33298bea7Sopenharmony_ci * SPDX-License-Identifier: GPL-2.0 43298bea7Sopenharmony_ci * 53298bea7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 63298bea7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 73298bea7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 83298bea7Sopenharmony_ci * See the License for the specific language governing permissions and 93298bea7Sopenharmony_ci * limitations under the License. 103298bea7Sopenharmony_ci */ 113298bea7Sopenharmony_ci 123298bea7Sopenharmony_ci#include "accesstokenid_test.h" 133298bea7Sopenharmony_ci#include <cstdio> 143298bea7Sopenharmony_ci#include <cstdlib> 153298bea7Sopenharmony_ci#include <fcntl.h> 163298bea7Sopenharmony_ci#include <cerrno> 173298bea7Sopenharmony_ci#include <unistd.h> 183298bea7Sopenharmony_ci#include <sys/types.h> 193298bea7Sopenharmony_ci#include <sys/stat.h> 203298bea7Sopenharmony_ci#include <sys/mman.h> 213298bea7Sopenharmony_ci#include <sys/wait.h> 223298bea7Sopenharmony_ci#include <sys/ioctl.h> 233298bea7Sopenharmony_ci#include <ctime> 243298bea7Sopenharmony_ci#include <climits> 253298bea7Sopenharmony_ci#include <pthread.h> 263298bea7Sopenharmony_ci#include <sys/syscall.h> 273298bea7Sopenharmony_ci#include <grp.h> 283298bea7Sopenharmony_ci 293298bea7Sopenharmony_ciconstexpr unsigned char ACCESS_TOKEN_ID_IOCTL_BASE = 'A'; 303298bea7Sopenharmony_ciconstexpr unsigned int GET_TOKEN_ID = 1; 313298bea7Sopenharmony_ciconstexpr unsigned int SET_TOKEN_ID = 2; 323298bea7Sopenharmony_ciconstexpr unsigned int GET_FTOKEN_ID = 3; 333298bea7Sopenharmony_ciconstexpr unsigned int SET_FTOKEN_ID = 4; 343298bea7Sopenharmony_ciconstexpr unsigned int ACCESS_TOKENID_MAX_NR = 5; 353298bea7Sopenharmony_ci#define ACCESS_TOKENID_GET_TOKENID \ 363298bea7Sopenharmony_ci _IOR(ACCESS_TOKEN_ID_IOCTL_BASE, GET_TOKEN_ID, unsigned long long) 373298bea7Sopenharmony_ci#define ACCESS_TOKENID_SET_TOKENID \ 383298bea7Sopenharmony_ci _IOW(ACCESS_TOKEN_ID_IOCTL_BASE, SET_TOKEN_ID, unsigned long long) 393298bea7Sopenharmony_ci#define ACCESS_TOKENID_GET_FTOKENID \ 403298bea7Sopenharmony_ci _IOR(ACCESS_TOKEN_ID_IOCTL_BASE, GET_FTOKEN_ID, unsigned long long) 413298bea7Sopenharmony_ci#define ACCESS_TOKENID_SET_FTOKENID \ 423298bea7Sopenharmony_ci _IOW(ACCESS_TOKEN_ID_IOCTL_BASE, SET_FTOKEN_ID, unsigned long long) 433298bea7Sopenharmony_ci#define ACCESS_TOKENID_ILLEGAL1 \ 443298bea7Sopenharmony_ci _IOW(ACCESS_TOKEN_ID_IOCTL_BASE, 0, unsigned long long) 453298bea7Sopenharmony_ci#define ACCESS_TOKENID_ILLEGAL2 \ 463298bea7Sopenharmony_ci _IOW(ACCESS_TOKEN_ID_IOCTL_BASE, ACCESS_TOKENID_MAX_NR, unsigned long long) 473298bea7Sopenharmony_ci 483298bea7Sopenharmony_ciconstexpr unsigned long long INVAL_TOKEN = 0xffffffffffffffff; 493298bea7Sopenharmony_ci 503298bea7Sopenharmony_ci#define CHILDREN_NUM 3 513298bea7Sopenharmony_ci#define WAIT_FOR_SHELL_OP_TIME 1 523298bea7Sopenharmony_ci#define FATHER_WAIT_TIME (WAIT_FOR_SHELL_OP_TIME * (CHILDREN_NUM + 1)) 533298bea7Sopenharmony_ci 543298bea7Sopenharmony_ciconstexpr unsigned int ACCESS_TOKEN_GRPID = 3020; 553298bea7Sopenharmony_ciconstexpr unsigned int ACCESS_TOKEN_OTHER_UID = 1234; 563298bea7Sopenharmony_ciconstexpr unsigned int ACCESS_TOKEN_OTHER_GRPID = 1234; 573298bea7Sopenharmony_ci 583298bea7Sopenharmony_ciconst char DEV_ACCESSTOKENID[] = "/dev/access_token_id"; 593298bea7Sopenharmony_ci 603298bea7Sopenharmony_cistruct Tokeninfo { 613298bea7Sopenharmony_ci pid_t pid; 623298bea7Sopenharmony_ci pid_t tid; 633298bea7Sopenharmony_ci unsigned long long tokenid; 643298bea7Sopenharmony_ci unsigned long long ftokenid; 653298bea7Sopenharmony_ci}; 663298bea7Sopenharmony_ci 673298bea7Sopenharmony_cinamespace { 683298bea7Sopenharmony_cistatic unsigned long long GenRand64(void) 693298bea7Sopenharmony_ci{ 703298bea7Sopenharmony_ci struct timespec time = {0, 0}; 713298bea7Sopenharmony_ci unsigned long long randvalue = 0; 723298bea7Sopenharmony_ci int fd; 733298bea7Sopenharmony_ci 743298bea7Sopenharmony_ci fd = open("/dev/random", O_RDONLY); 753298bea7Sopenharmony_ci if (fd > 0) { 763298bea7Sopenharmony_ci read(fd, &randvalue, sizeof(unsigned long long)); 773298bea7Sopenharmony_ci } 783298bea7Sopenharmony_ci close(fd); 793298bea7Sopenharmony_ci 803298bea7Sopenharmony_ci sleep(1); 813298bea7Sopenharmony_ci clock_gettime(CLOCK_REALTIME, &time); 823298bea7Sopenharmony_ci 833298bea7Sopenharmony_ci return randvalue + time.tv_nsec; 843298bea7Sopenharmony_ci} 853298bea7Sopenharmony_ci 863298bea7Sopenharmony_cistatic int GetTokenid(unsigned long long *token) 873298bea7Sopenharmony_ci{ 883298bea7Sopenharmony_ci int fd = open(DEV_ACCESSTOKENID, O_RDWR); 893298bea7Sopenharmony_ci if (fd < 0) { 903298bea7Sopenharmony_ci printf("open %s failed\r\n", DEV_ACCESSTOKENID); 913298bea7Sopenharmony_ci return -1; 923298bea7Sopenharmony_ci } 933298bea7Sopenharmony_ci 943298bea7Sopenharmony_ci int ret = ioctl(fd, ACCESS_TOKENID_GET_TOKENID, token); 953298bea7Sopenharmony_ci if (ret) { 963298bea7Sopenharmony_ci printf("ioctl ACCESS_TOKENID_GET_TOKENID failed\r\n"); 973298bea7Sopenharmony_ci close(fd); 983298bea7Sopenharmony_ci return -1; 993298bea7Sopenharmony_ci } 1003298bea7Sopenharmony_ci 1013298bea7Sopenharmony_ci close(fd); 1023298bea7Sopenharmony_ci return 0; 1033298bea7Sopenharmony_ci} 1043298bea7Sopenharmony_ci 1053298bea7Sopenharmony_cistatic int SetTokenid(unsigned long long *token) 1063298bea7Sopenharmony_ci{ 1073298bea7Sopenharmony_ci int fd = open(DEV_ACCESSTOKENID, O_RDWR); 1083298bea7Sopenharmony_ci if (fd < 0) { 1093298bea7Sopenharmony_ci printf("open %s failed\r\n", DEV_ACCESSTOKENID); 1103298bea7Sopenharmony_ci return -1; 1113298bea7Sopenharmony_ci } 1123298bea7Sopenharmony_ci 1133298bea7Sopenharmony_ci int ret = ioctl(fd, ACCESS_TOKENID_SET_TOKENID, token); 1143298bea7Sopenharmony_ci if (ret) { 1153298bea7Sopenharmony_ci printf("ioctl ACCESS_TOKENID_SET_TOKENID failed\r\n"); 1163298bea7Sopenharmony_ci close(fd); 1173298bea7Sopenharmony_ci return -1; 1183298bea7Sopenharmony_ci } 1193298bea7Sopenharmony_ci 1203298bea7Sopenharmony_ci close(fd); 1213298bea7Sopenharmony_ci return 0; 1223298bea7Sopenharmony_ci} 1233298bea7Sopenharmony_ci 1243298bea7Sopenharmony_cistatic int GetfTokenid(unsigned long long *ftoken) 1253298bea7Sopenharmony_ci{ 1263298bea7Sopenharmony_ci int fd = open(DEV_ACCESSTOKENID, O_RDWR); 1273298bea7Sopenharmony_ci if (fd < 0) { 1283298bea7Sopenharmony_ci printf("open %s failed\r\n", DEV_ACCESSTOKENID); 1293298bea7Sopenharmony_ci return -1; 1303298bea7Sopenharmony_ci } 1313298bea7Sopenharmony_ci 1323298bea7Sopenharmony_ci int ret = ioctl(fd, ACCESS_TOKENID_GET_FTOKENID, ftoken); 1333298bea7Sopenharmony_ci if (ret) { 1343298bea7Sopenharmony_ci printf("ioctl ACCESS_TOKENID_GET_FTOKENID failed\r\n"); 1353298bea7Sopenharmony_ci close(fd); 1363298bea7Sopenharmony_ci return -1; 1373298bea7Sopenharmony_ci } 1383298bea7Sopenharmony_ci 1393298bea7Sopenharmony_ci close(fd); 1403298bea7Sopenharmony_ci return 0; 1413298bea7Sopenharmony_ci} 1423298bea7Sopenharmony_ci 1433298bea7Sopenharmony_cistatic int SetfTokenid(unsigned long long *ftoken) 1443298bea7Sopenharmony_ci{ 1453298bea7Sopenharmony_ci int fd = open(DEV_ACCESSTOKENID, O_RDWR); 1463298bea7Sopenharmony_ci if (fd < 0) { 1473298bea7Sopenharmony_ci printf("open %s failed\r\n", DEV_ACCESSTOKENID); 1483298bea7Sopenharmony_ci return -1; 1493298bea7Sopenharmony_ci } 1503298bea7Sopenharmony_ci 1513298bea7Sopenharmony_ci int ret = ioctl(fd, ACCESS_TOKENID_SET_FTOKENID, ftoken); 1523298bea7Sopenharmony_ci if (ret) { 1533298bea7Sopenharmony_ci printf("ioctl ACCESS_TOKENID_SET_FTOKENID failed\r\n"); 1543298bea7Sopenharmony_ci close(fd); 1553298bea7Sopenharmony_ci return -1; 1563298bea7Sopenharmony_ci } 1573298bea7Sopenharmony_ci 1583298bea7Sopenharmony_ci close(fd); 1593298bea7Sopenharmony_ci return 0; 1603298bea7Sopenharmony_ci} 1613298bea7Sopenharmony_ci 1623298bea7Sopenharmony_cistatic void GetCurToken(unsigned long long *token, unsigned long long *ftoken) 1633298bea7Sopenharmony_ci{ 1643298bea7Sopenharmony_ci GetTokenid(token); 1653298bea7Sopenharmony_ci GetfTokenid(ftoken); 1663298bea7Sopenharmony_ci 1673298bea7Sopenharmony_ci return; 1683298bea7Sopenharmony_ci} 1693298bea7Sopenharmony_ci 1703298bea7Sopenharmony_cistatic void *CheckChildThreadInheritance(void *args) 1713298bea7Sopenharmony_ci{ 1723298bea7Sopenharmony_ci struct Tokeninfo *tinfo = static_cast<struct Tokeninfo *>(args); 1733298bea7Sopenharmony_ci 1743298bea7Sopenharmony_ci tinfo->pid = getpid(); 1753298bea7Sopenharmony_ci tinfo->tid = gettid(); 1763298bea7Sopenharmony_ci GetTokenid(&(tinfo->tokenid)); 1773298bea7Sopenharmony_ci GetfTokenid(&(tinfo->ftokenid)); 1783298bea7Sopenharmony_ci 1793298bea7Sopenharmony_ci pthread_exit(nullptr); 1803298bea7Sopenharmony_ci return nullptr; 1813298bea7Sopenharmony_ci} 1823298bea7Sopenharmony_ci 1833298bea7Sopenharmony_cistatic void *CheckChildThreadSetIndepent(void *args) 1843298bea7Sopenharmony_ci{ 1853298bea7Sopenharmony_ci struct Tokeninfo *tinfo = static_cast<struct Tokeninfo *>(args); 1863298bea7Sopenharmony_ci unsigned long long tokenSet = GenRand64(); 1873298bea7Sopenharmony_ci unsigned long long ftokenSet = GenRand64(); 1883298bea7Sopenharmony_ci unsigned long long tokenidGet = INVAL_TOKEN; 1893298bea7Sopenharmony_ci unsigned long long ftokenidGet = INVAL_TOKEN; 1903298bea7Sopenharmony_ci 1913298bea7Sopenharmony_ci tinfo->pid = getpid(); 1923298bea7Sopenharmony_ci tinfo->tid = gettid(); 1933298bea7Sopenharmony_ci 1943298bea7Sopenharmony_ci GetTokenid(&tokenidGet); 1953298bea7Sopenharmony_ci GetfTokenid(&ftokenidGet); 1963298bea7Sopenharmony_ci SetTokenid(&tokenSet); 1973298bea7Sopenharmony_ci SetfTokenid(&ftokenSet); 1983298bea7Sopenharmony_ci GetTokenid(&(tinfo->tokenid)); 1993298bea7Sopenharmony_ci GetfTokenid(&(tinfo->ftokenid)); 2003298bea7Sopenharmony_ci 2013298bea7Sopenharmony_ci /* Indicate that the tokenid setting of each child thread does not met requirements. */ 2023298bea7Sopenharmony_ci if (ftokenidGet == 0 && tinfo->tokenid == tokenSet && tinfo->ftokenid == ftokenSet && tinfo->ftokenid != 0) { 2033298bea7Sopenharmony_ci tinfo->ftokenid = INVAL_TOKEN; 2043298bea7Sopenharmony_ci } 2053298bea7Sopenharmony_ci 2063298bea7Sopenharmony_ci pthread_exit(nullptr); 2073298bea7Sopenharmony_ci return nullptr; 2083298bea7Sopenharmony_ci} 2093298bea7Sopenharmony_ci} 2103298bea7Sopenharmony_ci 2113298bea7Sopenharmony_ciusing namespace testing::ext; 2123298bea7Sopenharmony_ciusing namespace std; 2133298bea7Sopenharmony_ci 2143298bea7Sopenharmony_civoid AccesstokenidTest::SetUp() {} 2153298bea7Sopenharmony_ci 2163298bea7Sopenharmony_civoid AccesstokenidTest::TearDown() {} 2173298bea7Sopenharmony_ci 2183298bea7Sopenharmony_civoid AccesstokenidTest::SetUpTestCase() {} 2193298bea7Sopenharmony_ci 2203298bea7Sopenharmony_civoid AccesstokenidTest::TearDownTestCase() {} 2213298bea7Sopenharmony_ci 2223298bea7Sopenharmony_ci/** 2233298bea7Sopenharmony_ci * @tc.name: CheckInitToken 2243298bea7Sopenharmony_ci * @tc.desc: Test init value of tokenid and ftokenid 2253298bea7Sopenharmony_ci * @tc.desc: tokenid equals to the father(hdcd) and ftokenid equals to 0 2263298bea7Sopenharmony_ci * @tc.type: FUNC 2273298bea7Sopenharmony_ci */ 2283298bea7Sopenharmony_ciHWTEST_F(AccesstokenidTest, CheckInitToken, Function | MediumTest | Level1) 2293298bea7Sopenharmony_ci{ 2303298bea7Sopenharmony_ci unsigned long long token = INVAL_TOKEN; 2313298bea7Sopenharmony_ci unsigned long long ftoken = INVAL_TOKEN; 2323298bea7Sopenharmony_ci 2333298bea7Sopenharmony_ci GetCurToken(&token, &ftoken); 2343298bea7Sopenharmony_ci 2353298bea7Sopenharmony_ci /* /data/service/el0/access_token/nativetoken.json 2363298bea7Sopenharmony_ci {"processName":"hdcd","APL":3,"version":1,"tokenId":680034571,"tokenAttr":0,"dcaps":[]} 2373298bea7Sopenharmony_ci */ 2383298bea7Sopenharmony_ci ASSERT_NE(0, token); 2393298bea7Sopenharmony_ci ASSERT_EQ(0, ftoken); 2403298bea7Sopenharmony_ci} 2413298bea7Sopenharmony_ci 2423298bea7Sopenharmony_ci/** 2433298bea7Sopenharmony_ci * @tc.name: CheckSetTokenid 2443298bea7Sopenharmony_ci * @tc.desc: Test setting of tokenid 2453298bea7Sopenharmony_ci * @tc.desc: tokenid equals to the setting value 2463298bea7Sopenharmony_ci * @tc.type: FUNC 2473298bea7Sopenharmony_ci */ 2483298bea7Sopenharmony_ciHWTEST_F(AccesstokenidTest, CheckSetTokenid, Function | MediumTest | Level1) 2493298bea7Sopenharmony_ci{ 2503298bea7Sopenharmony_ci unsigned long long token = INVAL_TOKEN; 2513298bea7Sopenharmony_ci unsigned long long tokenSet = GenRand64(); 2523298bea7Sopenharmony_ci 2533298bea7Sopenharmony_ci SetTokenid(&tokenSet); 2543298bea7Sopenharmony_ci GetTokenid(&token); 2553298bea7Sopenharmony_ci 2563298bea7Sopenharmony_ci ASSERT_EQ(tokenSet, token); 2573298bea7Sopenharmony_ci} 2583298bea7Sopenharmony_ci 2593298bea7Sopenharmony_ci/** 2603298bea7Sopenharmony_ci * @tc.name: CheckSetfTokenid 2613298bea7Sopenharmony_ci * @tc.desc: Test setting of ftokenid 2623298bea7Sopenharmony_ci * @tc.desc: ftokenid equals to the setting value 2633298bea7Sopenharmony_ci * @tc.type: FUNC 2643298bea7Sopenharmony_ci */ 2653298bea7Sopenharmony_ciHWTEST_F(AccesstokenidTest, CheckSetfTokenid, Function | MediumTest | Level1) 2663298bea7Sopenharmony_ci{ 2673298bea7Sopenharmony_ci unsigned long long ftoken = INVAL_TOKEN; 2683298bea7Sopenharmony_ci unsigned long long ftokenSet = GenRand64(); 2693298bea7Sopenharmony_ci 2703298bea7Sopenharmony_ci SetfTokenid(&ftokenSet); 2713298bea7Sopenharmony_ci GetfTokenid(&ftoken); 2723298bea7Sopenharmony_ci 2733298bea7Sopenharmony_ci ASSERT_EQ(ftokenSet, ftoken); 2743298bea7Sopenharmony_ci} 2753298bea7Sopenharmony_ci 2763298bea7Sopenharmony_ci/** 2773298bea7Sopenharmony_ci * @tc.name: CheckChildThreadInheritance 2783298bea7Sopenharmony_ci * @tc.desc: Test each child thread tokenid equals to father process while ftokenid not equals 2793298bea7Sopenharmony_ci * @tc.desc: The ftokenid of each child thread equals to 0 2803298bea7Sopenharmony_ci * @tc.type: FUNC 2813298bea7Sopenharmony_ci */ 2823298bea7Sopenharmony_ciHWTEST_F(AccesstokenidTest, CheckChildThreadInheritance, Function | MediumTest | Level1) 2833298bea7Sopenharmony_ci{ 2843298bea7Sopenharmony_ci pthread_t cid[10]; 2853298bea7Sopenharmony_ci 2863298bea7Sopenharmony_ci unsigned long long token = INVAL_TOKEN; 2873298bea7Sopenharmony_ci unsigned long long ftoken = INVAL_TOKEN; 2883298bea7Sopenharmony_ci unsigned long long tokenSet = GenRand64(); 2893298bea7Sopenharmony_ci unsigned long long ftokenSet = GenRand64(); 2903298bea7Sopenharmony_ci 2913298bea7Sopenharmony_ci struct Tokeninfo tinfo; 2923298bea7Sopenharmony_ci tinfo.pid = getpid(); 2933298bea7Sopenharmony_ci tinfo.tid = gettid(); 2943298bea7Sopenharmony_ci tinfo.tokenid = INVAL_TOKEN; 2953298bea7Sopenharmony_ci tinfo.ftokenid = INVAL_TOKEN; 2963298bea7Sopenharmony_ci 2973298bea7Sopenharmony_ci GetTokenid(&token); 2983298bea7Sopenharmony_ci GetfTokenid(&ftoken); 2993298bea7Sopenharmony_ci 3003298bea7Sopenharmony_ci SetTokenid(&tokenSet); 3013298bea7Sopenharmony_ci SetfTokenid(&ftokenSet); 3023298bea7Sopenharmony_ci 3033298bea7Sopenharmony_ci for (int i = 0; i < 10; i++) { 3043298bea7Sopenharmony_ci if (pthread_create(&cid[i], nullptr, CheckChildThreadInheritance, &tinfo) != 0) { 3053298bea7Sopenharmony_ci printf("thread %d (ID %ld) pthread_create error\n", i, cid[i]); 3063298bea7Sopenharmony_ci } 3073298bea7Sopenharmony_ci 3083298bea7Sopenharmony_ci if (pthread_join(cid[i], nullptr) != 0) { 3093298bea7Sopenharmony_ci printf("thread %d (ID %ld) pthread_join error\n", i, cid[i]); 3103298bea7Sopenharmony_ci } 3113298bea7Sopenharmony_ci 3123298bea7Sopenharmony_ci ASSERT_EQ(tinfo.tokenid, tokenSet); 3133298bea7Sopenharmony_ci ASSERT_NE(tinfo.ftokenid, ftokenSet); 3143298bea7Sopenharmony_ci ASSERT_EQ(0, tinfo.ftokenid); 3153298bea7Sopenharmony_ci } 3163298bea7Sopenharmony_ci} 3173298bea7Sopenharmony_ci 3183298bea7Sopenharmony_ci/** 3193298bea7Sopenharmony_ci * @tc.name: CheckChildThreadSetIndepent 3203298bea7Sopenharmony_ci * @tc.desc: Test each child thread tokenid and ftokenid is independent 3213298bea7Sopenharmony_ci * @tc.desc: The tokenid and ftokenid of each child thread not equal to father process 3223298bea7Sopenharmony_ci * @tc.type: FUNC 3233298bea7Sopenharmony_ci */ 3243298bea7Sopenharmony_ciHWTEST_F(AccesstokenidTest, CheckChildThreadSetIndepent, Function | MediumTest | Level1) 3253298bea7Sopenharmony_ci{ 3263298bea7Sopenharmony_ci pthread_t cid[10]; 3273298bea7Sopenharmony_ci 3283298bea7Sopenharmony_ci unsigned long long token = INVAL_TOKEN; 3293298bea7Sopenharmony_ci unsigned long long ftoken = INVAL_TOKEN; 3303298bea7Sopenharmony_ci unsigned long long tokenSet = GenRand64(); 3313298bea7Sopenharmony_ci unsigned long long ftokenSet = GenRand64(); 3323298bea7Sopenharmony_ci 3333298bea7Sopenharmony_ci struct Tokeninfo tinfo; 3343298bea7Sopenharmony_ci tinfo.pid = getpid(); 3353298bea7Sopenharmony_ci tinfo.tid = gettid(); 3363298bea7Sopenharmony_ci tinfo.tokenid = INVAL_TOKEN; 3373298bea7Sopenharmony_ci tinfo.ftokenid = INVAL_TOKEN; 3383298bea7Sopenharmony_ci 3393298bea7Sopenharmony_ci GetTokenid(&token); 3403298bea7Sopenharmony_ci GetfTokenid(&ftoken); 3413298bea7Sopenharmony_ci 3423298bea7Sopenharmony_ci SetTokenid(&tokenSet); 3433298bea7Sopenharmony_ci SetfTokenid(&ftokenSet); 3443298bea7Sopenharmony_ci 3453298bea7Sopenharmony_ci for (int i = 0; i < 10; i++) { 3463298bea7Sopenharmony_ci if (pthread_create(&cid[i], nullptr, CheckChildThreadSetIndepent, &tinfo) != 0) { 3473298bea7Sopenharmony_ci printf("thread %d (ID %ld) pthread_create error\n", i, cid[i]); 3483298bea7Sopenharmony_ci } 3493298bea7Sopenharmony_ci 3503298bea7Sopenharmony_ci if (pthread_join(cid[i], nullptr) != 0) { 3513298bea7Sopenharmony_ci printf("thread %d (ID %ld) pthread_join error\n", i, cid[i]); 3523298bea7Sopenharmony_ci } 3533298bea7Sopenharmony_ci 3543298bea7Sopenharmony_ci ASSERT_NE(tinfo.tokenid, tokenSet); 3553298bea7Sopenharmony_ci ASSERT_NE(tinfo.ftokenid, ftokenSet); 3563298bea7Sopenharmony_ci ASSERT_NE(0, tinfo.ftokenid); 3573298bea7Sopenharmony_ci } 3583298bea7Sopenharmony_ci} 3593298bea7Sopenharmony_ci 3603298bea7Sopenharmony_ci/** 3613298bea7Sopenharmony_ci * @tc.name: AbnormalGetTokenid 3623298bea7Sopenharmony_ci * @tc.desc: Test abnormal ioctl cmd of ACCESS_TOKENID_GET_TOKENID 3633298bea7Sopenharmony_ci * @tc.desc: using nullptr instead of the address of tokenid to ioctl 3643298bea7Sopenharmony_ci * @tc.type: FUNC 3653298bea7Sopenharmony_ci */ 3663298bea7Sopenharmony_ciHWTEST_F(AccesstokenidTest, AbnormalGetTokenid, Function | MediumTest | Level1) 3673298bea7Sopenharmony_ci{ 3683298bea7Sopenharmony_ci int fd = open(DEV_ACCESSTOKENID, O_RDWR); 3693298bea7Sopenharmony_ci if (fd < 0) { 3703298bea7Sopenharmony_ci printf("open %s failed\r\n", DEV_ACCESSTOKENID); 3713298bea7Sopenharmony_ci return; 3723298bea7Sopenharmony_ci } 3733298bea7Sopenharmony_ci 3743298bea7Sopenharmony_ci int ret = ioctl(fd, ACCESS_TOKENID_GET_TOKENID, nullptr); 3753298bea7Sopenharmony_ci close(fd); 3763298bea7Sopenharmony_ci 3773298bea7Sopenharmony_ci ASSERT_NE(0, ret); 3783298bea7Sopenharmony_ci} 3793298bea7Sopenharmony_ci 3803298bea7Sopenharmony_ci/** 3813298bea7Sopenharmony_ci * @tc.name: AbnormalSetTokenid 3823298bea7Sopenharmony_ci * @tc.desc: Test abnormal ioctl cmd of ACCESS_TOKENID_SET_TOKENID 3833298bea7Sopenharmony_ci * @tc.desc: using nullptr instead of the address of tokenid to ioctl 3843298bea7Sopenharmony_ci * @tc.type: FUNC 3853298bea7Sopenharmony_ci */ 3863298bea7Sopenharmony_ciHWTEST_F(AccesstokenidTest, AbnormalSetTokenid, Function | MediumTest | Level1) 3873298bea7Sopenharmony_ci{ 3883298bea7Sopenharmony_ci int fd = open(DEV_ACCESSTOKENID, O_RDWR); 3893298bea7Sopenharmony_ci if (fd < 0) { 3903298bea7Sopenharmony_ci printf("open %s failed\r\n", DEV_ACCESSTOKENID); 3913298bea7Sopenharmony_ci return; 3923298bea7Sopenharmony_ci } 3933298bea7Sopenharmony_ci 3943298bea7Sopenharmony_ci int ret = ioctl(fd, ACCESS_TOKENID_SET_TOKENID, nullptr); 3953298bea7Sopenharmony_ci close(fd); 3963298bea7Sopenharmony_ci 3973298bea7Sopenharmony_ci ASSERT_NE(0, ret); 3983298bea7Sopenharmony_ci} 3993298bea7Sopenharmony_ci 4003298bea7Sopenharmony_ci/** 4013298bea7Sopenharmony_ci * @tc.name: AbnormalGetfTokenid 4023298bea7Sopenharmony_ci * @tc.desc: Test abnormal ioctl cmd of ACCESS_TOKENID_GET_FTOKENID 4033298bea7Sopenharmony_ci * @tc.desc: using nullptr instead of the address of ftokenid to ioctl 4043298bea7Sopenharmony_ci * @tc.type: FUNC 4053298bea7Sopenharmony_ci */ 4063298bea7Sopenharmony_ciHWTEST_F(AccesstokenidTest, AbnormalGetfTokenid, Function | MediumTest | Level1) 4073298bea7Sopenharmony_ci{ 4083298bea7Sopenharmony_ci int fd = open(DEV_ACCESSTOKENID, O_RDWR); 4093298bea7Sopenharmony_ci if (fd < 0) { 4103298bea7Sopenharmony_ci printf("open %s failed\r\n", DEV_ACCESSTOKENID); 4113298bea7Sopenharmony_ci return; 4123298bea7Sopenharmony_ci } 4133298bea7Sopenharmony_ci 4143298bea7Sopenharmony_ci int ret = ioctl(fd, ACCESS_TOKENID_GET_FTOKENID, nullptr); 4153298bea7Sopenharmony_ci close(fd); 4163298bea7Sopenharmony_ci 4173298bea7Sopenharmony_ci ASSERT_NE(0, ret); 4183298bea7Sopenharmony_ci} 4193298bea7Sopenharmony_ci 4203298bea7Sopenharmony_ci/** 4213298bea7Sopenharmony_ci * @tc.name: AbnormalSetfTokenid 4223298bea7Sopenharmony_ci * @tc.desc: Test abnormal ioctl cmd of ACCESS_TOKENID_SET_FTOKENID 4233298bea7Sopenharmony_ci * @tc.desc: using nullptr instead of the address of ftokenid to ioctl 4243298bea7Sopenharmony_ci * @tc.type: FUNC 4253298bea7Sopenharmony_ci */ 4263298bea7Sopenharmony_ciHWTEST_F(AccesstokenidTest, AbnormalSetfTokenid, Function | MediumTest | Level1) 4273298bea7Sopenharmony_ci{ 4283298bea7Sopenharmony_ci int fd = open(DEV_ACCESSTOKENID, O_RDWR); 4293298bea7Sopenharmony_ci if (fd < 0) { 4303298bea7Sopenharmony_ci printf("open %s failed\r\n", DEV_ACCESSTOKENID); 4313298bea7Sopenharmony_ci return; 4323298bea7Sopenharmony_ci } 4333298bea7Sopenharmony_ci 4343298bea7Sopenharmony_ci int ret = ioctl(fd, ACCESS_TOKENID_SET_FTOKENID, nullptr); 4353298bea7Sopenharmony_ci close(fd); 4363298bea7Sopenharmony_ci 4373298bea7Sopenharmony_ci ASSERT_NE(0, ret); 4383298bea7Sopenharmony_ci} 4393298bea7Sopenharmony_ci 4403298bea7Sopenharmony_ci/** 4413298bea7Sopenharmony_ci * @tc.name: AbnormalIoctlCmd 4423298bea7Sopenharmony_ci * @tc.desc: Test abnormal ioctl cmd of ACCESS_TOKENID_ILLEGAL1 and ACCESS_TOKENID_ILLEGAL1 4433298bea7Sopenharmony_ci * @tc.desc: using illegal cmd instead of accesstokenid to ioctl 4443298bea7Sopenharmony_ci * @tc.type: FUNC 4453298bea7Sopenharmony_ci */ 4463298bea7Sopenharmony_ciHWTEST_F(AccesstokenidTest, AbnormalIoctlCmd, Function | MediumTest | Level1) 4473298bea7Sopenharmony_ci{ 4483298bea7Sopenharmony_ci unsigned long long token; 4493298bea7Sopenharmony_ci 4503298bea7Sopenharmony_ci int fd = open(DEV_ACCESSTOKENID, O_RDWR); 4513298bea7Sopenharmony_ci if (fd < 0) { 4523298bea7Sopenharmony_ci printf("open %s failed\r\n", DEV_ACCESSTOKENID); 4533298bea7Sopenharmony_ci return; 4543298bea7Sopenharmony_ci } 4553298bea7Sopenharmony_ci 4563298bea7Sopenharmony_ci int ret1 = ioctl(fd, ACCESS_TOKENID_ILLEGAL1, &token); 4573298bea7Sopenharmony_ci int ret2 = ioctl(fd, ACCESS_TOKENID_ILLEGAL2, &token); 4583298bea7Sopenharmony_ci close(fd); 4593298bea7Sopenharmony_ci 4603298bea7Sopenharmony_ci ASSERT_NE(0, ret1); 4613298bea7Sopenharmony_ci ASSERT_NE(0, ret2); 4623298bea7Sopenharmony_ci} 4633298bea7Sopenharmony_ci 4643298bea7Sopenharmony_ci/** 4653298bea7Sopenharmony_ci * @tc.name: OtherUidSetTokenid 4663298bea7Sopenharmony_ci * @tc.desc: Test ACCESS_TOKEN_OTHER_UID can not set tokenid 4673298bea7Sopenharmony_ci * @tc.desc: tokenid can be only set by uid 3020 4683298bea7Sopenharmony_ci * @tc.type: FUNC 4693298bea7Sopenharmony_ci */ 4703298bea7Sopenharmony_ciHWTEST_F(AccesstokenidTest, OtherUidSetTokenid, Function | MediumTest | Level1) 4713298bea7Sopenharmony_ci{ 4723298bea7Sopenharmony_ci unsigned long long tokenSet = GenRand64(); 4733298bea7Sopenharmony_ci int ret; 4743298bea7Sopenharmony_ci 4753298bea7Sopenharmony_ci ret = setuid(ACCESS_TOKEN_OTHER_UID); 4763298bea7Sopenharmony_ci if (ret != 0) { 4773298bea7Sopenharmony_ci printf("setuid error %d \r\n", ret); 4783298bea7Sopenharmony_ci } 4793298bea7Sopenharmony_ci 4803298bea7Sopenharmony_ci int fd = open(DEV_ACCESSTOKENID, O_RDWR); 4813298bea7Sopenharmony_ci if (fd < 0) { 4823298bea7Sopenharmony_ci printf("open %s failed\r\n", DEV_ACCESSTOKENID); 4833298bea7Sopenharmony_ci return; 4843298bea7Sopenharmony_ci } 4853298bea7Sopenharmony_ci 4863298bea7Sopenharmony_ci ret = ioctl(fd, ACCESS_TOKENID_SET_TOKENID, &tokenSet); 4873298bea7Sopenharmony_ci close(fd); 4883298bea7Sopenharmony_ci 4893298bea7Sopenharmony_ci ASSERT_NE(0, ret); 4903298bea7Sopenharmony_ci} 4913298bea7Sopenharmony_ci 4923298bea7Sopenharmony_ci/** 4933298bea7Sopenharmony_ci * @tc.name: OtherUidGetTokenid 4943298bea7Sopenharmony_ci * @tc.desc: Test ACCESS_TOKEN_OTHER_UID can get tokenid 4953298bea7Sopenharmony_ci * @tc.desc: tokenid can get not only by uid 3020 4963298bea7Sopenharmony_ci * @tc.type: FUNC 4973298bea7Sopenharmony_ci */ 4983298bea7Sopenharmony_ciHWTEST_F(AccesstokenidTest, OtherUidGetTokenid, Function | MediumTest | Level1) 4993298bea7Sopenharmony_ci{ 5003298bea7Sopenharmony_ci unsigned long long token = INVAL_TOKEN; 5013298bea7Sopenharmony_ci int ret; 5023298bea7Sopenharmony_ci 5033298bea7Sopenharmony_ci ret = setuid(ACCESS_TOKEN_OTHER_UID); 5043298bea7Sopenharmony_ci if (ret != 0) { 5053298bea7Sopenharmony_ci printf("setuid error %d \r\n", ret); 5063298bea7Sopenharmony_ci } 5073298bea7Sopenharmony_ci 5083298bea7Sopenharmony_ci int fd = open(DEV_ACCESSTOKENID, O_RDWR); 5093298bea7Sopenharmony_ci if (fd < 0) { 5103298bea7Sopenharmony_ci printf("open %s failed\r\n", DEV_ACCESSTOKENID); 5113298bea7Sopenharmony_ci return; 5123298bea7Sopenharmony_ci } 5133298bea7Sopenharmony_ci 5143298bea7Sopenharmony_ci ret = ioctl(fd, ACCESS_TOKENID_GET_TOKENID, &token); 5153298bea7Sopenharmony_ci close(fd); 5163298bea7Sopenharmony_ci 5173298bea7Sopenharmony_ci ASSERT_EQ(0, ret); 5183298bea7Sopenharmony_ci} 5193298bea7Sopenharmony_ci 5203298bea7Sopenharmony_ci/** 5213298bea7Sopenharmony_ci * @tc.name: WithoutGrpSetfTokenid 5223298bea7Sopenharmony_ci * @tc.desc: Test ACCESS_TOKEN_OTHER_GRPID can not set ftokenid 5233298bea7Sopenharmony_ci * @tc.desc: ftokenid can not set by groups without grpid 3020 5243298bea7Sopenharmony_ci * @tc.type: FUNC 5253298bea7Sopenharmony_ci */ 5263298bea7Sopenharmony_ciHWTEST_F(AccesstokenidTest, WithoutGrpSetfTokenid, Function | MediumTest | Level1) 5273298bea7Sopenharmony_ci{ 5283298bea7Sopenharmony_ci int ret; 5293298bea7Sopenharmony_ci size_t size = 1; 5303298bea7Sopenharmony_ci gid_t list[1] = {ACCESS_TOKEN_OTHER_GRPID}; 5313298bea7Sopenharmony_ci unsigned long long ftokenSet = GenRand64(); 5323298bea7Sopenharmony_ci 5333298bea7Sopenharmony_ci ret = setgroups(size, list); 5343298bea7Sopenharmony_ci if (ret != 0) { 5353298bea7Sopenharmony_ci printf("setgroups error %d \r\n", ret); 5363298bea7Sopenharmony_ci } 5373298bea7Sopenharmony_ci 5383298bea7Sopenharmony_ci int fd = open(DEV_ACCESSTOKENID, O_RDWR); 5393298bea7Sopenharmony_ci if (fd < 0) { 5403298bea7Sopenharmony_ci printf("open %s failed\r\n", DEV_ACCESSTOKENID); 5413298bea7Sopenharmony_ci return; 5423298bea7Sopenharmony_ci } 5433298bea7Sopenharmony_ci 5443298bea7Sopenharmony_ci ret = ioctl(fd, ACCESS_TOKENID_SET_FTOKENID, &ftokenSet); 5453298bea7Sopenharmony_ci close(fd); 5463298bea7Sopenharmony_ci 5473298bea7Sopenharmony_ci ASSERT_NE(0, ret); 5483298bea7Sopenharmony_ci} 5493298bea7Sopenharmony_ci 5503298bea7Sopenharmony_ci/** 5513298bea7Sopenharmony_ci * @tc.name: WithoutGrpGetfTokenid 5523298bea7Sopenharmony_ci * @tc.desc: Test ACCESS_TOKEN_OTHER_GRPID can not get ftokenid 5533298bea7Sopenharmony_ci * @tc.desc: ftokenid can not get by groups without grpid 3020 5543298bea7Sopenharmony_ci * @tc.type: FUNC 5553298bea7Sopenharmony_ci */ 5563298bea7Sopenharmony_ciHWTEST_F(AccesstokenidTest, WithoutGrpGetfTokenid, Function | MediumTest | Level1) 5573298bea7Sopenharmony_ci{ 5583298bea7Sopenharmony_ci int ret; 5593298bea7Sopenharmony_ci size_t size = 1; 5603298bea7Sopenharmony_ci gid_t list[1] = {ACCESS_TOKEN_OTHER_GRPID}; 5613298bea7Sopenharmony_ci unsigned long long ftoken = INVAL_TOKEN; 5623298bea7Sopenharmony_ci 5633298bea7Sopenharmony_ci ret = setgroups(size, list); 5643298bea7Sopenharmony_ci if (ret != 0) { 5653298bea7Sopenharmony_ci printf("setgroups error %d \r\n", ret); 5663298bea7Sopenharmony_ci } 5673298bea7Sopenharmony_ci 5683298bea7Sopenharmony_ci int fd = open(DEV_ACCESSTOKENID, O_RDWR); 5693298bea7Sopenharmony_ci if (fd < 0) { 5703298bea7Sopenharmony_ci printf("open %s failed\r\n", DEV_ACCESSTOKENID); 5713298bea7Sopenharmony_ci return; 5723298bea7Sopenharmony_ci } 5733298bea7Sopenharmony_ci 5743298bea7Sopenharmony_ci ret = ioctl(fd, ACCESS_TOKENID_GET_FTOKENID, &ftoken); 5753298bea7Sopenharmony_ci close(fd); 5763298bea7Sopenharmony_ci 5773298bea7Sopenharmony_ci ASSERT_NE(0, ret); 5783298bea7Sopenharmony_ci} 5793298bea7Sopenharmony_ci 5803298bea7Sopenharmony_ci/** 5813298bea7Sopenharmony_ci * @tc.name: WithGrpSetfTokenid 5823298bea7Sopenharmony_ci * @tc.desc: Test ACCESS_TOKEN_OTHER_GRPID and ACCESS_TOKEN_GRPID can set ftokenid 5833298bea7Sopenharmony_ci * @tc.desc: ftokenid can set by groups with grpid 3020 5843298bea7Sopenharmony_ci * @tc.type: FUNC 5853298bea7Sopenharmony_ci */ 5863298bea7Sopenharmony_ciHWTEST_F(AccesstokenidTest, WithGrpSetfTokenid, Function | MediumTest | Level1) 5873298bea7Sopenharmony_ci{ 5883298bea7Sopenharmony_ci int ret; 5893298bea7Sopenharmony_ci size_t size = 2; 5903298bea7Sopenharmony_ci gid_t list[2] = {ACCESS_TOKEN_OTHER_GRPID, ACCESS_TOKEN_GRPID}; 5913298bea7Sopenharmony_ci unsigned long long ftokenSet = GenRand64(); 5923298bea7Sopenharmony_ci 5933298bea7Sopenharmony_ci ret = setgroups(size, list); 5943298bea7Sopenharmony_ci if (ret != 0) { 5953298bea7Sopenharmony_ci printf("setgroups error %d \r\n", ret); 5963298bea7Sopenharmony_ci } 5973298bea7Sopenharmony_ci 5983298bea7Sopenharmony_ci int fd = open(DEV_ACCESSTOKENID, O_RDWR); 5993298bea7Sopenharmony_ci if (fd < 0) { 6003298bea7Sopenharmony_ci printf("open %s failed\r\n", DEV_ACCESSTOKENID); 6013298bea7Sopenharmony_ci return; 6023298bea7Sopenharmony_ci } 6033298bea7Sopenharmony_ci 6043298bea7Sopenharmony_ci ret = ioctl(fd, ACCESS_TOKENID_SET_FTOKENID, &ftokenSet); 6053298bea7Sopenharmony_ci close(fd); 6063298bea7Sopenharmony_ci 6073298bea7Sopenharmony_ci ASSERT_EQ(0, ret); 6083298bea7Sopenharmony_ci} 6093298bea7Sopenharmony_ci 6103298bea7Sopenharmony_ci/** 6113298bea7Sopenharmony_ci * @tc.name: WithGrpGetfTokenid 6123298bea7Sopenharmony_ci * @tc.desc: Test ACCESS_TOKEN_OTHER_GRPID and ACCESS_TOKEN_GRPID can set ftokenid 6133298bea7Sopenharmony_ci * @tc.desc: ftokenid can set by groups with grpid 3020 6143298bea7Sopenharmony_ci * @tc.type: FUNC 6153298bea7Sopenharmony_ci */ 6163298bea7Sopenharmony_ciHWTEST_F(AccesstokenidTest, WithGrpGetfTokenid, Function | MediumTest | Level1) 6173298bea7Sopenharmony_ci{ 6183298bea7Sopenharmony_ci int ret; 6193298bea7Sopenharmony_ci size_t size = 2; 6203298bea7Sopenharmony_ci gid_t list[2] = {ACCESS_TOKEN_OTHER_GRPID, ACCESS_TOKEN_GRPID}; 6213298bea7Sopenharmony_ci unsigned long long ftoken = INVAL_TOKEN; 6223298bea7Sopenharmony_ci 6233298bea7Sopenharmony_ci ret = setgroups(size, list); 6243298bea7Sopenharmony_ci if (ret != 0) { 6253298bea7Sopenharmony_ci printf("setgroups error %d \r\n", ret); 6263298bea7Sopenharmony_ci } 6273298bea7Sopenharmony_ci 6283298bea7Sopenharmony_ci int fd = open(DEV_ACCESSTOKENID, O_RDWR); 6293298bea7Sopenharmony_ci if (fd < 0) { 6303298bea7Sopenharmony_ci printf("open %s failed\r\n", DEV_ACCESSTOKENID); 6313298bea7Sopenharmony_ci return; 6323298bea7Sopenharmony_ci } 6333298bea7Sopenharmony_ci 6343298bea7Sopenharmony_ci ret = ioctl(fd, ACCESS_TOKENID_GET_FTOKENID, &ftoken); 6353298bea7Sopenharmony_ci close(fd); 6363298bea7Sopenharmony_ci 6373298bea7Sopenharmony_ci ASSERT_EQ(0, ret); 6383298bea7Sopenharmony_ci} 6393298bea7Sopenharmony_ci 640