Added BVolume tests.

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@2145 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold 2002-12-03 17:07:33 +00:00
parent 744761bf27
commit 467cfff600
4 changed files with 387 additions and 0 deletions

View File

@ -20,6 +20,7 @@ CommonTestLib libstoragetest.so
ResourceStringsTest.cpp
StatableTest.cpp
SymLinkTest.cpp
VolumeTest.cpp
: <boot!home!config!lib>libopenbeos.so be stdc++.r4
: be stdc++.r4
:

View File

@ -16,6 +16,7 @@
#include "ResourcesTest.h"
#include "ResourceStringsTest.h"
#include "SymLinkTest.h"
#include "VolumeTest.h"
BTestSuite* getTestSuite() {
BTestSuite *suite = new BTestSuite("Storage");
@ -33,6 +34,7 @@ BTestSuite* getTestSuite() {
suite->addTest("BResources", ResourcesTest::Suite());
suite->addTest("BResourceStrings", ResourceStringsTest::Suite());
suite->addTest("BSymLink", SymLinkTest::Suite());
suite->addTest("BVolume", VolumeTest::Suite());
suite->addTest("FindDirectory", FindDirectoryTest::Suite());
suite->addTest("MimeSniffer", MimeSnifferTest::Suite());

View File

@ -0,0 +1,346 @@
// VolumeTest.cpp
#include <stdio.h>
#include <string>
#include <unistd.h>
#include <Application.h>
#include <Bitmap.h>
#include <Directory.h>
#include <Entry.h>
#include <File.h>
#include <fs_attr.h>
#include <fs_info.h>
#include <Path.h>
#include <Resources.h>
#include <Roster.h>
#include <String.h>
#include <TypeConstants.h>
#include <Volume.h>
#include <cppunit/Test.h>
#include <cppunit/TestCaller.h>
#include <cppunit/TestSuite.h>
#include <TestShell.h>
#include <TestUtils.h>
#include <cppunit/TestAssert.h>
#include "VolumeTest.h"
#include "../app/bmessenger/Helpers.h"
// test dirs/files/types
static const char *testDir = "/tmp/testDir";
static const char *testFile1 = "/tmp/testDir/file1";
static const char *testMountPoint = "/tmp/testDir/mount_point";
// icon_equal
static
bool
icon_equal(const BBitmap *icon1, const BBitmap *icon2)
{
return (icon1->Bounds() == icon2->Bounds()
&& icon1->BitsLength() == icon2->BitsLength()
&& memcmp(icon1->Bits(), icon2->Bits(), icon1->BitsLength()) == 0);
}
// Suite
CppUnit::Test*
VolumeTest::Suite() {
CppUnit::TestSuite *suite = new CppUnit::TestSuite();
typedef CppUnit::TestCaller<VolumeTest> TC;
suite->addTest( new TC("BVolume::Init Test1",
&VolumeTest::InitTest1) );
suite->addTest( new TC("BVolume::Init Test2",
&VolumeTest::InitTest2) );
suite->addTest( new TC("BVolume::Assignment",
&VolumeTest::Assignment) );
suite->addTest( new TC("BVolume::Comparisson",
&VolumeTest::Comparisson) );
suite->addTest( new TC("BVolume::SetName",
&VolumeTest::SetName) );
return suite;
}
// setUp
void
VolumeTest::setUp()
{
BasicTest::setUp();
// create test dir and files
execCommand(
string("mkdir ") + testDir
);
// create and mount image
createVolume(testFile1, testMountPoint, 1);
// create app
fApplication
= new BApplication("application/x-vnd.obos.volume-test");
}
// tearDown
void
VolumeTest::tearDown()
{
// delete the application
delete fApplication;
fApplication = NULL;
// unmount and delete image
deleteVolume(testFile1, testMountPoint);
// delete the test dir
execCommand(string("rm -rf ") + testDir);
BasicTest::tearDown();
}
// CheckVolume
static
void
CheckVolume(BVolume &volume, dev_t device, status_t error)
{
CHK(volume.InitCheck() == error);
if (error == B_OK) {
fs_info info;
CHK(fs_stat_dev(device, &info) == 0);
// device
CHK(volume.Device() == device);
// root dir
BDirectory rootDir;
CHK(volume.GetRootDirectory(&rootDir) == B_OK);
node_ref rootNode;
rootNode.device = device;
rootNode.node = info.root;
BDirectory actualRootDir(&rootNode);
CHK(rootDir == actualRootDir);
// capacity, free bytes
CHK(volume.Capacity() == info.total_blocks * info.block_size);
CHK(volume.FreeBytes() == info.free_blocks * info.block_size);
// name
char name[B_FILE_NAME_LENGTH];
CHK(volume.GetName(name) == B_OK);
CHK(BString(name) == info.volume_name);
// icons
// mini
BBitmap miniIcon(BRect(0, 0, 15, 15), B_CMAP8);
BBitmap miniIcon2(BRect(0, 0, 15, 15), B_CMAP8);
status_t iconError = get_device_icon(info.device_name,
miniIcon2.Bits(), B_MINI_ICON);
CHK(volume.GetIcon(&miniIcon, B_MINI_ICON) == iconError);
if (iconError == B_OK)
CHK(icon_equal(&miniIcon, &miniIcon2));
// large
BBitmap largeIcon(BRect(0, 0, 31, 31), B_CMAP8);
BBitmap largeIcon2(BRect(0, 0, 31, 31), B_CMAP8);
iconError = get_device_icon(info.device_name, largeIcon2.Bits(),
B_LARGE_ICON);
CHK(volume.GetIcon(&largeIcon, B_LARGE_ICON) == iconError);
if (iconError == B_OK)
CHK(icon_equal(&largeIcon, &largeIcon2));
// flags
CHK(volume.IsRemovable() == bool(info.flags & B_FS_IS_REMOVABLE));
CHK(volume.IsReadOnly() == bool(info.flags & B_FS_IS_READONLY));
CHK(volume.IsPersistent() == bool(info.flags & B_FS_IS_PERSISTENT));
CHK(volume.IsShared() == bool(info.flags & B_FS_IS_SHARED));
CHK(volume.KnowsMime() == bool(info.flags & B_FS_HAS_MIME));
CHK(volume.KnowsAttr() == bool(info.flags & B_FS_HAS_ATTR));
CHK(volume.KnowsQuery() == bool(info.flags & B_FS_HAS_QUERY));
} else {
CHK(volume.Device() == -1);
// root dir
BDirectory rootDir;
CHK(volume.GetRootDirectory(&rootDir) == B_BAD_VALUE);
// capacity, free bytes
CHK(volume.Capacity() == B_BAD_VALUE);
CHK(volume.FreeBytes() == B_BAD_VALUE);
// name
char name[B_FILE_NAME_LENGTH];
CHK(volume.GetName(name) == B_BAD_VALUE);
// icons
// mini
BBitmap miniIcon(BRect(0, 0, 15, 15), B_CMAP8);
CHK(volume.GetIcon(&miniIcon, B_MINI_ICON) == B_BAD_VALUE);
// large
BBitmap largeIcon(BRect(0, 0, 31, 31), B_CMAP8);
CHK(volume.GetIcon(&largeIcon, B_LARGE_ICON) == B_BAD_VALUE);
// flags
CHK(volume.IsRemovable() == false);
CHK(volume.IsReadOnly() == false);
CHK(volume.IsPersistent() == false);
CHK(volume.IsShared() == false);
CHK(volume.KnowsMime() == false);
CHK(volume.KnowsAttr() == false);
CHK(volume.KnowsQuery() == false);
}
}
// InitTest1
void
VolumeTest::InitTest1()
{
// 1. BVolume(void)
{
BVolume volume;
CheckVolume(volume, -1, B_NO_INIT);
}
// 2. BVolume(dev_t dev)
// volumes for testing
const char *volumes[] = {
"/boot",
"/",
"/dev",
"/pipe",
"/unknown",
testMountPoint
};
int32 volumeCount = sizeof(volumes) / sizeof(const char*);
for (int32 i = 0; i < volumeCount; i++) {
NextSubTest();
const char *volumeRootDir = volumes[i];
dev_t device = dev_for_path(volumeRootDir);
BVolume volume(device);
CheckVolume(volume, device, (device >= 0 ? B_OK : B_BAD_VALUE));
}
}
// InitTest2
void
VolumeTest::InitTest2()
{
// volumes for testing
const char *volumes[] = {
"/boot",
"/",
"/dev",
"/pipe",
"/unknown",
testMountPoint
};
int32 volumeCount = sizeof(volumes) / sizeof(const char*);
BVolume volume1;
for (int32 i = 0; i < volumeCount; i++) {
NextSubTest();
const char *volumeRootDir = volumes[i];
dev_t device = dev_for_path(volumeRootDir);
status_t initError = (device >= 0 ? B_OK : B_BAD_VALUE);
// reinit already initialized volume
CHK(volume1.SetTo(device) == initError);
CheckVolume(volume1, device, initError);
// init fresh volume
BVolume volume2;
CHK(volume2.SetTo(device) == initError);
CheckVolume(volume2, device, initError);
// uninit volume
volume2.Unset();
CheckVolume(volume2, device, B_NO_INIT);
}
}
// Assignment
void
VolumeTest::Assignment()
{
// volumes for testing
const char *volumes[] = {
"/boot",
"/",
"/dev",
"/pipe",
"/unknown",
testMountPoint
};
int32 volumeCount = sizeof(volumes) / sizeof(const char*);
BVolume volume1;
for (int32 i = 0; i < volumeCount; i++) {
NextSubTest();
const char *volumeRootDir = volumes[i];
dev_t device = dev_for_path(volumeRootDir);
status_t initError = (device >= 0 ? B_OK : B_BAD_VALUE);
BVolume volume3(device);
CheckVolume(volume3, device, initError);
// assignment operation
CHK(&(volume1 = volume3) == &volume1);
CheckVolume(volume1, device, initError);
// copy constructor
BVolume volume2(volume3);
CheckVolume(volume2, device, initError);
}
}
// Comparisson
void
VolumeTest::Comparisson()
{
// volumes for testing
const char *volumes[] = {
"/boot",
"/",
"/dev",
"/pipe",
"/unknown",
testMountPoint
};
int32 volumeCount = sizeof(volumes) / sizeof(const char*);
for (int32 i = 0; i < volumeCount; i++) {
NextSubTest();
const char *volumeRootDir = volumes[i];
dev_t device = dev_for_path(volumeRootDir);
status_t initError = (device >= 0 ? B_OK : B_BAD_VALUE);
BVolume volume(device);
CheckVolume(volume, device, initError);
for (int32 k = 0; k < volumeCount; k++) {
const char *volumeRootDir2 = volumes[k];
dev_t device2 = dev_for_path(volumeRootDir2);
status_t initError2 = (device2 >= 0 ? B_OK : B_BAD_VALUE);
BVolume volume2(device2);
CheckVolume(volume2, device2, initError2);
bool equal = (i == k
|| initError == initError2 && initError2 != B_OK);
CHK((volume == volume2) == equal);
CHK((volume != volume2) == !equal);
}
}
}
// SetName
void
VolumeTest::SetName()
{
// status_t SetName(const char* name);
dev_t device = dev_for_path(testMountPoint);
BVolume volume(device);
CheckVolume(volume, device, B_OK);
// set a new name
NextSubTest();
const char *newName = "a new name";
CHK(volume.SetName(newName) == B_OK);
char name[B_FILE_NAME_LENGTH];
CHK(volume.GetName(name) == B_OK);
CHK(string(newName) == name);
CheckVolume(volume, device, B_OK);
// set another name
NextSubTest();
const char *newName2 = "another name";
CHK(volume.SetName(newName2) == B_OK);
CHK(volume.GetName(name) == B_OK);
CHK(string(newName2) == name);
CheckVolume(volume, device, B_OK);
// GetName() with NULL buffer
// R5: crashes when passing a NULL pointer
#ifndef TEST_R5
NextSubTest();
CHK(volume.GetName(NULL) == B_BAD_VALUE);
#endif
// bad name
// R5: crashes when passing a NULL pointer
#ifndef TEST_R5
NextSubTest();
CHK(volume.SetName(NULL) == B_BAD_VALUE);
#endif
// uninitialized volume
NextSubTest();
volume.Unset();
CHK(volume.SetName(newName) == B_BAD_VALUE);
}

View File

@ -0,0 +1,38 @@
// VolumeTest.h
#ifndef VOLUME_TEST_H
#define VOLUME_TEST_H
#include <StorageDefs.h>
#include <SupportDefs.h>
#include <BasicTest.h>
class BApplication;
class BBitmap;
class CppUnit::Test;
class VolumeTest : public BasicTest
{
public:
static CppUnit::Test* Suite();
// This function called before *each* test added in Suite()
void setUp();
// This function called after *each* test added in Suite()
void tearDown();
//------------------------------------------------------------
// Test functions
//------------------------------------------------------------
void InitTest1();
void InitTest2();
void Assignment();
void Comparisson();
void SetName();
private:
BApplication *fApplication;
};
#endif // VOLUME_TEST_H