1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
//===-- Unittests for file operations like fopen, flcose etc --------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "src/stdio/clearerr.h"
#include "src/stdio/fclose.h"
#include "src/stdio/feof.h"
#include "src/stdio/ferror.h"
#include "src/stdio/fflush.h"
#include "src/stdio/fopen.h"
#include "src/stdio/fputs.h"
#include "src/stdio/fread.h"
#include "src/stdio/fseek.h"
#include "src/stdio/fwrite.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"
#include "src/errno/libc_errno.h"
#include <stdio.h>
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::EQ;
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::NE;
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::returns;
TEST(LlvmLibcFILETest, SimpleFileOperations) {
constexpr char FILENAME[] = "testdata/simple_operations.test";
::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w");
ASSERT_FALSE(file == nullptr);
constexpr char CONTENT[] = "1234567890987654321";
ASSERT_EQ(sizeof(CONTENT) - 1,
LIBC_NAMESPACE::fwrite(CONTENT, 1, sizeof(CONTENT) - 1, file));
// This is not a readable file.
char read_data[sizeof(CONTENT)];
ASSERT_THAT(LIBC_NAMESPACE::fread(read_data, 1, sizeof(CONTENT), file),
returns(EQ(size_t(0))).with_errno(NE(0)));
ASSERT_NE(LIBC_NAMESPACE::ferror(file), 0);
LIBC_NAMESPACE::libc_errno = 0;
LIBC_NAMESPACE::clearerr(file);
ASSERT_EQ(LIBC_NAMESPACE::ferror(file), 0);
ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
file = LIBC_NAMESPACE::fopen(FILENAME, "r");
ASSERT_FALSE(file == nullptr);
constexpr size_t READ_SIZE = 5;
char data[READ_SIZE];
data[READ_SIZE - 1] = '\0';
ASSERT_EQ(LIBC_NAMESPACE::fread(data, 1, READ_SIZE - 1, file), READ_SIZE - 1);
ASSERT_STREQ(data, "1234");
ASSERT_EQ(LIBC_NAMESPACE::fseek(file, 5, SEEK_CUR), 0);
ASSERT_EQ(LIBC_NAMESPACE::fread(data, 1, READ_SIZE - 1, file), READ_SIZE - 1);
ASSERT_STREQ(data, "0987");
ASSERT_EQ(LIBC_NAMESPACE::fseek(file, -5, SEEK_CUR), 0);
ASSERT_EQ(LIBC_NAMESPACE::fread(data, 1, READ_SIZE - 1, file), READ_SIZE - 1);
ASSERT_STREQ(data, "9098");
// Reading another time should trigger eof.
ASSERT_NE(sizeof(CONTENT),
LIBC_NAMESPACE::fread(read_data, 1, sizeof(CONTENT), file));
ASSERT_NE(LIBC_NAMESPACE::feof(file), 0);
// Should be an error to write.
ASSERT_THAT(LIBC_NAMESPACE::fwrite(CONTENT, 1, sizeof(CONTENT), file),
returns(EQ(size_t(0))).with_errno(NE(0)));
ASSERT_NE(LIBC_NAMESPACE::ferror(file), 0);
LIBC_NAMESPACE::libc_errno = 0;
LIBC_NAMESPACE::clearerr(file);
// Should be an error to puts.
ASSERT_THAT(LIBC_NAMESPACE::fputs(CONTENT, file),
returns(EQ(EOF)).with_errno(NE(0)));
ASSERT_NE(LIBC_NAMESPACE::ferror(file), 0);
LIBC_NAMESPACE::libc_errno = 0;
LIBC_NAMESPACE::clearerr(file);
ASSERT_EQ(LIBC_NAMESPACE::ferror(file), 0);
LIBC_NAMESPACE::libc_errno = 0;
ASSERT_THAT(LIBC_NAMESPACE::fwrite("nothing", 1, 1, file),
returns(EQ(0)).with_errno(NE(0)));
LIBC_NAMESPACE::libc_errno = 0;
ASSERT_EQ(LIBC_NAMESPACE::fclose(file), 0);
// Now try puts.
file = LIBC_NAMESPACE::fopen(FILENAME, "w");
ASSERT_FALSE(file == nullptr);
// fputs returns a negative value on error (EOF) or any non-negative value on
// success. This assert checks that the return value is non-negative.
ASSERT_GE(LIBC_NAMESPACE::fputs(CONTENT, file), 0);
LIBC_NAMESPACE::clearerr(file);
ASSERT_EQ(LIBC_NAMESPACE::ferror(file), 0);
// This is not a readable file.
LIBC_NAMESPACE::libc_errno = 0;
ASSERT_THAT(LIBC_NAMESPACE::fread(data, 1, 1, file),
returns(EQ(0)).with_errno(NE(0)));
LIBC_NAMESPACE::libc_errno = 0;
ASSERT_EQ(0, LIBC_NAMESPACE::fclose(file));
file = LIBC_NAMESPACE::fopen(FILENAME, "r");
ASSERT_FALSE(file == nullptr);
ASSERT_EQ(LIBC_NAMESPACE::fread(read_data, 1, sizeof(CONTENT) - 1, file),
sizeof(CONTENT) - 1);
read_data[sizeof(CONTENT) - 1] = '\0';
ASSERT_STREQ(read_data, CONTENT);
ASSERT_EQ(LIBC_NAMESPACE::fclose(file), 0);
// Check that the other functions correctly set libc_errno.
// LIBC_NAMESPACE::libc_errno = 0;
// ASSERT_NE(LIBC_NAMESPACE::fseek(file, 0, SEEK_SET), 0);
// ASSERT_ERRNO_FAILURE();
// LIBC_NAMESPACE::libc_errno = 0;
// ASSERT_NE(LIBC_NAMESPACE::fclose(file), 0);
// ASSERT_ERRNO_FAILURE();
// LIBC_NAMESPACE::libc_errno = 0;
// ASSERT_EQ(LIBC_NAMESPACE::fopen("INVALID FILE NAME", "r"),
// static_cast<FILE *>(nullptr));
// ASSERT_ERRNO_FAILURE();
}
TEST(LlvmLibcFILETest, FFlush) {
constexpr char FILENAME[] = "testdata/fflush.test";
::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w+");
ASSERT_FALSE(file == nullptr);
constexpr char CONTENT[] = "1234567890987654321";
ASSERT_EQ(sizeof(CONTENT),
LIBC_NAMESPACE::fwrite(CONTENT, 1, sizeof(CONTENT), file));
// Flushing at this point should write the data to disk. So, we should be
// able to read it back.
ASSERT_EQ(0, LIBC_NAMESPACE::fflush(file));
char data[sizeof(CONTENT)];
ASSERT_EQ(LIBC_NAMESPACE::fseek(file, 0, SEEK_SET), 0);
ASSERT_EQ(LIBC_NAMESPACE::fread(data, 1, sizeof(CONTENT), file),
sizeof(CONTENT));
ASSERT_STREQ(data, CONTENT);
ASSERT_EQ(LIBC_NAMESPACE::fclose(file), 0);
}
TEST(LlvmLibcFILETest, FOpenFWriteSizeGreaterThanOne) {
using MyStruct = struct {
char c;
unsigned long long i;
};
constexpr MyStruct WRITE_DATA[] = {{'a', 1}, {'b', 2}, {'c', 3}};
constexpr size_t WRITE_NMEMB = sizeof(WRITE_DATA) / sizeof(MyStruct);
constexpr char FILENAME[] = "testdata/fread_fwrite.test";
LIBC_NAMESPACE::libc_errno = 0;
FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w");
ASSERT_FALSE(file == nullptr);
ASSERT_EQ(size_t(0), LIBC_NAMESPACE::fwrite(WRITE_DATA, 0, 1, file));
ASSERT_THAT(
LIBC_NAMESPACE::fwrite(WRITE_DATA, sizeof(MyStruct), WRITE_NMEMB, file),
returns(EQ(WRITE_NMEMB)).with_errno(EQ(0)));
ASSERT_EQ(LIBC_NAMESPACE::fclose(file), 0);
file = LIBC_NAMESPACE::fopen(FILENAME, "r");
ASSERT_FALSE(file == nullptr);
MyStruct read_data[WRITE_NMEMB];
ASSERT_EQ(size_t(0), LIBC_NAMESPACE::fread(read_data, 0, 1, file));
ASSERT_THAT(
LIBC_NAMESPACE::fread(read_data, sizeof(MyStruct), WRITE_NMEMB, file),
returns(EQ(WRITE_NMEMB)).with_errno(EQ(0)));
// Trying to read more should fetch nothing.
ASSERT_THAT(
LIBC_NAMESPACE::fread(read_data, sizeof(MyStruct), WRITE_NMEMB, file),
returns(EQ(0)).with_errno(EQ(0)));
EXPECT_NE(LIBC_NAMESPACE::feof(file), 0);
EXPECT_EQ(LIBC_NAMESPACE::ferror(file), 0);
ASSERT_EQ(LIBC_NAMESPACE::fclose(file), 0);
// Verify that the data which was read is correct.
for (size_t i = 0; i < WRITE_NMEMB; ++i) {
ASSERT_EQ(read_data[i].c, WRITE_DATA[i].c);
ASSERT_EQ(read_data[i].i, WRITE_DATA[i].i);
}
}
|