-
Notifications
You must be signed in to change notification settings - Fork 207
/
tbl_content_mang_test.c
485 lines (408 loc) · 23 KB
/
tbl_content_mang_test.c
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*************************************************************************
**
** GSC-18128-1, "Core Flight Executive Version 6.7"
**
** Copyright (c) 2006-2019 United States Government as represented by
** the Administrator of the National Aeronautics and Space Administration.
** All Rights Reserved.
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**
** File: tbl_content_mang_test.c
**
** Purpose:
** Functional test of Table Manage Content APIs
**
** Demonstration of how to register and use the UT assert functions.
**
*************************************************************************/
/*
* Includes
*/
#include "cfe_test.h"
#include "cfe_test_table.h"
static const char TESTTBL_OTHER_NAME[] = "OtherTbl";
/* A set of additional (generated) table images to test permutations of CFE_TBL_Load() */
static const char TESTTBL_NOMINAL_FILE[] =
"/cf/cfe_test_tbl.tbl"; /**< nominal image file, from table tool (elf2cfetbl or similar) */
static const char TESTTBL_NOTEXIST_FILE[] =
"/ram/notexist.tbl"; /**< a valid filename, but file does not actually exist */
static const char TESTTBL_BAD_STDHDR_FILE[] = "/ram/testtbl_shdr.tbl"; /**< bad image without a complete FS header */
static const char TESTTBL_BAD_TBLHDR_FILE[] = "/ram/testtbl_thdr.tbl"; /**< bad image without a complete TBL header */
static const char TESTTBL_BAD_CONTENT_FILE[] = "/ram/testtbl_cid.tbl"; /**< image with incorrect "content ID" field */
static const char TESTTBL_BAD_SUBTYPE_FILE[] = "/ram/testtbl_st.tbl"; /**< image with incorrect "subtype" field */
static const char TESTTBL_ALTERNATE_FILE[] = "/ram/testtbl_alt.tbl"; /**< good/complete image with different content */
static const char TESTTBL_OTHERTBL_FILE[] =
"/ram/testtbl_other.tbl"; /**< good/complete image with different content for a different table */
static const char TESTTBL_TRUNCATED_FILE[] =
"/ram/testtbl_trunc.tbl"; /**< truncated version (header info has more bytes than file) */
static const char TESTTBL_LONG_FILE[] =
"/ram/testtbl_long.tbl"; /**< long version (file has more bytes than header info) */
static const char TESTTBL_SHORT_FILE[] =
"/ram/testtbl_short.tbl"; /**< short version (header info matches file but smaller than tbl) */
static const char TESTTBL_PARTIAL_FILE[] =
"/ram/testtbl_part.tbl"; /**< partial (offset nonzero, remainder of data from short file) */
void TestLoad(void)
{
CFE_TBL_Handle_t BadTblHandle;
const char * BadTblName = "BadTableName";
CFE_TBL_Handle_t DumpTblHandle;
const char * DumpTblName = "DumpOnlyTable";
CFE_TBL_Handle_t SharedTblHandle;
const char * SharedTblName = CFE_FT_Global.RegisteredTblName;
TBL_TEST_Table_t TestTable = {0xd00d, 0xdad};
TBL_TEST_Table_t *TablePtr;
CFE_TBL_Handle_t OtherHandle;
UtPrintf("Testing: CFE_TBL_Load");
UtAssert_INT32_EQ(
CFE_TBL_Register(&BadTblHandle, BadTblName, sizeof(TBL_TEST_Table_t), CFE_TBL_OPT_DBL_BUFFER, NULL),
CFE_SUCCESS);
/* Create a second table handle, to keep things interesting */
UtAssert_INT32_EQ(CFE_TBL_Register(&OtherHandle, TESTTBL_OTHER_NAME, sizeof(TBL_TEST_Table_t), 0, NULL),
CFE_SUCCESS);
/* Some basic failure checks */
UtAssert_INT32_EQ(CFE_TBL_Load(CFE_FT_Global.TblHandle, CFE_TBL_SRC_FILE, TESTTBL_BAD_STDHDR_FILE),
CFE_TBL_ERR_NO_STD_HEADER);
UtAssert_INT32_EQ(CFE_TBL_Load(CFE_FT_Global.TblHandle, CFE_TBL_SRC_FILE, TESTTBL_BAD_TBLHDR_FILE),
CFE_TBL_ERR_NO_TBL_HEADER);
UtAssert_INT32_EQ(CFE_TBL_Load(CFE_FT_Global.TblHandle, CFE_TBL_SRC_FILE, TESTTBL_BAD_CONTENT_FILE),
CFE_TBL_ERR_BAD_CONTENT_ID);
UtAssert_INT32_EQ(CFE_TBL_Load(CFE_FT_Global.TblHandle, CFE_TBL_SRC_FILE, TESTTBL_BAD_SUBTYPE_FILE),
CFE_TBL_ERR_BAD_SUBTYPE_ID);
UtAssert_INT32_EQ(CFE_TBL_Load(CFE_FT_Global.TblHandle, ~CFE_TBL_SRC_FILE, TESTTBL_NOMINAL_FILE),
CFE_TBL_ERR_ILLEGAL_SRC_TYPE);
/* Load from partial file (offset nonzero, before any successful load) -
* This should be restricted and return an error */
UtAssert_INT32_EQ(CFE_TBL_Load(CFE_FT_Global.TblHandle, CFE_TBL_SRC_FILE, TESTTBL_PARTIAL_FILE),
CFE_TBL_ERR_PARTIAL_LOAD);
/* Load from short file (offset 0, but incomplete, also before any successful load) */
/* In the current TBL implementation, this actually returns SUCCESS here (which is misleading) */
UtAssert_INT32_EQ(CFE_TBL_Load(CFE_FT_Global.TblHandle, CFE_TBL_SRC_FILE, TESTTBL_SHORT_FILE), CFE_SUCCESS);
/* NOTE: _NOT_ checking content after above; although it returned a success code, it is not well defined
* as to what the content will be because the data was never fully loaded yet */
/* Load from file (nominal) - first full data load */
UtAssert_INT32_EQ(CFE_TBL_Load(CFE_FT_Global.TblHandle, CFE_TBL_SRC_FILE, TESTTBL_NOMINAL_FILE), CFE_SUCCESS);
/* confirm content (football) */
UtAssert_INT32_EQ(CFE_TBL_GetAddress((void **)&TablePtr, CFE_FT_Global.TblHandle), CFE_TBL_INFO_UPDATED);
UtAssert_UINT32_EQ(TablePtr->Int1, 0xf007);
UtAssert_UINT32_EQ(TablePtr->Int2, 0xba11);
UtAssert_INT32_EQ(CFE_TBL_ReleaseAddress(CFE_FT_Global.TblHandle), CFE_SUCCESS);
/* Load from file too big */
UtAssert_INT32_EQ(CFE_TBL_Load(CFE_FT_Global.TblHandle, CFE_TBL_SRC_FILE, TESTTBL_LONG_FILE),
CFE_TBL_ERR_FILE_TOO_LARGE);
/* confirm content again (note content should not have been updated) */
UtAssert_INT32_EQ(CFE_TBL_GetAddress((void **)&TablePtr, CFE_FT_Global.TblHandle), CFE_SUCCESS);
UtAssert_UINT32_EQ(TablePtr->Int1, 0xf007);
UtAssert_UINT32_EQ(TablePtr->Int2, 0xba11);
UtAssert_INT32_EQ(CFE_TBL_ReleaseAddress(CFE_FT_Global.TblHandle), CFE_SUCCESS);
/* Load again with alternate content */
UtAssert_INT32_EQ(CFE_TBL_Load(CFE_FT_Global.TblHandle, CFE_TBL_SRC_FILE, TESTTBL_ALTERNATE_FILE), CFE_SUCCESS);
/* confirm content again (changed to alternate data) */
UtAssert_INT32_EQ(CFE_TBL_GetAddress((void **)&TablePtr, CFE_FT_Global.TblHandle), CFE_TBL_INFO_UPDATED);
UtAssert_UINT32_EQ(TablePtr->Int1, 0xdead);
UtAssert_UINT32_EQ(TablePtr->Int2, 0xbeef);
UtAssert_INT32_EQ(CFE_TBL_ReleaseAddress(CFE_FT_Global.TblHandle), CFE_SUCCESS);
/* Load from file truncated */
UtAssert_INT32_EQ(CFE_TBL_Load(CFE_FT_Global.TblHandle, CFE_TBL_SRC_FILE, TESTTBL_TRUNCATED_FILE),
CFE_TBL_ERR_LOAD_INCOMPLETE);
/* confirm content again (should not be changed) */
UtAssert_INT32_EQ(CFE_TBL_GetAddress((void **)&TablePtr, CFE_FT_Global.TblHandle), CFE_SUCCESS);
UtAssert_UINT32_EQ(TablePtr->Int1, 0xdead);
UtAssert_UINT32_EQ(TablePtr->Int2, 0xbeef);
UtAssert_INT32_EQ(CFE_TBL_ReleaseAddress(CFE_FT_Global.TblHandle), CFE_SUCCESS);
/* Load the other table (nominal data) */
UtAssert_INT32_EQ(CFE_TBL_Load(OtherHandle, CFE_TBL_SRC_FILE, TESTTBL_OTHERTBL_FILE), CFE_SUCCESS);
/* confirm content of first table again (should not be changed) */
UtAssert_INT32_EQ(CFE_TBL_GetAddress((void **)&TablePtr, CFE_FT_Global.TblHandle), CFE_SUCCESS);
UtAssert_UINT32_EQ(TablePtr->Int1, 0xdead);
UtAssert_UINT32_EQ(TablePtr->Int2, 0xbeef);
UtAssert_INT32_EQ(CFE_TBL_ReleaseAddress(CFE_FT_Global.TblHandle), CFE_SUCCESS);
/* confirm content of other table (boatload) */
UtAssert_INT32_EQ(CFE_TBL_GetAddress((void **)&TablePtr, OtherHandle), CFE_TBL_INFO_UPDATED);
UtAssert_UINT32_EQ(TablePtr->Int1, 0xb0a7);
UtAssert_UINT32_EQ(TablePtr->Int2, 0x10ad);
UtAssert_INT32_EQ(CFE_TBL_ReleaseAddress(OtherHandle), CFE_SUCCESS);
/* Load from short file again (different response after successful load) */
UtAssert_INT32_EQ(CFE_TBL_Load(CFE_FT_Global.TblHandle, CFE_TBL_SRC_FILE, TESTTBL_SHORT_FILE), CFE_SUCCESS);
/* confirm content again (reported as updated from partial load) */
/* Should have updated the first word only */
UtAssert_INT32_EQ(CFE_TBL_GetAddress((void **)&TablePtr, CFE_FT_Global.TblHandle), CFE_TBL_INFO_UPDATED);
UtAssert_UINT32_EQ(TablePtr->Int1, 0x5555);
UtAssert_UINT32_EQ(TablePtr->Int2, 0xbeef);
UtAssert_INT32_EQ(CFE_TBL_ReleaseAddress(CFE_FT_Global.TblHandle), CFE_SUCCESS);
/* Load from short file again (different response after successful load) */
UtAssert_INT32_EQ(CFE_TBL_Load(CFE_FT_Global.TblHandle, CFE_TBL_SRC_FILE, TESTTBL_PARTIAL_FILE), CFE_SUCCESS);
/* confirm content again (reported as updated from partial load) */
/* Should have updated the second word only */
UtAssert_INT32_EQ(CFE_TBL_GetAddress((void **)&TablePtr, CFE_FT_Global.TblHandle), CFE_TBL_INFO_UPDATED);
UtAssert_UINT32_EQ(TablePtr->Int1, 0x5555);
UtAssert_UINT32_EQ(TablePtr->Int2, 0x6666);
UtAssert_INT32_EQ(CFE_TBL_ReleaseAddress(CFE_FT_Global.TblHandle), CFE_SUCCESS);
/* Table name mismatches */
UtAssert_INT32_EQ(CFE_TBL_Load(BadTblHandle, CFE_TBL_SRC_FILE, TESTTBL_NOMINAL_FILE),
CFE_TBL_ERR_FILE_FOR_WRONG_TABLE);
UtAssert_INT32_EQ(CFE_TBL_Load(CFE_FT_Global.TblHandle, CFE_TBL_SRC_FILE, TESTTBL_OTHERTBL_FILE),
CFE_TBL_ERR_FILE_FOR_WRONG_TABLE);
UtAssert_INT32_EQ(CFE_TBL_Load(CFE_FT_Global.TblHandle, CFE_TBL_SRC_FILE, TESTTBL_NOTEXIST_FILE),
CFE_TBL_ERR_ACCESS);
UtAssert_INT32_EQ(CFE_TBL_Load(CFE_TBL_BAD_TABLE_HANDLE, CFE_TBL_SRC_FILE, TESTTBL_NOMINAL_FILE),
CFE_TBL_ERR_INVALID_HANDLE);
/* Load from memory */
UtAssert_INT32_EQ(CFE_TBL_Load(CFE_FT_Global.TblHandle, CFE_TBL_SRC_ADDRESS, &TestTable), CFE_SUCCESS);
UtAssert_INT32_EQ(CFE_TBL_Load(CFE_FT_Global.TblHandle, CFE_TBL_SRC_ADDRESS, NULL), CFE_TBL_BAD_ARGUMENT);
UtAssert_INT32_EQ(CFE_TBL_Load(CFE_TBL_BAD_TABLE_HANDLE, CFE_TBL_SRC_ADDRESS, &TestTable),
CFE_TBL_ERR_INVALID_HANDLE);
/* Attempt to load a dump only table */
UtAssert_INT32_EQ(
CFE_TBL_Register(&DumpTblHandle, DumpTblName, sizeof(TBL_TEST_Table_t), CFE_TBL_OPT_DUMP_ONLY, NULL),
CFE_SUCCESS);
UtAssert_INT32_EQ(CFE_TBL_Load(DumpTblHandle, CFE_TBL_SRC_FILE, TESTTBL_NOMINAL_FILE), CFE_TBL_ERR_DUMP_ONLY);
/* Load a shared table */
UtAssert_INT32_EQ(CFE_TBL_Share(&SharedTblHandle, SharedTblName), CFE_SUCCESS);
UtAssert_INT32_EQ(CFE_TBL_Load(SharedTblHandle, CFE_TBL_SRC_FILE, TESTTBL_NOMINAL_FILE), CFE_SUCCESS);
}
void TestUpdate(void)
{
UtPrintf("Testing: CFE_TBL_Update");
/* Haven't figured out how to get an update pending */
UtAssert_INT32_EQ(CFE_TBL_Update(CFE_FT_Global.TblHandle), CFE_TBL_INFO_NO_UPDATE_PENDING);
UtAssert_INT32_EQ(CFE_TBL_Update(CFE_TBL_BAD_TABLE_HANDLE), CFE_TBL_ERR_INVALID_HANDLE);
}
void TestValidate(void)
{
UtPrintf("Testing: CFE_TBL_Validate");
/* Haven't figured out how to get a validation pending */
UtAssert_INT32_EQ(CFE_TBL_Validate(CFE_FT_Global.TblHandle), CFE_TBL_INFO_NO_VALIDATION_PENDING);
UtAssert_INT32_EQ(CFE_TBL_Validate(CFE_TBL_BAD_TABLE_HANDLE), CFE_TBL_ERR_INVALID_HANDLE);
}
void TestManage(void)
{
UtPrintf("Testing: CFE_TBL_Manage");
UtAssert_INT32_EQ(CFE_TBL_Manage(CFE_FT_Global.TblHandle), CFE_SUCCESS);
UtAssert_INT32_EQ(CFE_TBL_Manage(CFE_TBL_BAD_TABLE_HANDLE), CFE_TBL_ERR_INVALID_HANDLE);
}
void TestDumpToBuffer(void)
{
UtPrintf("Testing: CFE_TBL_DumpToBuffer");
/* This should at least return an info code such as CFE_TBL_INFO_NO_UPDATE_PENDING when CFE_TBL_Update is called
* with no pending update instead of returning CFE_SUCCESS whether or not it actually dumped*/
UtAssert_INT32_EQ(CFE_TBL_DumpToBuffer(CFE_FT_Global.TblHandle), CFE_SUCCESS);
UtAssert_INT32_EQ(CFE_TBL_DumpToBuffer(CFE_TBL_BAD_TABLE_HANDLE), CFE_TBL_ERR_INVALID_HANDLE);
}
void TestModified(void)
{
UtPrintf("Testing: CFE_TBL_Modified");
UtAssert_INT32_EQ(CFE_TBL_Modified(CFE_FT_Global.TblHandle), CFE_SUCCESS);
UtAssert_INT32_EQ(CFE_TBL_Modified(CFE_TBL_BAD_TABLE_HANDLE), CFE_TBL_ERR_INVALID_HANDLE);
}
/* Helper function to set a CFE_ES_MemOffset_t value (must be big-endian) */
void TblTest_UpdateOffset(CFE_ES_MemOffset_t *TgtVal, CFE_ES_MemOffset_t SetVal)
{
union
{
CFE_ES_MemOffset_t offset;
uint8 bytes[sizeof(CFE_ES_MemOffset_t)];
} offsetbuf;
offsetbuf.bytes[3] = SetVal & 0xFF;
SetVal >>= 8;
offsetbuf.bytes[2] = SetVal & 0xFF;
SetVal >>= 8;
offsetbuf.bytes[1] = SetVal & 0xFF;
SetVal >>= 8;
offsetbuf.bytes[0] = SetVal & 0xFF;
*TgtVal = offsetbuf.offset;
}
/*
* A helper function that intentionally creates flawed table image files -
* This takes the good image file produced during the build, and creates
* variants with certain header fields modified and data truncated, to
* validate the error detection logic in CFE_TBL_Load().
*/
void TblTest_GenerateTblFiles(void)
{
osal_id_t fh1;
osal_id_t fh2;
uint32 PartialOffset;
uint32 PartialSize;
union
{
uint8 u8;
uint16 u16;
uint32 u32;
CFE_FS_Header_t FsHdr;
CFE_TBL_File_Hdr_t TblHdr;
TBL_TEST_Table_t Content;
} buf;
/* Open the original (correct) table image file for reference */
UtAssert_INT32_EQ(OS_OpenCreate(&fh1, TESTTBL_NOMINAL_FILE, 0, OS_READ_ONLY), OS_SUCCESS);
/* create a file which does not have a valid FS header */
UtAssert_INT32_EQ(
OS_OpenCreate(&fh2, TESTTBL_BAD_STDHDR_FILE, OS_FILE_FLAG_CREATE | OS_FILE_FLAG_TRUNCATE, OS_WRITE_ONLY),
OS_SUCCESS);
buf.u32 = 0x12345678;
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.u32)), sizeof(buf.u32));
UtAssert_INT32_EQ(OS_close(fh2), OS_SUCCESS);
/* create a file which has an FS header but not a valid TBL header */
OS_lseek(fh1, 0, OS_SEEK_SET);
UtAssert_INT32_EQ(
OS_OpenCreate(&fh2, TESTTBL_BAD_TBLHDR_FILE, OS_FILE_FLAG_CREATE | OS_FILE_FLAG_TRUNCATE, OS_WRITE_ONLY),
OS_SUCCESS);
UtAssert_INT32_EQ(OS_read(fh1, &buf, sizeof(buf.FsHdr)), sizeof(buf.FsHdr));
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.FsHdr)), sizeof(buf.FsHdr));
buf.u32 = 0x12345678;
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.u32)), sizeof(buf.u32));
UtAssert_INT32_EQ(OS_close(fh2), OS_SUCCESS);
/* Create a tbl image that has the wrong content ID field */
OS_lseek(fh1, 0, OS_SEEK_SET);
UtAssert_INT32_EQ(
OS_OpenCreate(&fh2, TESTTBL_BAD_CONTENT_FILE, OS_FILE_FLAG_CREATE | OS_FILE_FLAG_TRUNCATE, OS_WRITE_ONLY),
OS_SUCCESS);
/* copy headers and modify */
UtAssert_INT32_EQ(OS_read(fh1, &buf, sizeof(buf.FsHdr)), sizeof(buf.FsHdr));
buf.FsHdr.ContentType = 0x09abcdef;
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.FsHdr)), sizeof(buf.FsHdr));
UtAssert_INT32_EQ(OS_read(fh1, &buf, sizeof(buf.TblHdr)), sizeof(buf.TblHdr));
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.TblHdr)), sizeof(buf.TblHdr));
/* Identifiable content, different from original */
buf.Content.Int1 = 0x7777;
buf.Content.Int2 = 0x8888;
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.Content)), sizeof(buf.Content));
UtAssert_INT32_EQ(OS_close(fh2), OS_SUCCESS);
/* Create a tbl image that has the wrong content ID field */
OS_lseek(fh1, 0, OS_SEEK_SET);
UtAssert_INT32_EQ(
OS_OpenCreate(&fh2, TESTTBL_BAD_SUBTYPE_FILE, OS_FILE_FLAG_CREATE | OS_FILE_FLAG_TRUNCATE, OS_WRITE_ONLY),
OS_SUCCESS);
/* copy headers as-is */
UtAssert_INT32_EQ(OS_read(fh1, &buf, sizeof(buf.FsHdr)), sizeof(buf.FsHdr));
buf.FsHdr.SubType = 0x09abcdef;
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.FsHdr)), sizeof(buf.FsHdr));
UtAssert_INT32_EQ(OS_read(fh1, &buf, sizeof(buf.TblHdr)), sizeof(buf.TblHdr));
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.TblHdr)), sizeof(buf.TblHdr));
/* Identifiable content, different from original */
buf.Content.Int1 = 0x9999;
buf.Content.Int2 = 0xaaaa;
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.Content)), sizeof(buf.Content));
UtAssert_INT32_EQ(OS_close(fh2), OS_SUCCESS);
/* Create a tbl image that is complete but with different content */
OS_lseek(fh1, 0, OS_SEEK_SET);
UtAssert_INT32_EQ(
OS_OpenCreate(&fh2, TESTTBL_ALTERNATE_FILE, OS_FILE_FLAG_CREATE | OS_FILE_FLAG_TRUNCATE, OS_WRITE_ONLY),
OS_SUCCESS);
/* copy headers as-is */
UtAssert_INT32_EQ(OS_read(fh1, &buf, sizeof(buf.FsHdr)), sizeof(buf.FsHdr));
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.FsHdr)), sizeof(buf.FsHdr));
UtAssert_INT32_EQ(OS_read(fh1, &buf, sizeof(buf.TblHdr)), sizeof(buf.TblHdr));
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.TblHdr)), sizeof(buf.TblHdr));
/* Identifiable content, different from original */
buf.Content.Int1 = 0xdead;
buf.Content.Int2 = 0xbeef;
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.Content)), sizeof(buf.Content));
UtAssert_INT32_EQ(OS_close(fh2), OS_SUCCESS);
/* Create a tbl image that is complete but for the OTHER table (also different content) */
OS_lseek(fh1, 0, OS_SEEK_SET);
UtAssert_INT32_EQ(
OS_OpenCreate(&fh2, TESTTBL_OTHERTBL_FILE, OS_FILE_FLAG_CREATE | OS_FILE_FLAG_TRUNCATE, OS_WRITE_ONLY),
OS_SUCCESS);
/* copy headers as-is */
UtAssert_INT32_EQ(OS_read(fh1, &buf, sizeof(buf.FsHdr)), sizeof(buf.FsHdr));
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.FsHdr)), sizeof(buf.FsHdr));
UtAssert_INT32_EQ(OS_read(fh1, &buf, sizeof(buf.TblHdr)), sizeof(buf.TblHdr));
snprintf(buf.TblHdr.TableName, sizeof(buf.TblHdr.TableName), "%s.%s", "CFE_TEST_APP", TESTTBL_OTHER_NAME);
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.TblHdr)), sizeof(buf.TblHdr));
/* Identifiable content, different from original */
buf.Content.Int1 = 0xb0a7;
buf.Content.Int2 = 0x10ad;
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.Content)), sizeof(buf.Content));
UtAssert_INT32_EQ(OS_close(fh2), OS_SUCCESS);
/* Create a tbl image that is too big */
OS_lseek(fh1, 0, OS_SEEK_SET);
UtAssert_INT32_EQ(
OS_OpenCreate(&fh2, TESTTBL_LONG_FILE, OS_FILE_FLAG_CREATE | OS_FILE_FLAG_TRUNCATE, OS_WRITE_ONLY), OS_SUCCESS);
/* copy headers as-is */
UtAssert_INT32_EQ(OS_read(fh1, &buf, sizeof(buf.FsHdr)), sizeof(buf.FsHdr));
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.FsHdr)), sizeof(buf.FsHdr));
UtAssert_INT32_EQ(OS_read(fh1, &buf, sizeof(buf.TblHdr)), sizeof(buf.TblHdr));
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.TblHdr)), sizeof(buf.TblHdr));
/* write content, but make it identifiable/unique */
buf.Content.Int1 = 0x1111;
buf.Content.Int1 = 0x2222;
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.Content)), sizeof(buf.Content));
/* Write extra byte at the end */
buf.u8 = 0x33;
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.u8)), sizeof(buf.u8));
UtAssert_INT32_EQ(OS_close(fh2), OS_SUCCESS);
/* Create a tbl image that is truncated; header byte count is larger than file */
OS_lseek(fh1, 0, OS_SEEK_SET);
UtAssert_INT32_EQ(
OS_OpenCreate(&fh2, TESTTBL_TRUNCATED_FILE, OS_FILE_FLAG_CREATE | OS_FILE_FLAG_TRUNCATE, OS_WRITE_ONLY),
OS_SUCCESS);
/* copy headers as-is */
UtAssert_INT32_EQ(OS_read(fh1, &buf, sizeof(buf.FsHdr)), sizeof(buf.FsHdr));
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.FsHdr)), sizeof(buf.FsHdr));
UtAssert_INT32_EQ(OS_read(fh1, &buf, sizeof(buf.TblHdr)), sizeof(buf.TblHdr));
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.TblHdr)), sizeof(buf.TblHdr));
/* But write only one byte of data into the content part (so will be too small) */
buf.u8 = 0x44;
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.u8)), sizeof(buf.u8));
UtAssert_INT32_EQ(OS_close(fh2), OS_SUCCESS);
/* Make a file that is "short" (byte count is correct, just not enough bytes to fill table) */
OS_lseek(fh1, 0, OS_SEEK_SET);
UtAssert_INT32_EQ(
OS_OpenCreate(&fh2, TESTTBL_SHORT_FILE, OS_FILE_FLAG_CREATE | OS_FILE_FLAG_TRUNCATE, OS_WRITE_ONLY),
OS_SUCCESS);
/* copy headers, but modify TBL header */
/* NOTE: headers must be in big-endian/network byte order! */
UtAssert_INT32_EQ(OS_read(fh1, &buf, sizeof(buf.FsHdr)), sizeof(buf.FsHdr));
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.FsHdr)), sizeof(buf.FsHdr));
UtAssert_INT32_EQ(OS_read(fh1, &buf, sizeof(buf.TblHdr)), sizeof(buf.TblHdr));
PartialOffset = 0;
PartialSize = offsetof(TBL_TEST_Table_t, Int2);
TblTest_UpdateOffset(&buf.TblHdr.Offset, PartialOffset);
TblTest_UpdateOffset(&buf.TblHdr.NumBytes, PartialSize);
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.TblHdr)), sizeof(buf.TblHdr));
/* write partial content */
buf.Content.Int1 = 0x5555;
UtAssert_INT32_EQ(OS_write(fh2, &buf, PartialSize), PartialSize);
UtAssert_INT32_EQ(OS_close(fh2), OS_SUCCESS);
/* Make a file that is "partial" (contains remainder of bytes from above) */
OS_lseek(fh1, 0, OS_SEEK_SET);
UtAssert_INT32_EQ(
OS_OpenCreate(&fh2, TESTTBL_PARTIAL_FILE, OS_FILE_FLAG_CREATE | OS_FILE_FLAG_TRUNCATE, OS_WRITE_ONLY),
OS_SUCCESS);
/* copy headers, but modify TBL header */
UtAssert_INT32_EQ(OS_read(fh1, &buf, sizeof(buf.FsHdr)), sizeof(buf.FsHdr));
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.FsHdr)), sizeof(buf.FsHdr));
UtAssert_INT32_EQ(OS_read(fh1, &buf, sizeof(buf.TblHdr)), sizeof(buf.TblHdr));
PartialOffset = offsetof(TBL_TEST_Table_t, Int2);
PartialSize = sizeof(buf.Content) - offsetof(TBL_TEST_Table_t, Int2);
TblTest_UpdateOffset(&buf.TblHdr.Offset, PartialOffset);
TblTest_UpdateOffset(&buf.TblHdr.NumBytes, PartialSize);
UtAssert_INT32_EQ(OS_write(fh2, &buf, sizeof(buf.TblHdr)), sizeof(buf.TblHdr));
/* Copy partial content */
buf.Content.Int2 = 0x6666;
UtAssert_INT32_EQ(OS_write(fh2, &buf.Content.Int2, PartialSize), PartialSize);
UtAssert_INT32_EQ(OS_close(fh2), OS_SUCCESS);
/* Close the source file */
UtAssert_INT32_EQ(OS_close(fh1), OS_SUCCESS);
}
void TBLContentMangTestSetup(void)
{
TblTest_GenerateTblFiles();
UtTest_Add(TestLoad, RegisterTestTable, UnregisterTestTable, "Test Table Load");
UtTest_Add(TestUpdate, RegisterTestTable, UnregisterTestTable, "Test Table Update");
UtTest_Add(TestValidate, RegisterTestTable, UnregisterTestTable, "Test Table Validate");
UtTest_Add(TestManage, RegisterTestTable, UnregisterTestTable, "Test Table Manage");
UtTest_Add(TestDumpToBuffer, RegisterTestTable, UnregisterTestTable, "Test Table Dump to Buffer");
UtTest_Add(TestModified, RegisterTestTable, UnregisterTestTable, "Test Table Modified");
}