test/storage: Fix BFile tests

After this patch "UnitTester BFile" runs with no failures.

This includes some changes to NodeTest as well since the BFile suite
inherits some tests from it, but "UnitTester BNode" doesn't pass yet.

FileTest:
* Fix format strings in log messages to use the correct types and
  quite warnings.

* Test for expected behavior with BFile(..., B_READ_ONLY | B_ERASE_FILE)

  There is a table in the BFile tests which is used as input for a
  series of tests which construct BFile objects with various
  permutations of the mode parameter.

  For two of these, the combination of B_ERASE_FILE and B_READ_ONLY
  are used for the file mode. The system rejects these with the error
  B_NOT_ALLOWED. This is enforced in bfs_open(), which looks for that
  particular combination (O_RDONLY | O_TRUNC).

  I tested this on BeOS R5 and it will indeed let you create a
  BFile(path, B_READ_ONLY | B_ERASE_FILE) which basically replaces the
  file at path with an empty file.

  The Haiku behavior seems way more sensible, so I'm changing these
  tests to match its current behavior.

* BFile(..., B_READ_ONLY).SetSize() isn't allowed.

  BeOS R5 allowed you create a BFile with the B_READ_ONLY flag and
  then successfully set the size of the file with
  BFile::SetSize(). Haiku doesn't, and that seems way better. Updating
  BFileTest::SizeTest to match Haiku behavior.

  One whole sub-test was removed from FileTest::SizeTest because it
  calls SetSize() on a `B_READ_ONLY | B_ERASE_FILE` BFile. This is
  redundant as other tests in this suite already verify that it is not
  possible to construct a BFile with that combination of flags.

NodeTest:
* Fix tests to match Haiku's max attribute length.

  Many of these tests assume that the max size of an attribute name is
  255 bytes, and it was in BeOS R5, which I just confirmed.

  But Haiku allows 256 bytes. In 4069e1f30 some compromise was struck
  that allowed this to avoid breaking userspace which had been allowed
  to use 256 byte keys at some point.

* WriteAttr, ReadAttr and RemoveAttr all return B_NAME_TOO_LONG if an
  attribute name longer than 256 bytes (excluding null terminator) are
  provided.

* Disable NodeTest::AttrRenameTest since attr rename is not supported

  Tests in BNodeTest (inherited by BFile tests) exercize
  BNode::RenameAttr(), but from what I can see attribute renaming is
  not implemented at all. bfs_rename_attr() has a TODO comment and
  just always returns EOPNOTSUPP (B_NOT_SUPPORTED). And that is the
  value returned from BNode::RenameAttr() in these tests.

  So for now I made these tests just check that B_NOT_SUPPORTED is
  returned from BNode::RenameAttr(), so when this functionality is
  implemented these tests will fail and can be cleaned up.

Change-Id: I6cfe90ca45f3a8afa709edc9b85e648fdc865e82
Reviewed-on: https://review.haiku-os.org/c/haiku/+/2182
Reviewed-by: Adrien Destugues <pulkomandy@gmail.com>
This commit is contained in:
Kyle Ambroff-Kao 2020-02-02 01:47:53 -08:00 committed by Adrien Destugues
parent 15e39e1c67
commit 5f4f455cfd
2 changed files with 50 additions and 41 deletions

View File

@ -88,7 +88,7 @@ FileTest::InitTest1()
{
for (int32 i = 0; i < initTestCasesCount; i++) {
if (BTestShell::GlobalBeVerbose()) {
printf("[%ld]", i);
printf("[%" B_PRId32 "]", i);
fflush(stdout);
}
test(initTestCases[i]);
@ -197,7 +197,7 @@ FileTest::InitTest2()
{
for (int32 i = 0; i < initTestCasesCount; i++) {
if (BTestShell::GlobalBeVerbose()) {
printf("[%ld]", i);
printf("[%" B_PRId32 "]", i);
fflush(stdout);
}
test(initTestCases[i]);
@ -558,22 +558,22 @@ FileTest::SizeTest()
off_t size;
CPPUNIT_ASSERT( file.GetSize(&size) != B_OK );
CPPUNIT_ASSERT( file.SetSize(100) != B_OK );
// read only file
// read only file, SetSize will not succeed
NextSubTest();
file.SetTo(testFilename1, B_READ_ONLY | B_CREATE_FILE);
CPPUNIT_ASSERT( file.InitCheck() == B_OK );
CPPUNIT_ASSERT( file.GetSize(&size) == B_OK );
CPPUNIT_ASSERT( size == 0 );
CPPUNIT_ASSERT( file.SetSize(100) == B_OK );
CPPUNIT_ASSERT_EQUAL(file.SetSize(100), B_BAD_VALUE);
CPPUNIT_ASSERT( file.GetSize(&size) == B_OK );
CPPUNIT_ASSERT( size == 100 );
CPPUNIT_ASSERT_EQUAL(size, 0);
file.Unset();
// shorten existing file
// successfully set size of file with appropriate flags
NextSubTest();
file.SetTo(testFilename1, B_WRITE_ONLY);
CPPUNIT_ASSERT( file.InitCheck() == B_OK );
CPPUNIT_ASSERT( file.GetSize(&size) == B_OK );
CPPUNIT_ASSERT( size == 100 );
CPPUNIT_ASSERT_EQUAL(size, 0);
CPPUNIT_ASSERT( file.SetSize(73) == B_OK );
CPPUNIT_ASSERT( file.GetSize(&size) == B_OK );
CPPUNIT_ASSERT( size == 73 );
@ -588,16 +588,6 @@ FileTest::SizeTest()
CPPUNIT_ASSERT( file.GetSize(&size) == B_OK );
CPPUNIT_ASSERT( size == 147 );
file.Unset();
// erase existing file (read only)
NextSubTest();
file.SetTo(testFilename1, B_READ_ONLY | B_ERASE_FILE);
CPPUNIT_ASSERT( file.InitCheck() == B_OK );
CPPUNIT_ASSERT( file.GetSize(&size) == B_OK );
CPPUNIT_ASSERT( size == 0 );
CPPUNIT_ASSERT( file.SetSize(132) == B_OK );
CPPUNIT_ASSERT( file.GetSize(&size) == B_OK );
CPPUNIT_ASSERT( size == 132 );
file.Unset();
// erase existing file (write only)
NextSubTest();
file.SetTo(testFilename1, B_WRITE_ONLY | B_ERASE_FILE);
@ -728,13 +718,13 @@ const FileTest::InitTestCase FileTest::initTestCases[] = {
{ existingFilename , B_READ_ONLY , 0, 1, 0, false, B_OK },
{ existingFilename , B_WRITE_ONLY, 0, 1, 0, false, B_OK },
{ existingFilename , B_READ_WRITE, 0, 1, 0, false, B_OK },
{ existingFilename , B_READ_ONLY , 0, 0, 1, false, B_OK },
{ existingFilename , B_READ_ONLY , 0, 0, 1, false, B_NOT_ALLOWED },
{ existingFilename , B_WRITE_ONLY, 0, 0, 1, false, B_OK },
{ existingFilename , B_READ_WRITE, 0, 0, 1, false, B_OK },
{ existingFilename , B_READ_ONLY , 1, 1, 0, false, B_FILE_EXISTS },
{ existingFilename , B_WRITE_ONLY, 1, 1, 0, false, B_FILE_EXISTS },
{ existingFilename , B_READ_WRITE, 1, 1, 0, false, B_FILE_EXISTS },
{ existingFilename , B_READ_ONLY , 1, 0, 1, false, B_OK },
{ existingFilename , B_READ_ONLY , 1, 0, 1, false, B_NOT_ALLOWED },
{ existingFilename , B_WRITE_ONLY, 1, 0, 1, false, B_OK },
{ existingFilename , B_READ_WRITE, 1, 0, 1, false, B_OK },
{ existingFilename , B_READ_ONLY , 1, 1, 1, false, B_FILE_EXISTS },

View File

@ -733,16 +733,23 @@ NodeTest::AttrTest(BNode &node)
B_BAD_ADDRESS, B_BAD_VALUE) );
CPPUNIT_ASSERT( equals(node.RemoveAttr(NULL), B_BAD_ADDRESS, B_BAD_VALUE) );
// too long attribute name
// R5: Read/RemoveAttr() do not return B_NAME_TOO_LONG, but B_ENTRY_NOT_FOUND
// R5: WriteAttr() does not return B_NAME_TOO_LONG, but B_BAD_VALUE
char tooLongAttrName[B_ATTR_NAME_LENGTH + 1];
memset(tooLongAttrName, 'a', B_ATTR_NAME_LENGTH);
tooLongAttrName[B_ATTR_NAME_LENGTH + 1] = 0;
CPPUNIT_ASSERT( node.WriteAttr(tooLongAttrName, B_STRING_TYPE, 0, buffer,
sizeof(buffer)) == B_BAD_VALUE );
CPPUNIT_ASSERT( node.ReadAttr(tooLongAttrName, B_STRING_TYPE, 0, buffer,
sizeof(buffer)) == B_ENTRY_NOT_FOUND );
CPPUNIT_ASSERT( node.RemoveAttr(tooLongAttrName) == B_ENTRY_NOT_FOUND );
// R5: Read/RemoveAttr() do not return B_NAME_TOO_LONG, but B_ENTRY_NOT_FOUND
// R5: WriteAttr() does not return B_NAME_TOO_LONG, but B_BAD_VALUE
// R5: Haiku has a max attribute size of 256, while R5's was 255, exclusive
// of the null terminator. See changeset 4069e1f30.
char tooLongAttrName[B_ATTR_NAME_LENGTH + 3];
memset(tooLongAttrName, 'a', B_ATTR_NAME_LENGTH + 1);
tooLongAttrName[B_ATTR_NAME_LENGTH + 2] = '\0';
CPPUNIT_ASSERT_EQUAL(
node.WriteAttr(tooLongAttrName, B_STRING_TYPE, 0, buffer,
sizeof(buffer)),
B_NAME_TOO_LONG);
CPPUNIT_ASSERT_EQUAL(
node.ReadAttr(tooLongAttrName, B_STRING_TYPE, 0, buffer,
sizeof(buffer)),
B_NAME_TOO_LONG);
CPPUNIT_ASSERT_EQUAL(node.RemoveAttr(tooLongAttrName), B_NAME_TOO_LONG);
// remove the attributes and try to read them
for (int32 i = 0; i < attrCount; i++) {
const char *attrName = attrNames[i];
@ -787,20 +794,32 @@ NodeTest::AttrTest()
void
NodeTest::AttrRenameTest(BNode &node)
{
#if !TEST_R5
const char attr1[] = "StorageKit::SomeAttribute";
const char attr2[] = "StorageKit::AnotherAttribute";
CPPUNIT_ASSERT( node.SetTo("./") == B_OK );
// Test the case of the first attribute not existing
node.RemoveAttr(attr1);
#if 1
// The actual tests in the else block below are disabled because as of
// right now, BFS doesn't support attribute rename. bfs_rename_attr()
// always reutrns B_NOT_SUPPORTED, which means BNode::RenameAttr() will
// also always return that result.
//
// So until that is implemented, we'll just test for B_NOT_SUPPORTED here.
// Once that functionality is implemented, this test will pass and someone
// can remove this section.
CPPUNIT_ASSERT_EQUAL(node.RenameAttr(attr1, attr2), B_NOT_SUPPORTED);
#else
const char str[] = "This is my testing string and it rules your world.";
const int strLen = strlen(str) + 1;
const int dataLen = 1024;
char data[dataLen];
CPPUNIT_ASSERT( node.SetTo("./") == B_OK );
// Test the case of the first attribute not existing
node.RemoveAttr(attr1);
CPPUNIT_ASSERT( node.RenameAttr(attr1, attr2) == B_BAD_VALUE );
// Write an attribute, read it to verify it, rename it, read the
// new attribute, read the old (which fails), and then remove the new.
CPPUNIT_ASSERT( node.WriteAttr(attr1, B_STRING_TYPE, 0, str, strLen) == strLen );
@ -821,9 +840,9 @@ NodeTest::AttrRenameTest(BNode &node)
B_BAD_VALUE) );
// too long attribute name
// R5: RenameAttr() returns B_BAD_VALUE instead of B_NAME_TOO_LONG
char tooLongAttrName[B_ATTR_NAME_LENGTH + 1];
char tooLongAttrName[B_ATTR_NAME_LENGTH + 2];
memset(tooLongAttrName, 'a', B_ATTR_NAME_LENGTH);
tooLongAttrName[B_ATTR_NAME_LENGTH + 1] = 0;
tooLongAttrName[B_ATTR_NAME_LENGTH + 1] = '\0';
CPPUNIT_ASSERT( node.RenameAttr(attr1, tooLongAttrName)
== B_BAD_VALUE );
CPPUNIT_ASSERT( node.RenameAttr(tooLongAttrName, attr1)
@ -908,9 +927,9 @@ NodeTest::AttrInfoTest(BNode &node)
B_BAD_VALUE) );
// too long attribute name
// R5: GetAttrInfo() does not return B_NAME_TOO_LONG
char tooLongAttrName[B_ATTR_NAME_LENGTH + 1];
char tooLongAttrName[B_ATTR_NAME_LENGTH + 2];
memset(tooLongAttrName, 'a', B_ATTR_NAME_LENGTH);
tooLongAttrName[B_ATTR_NAME_LENGTH + 1] = 0;
tooLongAttrName[B_ATTR_NAME_LENGTH + 1] = '\0';
CPPUNIT_ASSERT( node.GetAttrInfo(tooLongAttrName, &info)
== B_ENTRY_NOT_FOUND );
}
@ -1007,9 +1026,9 @@ NodeTest::AttrBStringTest(BNode &node)
}
// too long attribute name
// R5: Read/WriteAttrString() do not return B_NAME_TOO_LONG
char tooLongAttrName[B_ATTR_NAME_LENGTH + 1];
char tooLongAttrName[B_ATTR_NAME_LENGTH + 2];
memset(tooLongAttrName, 'a', B_ATTR_NAME_LENGTH);
tooLongAttrName[B_ATTR_NAME_LENGTH + 1] = 0;
tooLongAttrName[B_ATTR_NAME_LENGTH + 1] = '\0';
CPPUNIT_ASSERT( node.WriteAttrString(tooLongAttrName, &writeValue)
== B_BAD_VALUE );
CPPUNIT_ASSERT( node.ReadAttrString(tooLongAttrName, &readValue)