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 <fcntl.h>
17 #include "functionalext.h"
18 #include "filepath_util.h"
19 
20 /**
21  * @tc.name      : getline_0100
22  * @tc.desc      : Verify that a row of data can be read (parameters are valid).
23  * @tc.level     : Level 0
24  */
getline_0100(void)25 void getline_0100(void)
26 {
27     char *wrstring = "helloworld";
28     char *line = NULL;
29     size_t len = 0;
30     char ptr[PATH_MAX] = {0};
31     FILE_ABSOLUTE_PATH(STR_TEST_TXT, ptr);
32     FILE *fp = fopen(ptr, "w+");
33     EXPECT_PTRNE("getline_0100", fp, NULL);
34     fwrite(wrstring, sizeof(char), strlen(wrstring), fp);
35     fseek(fp, 0, SEEK_SET);
36 
37     ssize_t read = getline(&line, &len, fp);
38     EXPECT_EQ("getline_0100", read, 10);
39     EXPECT_STREQ("getline_0100", line, "helloworld");
40 
41     fclose(fp);
42     remove(ptr);
43 }
44 
45 /**
46  * @tc.name      : getline_0200
47  * @tc.desc      : Validation cannot read a row of data (parameter is NULL).
48  * @tc.level     : Level 2
49  */
getline_0200(void)50 void getline_0200(void)
51 {
52     size_t len = 0;
53     char ptr[PATH_MAX] = {0};
54     FILE_ABSOLUTE_PATH(STR_FILE_TXT, ptr);
55     FILE *fp = fopen(ptr, "w+");
56     EXPECT_PTRNE("getline_0100", fp, NULL);
57 
58     ssize_t read = getline(NULL, &len, fp);
59     EXPECT_EQ("getline_0200", read, -1);
60 
61     fclose(fp);
62     remove(ptr);
63 }
64 
65 /**
66  * @tc.name      : getline_0300
67  * @tc.desc      : TValidation cannot read a row of data (parameter is 0).
68  * @tc.level     : Level 2
69  */
getline_0300(void)70 void getline_0300(void)
71 {
72     char *line = NULL;
73     char ptr[PATH_MAX] = {0};
74     FILE_ABSOLUTE_PATH(STR_FILE_TXT, ptr);
75     FILE *fp = fopen(ptr, "w+");
76     EXPECT_PTRNE("getline_0100", fp, NULL);
77 
78     ssize_t read = getline(&line, 0, fp);
79     EXPECT_EQ("getline_0300", read, -1);
80     fclose(fp);
81     remove(ptr);
82 }
83 
main(int argc, char *argv[])84 int main(int argc, char *argv[])
85 {
86     getline_0100();
87     getline_0200();
88     getline_0300();
89     return t_status;
90 }