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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
/**
* SPDX-License-Identifier: Apache-2.0
*
* @file fileop_test_unit.cpp
* @brief Unit test fot fileop.c
*/
#include <gtest/gtest.h>
#include "mock/syscall_io_mock.hpp"
// Test Terget files ---------------------------------------
extern "C" {
#include "../lib/fileop.c"
}
// Test Terget files ---------------------------------------
using namespace ::testing;
struct fileop_test_unit_test : Test, SyscallIOMockBase {};
//--------------------------------------------------------------------------------------------------------
//stubs
ssize_t g_safe_read_ret = 0;
ssize_t safe_read(int fd, void *buf, size_t count)
{
if (g_safe_read_ret == sizeof(s_refop_file_header)) {
refop_header_create((s_refop_file_header*)buf, 100, refop_get_config_data_size_limit()+1);
}
return g_safe_read_ret;
}
ssize_t g_safe_write_ret = 0;
ssize_t safe_write(int fd, void *buf, size_t count)
{
return g_safe_write_ret;
}
//--------------------------------------------------------------------------------------------------------
TEST_F(fileop_test_unit_test, unit_test_refop_new_file_write__arg_error)
{
int ret = -1;
refop_handle_t handle = NULL;
uint8_t dmybuf[128];
//dummy data
ret = refop_new_file_write(handle, dmybuf, refop_get_config_data_size_limit() + 1);
ASSERT_EQ(-2, ret);
ret = refop_new_file_write(handle, dmybuf, 0);
ASSERT_EQ(-2, ret);
ret = refop_new_file_write(handle, dmybuf, -1);
ASSERT_EQ(-2, ret);
}
//--------------------------------------------------------------------------------------------------------
TEST_F(fileop_test_unit_test, unit_test_refop_new_file_write__unlink_error)
{
int ret = -1;
refop_handle_t handle = (refop_handle_t)calloc(1,sizeof(struct refop_halndle));
uint8_t dmybuf[128];
EXPECT_CALL(sysiom, unlink(_)).WillOnce(SetErrnoAndReturn(EACCES, -1));
ret = refop_new_file_write(handle, dmybuf, refop_get_config_data_size_limit());
ASSERT_EQ(-1, ret);
free(handle);
}
//--------------------------------------------------------------------------------------------------------
TEST_F(fileop_test_unit_test, unit_test_refop_new_file_write__open_error)
{
int ret = -1;
refop_handle_t handle = (refop_handle_t)calloc(1,sizeof(struct refop_halndle));
uint8_t *dmybuf = (uint8_t*)calloc(1, refop_get_config_data_size_limit());
EXPECT_CALL(sysiom, unlink(_)).WillOnce(SetErrnoAndReturn(ENOENT, -1));
EXPECT_CALL(sysiom, open(_,_)).WillOnce(Return(-1));
ret = refop_new_file_write(handle, dmybuf, refop_get_config_data_size_limit());
ASSERT_EQ(-1, ret);
EXPECT_CALL(sysiom, unlink(_)).WillOnce(Return(0));
EXPECT_CALL(sysiom, open(_,_)).WillOnce(Return(-1));
ret = refop_new_file_write(handle, dmybuf, refop_get_config_data_size_limit());
ASSERT_EQ(-1, ret);
free(dmybuf);
free(handle);
}
//--------------------------------------------------------------------------------------------------------
TEST_F(fileop_test_unit_test, unit_test_refop_new_file_write__safe_write_error)
{
int ret = -1;
refop_handle_t handle = (refop_handle_t)calloc(1,sizeof(struct refop_halndle));
uint8_t *dmybuf = (uint8_t*)calloc(1, refop_get_config_data_size_limit());
//dummy
g_safe_read_ret = 0;
g_safe_write_ret = 0;
g_safe_write_ret = -1;
EXPECT_CALL(sysiom, unlink(_)).WillOnce(Return(0));
EXPECT_CALL(sysiom, open(_,_)).WillOnce(Return(100));
EXPECT_CALL(sysiom, close(100)).WillOnce(Return(0));
ret = refop_new_file_write(handle, dmybuf, refop_get_config_data_size_limit());
ASSERT_EQ(-1, ret);
g_safe_write_ret = 0;
EXPECT_CALL(sysiom, unlink(_)).WillOnce(Return(0));
EXPECT_CALL(sysiom, open(_,_)).WillOnce(Return(100));
EXPECT_CALL(sysiom, fsync(100)).WillOnce(Return(0));
EXPECT_CALL(sysiom, close(100)).WillOnce(Return(0));
ret = refop_new_file_write(handle, dmybuf, refop_get_config_data_size_limit());
ASSERT_EQ(0, ret);
g_safe_write_ret = 0;
EXPECT_CALL(sysiom, unlink(_)).WillOnce(Return(0));
EXPECT_CALL(sysiom, open(_,_)).WillOnce(Return(100));
EXPECT_CALL(sysiom, fsync(100)).WillOnce(Return(0));
EXPECT_CALL(sysiom, close(100)).WillOnce(Return(0));
ret = refop_new_file_write(handle, dmybuf, refop_get_config_data_size_limit());
ASSERT_EQ(0, ret);
free(dmybuf);
free(handle);
}
//--------------------------------------------------------------------------------------------------------
TEST_F(fileop_test_unit_test, unit_test_refop_file_rotation__stat_error)
{
int ret = -1;
refop_handle_t handle = (refop_handle_t)calloc(1,sizeof(struct refop_halndle));
//dummy
g_safe_read_ret = 0;
g_safe_write_ret = 0;
g_safe_write_ret = 0;
EXPECT_CALL(sysiom, stat(_, _))
.WillOnce(SetErrnoAndReturn(EACCES, -1))
.WillOnce(SetErrnoAndReturn(EACCES, -1));
ret = refop_file_rotation(handle);
ASSERT_EQ(-1, ret);
EXPECT_CALL(sysiom, stat(_, _))
.WillOnce(SetErrnoAndReturn(ENOENT, -1))
.WillOnce(SetErrnoAndReturn(EACCES, -1));
ret = refop_file_rotation(handle);
ASSERT_EQ(-1, ret);
EXPECT_CALL(sysiom, stat(_, _))
.WillOnce(SetErrnoAndReturn(EACCES, -1))
.WillOnce(SetErrnoAndReturn(ENOENT, -1));
ret = refop_file_rotation(handle);
ASSERT_EQ(-1, ret);
free(handle);
}
//--------------------------------------------------------------------------------------------------------
TEST_F(fileop_test_unit_test, unit_test_refop_file_rotation__open_error)
{
int ret = -1;
refop_handle_t handle = (refop_handle_t)calloc(1,sizeof(struct refop_halndle));
//dummy
g_safe_read_ret = 0;
g_safe_write_ret = 0;
g_safe_write_ret = 0;
// use a4 mode
EXPECT_CALL(sysiom, stat(_, _))
.WillOnce(SetErrnoAndReturn(ENOENT, -1))
.WillOnce(SetErrnoAndReturn(ENOENT, -1));
EXPECT_CALL(sysiom, rename(_, _))
.WillOnce(Return(0));
EXPECT_CALL(sysiom, open(_, _))
.WillOnce(Return(-1));
ret = refop_file_rotation(handle);
ASSERT_EQ(0, ret);
free(handle);
}
//--------------------------------------------------------------------------------------------------------
TEST_F(fileop_test_unit_test, unit_test_refop_file_rotation__a1_a2_a3_a4)
{
int ret = -1;
refop_handle_t handle = (refop_handle_t)calloc(1,sizeof(struct refop_halndle));
//dummy
g_safe_read_ret = 0;
g_safe_write_ret = 0;
g_safe_write_ret = 0;
strncpy(handle->backupfile1,"backup1",sizeof(handle->backupfile1));
strncpy(handle->latestfile,"latestfile",sizeof(handle->backupfile1));
strncpy(handle->newfile,"newfile",sizeof(handle->backupfile1));
// a1 mode
EXPECT_CALL(sysiom, stat(_, _))
.WillOnce(Return(0))
.WillOnce(Return(0));
EXPECT_CALL(sysiom, unlink(handle->backupfile1))
.WillOnce(Return(0));
EXPECT_CALL(sysiom, rename(handle->latestfile, handle->backupfile1))
.WillOnce(Return(0));
EXPECT_CALL(sysiom, rename(handle->newfile, handle->latestfile))
.WillOnce(Return(0));
EXPECT_CALL(sysiom, open(_,_)).WillOnce(Return(100));
EXPECT_CALL(sysiom, fsync(100)).WillOnce(Return(0));
EXPECT_CALL(sysiom, close(100)).WillOnce(Return(0));
ret = refop_file_rotation(handle);
ASSERT_EQ(0, ret);
// a2 mode
EXPECT_CALL(sysiom, stat(_, _))
.WillOnce(Return(0))
.WillOnce(SetErrnoAndReturn(ENOENT, -1));
//EXPECT_CALL(sysiom, unlink(handle->backupfile1))
// .WillOnce(Return(0));
EXPECT_CALL(sysiom, rename(handle->latestfile, handle->backupfile1))
.WillOnce(Return(0));
EXPECT_CALL(sysiom, rename(handle->newfile, handle->latestfile))
.WillOnce(Return(0));
EXPECT_CALL(sysiom, open(_,_)).WillOnce(Return(100));
EXPECT_CALL(sysiom, fsync(100)).WillOnce(Return(0));
EXPECT_CALL(sysiom, close(100)).WillOnce(Return(0));
ret = refop_file_rotation(handle);
ASSERT_EQ(0, ret);
// a3 mode
EXPECT_CALL(sysiom, stat(_, _))
.WillOnce(SetErrnoAndReturn(ENOENT, -1))
.WillOnce(Return(0));
//EXPECT_CALL(sysiom, unlink(handle->backupfile1))
// .WillOnce(Return(0));
//EXPECT_CALL(sysiom, rename(handle->latestfile, handle->backupfile1))
// .WillOnce(Return(0));
EXPECT_CALL(sysiom, rename(handle->newfile, handle->latestfile))
.WillOnce(Return(0));
EXPECT_CALL(sysiom, open(_,_)).WillOnce(Return(100));
EXPECT_CALL(sysiom, fsync(100)).WillOnce(Return(0));
EXPECT_CALL(sysiom, close(100)).WillOnce(Return(0));
ret = refop_file_rotation(handle);
ASSERT_EQ(0, ret);
// a4 mode
EXPECT_CALL(sysiom, stat(_, _))
.WillOnce(SetErrnoAndReturn(ENOENT, -1))
.WillOnce(SetErrnoAndReturn(ENOENT, -1));
//EXPECT_CALL(sysiom, unlink(handle->backupfile1))
// .WillOnce(Return(0));
//EXPECT_CALL(sysiom, rename(handle->latestfile, handle->backupfile1))
// .WillOnce(Return(0));
EXPECT_CALL(sysiom, rename(handle->newfile, handle->latestfile))
.WillOnce(Return(0));
EXPECT_CALL(sysiom, open(_,_)).WillOnce(Return(100));
EXPECT_CALL(sysiom, fsync(100)).WillOnce(Return(0));
EXPECT_CALL(sysiom, close(100)).WillOnce(Return(0));
ret = refop_file_rotation(handle);
ASSERT_EQ(0, ret);
free(handle);
}
//--------------------------------------------------------------------------------------------------------
TEST_F(fileop_test_unit_test, fileop_test_unit_test_refop_file_get_with_validation__1st_open_error)
{
int ret = -1;
//dummy data
char testfilename[] = "/tmp/test.bin";
uint8_t *pbuf = NULL;
int64_t sz = 256 * 1024;
int64_t szr = 0;
pbuf = (uint8_t*)malloc(sz);
EXPECT_CALL(sysiom, open(_,_)).WillOnce(SetErrnoAndReturn(EACCES, -1));
ret = refop_file_get_with_validation(testfilename, pbuf, sz, &szr);
ASSERT_EQ(-6, ret);
EXPECT_CALL(sysiom, open(_,_)).WillOnce(SetErrnoAndReturn(ENOMEM, -1));
ret = refop_file_get_with_validation(testfilename, pbuf, sz, &szr);
ASSERT_EQ(-6, ret);
EXPECT_CALL(sysiom, open(_,_)).WillOnce(SetErrnoAndReturn(ENOENT, -1));
ret = refop_file_get_with_validation(testfilename, pbuf, sz, &szr);
ASSERT_EQ(-1, ret);
free(pbuf);
}
//--------------------------------------------------------------------------------------------------------
TEST_F(fileop_test_unit_test, fileop_test_unit_test_refop_file_get_with_validation__safe_read_error)
{
int ret = -1;
//dummy data
char testfilename[] = "/tmp/test.bin";
uint8_t *pbuf = NULL;
int64_t sz = 256 * 1024;
int64_t szr = 0;
pbuf = (uint8_t*)malloc(sz);
g_safe_read_ret = 0;
g_safe_write_ret = 0;
EXPECT_CALL(sysiom, open(_,_)).WillOnce(Return(100));
g_safe_read_ret = sizeof(s_refop_file_header)*2;
EXPECT_CALL(sysiom, close(100)).WillOnce(Return(0));
ret = refop_file_get_with_validation(testfilename, pbuf, sz, &szr);
ASSERT_EQ(-2, ret);
EXPECT_CALL(sysiom, open(_,_)).WillOnce(Return(200));
g_safe_read_ret = sizeof(s_refop_file_header)/2;
EXPECT_CALL(sysiom, close(200)).WillOnce(Return(0));
ret = refop_file_get_with_validation(testfilename, pbuf, sz, &szr);
ASSERT_EQ(-2, ret);
free(pbuf);
}
//--------------------------------------------------------------------------------------------------------
TEST_F(fileop_test_unit_test, fileop_test_unit_test_refop_file_get_with_validation__header_error)
{
int ret = -1;
//dummy data
char testfilename[] = "/tmp/test.bin";
uint8_t *pbuf = NULL;
int64_t sz = 256 * 1024;
int64_t szr = 0;
pbuf = (uint8_t*)malloc(sz);
g_safe_read_ret = 0;
g_safe_write_ret = 0;
EXPECT_CALL(sysiom, open(_,_)).WillOnce(Return(100));
g_safe_read_ret = sizeof(s_refop_file_header);
EXPECT_CALL(sysiom, close(100)).WillOnce(Return(0));
ret = refop_file_get_with_validation(testfilename, pbuf, sz, &szr);
ASSERT_EQ(-4, ret);
free(pbuf);
}
|