?? tfstest.c
字號:
/* tfstest.c:
*
* This file is included with the common monitor code, but it is not
* actually part of the monitor. It is included here because it is
* an integral part of the monitor's stability... TFS test suite.
* It can be built on any platform the monitor supports by making this
* file a simple application that runs out of TFS.
*
* General notice:
* This code is part of a boot-monitor package developed as a generic base
* platform for embedded system designs. As such, it is likely to be
* distributed to various projects beyond the control of the original
* author. Please notify the author of any enhancements made or bugs found
* so that all may benefit from the changes. In addition, notification back
* to the author will allow the new user to pick up changes that may have
* been made by other users after this version of the code was distributed.
*
* Author: Ed Sutter
* email: esutter@lucent.com (home: lesutter@worldnet.att.net)
* phone: 908-582-2351 (home: 908-889-5161)
*/
#include "tfs.h"
typedef unsigned long ulong;
typedef unsigned short ushort;
typedef unsigned char uchar;
#define TMPFILE "newfile"
#define TRUNCATE_SIZE 5
#define die() errexit(__LINE__);
#define tfsdie(errno) errtfs(__LINE__,errno);
int verbose;
char buffer1[512];
char buffer2[512];
/* data1 & new_data1:
* These arrays are used to create files and test file operations.
* Do NOT modify these arrays, their content is used to verify files
* that are created and modified.
*/
char *data1 =
"abcdefghij\nBBBBBBBBBB\nCCCCCCCCCC\n1234567890\nEEEEEEEEEE\n";
char *new_data1 =
"abceefghij\nBBBBBBBBBB\nCCCCCCCCCC\n1234567890\nEEEEEEEEEE\n";
/* errexit() and errtfs():
* Functions used (via macros above) to report error conditions detected
* by the tests below.
*/
void
errexit(int line)
{
mon_printf("ERROR line %d\n",line);
mon_appexit(-1);
}
void
errtfs(int line, int tfserrno)
{
mon_printf("TFSERROR_%d (line %d): %s\n",(int)tfserrno,
line,(char *)mon_tfsctrl(TFS_ERRMSG,tfserrno,0));
mon_appexit(-1);
}
/*
* getlinetest():
* The incoming filename is assumed to have been previously created with
* the data pointed to by the incoming data pointer.
* Lines are read from the file via tfsgetline() and the buffer is filled
* with what should be the same data as is pulled in by tfsgetline().
*/
void
getlinetest(char *file, char *data)
{
int llen, tfd, err;
tfd = mon_tfsopen(file,TFS_RDONLY,0);
if (tfd < 0)
tfsdie(tfd);
/* Retrieve a line from the file: */
err = mon_tfsgetline(tfd,buffer1,sizeof(buffer1));
if (err < 0)
tfsdie(err);
/* Retrieve the same line from the data: */
for(llen=0;*data;llen++,data++) {
buffer2[llen] = *data;
if (*data == '\n')
break;
}
buffer2[llen+1] = 0;
/* They should match... */
if (memcmp(buffer1,buffer2,llen))
die();
/* Again... */
/* Retrieve a line from the file: */
err = mon_tfsgetline(tfd,buffer1,sizeof(buffer1));
if (err < 0)
tfsdie(err);
/* Retrieve the same line from the data: */
for(llen=0;*data;llen++,data++) {
buffer2[llen] = *data;
if (*data == '\n')
break;
}
buffer2[llen+1] = 0;
/* They should match... */
if (memcmp(buffer1,buffer2,llen))
die();
/* One more line... */
/* Retrieve a line from the file: */
err = mon_tfsgetline(tfd,buffer1,sizeof(buffer1));
if (err < 0)
tfsdie(err);
/* Retrieve the same line from the data: */
for(llen=0;*data;llen++,data++) {
buffer2[llen] = *data;
if (*data == '\n')
break;
}
buffer2[llen+1] = 0;
/* They should match... */
if (memcmp(buffer1,buffer2,llen))
die();
mon_tfsclose(tfd,0);
}
/*
* cmp():
* File Compare...
* Use the standard TFS facilities to compare two different incoming files.
* Return 0 if files match, else -1.
*/
int
cmp(char *f1, char *f2)
{
TFILE finfo1, *tfp1;
TFILE finfo2, *tfp2;
int fd1, fd2, size, ret;
char *buf1, *buf2;
/* Check sizes first: */
tfp1 = &finfo1;
if (mon_tfsfstat(f1,tfp1) == -1)
die();
tfp2 = &finfo2;
if (mon_tfsfstat(f2,tfp2) == -1)
die();
if (tfp1->filsize != tfp2->filsize)
return(-1);
/* Copy f1 to buffer: */
buf1 = mon_malloc(TFS_SIZE(tfp1));
if (!buf1)
die();
fd1 = mon_tfsopen(f1,TFS_RDONLY,0);
if (fd1 < 0)
tfsdie(fd1);
size = mon_tfsread(fd1,buf1,TFS_SIZE(tfp1));
if (size != TFS_SIZE(tfp1))
tfsdie(size);
ret = mon_tfsclose(fd1,0);
if (ret != TFS_OKAY)
tfsdie(ret);
/* Copy f2 to buffer: */
buf2 = mon_malloc(TFS_SIZE(tfp2));
if (!buf2)
die();
fd2 = mon_tfsopen(f2,TFS_RDONLY,0);
if (fd2 < 0)
tfsdie(fd2);
size = mon_tfsread(fd2,buf2,TFS_SIZE(tfp2));
if (size != TFS_SIZE(tfp2))
tfsdie(size);
ret = mon_tfsclose(fd2,0);
if (ret != TFS_OKAY)
tfsdie(ret);
/* Compare the buffers: */
if (memcmp(buf1,buf2,TFS_SIZE(tfp2)))
ret = -1;
else
ret = 0;
mon_free(buf1);
mon_free(buf2);
return(ret);
}
/*
* cp():
* File Copy...
* Use standard TFS facilities to copy one file to another.
* If successful, return the size of the copy; else die.
*/
int
cp(char *to, char *from)
{
TFILE finfo, *tfp;
long flags;
int ffd, tfd, size, ret;
char line[80], *buffer;
/* Open the source file: */
ffd = mon_tfsopen(from,TFS_RDONLY,0);
if (ffd < 0)
tfsdie(ffd);
/* Retrieve stats of the source file: */
tfp = &finfo;
if (mon_tfsfstat(from,tfp) == -1)
die();
/* The buffer used to open the destination file must be as large as
* the source file ...
*/
buffer = mon_malloc(TFS_SIZE(tfp));
if (!buffer)
die();
/* Open the destination file for creation with the same flags
* as the source file:
*/
tfd = mon_tfsopen(to,TFS_CREATE | finfo.flags,buffer);
if (tfd < 0)
tfsdie(tfd);
/* Read the entire source file into buffer, then write the entire
* buffer to the destination file...
*/
size = mon_tfsread(ffd,buffer,TFS_SIZE(tfp));
if (size < 0)
tfsdie(size);
ret = mon_tfswrite(tfd,buffer,TFS_SIZE(tfp));
if (ret != TFS_OKAY)
tfsdie(ret);
mon_tfsclose(ffd,0);
mon_tfsclose(tfd,finfo.info);
mon_free(buffer);
return(TFS_SIZE(tfp));
}
/*
* inusetest():
* Open a file, then try to remove it or add a file with the same name.
* This should fail...
*/
int
inusetest(char *fname)
{
int tfd, err;
/* Open a file, then try to run tfsadd() on it or try to remove it... */
tfd = mon_tfsopen(fname,TFS_RDONLY,0);
if (tfd < 0)
tfsdie(tfd);
err = mon_tfsunlink(fname);
if (err != TFSERR_FILEINUSE)
tfsdie(err);
err = mon_tfsadd(fname,0,0,buffer1,10);
if (err != TFSERR_FILEINUSE)
tfsdie(err);
mon_tfsclose(tfd,0);
return(0);
}
/* seektest():
* Run some tests on tfsseek().
* Verify that the character in the file specified at the incoming offset
* is as expected. Seek to beyond the end of file and verify error, etc...
*/
int
seektest(char *fname, int offset, char value)
{
char c, buf1[16], buf2[16];
TFILE *tfp;
int tfd, end, err;
/* Open the source file: */
tfd = mon_tfsopen(fname,TFS_RDONLY,0);
if (tfd < 0)
tfsdie(tfd);
if (mon_tfsseek(tfd,offset,TFS_BEGIN) != TFS_OKAY)
tfsdie(tfd);
if (mon_tfsread(tfd,&c,1) != 1)
tfsdie(tfd);
if (c != value)
die();
tfp = mon_tfsstat(fname);
if (!tfp)
die();
end = TFS_SIZE(tfp);
/*
* Seek to various limits in the file and verify proper return
* value...
*/
err = mon_tfsseek(tfd,0,TFS_BEGIN);
if (err != TFS_OKAY)
tfsdie(err);
err = mon_tfsseek(tfd,end,TFS_CURRENT);
if (err != TFS_OKAY)
tfsdie(err);
err = mon_tfsseek(tfd,1,TFS_CURRENT);
if (err != TFSERR_EOF)
tfsdie(err);
err = mon_tfsseek(tfd,end,TFS_BEGIN);
if (err != TFS_OKAY)
tfsdie(err);
err = mon_tfsseek(tfd,end+1,TFS_BEGIN);
if (err != TFSERR_EOF)
tfsdie(err);
err = mon_tfsseek(tfd,0,TFS_BEGIN);
if (err != TFS_OKAY)
tfsdie(err);
err = mon_tfsseek(tfd,-1,TFS_CURRENT);
if (err != TFSERR_EOF)
tfsdie(err);
err = mon_tfsseek(tfd,end,TFS_BEGIN);
if (err != TFS_OKAY)
tfsdie(err);
err = mon_tfsseek(tfd,-1,TFS_CURRENT);
if (err != TFS_OKAY)
tfsdie(err);
err = mon_tfsseek(tfd,2,TFS_CURRENT);
if (err != TFSERR_EOF)
tfsdie(err);
/* Seek to beginning, read 10, seek to beginning again read 10 again.
* Verify that both reads have same data.
*/
err = mon_tfsseek(tfd,0,TFS_BEGIN);
if (err != TFS_OKAY)
tfsdie(err);
err = mon_tfsread(tfd,buf1,10);
if (err != 10)
tfsdie(err);
err = mon_tfsseek(tfd,0,TFS_BEGIN);
if (err != TFS_OKAY)
tfsdie(err);
err = mon_tfsread(tfd,buf2,10);
if (err != 10)
tfsdie(err);
if (memcmp(buf1,buf2,10))
die();
/*
* Seek to end, then verify that read() returns EOF and tfs_eof()
* returns true.
*/
err = mon_tfsseek(tfd,end,TFS_BEGIN);
if (err != TFS_OKAY)
tfsdie(err);
err = mon_tfsread(tfd,buf1,1);
if (err != TFSERR_EOF)
tfsdie(err);
if (mon_tfseof(tfd) != 1)
die();
mon_tfsclose(tfd,0);
return(0);
}
/* writetest():
* Open the specified file in APPEND (modify) mode. Seek into the file
* and read 1 byte. Increment that byte by one and then write it back
* to the same location from which it was read. Then, close the file.
* Build a new file that is what "should" be the content of the file we
* just modified and compare the two files. They better match.
*/
int
writetest(char *fname, char *newdata)
{
TFILE *tfp;
char c;
int size, tfd, err;
/* Open the source file: */
tfp = mon_tfsstat(fname);
if (!tfp)
die();
size = TFS_SIZE(tfp);
tfd = mon_tfsopen(fname,TFS_APPEND,buffer1);
if (tfd < 0)
tfsdie(tfd);
err = mon_tfsseek(tfd,3,TFS_BEGIN);
if (err != TFS_OKAY)
tfsdie(err);
err = mon_tfsread(tfd,&c,1);
if (err != 1)
tfsdie(err);
c++;
err = mon_tfsseek(tfd,-1,TFS_CURRENT);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -