libcryfs/test/data/FixedSizeDataTest.cpp

144 lines
5.5 KiB
C++
Raw Normal View History

2015-04-25 17:45:18 +02:00
#include "../../data/DataFixture.h"
2015-04-25 02:21:39 +02:00
#include "../../data/FixedSizeData.h"
#include "../../data/Data.h"
#include "google/gtest/gtest.h"
using ::testing::Test;
using ::testing::WithParamInterface;
using ::testing::Values;
using std::string;
using namespace cpputils;
class FixedSizeDataTest: public Test {
public:
static constexpr unsigned int SIZE = 16;
const string DATA1_AS_STRING = "1491BB4932A389EE14BC7090AC772972";
const string DATA2_AS_STRING = "272EE5517627CFA147A971A8E6E747E0";
2015-04-25 17:45:18 +02:00
const Data DATA3_AS_BINARY;
const Data DATA4_AS_BINARY;
2015-04-25 02:21:39 +02:00
2015-04-25 17:45:18 +02:00
FixedSizeDataTest() : DATA3_AS_BINARY(DataFixture::generate(SIZE, 1)), DATA4_AS_BINARY(DataFixture::generate(SIZE, 2)) {}
2015-04-25 02:21:39 +02:00
template<unsigned int SIZE>
2015-04-25 17:45:18 +02:00
void EXPECT_DATA_EQ(const Data &expected, const FixedSizeData<SIZE> &actual) {
2015-04-25 02:21:39 +02:00
EXPECT_EQ(expected.size(), SIZE);
EXPECT_EQ(0, std::memcmp(expected.data(), actual.data(), SIZE));
}
};
constexpr unsigned int FixedSizeDataTest::SIZE;
TEST_F(FixedSizeDataTest, EqualsTrue) {
FixedSizeData<SIZE> DATA1_1 = FixedSizeData<SIZE>::FromString(DATA1_AS_STRING);
FixedSizeData<SIZE> DATA1_2 = FixedSizeData<SIZE>::FromString(DATA1_AS_STRING);
EXPECT_TRUE(DATA1_1 == DATA1_2);
EXPECT_TRUE(DATA1_2 == DATA1_1);
}
TEST_F(FixedSizeDataTest, EqualsFalse) {
FixedSizeData<SIZE> DATA1_1 = FixedSizeData<SIZE>::FromString(DATA1_AS_STRING);
FixedSizeData<SIZE> DATA2_1 = FixedSizeData<SIZE>::FromString(DATA2_AS_STRING);
EXPECT_FALSE(DATA1_1 == DATA2_1);
EXPECT_FALSE(DATA2_1 == DATA1_1);
}
TEST_F(FixedSizeDataTest, NotEqualsFalse) {
FixedSizeData<SIZE> DATA1_1 = FixedSizeData<SIZE>::FromString(DATA1_AS_STRING);
FixedSizeData<SIZE> DATA1_2 = FixedSizeData<SIZE>::FromString(DATA1_AS_STRING);
EXPECT_FALSE(DATA1_1 != DATA1_2);
EXPECT_FALSE(DATA1_2 != DATA1_1);
}
TEST_F(FixedSizeDataTest, NotEqualsTrue) {
FixedSizeData<SIZE> DATA1_1 = FixedSizeData<SIZE>::FromString(DATA1_AS_STRING);
FixedSizeData<SIZE> DATA2_1 = FixedSizeData<SIZE>::FromString(DATA2_AS_STRING);
EXPECT_TRUE(DATA1_1 != DATA2_1);
EXPECT_TRUE(DATA2_1 != DATA1_1);
}
class FixedSizeDataTestWithStringParam: public FixedSizeDataTest, public WithParamInterface<string> {};
INSTANTIATE_TEST_CASE_P(FixedSizeDataTestWithStringParam, FixedSizeDataTestWithStringParam, Values("2898B4B8A13CA63CBE0F0278CCE465DB", "6FFEBAD90C0DAA2B79628F0627CE9841"));
TEST_P(FixedSizeDataTestWithStringParam, FromAndToString) {
FixedSizeData<SIZE> data = FixedSizeData<SIZE>::FromString(GetParam());
EXPECT_EQ(GetParam(), data.ToString());
}
TEST_P(FixedSizeDataTestWithStringParam, ToAndFromString) {
FixedSizeData<SIZE> data = FixedSizeData<SIZE>::FromString(GetParam());
FixedSizeData<SIZE> data2 = FixedSizeData<SIZE>::FromString(data.ToString());
EXPECT_EQ(data, data2);
}
2015-04-25 17:45:18 +02:00
class FixedSizeDataTestWithBinaryParam: public FixedSizeDataTest, public WithParamInterface<const Data*> {
2015-04-25 02:21:39 +02:00
public:
2015-04-25 17:45:18 +02:00
static const Data VALUE1;
static const Data VALUE2;
2015-04-25 02:21:39 +02:00
};
2015-04-25 17:45:18 +02:00
const Data FixedSizeDataTestWithBinaryParam::VALUE1(DataFixture::generate(SIZE, 3));
const Data FixedSizeDataTestWithBinaryParam::VALUE2(DataFixture::generate(SIZE, 4));
2015-04-25 02:21:39 +02:00
INSTANTIATE_TEST_CASE_P(FixedSizeDataTestWithBinaryParam, FixedSizeDataTestWithBinaryParam, Values(&FixedSizeDataTestWithBinaryParam::VALUE1, &FixedSizeDataTestWithBinaryParam::VALUE2));
TEST_P(FixedSizeDataTestWithBinaryParam, FromBinary) {
FixedSizeData<SIZE> data = FixedSizeData<SIZE>::FromBinary((uint8_t*)GetParam()->data());
EXPECT_DATA_EQ(*GetParam(), data);
}
TEST_P(FixedSizeDataTestWithBinaryParam, FromAndToBinary) {
FixedSizeData<SIZE> data = FixedSizeData<SIZE>::FromBinary((uint8_t*)GetParam()->data());
Data output(FixedSizeData<SIZE>::BINARY_LENGTH);
data.ToBinary(output.data());
2015-04-25 17:45:18 +02:00
EXPECT_EQ(*GetParam(), output);
2015-04-25 02:21:39 +02:00
}
TEST_P(FixedSizeDataTestWithBinaryParam, ToAndFromBinary) {
FixedSizeData<SIZE> data = FixedSizeData<SIZE>::FromBinary((uint8_t*)GetParam()->data());
Data stored(FixedSizeData<SIZE>::BINARY_LENGTH);
data.ToBinary(stored.data());
FixedSizeData<SIZE> loaded = FixedSizeData<SIZE>::FromBinary(stored.data());
EXPECT_EQ(data, loaded);
}
class FixedSizeDataTestWithParam: public FixedSizeDataTest, public WithParamInterface<FixedSizeData<FixedSizeDataTest::SIZE>> {};
INSTANTIATE_TEST_CASE_P(FixedSizeDataTestWithParam, FixedSizeDataTestWithParam, Values(FixedSizeData<FixedSizeDataTest::SIZE>::FromString("2898B4B8A13CA63CBE0F0278CCE465DB"), FixedSizeData<FixedSizeDataTest::SIZE>::FromString("6FFEBAD90C0DAA2B79628F0627CE9841")));
TEST_P(FixedSizeDataTestWithParam, CopyConstructor) {
FixedSizeData<SIZE> copy(GetParam());
EXPECT_EQ(GetParam(), copy);
}
TEST_F(FixedSizeDataTest, CopyConstructorDoesntChangeSource) {
FixedSizeData<SIZE> data1 = FixedSizeData<SIZE>::FromString(DATA1_AS_STRING);
FixedSizeData<SIZE> data2(data1);
EXPECT_EQ(DATA1_AS_STRING, data1.ToString());
}
TEST_P(FixedSizeDataTestWithParam, IsEqualAfterAssignment1) {
FixedSizeData<SIZE> data2 = FixedSizeData<SIZE>::FromString(DATA2_AS_STRING);
EXPECT_NE(GetParam(), data2);
data2 = GetParam();
EXPECT_EQ(GetParam(), data2);
}
TEST_F(FixedSizeDataTest, AssignmentDoesntChangeSource) {
FixedSizeData<SIZE> data1 = FixedSizeData<SIZE>::FromString(DATA1_AS_STRING);
FixedSizeData<SIZE> data2 = FixedSizeData<SIZE>::FromString(DATA2_AS_STRING);
data2 = data1;
EXPECT_EQ(DATA1_AS_STRING, data1.ToString());
}
// This tests that a FixedSizeData object is very lightweight
// (it is meant to be kept on stack and passed around)
TEST_F(FixedSizeDataTest, IsLightweightObject) {
EXPECT_EQ(FixedSizeData<SIZE>::BINARY_LENGTH, sizeof(FixedSizeData<SIZE>));
}