📄 tcases.c
字号:
// variable 'xx'
if (flags != FFS_O_RDONLY) return 1;
if (size != 20) return 1;
if (error != -10) return 1;
if (whence != FFS_SEEK_SET) return 1;
if (fd != FFS_FD_OFFSET) return 1;
if (objt != OT_FILE) return 1;
stat.type = 1;
if (stat.type != 1) return 1;
dir.this = 5;
if (dir.this != 5) return 1;
callback.addr_id = 3;
if (callback.addr_id != 3) return 1;
confirm.error = -1;
if (confirm.error != -1) return 1;
return 0;
}
/******************************************************************************
* Pseudo Tests
******************************************************************************/
// Recursively read objects in directory. Does NOT check for buffer
// overflow!
int case_dir_list(char *dirname, struct object_s **pplist, char **ppnames)
{
struct object_s *plist, *plist_start, *plist_end;
char pathname[6 * 20];
struct dir_s dir;
char *pnames, *pn;
int i, pathlen;
//tw(tr(TR_BEGIN, TrTestHigh, "dir_list('%s', %d, 0x%x)\n",
// dirname, (int)*pplist/sizeof(struct object_s), *ppnames));
plist_start = plist = *pplist;
pnames = *ppnames;
strcpy(pathname, dirname);
pathlen = strlen(pathname);
// remove trailing slash. It is tiring to handle the root directory
// differently from other directories. In a future ffs revision, this
// trailing slash should be allowed!
if (pathname[pathlen - 1] == '/') {
pathname[pathlen - 1] = 0;
pathlen--;
}
if (strlen(pathname) == 0)
error = tffs_opendir("/", &dir);
else
error = tffs_opendir(pathname, &dir);
expect_ok(error);
pn = pathname + strlen(pathname);
*pn++ = '/';
*pn = 0;
error = 1;
for (i = 0; (error = tffs_readdir(&dir, pn, 21)) > 0; i++)
{
error = tffs_xlstat(pathname, &plist->stat);
expect(error, EFFS_OK);
// Copy full object pathname to buffer, working downwards.
pnames -= strlen(pathname) + 1;
// Check for buffer overflow (if pnames <= plist)
expect_gt((int) pnames, (int) plist);
strcpy(pnames, pathname);
plist->name = pnames;
plist++;
}
*pplist = plist;
*ppnames = pnames;
// For each directory in the retrieved list, recurse.
plist_end = plist;
for (plist = plist_start; plist < plist_end; plist++) {
if (plist->stat.type == OT_DIR)
i += case_dir_list(plist->name, pplist, ppnames);
}
// tw(tr(TR_END, TrTestHigh, "} %d\n", i));
return i;
}
int case_find(int p0, int p1)
{
struct object_s *plist, *plist_start;
char *pnames, *pnames_start;
int n, names_used, list_used;
plist = plist_start = (struct object_s *) bigbuf;
pnames = pnames_start = bigbuf + bigbuf_size;
n = case_dir_list("/", &plist, &pnames);
list_used = n * sizeof(struct object_s);
names_used = pnames_start - pnames;
tw(tr(TR_FUNC, TrTestHigh, "Buffer space used: %d + %d = %d\n",
list_used, names_used, list_used + names_used));
ttw(ttr(TTrTest, "Buffer space used: %d + %d = %d" NL,
list_used, names_used, list_used + names_used));
return 0;
}
// TODO: We should accumulate all stat.space and check it vs. the number of
// bytes used!
int case_lsr(int p0, int p1)
{
struct object_s *plist, *plist_start;
char *pnames, *pnames_start;
char of[3];
int i, n;
plist = plist_start = (struct object_s *) bigbuf;
pnames = pnames_start = bigbuf + bigbuf_size;
n = case_dir_list("/", &plist, &pnames);
tw(tr(TR_FUNC, TrTestHigh, "Total %d objects.\n", n));
ttw(ttr(TTrTest, "Total %d objects" NL, n));
plist = plist_start;
for (i = 0; i < n; i++, plist++) {
strcpy(of, " ");
switch (plist->stat.type) {
case OT_FILE: of[0] = ' '; break;
case OT_DIR: of[0] = 'd'; break;
case OT_LINK: of[0] = 'l'; break;
}
if (plist->stat.flags & OF_READONLY)
of[1] = 'r';
#if (TARGET == 0)
printf("%3d: %s %3d %2d/%04X (%4d,%4d) %5d %s\n",
i, of,
plist->stat.inode, plist->stat.block,
plist->stat.location, plist->stat.sequence, plist->stat.updates,
plist->stat.size, &plist->name[1]);
#else
ttw(ttr(TTrTest, "%3d: %s %3d %2d/%04X (%4d,%4d) %5d %s" NL,
i, of,
plist->stat.inode, plist->stat.block,
plist->stat.location, plist->stat.sequence, plist->stat.updates,
plist->stat.size, &plist->name[1]));
#endif
}
return 0;
}
/******************************************************************************
* Normal Tests
******************************************************************************/
int case_format(int p0, int p1)
{
error = tffs_preformat(0xD00D);
expect(error, EFFS_INVALID);
error = tffs_preformat(0xDEAD);
expect(error, EFFS_OK);
error = tffs_format("/ffs/", 0xDEAD);
expect(error, EFFS_INVALID);
error = tffs_format("ffs", 0x2BAD);
expect(error, EFFS_BADNAME);
error = tffs_format("", 0x2BAD);
expect(error, EFFS_BADNAME);
error = tffs_format("/", 0x2BAD);
expect(error, EFFS_OK);
error = tffs_format("/ffs", 0x2BAD);
expect(error, EFFS_NOPREFORMAT);
error = tffs_preformat(0xDEAD);
expect(error, EFFS_OK);
error = tffs_format(0, 0x2BAD);
//error = tffs_format("/ffs/i256o128", 0x2BAD);
expect(error, EFFS_OK);
return 0;
}
// Test that it is illegal to modify root inode as if it was a normal file
// or directory
int case_root(int p0, int p1)
{
error = tffs_opendir("/", &dir);
expect_ok(error);
error = tffs_file_write("/", "foo", 3, FFS_O_CREATE | FFS_O_EXCL);
expect(error, EFFS_EXISTS);
error = tffs_file_write("/", "bar", 3, FFS_O_TRUNC);
expect(error, EFFS_NOTAFILE);
error = tffs_file_write("/", "foo", 3, FFS_O_CREATE | FFS_O_TRUNC);
expect(error, EFFS_NOTAFILE);
error = tffs_remove("/");
expect(error, EFFS_ACCESS);
return 0;
}
// Test object lookup, object names etc.
int case_lookup(int p0, int p1)
{
// Swedish Provinces: Blekinge, Smaaland, Halland, Vaermland, Dalarna,
// Dalsland, Gotland, Gaestrikland, Haelsingland, Bohuslaen,
// Haerjedalen, Jaemtland, Lappland, Medelpad, Norrbotten, Naerke,
// Soedermanland, Uppland, Vaesterbotten
// Smaaland Cities: Vetlanda, Bodafors, Rottne, Ljungby, Nybro,
// Hultsfred, Oskarshamn, Vimmerby, Hyltebruk, Joenkoeping, Vaexjoe
tffs_mkdir("/europe");
tffs_mkdir("/europe/sweden");
// test init
error = tffs_mkdir(LUDIR "/Smaaland");
expect(error, EFFS_OK);
// test BAD_FILENAME
error = tffs_file_write("", TDATA(0), FFS_O_CREATE | FFS_O_EXCL);
expect(error, EFFS_BADNAME);
// test EFFS_EXISTS
error = tffs_file_write(LUDIR "/Smaaland/vetlanda", TDATA(0),
FFS_O_CREATE | FFS_O_EXCL);
expect(error, EFFS_OK);
error = tffs_file_write(LUDIR "/Smaaland/vetlanda", TDATA(0),
FFS_O_CREATE | FFS_O_EXCL);
expect(error, EFFS_EXISTS);
error = tffs_mkdir(LUDIR "/Smaaland");
expect(error, EFFS_EXISTS);
error = tffs_file_write(LUDIR "/Smaaland/vetlanda", TDATA(1), FFS_O_CREATE | FFS_O_TRUNC);
expect(error, EFFS_OK);
// test EFFS_BADNAME
error = tffs_file_write(LUDIR "/Smaaland/A_Zaz.0+9-7!", TDATA(2),
FFS_O_CREATE | FFS_O_EXCL);
expect(error, EFFS_BADNAME);
error = tffs_file_write(LUDIR "/Smaaland/A_Zaz.0+9-7#%$", TDATA(2),
FFS_O_CREATE | FFS_O_EXCL);
expect(error, EFFS_OK);
// test ending slash
error = tffs_mkdir(LUDIR "/Smaaland/Vaexjoe/");
expect(error, EFFS_NOTADIR);
error = tffs_file_write(LUDIR "/Smaaland/Vaexjoe/", TDATA(3),
FFS_O_CREATE | FFS_O_EXCL);
expect(error, EFFS_NOTADIR);
// test EFFS_NAMETOOLONG
error = tffs_file_write(LUDIR "/Smaaland/Hultsfred-is-21-chars", TDATA(4),
FFS_O_CREATE | FFS_O_EXCL);
expect(error, EFFS_NAMETOOLONG);
error = tffs_file_write(LUDIR "/Smaaland/Bodafors-is-20-chars", TDATA(4),
FFS_O_CREATE | FFS_O_EXCL);
expect(error, EFFS_OK);
error = tffs_mkdir(LUDIR "/Vaermland-is-21-chars");
expect(error, EFFS_NAMETOOLONG);
error = tffs_mkdir(LUDIR "/Dalsland-is-20-chars");
expect(error, EFFS_OK);
// test EFFS_NOTADIR
error = tffs_mkdir(LUDIR "/DontMakeSeveral/DirsAt/TheSameTime");
expect(error, EFFS_NOTADIR);
error = tffs_fread(LUDIR "/Lappland/Bodafors-is-20-chars", bigbuf, 1024);
tw(tr(TR_END, TrApi, "} %d\n", error)); // Avoid wrong indent
expect(error, EFFS_NOTADIR);
error = tffs_fread(LUDIR "/Lappland/", bigbuf, 1024);
tw(tr(TR_END, TrApi, "} %d\n", error)); // Avoid wrong indent
expect(error, EFFS_NOTADIR);
error = tffs_fread(LUDIR "/Smaaland/Bodafors", TDATA(4));
tw(tr(TR_END, TrApi, "} %d\n", error)); // Avoid wrong indent
expect(error, EFFS_NOTFOUND);
// test EFFS_PATHTOODEEP
error = tffs_mkdir(LUDIR "/Gotland"); // 3. level
expect(error, EFFS_OK);
error = tffs_mkdir(LUDIR "/Gotland/Visby"); // 4. level
expect(error, EFFS_OK);
error = tffs_mkdir(LUDIR "/Gotland/Visby/level5"); // 5. level
expect(error, EFFS_OK);
error = tffs_mkdir(LUDIR "/Gotland/Visby/level5/level6"); // 6. level
expect(error, EFFS_OK);
error = tffs_mkdir(LUDIR "/Gotland/Visby/level5/level6/level7"); // 7. level
expect(error, EFFS_PATHTOODEEP);
error = tffs_file_write(LUDIR "/Gotland/Visby/level5/level6/level7",
TDATA(5), FFS_O_CREATE | FFS_O_EXCL); // 7. level
expect(error, EFFS_PATHTOODEEP);
// final checks
error = test_expect_file(LUDIR "/Smaaland/vetlanda", TDATA(1));
if (error) return 1;
error = test_expect_file(LUDIR "/Smaaland/Bodafors-is-20-chars", TDATA(4));
if (error) return 1;
error = tffs_opendir(LUDIR "/Dalsland-is-20-chars", &dir);
expect_ok(error);
error = tffs_opendir(LUDIR "/Gotland/Visby/level5/level6", &dir);
expect_ok(error);
// cleanup
error = tffs_remove(LUDIR "/Smaaland/A_Zaz.0+9-7");
return 0;
}
// Test fcontrol and read-only semantics. TODO: We still need to perform
// same tests on a dir and a symlink and thru a symlink.
int case_fcontrol(int p0, int p1)
{
struct ffs_state_s old, new;
const char rofile[] = "/europe/norway/rofile";
const char imeifile[] = "/europe/norway/IMEI";
fd_t fdi;
// Cleanup
tffs_remove(rofile);
tffs_remove("/europe/norway");
tffs_remove("/europe");
// Initialize
error = tffs_mkdir("/europe");
error = tffs_mkdir("/europe/norway");
error = tffs_file_write(rofile, TDATA(2), FFS_O_CREATE | FFS_O_EXCL);
expect(error, EFFS_OK);
error = tffs_stat(rofile, &tstat);
expect(error, EFFS_OK);
expect_eq(tstat.flags, 0);
test_ffs_state_get(&old);
// set read-only flag
error = tffs_fcontrol(rofile, OC_FLAGS, OF_READONLY);
expect(error, EFFS_OK);
error = tffs_stat(rofile, &tstat);
expect(error, EFFS_OK);
expect_eq(tstat.flags, OF_READONLY);
test_ffs_state_get(&new);
expect_eq(new.objects_total, old.objects_total);
expect_ne(new.inodes_used, old.inodes_used);
test_ffs_state_copy(&old, &new);
// Set illegal flags. Try to fupdate file. Then try to set read-only
// flag again.
error = tffs_fcontrol(rofile, OC_FLAGS, 1<<3);
expect(error, EFFS_INVALID);
error = tffs_fcontrol(rofile, OC_FLAGS, 1<<5);
expect(error, EFFS_INVALID);
error = tffs_fcontrol(rofile, OC_FLAGS, 1<<6);
expect(error, EFFS_INVALID);
error = tffs_file_write(rofile, TDATA(3), FFS_O_TRUNC);
expect(error, EFFS_OK);
error = tffs_file_write("/europe/norway/tease", TDATA(2), FFS_O_CREATE | FFS_O_EXCL);
expect(error, EFFS_OK);
tffs_remove("/europe/norway/tease");
error = tffs_fcontrol(rofile, OC_FLAGS, OF_READONLY);
expect(error, EFFS_OK);
error = tffs_stat(rofile, &tstat);
expect(error, EFFS_OK);
expect_eq(tstat.flags, OF_READONLY);
test_ffs_state_get(&new);
expect_eq(new.objects_total, old.objects_total);
expect_ne(new.inodes_used, old.inodes_used);
test_ffs_state_copy(&old, &new);
// clear read-only flag (this works because ffs_is_modifiable() by
// default returns true for all objects except for object names ending
// in "IMEI".
error = tffs_fcontrol(rofile, OC_FLAGS, 0);
expect(error, EFFS_OK);
error = tffs_stat(rofile, &tstat);
expect(error, EFFS_OK);
expect_eq(tstat.flags, 0);
test_ffs_state_get(&new);
expect_eq(new.objects_total, old.objects_total);
expect_ne(new.inodes_used, old.inodes_used);
test_ffs_state_copy(&old, &new);
// Set read-only flag (again)
error = tffs_fcontrol(rofile, OC_FLAGS, OF_READONLY);
expect(error, EFFS_OK);
error = tffs_stat(rofile, &tstat);
expect(error, EFFS_OK);
expect_eq(tstat.flags, OF_READONLY);
test_ffs_state_get(&new);
expect_eq(new.objects_total, old.objects_total);
expect_ne(new.inodes_used, old.inodes_used);
test_ffs_state_copy(&old, &new);
// Set read-only flag of IMEI file
error = tffs_file_write(imeifile, TDATA(2), FFS_O_CREATE | FFS_O_EXCL);
expect(error, EFFS_OK);
error = tffs_stat(imeifile, &tstat);
expect(error, EFFS_OK);
expect_eq(tstat.flags, 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -