2015-03-12 14:27:51 +01:00
|
|
|
#include "../testutils/DataBlockFixture.h"
|
|
|
|
#include "../../utils/Data.h"
|
|
|
|
#include "../../utils/Key.h"
|
2015-02-17 00:23:33 +01:00
|
|
|
#include "google/gtest/gtest.h"
|
2014-12-09 20:36:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
using ::testing::Test;
|
2014-12-13 12:31:24 +01:00
|
|
|
using ::testing::WithParamInterface;
|
|
|
|
using ::testing::Values;
|
2014-12-09 20:36:32 +01:00
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
using namespace blockstore;
|
|
|
|
|
2015-04-09 20:14:12 +02:00
|
|
|
//TODO Test blockstore::FixedSizeData instead of blockstore::Key
|
|
|
|
|
2014-12-09 20:46:34 +01:00
|
|
|
class KeyTest: public Test {
|
|
|
|
public:
|
2014-12-13 11:59:48 +01:00
|
|
|
const string KEY1_AS_STRING = "1491BB4932A389EE14BC7090AC772972";
|
|
|
|
const string KEY2_AS_STRING = "272EE5517627CFA147A971A8E6E747E0";
|
|
|
|
|
|
|
|
const DataBlockFixture KEY3_AS_BINARY;
|
|
|
|
const DataBlockFixture KEY4_AS_BINARY;
|
|
|
|
|
2015-04-09 20:07:03 +02:00
|
|
|
KeyTest() : KEY3_AS_BINARY(Key::BINARY_LENGTH, 1), KEY4_AS_BINARY(Key::BINARY_LENGTH, 2) {}
|
2014-12-09 20:36:32 +01:00
|
|
|
|
2014-12-13 12:31:24 +01:00
|
|
|
void EXPECT_DATA_EQ(const DataBlockFixture &expected, const Data &actual) {
|
|
|
|
EXPECT_EQ(expected.size(), actual.size());
|
|
|
|
EXPECT_EQ(0, std::memcmp(expected.data(), actual.data(), expected.size()));
|
|
|
|
}
|
|
|
|
};
|
2014-12-13 11:59:48 +01:00
|
|
|
|
2014-12-09 20:36:32 +01:00
|
|
|
TEST_F(KeyTest, CanGenerateRandomKeysWithoutCrashing) {
|
2015-04-09 20:07:03 +02:00
|
|
|
Key result = Key::CreateRandom();
|
2014-12-09 20:36:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(KeyTest, CreatedRandomKeysHaveCorrectLength) {
|
2015-04-09 20:07:03 +02:00
|
|
|
Key key = Key::CreateRandom();
|
|
|
|
EXPECT_EQ(Key::STRING_LENGTH, key.ToString().size());
|
2014-12-09 20:36:32 +01:00
|
|
|
}
|
2014-12-09 20:46:34 +01:00
|
|
|
|
|
|
|
TEST_F(KeyTest, EqualsTrue) {
|
2014-12-13 11:59:48 +01:00
|
|
|
Key key1_1 = Key::FromString(KEY1_AS_STRING);
|
|
|
|
Key key1_2 = Key::FromString(KEY1_AS_STRING);
|
2014-12-09 20:46:34 +01:00
|
|
|
|
|
|
|
EXPECT_TRUE(key1_1 == key1_2);
|
|
|
|
EXPECT_TRUE(key1_2 == key1_1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(KeyTest, EqualsFalse) {
|
2014-12-13 11:59:48 +01:00
|
|
|
Key key1_1 = Key::FromString(KEY1_AS_STRING);
|
|
|
|
Key key2_1 = Key::FromString(KEY2_AS_STRING);
|
2014-12-09 20:46:34 +01:00
|
|
|
|
|
|
|
EXPECT_FALSE(key1_1 == key2_1);
|
|
|
|
EXPECT_FALSE(key2_1 == key1_1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(KeyTest, NotEqualsFalse) {
|
2014-12-13 11:59:48 +01:00
|
|
|
Key key1_1 = Key::FromString(KEY1_AS_STRING);
|
|
|
|
Key key1_2 = Key::FromString(KEY1_AS_STRING);
|
2014-12-09 20:46:34 +01:00
|
|
|
|
|
|
|
EXPECT_FALSE(key1_1 != key1_2);
|
|
|
|
EXPECT_FALSE(key1_2 != key1_1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(KeyTest, NotEqualsTrue) {
|
2014-12-13 11:59:48 +01:00
|
|
|
Key key1_1 = Key::FromString(KEY1_AS_STRING);
|
|
|
|
Key key2_1 = Key::FromString(KEY2_AS_STRING);
|
2014-12-09 20:46:34 +01:00
|
|
|
|
|
|
|
EXPECT_TRUE(key1_1 != key2_1);
|
|
|
|
EXPECT_TRUE(key2_1 != key1_1);
|
|
|
|
}
|
|
|
|
|
2014-12-13 12:31:24 +01:00
|
|
|
class KeyTestWithStringKeyParam: public KeyTest, public WithParamInterface<string> {};
|
|
|
|
INSTANTIATE_TEST_CASE_P(KeyTestWithStringKeyParam, KeyTestWithStringKeyParam, Values("2898B4B8A13CA63CBE0F0278CCE465DB", "6FFEBAD90C0DAA2B79628F0627CE9841"));
|
2014-12-09 20:46:34 +01:00
|
|
|
|
2014-12-13 12:31:24 +01:00
|
|
|
TEST_P(KeyTestWithStringKeyParam, FromAndToString) {
|
|
|
|
Key key = Key::FromString(GetParam());
|
|
|
|
EXPECT_EQ(GetParam(), key.ToString());
|
2014-12-09 20:46:34 +01:00
|
|
|
}
|
|
|
|
|
2014-12-13 12:31:24 +01:00
|
|
|
TEST_P(KeyTestWithStringKeyParam, ToAndFromString) {
|
|
|
|
Key key = Key::FromString(GetParam());
|
2014-12-13 11:59:48 +01:00
|
|
|
Key key2 = Key::FromString(key.ToString());
|
2014-12-09 20:46:34 +01:00
|
|
|
EXPECT_EQ(key, key2);
|
|
|
|
}
|
2014-12-09 20:47:49 +01:00
|
|
|
|
2014-12-13 12:31:24 +01:00
|
|
|
class KeyTestWithBinaryKeyParam: public KeyTest, public WithParamInterface<const DataBlockFixture*> {
|
|
|
|
public:
|
|
|
|
static const DataBlockFixture VALUE1;
|
|
|
|
static const DataBlockFixture VALUE2;
|
|
|
|
};
|
2015-04-09 20:07:03 +02:00
|
|
|
const DataBlockFixture KeyTestWithBinaryKeyParam::VALUE1(Key::BINARY_LENGTH, 3);
|
|
|
|
const DataBlockFixture KeyTestWithBinaryKeyParam::VALUE2(Key::BINARY_LENGTH, 4);
|
2014-12-13 12:31:24 +01:00
|
|
|
INSTANTIATE_TEST_CASE_P(KeyTestWithBinaryKeyParam, KeyTestWithBinaryKeyParam, Values(&KeyTestWithBinaryKeyParam::VALUE1, &KeyTestWithBinaryKeyParam::VALUE2));
|
2014-12-13 11:59:48 +01:00
|
|
|
|
2014-12-13 12:31:24 +01:00
|
|
|
TEST_P(KeyTestWithBinaryKeyParam, FromAndToBinary) {
|
|
|
|
Key key = Key::FromBinary((uint8_t*)GetParam()->data());
|
2015-04-09 20:07:03 +02:00
|
|
|
Data keydata(Key::BINARY_LENGTH);
|
2014-12-13 11:59:48 +01:00
|
|
|
key.ToBinary(keydata.data());
|
2014-12-13 12:31:24 +01:00
|
|
|
EXPECT_DATA_EQ(*GetParam(), keydata);
|
2014-12-13 11:59:48 +01:00
|
|
|
}
|
|
|
|
|
2014-12-13 12:31:24 +01:00
|
|
|
TEST_P(KeyTestWithBinaryKeyParam, ToAndFromBinary) {
|
|
|
|
Key key = Key::FromBinary((uint8_t*)GetParam()->data());
|
2015-04-09 20:07:03 +02:00
|
|
|
Data stored(Key::BINARY_LENGTH);
|
2014-12-13 11:59:48 +01:00
|
|
|
key.ToBinary(stored.data());
|
|
|
|
Key loaded = Key::FromBinary(stored.data());
|
|
|
|
EXPECT_EQ(key, loaded);
|
|
|
|
}
|
|
|
|
|
2014-12-13 12:31:24 +01:00
|
|
|
class KeyTestWithKeyParam: public KeyTest, public WithParamInterface<Key> {};
|
|
|
|
INSTANTIATE_TEST_CASE_P(KeyTestWithKeyParam, KeyTestWithKeyParam, Values(Key::FromString("2898B4B8A13CA63CBE0F0278CCE465DB"), Key::FromString("6FFEBAD90C0DAA2B79628F0627CE9841")));
|
2014-12-13 11:59:48 +01:00
|
|
|
|
2014-12-13 12:31:24 +01:00
|
|
|
TEST_P(KeyTestWithKeyParam, CopyConstructor) {
|
|
|
|
Key copy(GetParam());
|
|
|
|
EXPECT_EQ(GetParam(), copy);
|
2014-12-13 11:59:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(KeyTest, CopyConstructorDoesntChangeSource) {
|
|
|
|
Key key1 = Key::FromString(KEY1_AS_STRING);
|
|
|
|
Key key2(key1);
|
|
|
|
EXPECT_EQ(KEY1_AS_STRING, key1.ToString());
|
|
|
|
}
|
|
|
|
|
2014-12-13 12:31:24 +01:00
|
|
|
TEST_P(KeyTestWithKeyParam, IsEqualAfterAssignment1) {
|
2014-12-13 11:59:48 +01:00
|
|
|
Key key2 = Key::FromString(KEY2_AS_STRING);
|
2014-12-13 12:31:24 +01:00
|
|
|
EXPECT_NE(GetParam(), key2);
|
|
|
|
key2 = GetParam();
|
|
|
|
EXPECT_EQ(GetParam(), key2);
|
2014-12-13 11:59:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(KeyTest, AssignmentDoesntChangeSource) {
|
|
|
|
Key key1 = Key::FromString(KEY1_AS_STRING);
|
|
|
|
Key key2 = Key::FromString(KEY2_AS_STRING);
|
|
|
|
key2 = key1;
|
|
|
|
EXPECT_EQ(KEY1_AS_STRING, key1.ToString());
|
|
|
|
}
|
|
|
|
|
2014-12-09 20:47:49 +01:00
|
|
|
// This tests that a Key object is very lightweight
|
|
|
|
// (we will often pass keys around)
|
|
|
|
TEST_F(KeyTest, KeyIsLightweightObject) {
|
2015-04-09 20:07:03 +02:00
|
|
|
EXPECT_EQ(Key::BINARY_LENGTH, sizeof(Key));
|
2014-12-09 20:47:49 +01:00
|
|
|
}
|