libcryfs/test/utils/KeyTest.cpp

145 lines
4.5 KiB
C++
Raw Normal View History

#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"
using ::testing::Test;
2014-12-13 12:31:24 +01:00
using ::testing::WithParamInterface;
using ::testing::Values;
using std::string;
using namespace blockstore;
//TODO Test blockstore::FixedSizeData instead of blockstore::Key
2014-12-09 20:46:34 +01:00
class KeyTest: public Test {
public:
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-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()));
}
};
TEST_F(KeyTest, CanGenerateRandomKeysWithoutCrashing) {
2015-04-09 20:07:03 +02:00
Key result = Key::CreateRandom();
}
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:46:34 +01:00
TEST_F(KeyTest, EqualsTrue) {
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) {
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) {
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) {
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());
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 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);
key.ToBinary(keydata.data());
2014-12-13 12:31:24 +01:00
EXPECT_DATA_EQ(*GetParam(), keydata);
}
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);
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 12:31:24 +01:00
TEST_P(KeyTestWithKeyParam, CopyConstructor) {
Key copy(GetParam());
EXPECT_EQ(GetParam(), copy);
}
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) {
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);
}
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
}