Permalink
Browse files

Add more tests

Change-Id: Ib75f7db3de38a6861e249c6aafa026beb5c5195a
Reviewed-on: http://review.couchbase.org/15971
Reviewed-by: Chiyoung Seo <chiyoung.seo@gmail.com>
Tested-by: Trond Norbye <trond.norbye@gmail.com>
  • Loading branch information...
trondn committed May 14, 2012
1 parent b77d1e2 commit 96b65ae264e84c1699db6a257710fc4d16e3e1e5
Showing with 115 additions and 0 deletions.
  1. +115 −0 tests/instance_unit_tests.cc
@@ -19,6 +19,8 @@
#include <cerrno>
#include <cstring>
#include <cstdlib>
+#include <fcntl.h>
+#include <fstream>
#include <gtest/gtest.h>
using namespace std;
@@ -37,6 +39,13 @@ class LibcbioTest : public ::testing::Test
removeDb();
}
+ void createRubbishFile(void) {
+ ofstream out(dbfile);
+ for (int ii = 0; ii < 80; ++ii) {
+ out << "Rubbish file" << endl;
+ }
+ }
+
protected:
void removeDb(void) {
EXPECT_EQ(0, (remove(dbfile) == -1 && errno != ENOENT));
@@ -123,6 +132,32 @@ TEST_F(LibcbioOpenTest, HandleNonexistentNameOpenCREATE)
CBIO_OPEN_CREATE, &handle));
}
+TEST_F(LibcbioOpenTest, HandleIllegalFileRDONLY)
+{
+ createRubbishFile();
+ libcbio_t handle;
+ EXPECT_EQ(CBIO_ERROR_NO_HEADER,
+ cbio_open_handle(dbfile, CBIO_OPEN_RDONLY, &handle));
+}
+
+TEST_F(LibcbioOpenTest, HandleIllegalFileRW)
+{
+ createRubbishFile();
+ libcbio_t handle;
+ EXPECT_EQ(CBIO_ERROR_NO_HEADER,
+ cbio_open_handle(dbfile, CBIO_OPEN_RW, &handle));
+}
+
+TEST_F(LibcbioOpenTest, HandleIllegalFileCREATE)
+{
+ createRubbishFile();
+ libcbio_t handle;
+ EXPECT_EQ(CBIO_ERROR_NO_HEADER,
+ cbio_open_handle(dbfile, CBIO_OPEN_CREATE, &handle));
+}
+
+
+
class LibcbioCreateDatabaseTest : public LibcbioTest {};
@@ -345,6 +380,23 @@ class LibcbioDataAccessTest : public LibcbioTest
}
}
+ void randomWriteBytes(off_t offset, int num) {
+ struct stat before;
+ EXPECT_NE(-1, stat(dbfile, &before));
+ // Destroy the last header
+ int fd;
+ EXPECT_NE(-1, (fd = open(dbfile, O_RDWR)));
+ EXPECT_EQ(offset, lseek(fd, offset, SEEK_SET));
+ for (int ii = 0; ii < num; ++ii) {
+ EXPECT_EQ(1, write(fd, "a", 1));
+ }
+ EXPECT_EQ(0, close(fd));
+
+ struct stat after;
+ EXPECT_NE(-1, stat(dbfile, &after));
+ EXPECT_EQ(before.st_size, after.st_size);
+ }
+
char *blob;
size_t blobsize;
libcbio_t handle;
@@ -422,6 +474,69 @@ TEST_F(LibcbioDataAccessTest, testChangesSinceDocuments)
EXPECT_EQ(5000, total);
}
+TEST_F(LibcbioDataAccessTest, testGetHeaderPosition)
+{
+ EXPECT_EQ((off_t)0, cbio_get_header_position(handle));
+ bulkStoreDocuments(10);
+ EXPECT_EQ(CBIO_SUCCESS, cbio_commit(handle));
+
+ off_t offset = cbio_get_header_position(handle);
+ EXPECT_LT(0, offset);
+ cbio_close_handle(handle);
+
+ // Verify that the header position is the same after I
+ // reopen the file
+ EXPECT_EQ(CBIO_SUCCESS,
+ cbio_open_handle(dbfile, CBIO_OPEN_RW, &handle));
+ EXPECT_EQ(offset, cbio_get_header_position(handle));
+ cbio_close_handle(handle);
+
+ // Write some bytes in the header...
+ randomWriteBytes(offset + 1, 10);
+
+ EXPECT_EQ(CBIO_SUCCESS,
+ cbio_open_handle(dbfile, CBIO_OPEN_RW, &handle));
+
+ // The last header should be garbled, so we should use the one
+ // before
+ EXPECT_GT(offset, cbio_get_header_position(handle));
+
+ int total = 0;
+ EXPECT_EQ(CBIO_SUCCESS,
+ cbio_changes_since(handle, 0, count_callback,
+ static_cast<void *>(&total)));
+ EXPECT_NE(10, total);
+}
+
+TEST_F(LibcbioDataAccessTest, testGarbledData)
+{
+ EXPECT_EQ((off_t)0, cbio_get_header_position(handle));
+ string key = "key";
+ char *data = new char[8192];
+ for (int ii = 0; ii < 8192; ++ii) {
+ data[ii] = 'a';
+ }
+ string value(data, 8192);
+ storeSingleDocument(key, value);
+ validateExistingDocument(key, value);
+ EXPECT_EQ(CBIO_SUCCESS, cbio_commit(handle));
+ off_t offset = cbio_get_header_position(handle);
+ EXPECT_NE(0, offset);
+ cbio_close_handle(handle);
+
+ // try to write some rubbish inside the document..
+ randomWriteBytes(offset - 4090, 2000);
+
+ EXPECT_EQ(CBIO_SUCCESS,
+ cbio_open_handle(dbfile, CBIO_OPEN_RDONLY, &handle));
+
+ libcbio_document_t doc;
+ EXPECT_EQ(CBIO_ERROR_EIO,
+ cbio_get_document_ex(handle, key.data(), key.length(), &doc));
+
+ delete []data;
+}
+
class LibcbioLocalDocumentTest : public LibcbioDataAccessTest
{
};

0 comments on commit 96b65ae

Please sign in to comment.