1570af302Sopenharmony_ci/* 2570af302Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4570af302Sopenharmony_ci * you may not use this file except in compliance with the License. 5570af302Sopenharmony_ci * You may obtain a copy of the License at 6570af302Sopenharmony_ci * 7570af302Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8570af302Sopenharmony_ci * 9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12570af302Sopenharmony_ci * See the License for the specific language governing permissions and 13570af302Sopenharmony_ci * limitations under the License. 14570af302Sopenharmony_ci */ 15570af302Sopenharmony_ci 16570af302Sopenharmony_ci#include <fcntl.h> 17570af302Sopenharmony_ci#include <stdio.h> 18570af302Sopenharmony_ci#include <stdlib.h> 19570af302Sopenharmony_ci#include <sys/stat.h> 20570af302Sopenharmony_ci#include <stdint.h> 21570af302Sopenharmony_ci#include <signal.h> 22570af302Sopenharmony_ci#include <unistd.h> 23570af302Sopenharmony_ci#include "functionalext.h" 24570af302Sopenharmony_ci 25570af302Sopenharmony_citypedef void (*TEST_FUN)(); 26570af302Sopenharmony_ciconst int COUNT_ZERO = 0; 27570af302Sopenharmony_ciconst int COUNT_NEFATIVE = -1; 28570af302Sopenharmony_ci 29570af302Sopenharmony_ci/** 30570af302Sopenharmony_ci * @tc.name : fchown_0100 31570af302Sopenharmony_ci * @tc.desc : Parameter is valid and can change the user and group of the file's owner 32570af302Sopenharmony_ci * @tc.level : Level 0 33570af302Sopenharmony_ci */ 34570af302Sopenharmony_civoid fchown_0100() 35570af302Sopenharmony_ci{ 36570af302Sopenharmony_ci int fd = open("test.txt", O_RDONLY | O_CREAT, TEST_MODE); 37570af302Sopenharmony_ci int result = fchown(fd, 0, 0); 38570af302Sopenharmony_ci struct stat buff; 39570af302Sopenharmony_ci EXPECT_EQ("fchown_0100", result, COUNT_ZERO); 40570af302Sopenharmony_ci stat("test.txt", &buff); 41570af302Sopenharmony_ci EXPECT_EQ("fchown_0100", buff.st_uid, COUNT_ZERO); 42570af302Sopenharmony_ci close(fd); 43570af302Sopenharmony_ci remove("test.txt"); 44570af302Sopenharmony_ci} 45570af302Sopenharmony_ci 46570af302Sopenharmony_ci/** 47570af302Sopenharmony_ci * @tc.name : fchown_0200 48570af302Sopenharmony_ci * @tc.desc : Parameter valid, can change the file owner of the user 49570af302Sopenharmony_ci * @tc.level : Level 0 50570af302Sopenharmony_ci */ 51570af302Sopenharmony_civoid fchown_0200() 52570af302Sopenharmony_ci{ 53570af302Sopenharmony_ci int fd = open("test.txt", O_RDONLY | O_CREAT, TEST_MODE); 54570af302Sopenharmony_ci int result = fchown(fd, 0, -1); 55570af302Sopenharmony_ci struct stat buff; 56570af302Sopenharmony_ci EXPECT_EQ("fchown_0200", result, COUNT_ZERO); 57570af302Sopenharmony_ci stat("test.txt", &buff); 58570af302Sopenharmony_ci EXPECT_EQ("fchown_0200", buff.st_uid, COUNT_ZERO); 59570af302Sopenharmony_ci close(fd); 60570af302Sopenharmony_ci remove("test.txt"); 61570af302Sopenharmony_ci} 62570af302Sopenharmony_ci 63570af302Sopenharmony_ci/** 64570af302Sopenharmony_ci * @tc.name : fchown_0300 65570af302Sopenharmony_ci * @tc.desc : Parameter valid to change the owner group of the file 66570af302Sopenharmony_ci * @tc.level : Level 0 67570af302Sopenharmony_ci */ 68570af302Sopenharmony_civoid fchown_0300() 69570af302Sopenharmony_ci{ 70570af302Sopenharmony_ci int fd = open("test.txt", O_RDONLY | O_CREAT, TEST_MODE); 71570af302Sopenharmony_ci int result = fchown(fd, -1, 0); 72570af302Sopenharmony_ci struct stat buff; 73570af302Sopenharmony_ci EXPECT_EQ("fchown_0300", result, COUNT_ZERO); 74570af302Sopenharmony_ci stat("test.txt", &buff); 75570af302Sopenharmony_ci EXPECT_EQ("fchown_0300", buff.st_gid, COUNT_ZERO); 76570af302Sopenharmony_ci close(fd); 77570af302Sopenharmony_ci remove("test.txt"); 78570af302Sopenharmony_ci} 79570af302Sopenharmony_ci 80570af302Sopenharmony_ci/** 81570af302Sopenharmony_ci * @tc.name : fchown_0400 82570af302Sopenharmony_ci * @tc.desc : The user and group of the file's owner cannot be changed 83570af302Sopenharmony_ci * @tc.level : Level 2 84570af302Sopenharmony_ci */ 85570af302Sopenharmony_civoid fchown_0400() 86570af302Sopenharmony_ci{ 87570af302Sopenharmony_ci int fd = open("test.txt", O_RDONLY | O_CREAT, TEST_MODE); 88570af302Sopenharmony_ci close(fd); 89570af302Sopenharmony_ci int result = fchown(fd, 0, 0); 90570af302Sopenharmony_ci EXPECT_EQ("fchown_0400", result, COUNT_NEFATIVE); 91570af302Sopenharmony_ci remove("test.txt"); 92570af302Sopenharmony_ci} 93570af302Sopenharmony_ci 94570af302Sopenharmony_ci/** 95570af302Sopenharmony_ci * @tc.name : fchown_0500 96570af302Sopenharmony_ci * @tc.desc : The user and group of the file's owner cannot be changed 97570af302Sopenharmony_ci * @tc.level : Level 2 98570af302Sopenharmony_ci */ 99570af302Sopenharmony_civoid fchown_0500() 100570af302Sopenharmony_ci{ 101570af302Sopenharmony_ci int fd = open("test.txt", O_RDONLY | O_CREAT, TEST_MODE); 102570af302Sopenharmony_ci int result = fchown(-1, 0, 0); 103570af302Sopenharmony_ci EXPECT_EQ("fchown_0500", result, COUNT_NEFATIVE); 104570af302Sopenharmony_ci close(fd); 105570af302Sopenharmony_ci remove("test.txt"); 106570af302Sopenharmony_ci} 107570af302Sopenharmony_ci 108570af302Sopenharmony_ciTEST_FUN G_Fun_Array[] = { 109570af302Sopenharmony_ci fchown_0100, 110570af302Sopenharmony_ci fchown_0200, 111570af302Sopenharmony_ci fchown_0300, 112570af302Sopenharmony_ci fchown_0400, 113570af302Sopenharmony_ci fchown_0500, 114570af302Sopenharmony_ci}; 115570af302Sopenharmony_ci 116570af302Sopenharmony_ciint main(int argc, char *argv[]) 117570af302Sopenharmony_ci{ 118570af302Sopenharmony_ci int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN); 119570af302Sopenharmony_ci for (int pos = 0; pos < num; ++pos) { 120570af302Sopenharmony_ci G_Fun_Array[pos](); 121570af302Sopenharmony_ci } 122570af302Sopenharmony_ci 123570af302Sopenharmony_ci return t_status; 124570af302Sopenharmony_ci}