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 <string.h> 19570af302Sopenharmony_ci#include <unistd.h> 20570af302Sopenharmony_ci#include "filepath_util.h" 21570af302Sopenharmony_ci 22570af302Sopenharmony_ci/** 23570af302Sopenharmony_ci * @tc.name : write_0100 24570af302Sopenharmony_ci * @tc.desc : Test the write function, the return value is the specified buf length 25570af302Sopenharmony_ci * @tc.level : Level 0 26570af302Sopenharmony_ci */ 27570af302Sopenharmony_civoid write_0100(void) 28570af302Sopenharmony_ci{ 29570af302Sopenharmony_ci const char *msg = "This is a c test code for write function"; 30570af302Sopenharmony_ci char path[PATH_MAX] = {0}; 31570af302Sopenharmony_ci FILE_ABSOLUTE_PATH(STR_WRITE_TEST_TXT, path); 32570af302Sopenharmony_ci int len = strlen(msg); 33570af302Sopenharmony_ci char buf[1024] = {0}; 34570af302Sopenharmony_ci int fd = open(path, O_RDWR | O_RSYNC | O_CREAT, 0664); 35570af302Sopenharmony_ci if (fd == -1) { 36570af302Sopenharmony_ci t_error("%s write create file error", __func__); 37570af302Sopenharmony_ci return; 38570af302Sopenharmony_ci } 39570af302Sopenharmony_ci int result = write(fd, msg, len); 40570af302Sopenharmony_ci if (result != len) { 41570af302Sopenharmony_ci t_error("%s write get result is %d not want %d", __func__, result, len); 42570af302Sopenharmony_ci } 43570af302Sopenharmony_ci close(fd); 44570af302Sopenharmony_ci fd = open(path, O_RDWR); 45570af302Sopenharmony_ci int bytes = read(fd, buf, len); 46570af302Sopenharmony_ci if (bytes == -1) { 47570af302Sopenharmony_ci t_error("%s read file failed", __func__); 48570af302Sopenharmony_ci return; 49570af302Sopenharmony_ci } 50570af302Sopenharmony_ci if (strcmp(msg, buf)) { 51570af302Sopenharmony_ci t_error("%s wrong string written to file", __func__); 52570af302Sopenharmony_ci } 53570af302Sopenharmony_ci close(fd); 54570af302Sopenharmony_ci remove(path); 55570af302Sopenharmony_ci} 56570af302Sopenharmony_ci 57570af302Sopenharmony_ci/** 58570af302Sopenharmony_ci * @tc.name : write_0200 59570af302Sopenharmony_ci * @tc.desc : test the return value of write when the count parameter is 0 60570af302Sopenharmony_ci * @tc.level : Level 1 61570af302Sopenharmony_ci */ 62570af302Sopenharmony_civoid write_0200(void) 63570af302Sopenharmony_ci{ 64570af302Sopenharmony_ci const char *msg = "This is a c test code for write function"; 65570af302Sopenharmony_ci char path[PATH_MAX] = {0}; 66570af302Sopenharmony_ci FILE_ABSOLUTE_PATH(STR_WRITE_TEST_TXT, path); 67570af302Sopenharmony_ci int len = 0; 68570af302Sopenharmony_ci int fd = open(path, O_RDWR | O_RSYNC | O_CREAT, 0664); 69570af302Sopenharmony_ci if (fd == -1) { 70570af302Sopenharmony_ci t_error("%s write create file error", __func__); 71570af302Sopenharmony_ci return; 72570af302Sopenharmony_ci } 73570af302Sopenharmony_ci int result = write(fd, msg, len); 74570af302Sopenharmony_ci if (result != len) { 75570af302Sopenharmony_ci t_error("%s write get result is %d not want %d", __func__, result, len); 76570af302Sopenharmony_ci } 77570af302Sopenharmony_ci close(fd); 78570af302Sopenharmony_ci remove(path); 79570af302Sopenharmony_ci} 80570af302Sopenharmony_ci 81570af302Sopenharmony_ci/** 82570af302Sopenharmony_ci * @tc.name : write_0300 83570af302Sopenharmony_ci * @tc.desc : test the return result of write when count is greater than the length of buf 84570af302Sopenharmony_ci * @tc.level : Level 1 85570af302Sopenharmony_ci */ 86570af302Sopenharmony_civoid write_0300(void) 87570af302Sopenharmony_ci{ 88570af302Sopenharmony_ci const char *msg = "This is a c test code for write function"; 89570af302Sopenharmony_ci char path[PATH_MAX] = {0}; 90570af302Sopenharmony_ci FILE_ABSOLUTE_PATH(STR_WRITE_TEST_TXT, path); 91570af302Sopenharmony_ci int len = strlen(msg) + 1; 92570af302Sopenharmony_ci int fd = open(path, O_RDWR | O_RSYNC | O_CREAT, 0664); 93570af302Sopenharmony_ci if (fd == -1) { 94570af302Sopenharmony_ci t_error("%s write create file error", __func__); 95570af302Sopenharmony_ci return; 96570af302Sopenharmony_ci } 97570af302Sopenharmony_ci int result = write(fd, msg, len); 98570af302Sopenharmony_ci if (result == -1) { 99570af302Sopenharmony_ci t_error("%s write get result is %d not want -1", __func__, result); 100570af302Sopenharmony_ci } 101570af302Sopenharmony_ci close(fd); 102570af302Sopenharmony_ci remove(path); 103570af302Sopenharmony_ci} 104570af302Sopenharmony_ci 105570af302Sopenharmony_ci/** 106570af302Sopenharmony_ci * @tc.name : write_0400 107570af302Sopenharmony_ci * @tc.desc : test the return value of write when fd is abnormal 108570af302Sopenharmony_ci * @tc.level : Level 2 109570af302Sopenharmony_ci */ 110570af302Sopenharmony_civoid write_0400(void) 111570af302Sopenharmony_ci{ 112570af302Sopenharmony_ci const char *msg = "This is a c test code for write function"; 113570af302Sopenharmony_ci char path[PATH_MAX] = {0}; 114570af302Sopenharmony_ci FILE_ABSOLUTE_PATH(STR_WRITE_TEST_TXT, path); 115570af302Sopenharmony_ci int len = strlen(msg); 116570af302Sopenharmony_ci int fd = open(path, O_RDWR); 117570af302Sopenharmony_ci int result = write(fd, msg, len); 118570af302Sopenharmony_ci if (result != -1) { 119570af302Sopenharmony_ci t_error("%s write get result is %d not want -1", __func__, result); 120570af302Sopenharmony_ci } 121570af302Sopenharmony_ci close(fd); 122570af302Sopenharmony_ci remove(path); 123570af302Sopenharmony_ci} 124570af302Sopenharmony_ci 125570af302Sopenharmony_ci/** 126570af302Sopenharmony_ci * @tc.name : write_0500 127570af302Sopenharmony_ci * @tc.desc : test the return value of write when buf is NULL 128570af302Sopenharmony_ci * @tc.level : Level 2 129570af302Sopenharmony_ci */ 130570af302Sopenharmony_civoid write_0500(void) 131570af302Sopenharmony_ci{ 132570af302Sopenharmony_ci const char *msg = NULL; 133570af302Sopenharmony_ci char path[PATH_MAX] = {0}; 134570af302Sopenharmony_ci FILE_ABSOLUTE_PATH(STR_WRITE_TEST_TXT, path); 135570af302Sopenharmony_ci int len = 1; 136570af302Sopenharmony_ci int fd = open(path, O_RDWR | O_RSYNC | O_CREAT, 0664); 137570af302Sopenharmony_ci if (fd == -1) { 138570af302Sopenharmony_ci t_error("%s write create file error", __func__); 139570af302Sopenharmony_ci return; 140570af302Sopenharmony_ci } 141570af302Sopenharmony_ci int result = write(fd, msg, len); 142570af302Sopenharmony_ci if (result != -1) { 143570af302Sopenharmony_ci t_error("%s write get result is %d not want -1", __func__, result); 144570af302Sopenharmony_ci } 145570af302Sopenharmony_ci close(fd); 146570af302Sopenharmony_ci remove(path); 147570af302Sopenharmony_ci} 148570af302Sopenharmony_ci 149570af302Sopenharmony_ciint main(int argc, char *argv[]) 150570af302Sopenharmony_ci{ 151570af302Sopenharmony_ci write_0100(); 152570af302Sopenharmony_ci write_0200(); 153570af302Sopenharmony_ci write_0300(); 154570af302Sopenharmony_ci write_0400(); 155570af302Sopenharmony_ci write_0500(); 156570af302Sopenharmony_ci return t_status; 157570af302Sopenharmony_ci}