1/* 2 * Copyright (c) 2022 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 16#include "functionalext.h" 17#include <unistd.h> 18#include <fcntl.h> 19#include <stdlib.h> 20 21const char *path = "/data/test.txt"; 22 23/** 24 * @tc.name : fwrite_0100 25 * @tc.desc : File pointer at the end of a file, the position to which the current file pointer points 26 * @tc.level : Level 0 27 */ 28void fwrite_0100(void) 29{ 30 FILE *fptr = fopen(path, "w+"); 31 EXPECT_PTRNE("fwrite_0100", fptr, NULL); 32 33 char buf[] = "this is test"; 34 int result = fwrite(buf, sizeof(char), strlen(buf), fptr); 35 EXPECT_TRUE("fwrite_0100", result == strlen(buf)); 36 37 fclose(fptr); 38 remove(path); 39} 40 41/** 42 * @tc.name : fwrite_0200 43 * @tc.desc : File pointer at the beginning of a file, the location to which the current file pointer points 44 * @tc.level : Level 0 45 */ 46void fwrite_0200(void) 47{ 48 FILE *fptr = fopen(path, "w+"); 49 EXPECT_PTRNE("fwrite_0200", fptr, NULL); 50 51 char buf[] = "this is test"; 52 int result = fwrite(buf, 0, strlen(buf), fptr); 53 EXPECT_EQ("fwrite_0200", result, 0); 54 55 fclose(fptr); 56 remove(path); 57} 58 59/** 60 * @tc.name : fwrite_0300 61 * @tc.desc : Invalid argument. Cannot get the location of the current file pointer 62 * @tc.level : Level 2 63 */ 64void fwrite_0300(void) 65{ 66 FILE *fptr = fopen(path, "w+"); 67 EXPECT_PTRNE("fwrite_0300", fptr, NULL); 68 69 char buf[] = "this is test"; 70 int result = fwrite(buf, sizeof(char), 0, fptr); 71 EXPECT_EQ("fwrite_0300", result, 0); 72 73 fclose(fptr); 74 remove(path); 75} 76 77void fwrite_0400(void) 78{ 79 pid_t childPid = 0; 80 int fds[2] = {0}; 81 pipe(fds); 82 int pipeRead = 0; 83 int pipeWrite = 1; 84 85 char buf[1024] = {0}; 86 87 childPid = fork(); 88 EXPECT_NE("fwrite_0400", childPid, -1); 89 if (childPid == 0) { 90 // childr 91 dup2(fds[pipeWrite], STDOUT_FILENO); 92 dup2(fds[pipeRead], STDIN_FILENO); 93 94 close(fds[pipeWrite]); 95 close(fds[pipeRead]); 96 97 // exec 98 execl("/bin/sh", "/bin/sh", "-c", "/system/bin/bm get -u", NULL); 99 exit(0); 100 } else { 101 // parent 102 close(fds[pipeWrite]); 103 fcntl(fds[pipeRead], F_SETFD, F_DUPFD_CLOEXEC); 104 int cn = read(fds[pipeRead], buf, sizeof(buf)); 105 EXPECT_MT("fwrite_0400", cn, 0); 106 } 107} 108 109int main(int argc, char *argv[]) 110{ 111 fwrite_0100(); 112 fwrite_0200(); 113 fwrite_0300(); 114 return t_status; 115} 116