• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "hc_file_utils_test.h"
17 
18 #include <hctest.h>
19 #include <securec.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 
23 #include "hc_file_common.h"
24 #include "print_log.h"
25 #include "test_timer.h"
26 #include "utils_file.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #if UTILS_FILE_IMPLEMENTED
33 
TestHcFileUtilsFileOpenAndUtilsFileClose(void)34 static void TestHcFileUtilsFileOpenAndUtilsFileClose(void)
35 {
36     const char *fileName = TEST_FILE_NAME;
37     LOGI("begin to open file: %s", fileName);
38 
39     int fd;
40     TEST_ASSERT_EQUAL(-1, UtilsFileOpen(fileName, O_RDONLY_FS, 0));
41     RUN_AND_PRINT_ELAPSED_TIME(fd, UtilsFileOpen(fileName, O_RDONLY_FS | O_CREAT_FS, 0));
42     TEST_ASSERT_GREATER_OR_EQUAL(0, fd);
43 
44     int ret;
45     LOGI("begin to close file: %s", fileName);
46     RUN_AND_PRINT_ELAPSED_TIME(ret, UtilsFileClose(fd));
47     TEST_ASSERT_EQUAL(0, ret);
48 }
49 
TestHcFileUtilsFileReadAndFileWrite(void)50 static void TestHcFileUtilsFileReadAndFileWrite(void)
51 {
52     const char *fileName = TEST_FILE_NAME;
53     for (int i = 0; i < TEST_FILE_SIZE_LIST_LEN; i++) {
54         int fd = UtilsFileOpen(fileName, O_RDWR_FS | O_CREAT_FS, 0);
55         TEST_ASSERT_GREATER_OR_EQUAL(0, fd);
56         unsigned int size = testFileSizeList[i];
57 
58         LOGI("begin to write file [%s] for size: %d", fileName, size);
59         char *writeBuffer = GenerateTestingText(size);
60         TEST_ASSERT_NOT_NULL(writeBuffer);
61         RUN_AND_PRINT_ELAPSED_TIME_WITHOUT_RESULT(UtilsFileWrite(fd, writeBuffer, size));
62         TEST_ASSERT_EQUAL(0, UtilsFileClose(fd));
63 
64         fd = UtilsFileOpen(fileName, O_RDWR_FS | O_CREAT_FS, 0);
65         TEST_ASSERT_GREATER_OR_EQUAL(0, fd);
66         unsigned int fileSize;
67         TEST_ASSERT_EQUAL(0, UtilsFileStat(fileName, &fileSize));
68         TEST_ASSERT_EQUAL(size, fileSize);
69 
70         LOGI("begin to read file [%s] for size: %d", fileName, fileSize);
71         char *readBuffer = (char *)malloc(fileSize + 1);
72         TEST_ASSERT_NOT_NULL(readBuffer);
73         TEST_ASSERT_EQUAL(0, memset_s(readBuffer, fileSize + 1, 0, fileSize + 1));
74         RUN_AND_PRINT_ELAPSED_TIME_WITHOUT_RESULT(UtilsFileRead(fd, readBuffer, fileSize));
75         TEST_ASSERT_EQUAL_STRING(writeBuffer, readBuffer);
76 
77         free(readBuffer);
78         free(writeBuffer);
79         TEST_ASSERT_EQUAL(0, UtilsFileClose(fd));
80         sleep(1);
81     }
82 }
83 
TestHcFileUtilsFileSize(void)84 static void TestHcFileUtilsFileSize(void)
85 {
86     const char *fileName = TEST_FILE_NAME;
87     LOGI("begin to count file size");
88 
89     unsigned int size;
90     int ret;
91     RUN_AND_PRINT_ELAPSED_TIME(ret, UtilsFileStat(fileName, &size));
92     TEST_ASSERT_EQUAL(0, ret);
93     TEST_ASSERT_EQUAL(TEN_KILOBYTE, size);
94     LOGI("the file size is: %d", size);
95 
96     RUN_AND_PRINT_ELAPSED_TIME(ret, UtilsFileDelete(fileName));
97     TEST_ASSERT_EQUAL(0, ret);
98 }
99 
TestHcFileUtilsFileDelete(void)100 static void TestHcFileUtilsFileDelete(void)
101 {
102     const char *fileName = TEST_FILE_NAME;
103     LOGI("begin to remove file: %s", fileName);
104 
105     int ret;
106     RUN_AND_PRINT_ELAPSED_TIME(ret, UtilsFileDelete(fileName));
107     TEST_ASSERT_EQUAL(0, ret);
108 }
109 
TestHcFileUtilsFile(void)110 void TestHcFileUtilsFile(void)
111 {
112     LOGI("test opening and closing file");
113     TestHcFileUtilsFileOpenAndUtilsFileClose();
114 
115     LOGI("test removing file");
116     TestHcFileUtilsFileDelete();
117 
118     LOGI("test reading and writing file");
119     TestHcFileUtilsFileReadAndFileWrite();
120 
121     LOGI("test counting file size");
122     TestHcFileUtilsFileSize();
123 }
124 
125 #else // UTILS_FILE_IMPLEMENTED
126 void TestHcFileUtilsFile(void)
127 {
128     LOGE("no UTILS_FILE_IMPLEMENTED, do not test utils file!");
129 }
130 #endif // UTILS_FILE_IMPLEMENTED
131 
132 #ifdef __cplusplus
133 }
134 #endif
135