2017-09-23 21:17:05 +02:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <cryfs/localstate/LocalStateMetadata.h>
|
|
|
|
#include <cpp-utils/tempfile/TempDir.h>
|
|
|
|
#include <fstream>
|
2017-09-30 09:49:24 +02:00
|
|
|
#include <cpp-utils/data/DataFixture.h>
|
2017-09-23 21:17:05 +02:00
|
|
|
|
|
|
|
using cpputils::TempDir;
|
2017-09-30 09:49:24 +02:00
|
|
|
using cpputils::Data;
|
2017-09-23 21:17:05 +02:00
|
|
|
using cryfs::LocalStateMetadata;
|
2017-09-30 09:49:24 +02:00
|
|
|
using cpputils::DataFixture;
|
2017-09-23 21:17:05 +02:00
|
|
|
using std::ofstream;
|
|
|
|
|
|
|
|
class LocalStateMetadataTest : public ::testing::Test {
|
|
|
|
public:
|
|
|
|
TempDir stateDir;
|
|
|
|
TempDir stateDir2;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(LocalStateMetadataTest, myClientId_ValueIsConsistent) {
|
2018-02-08 08:08:01 +01:00
|
|
|
LocalStateMetadata metadata1 = LocalStateMetadata::loadOrGenerate(stateDir.path(), Data(0), false);
|
|
|
|
LocalStateMetadata metadata2 = LocalStateMetadata::loadOrGenerate(stateDir.path(), Data(0), false);
|
2017-09-23 21:17:05 +02:00
|
|
|
EXPECT_EQ(metadata1.myClientId(), metadata2.myClientId());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LocalStateMetadataTest, myClientId_ValueIsRandomForNewClient) {
|
2018-02-08 08:08:01 +01:00
|
|
|
LocalStateMetadata metadata1 = LocalStateMetadata::loadOrGenerate(stateDir.path(), Data(0), false);
|
|
|
|
LocalStateMetadata metadata2 = LocalStateMetadata::loadOrGenerate(stateDir2.path(), Data(0), false);
|
2017-09-23 21:17:05 +02:00
|
|
|
EXPECT_NE(metadata1.myClientId(), metadata2.myClientId());
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef CRYFS_NO_COMPATIBILITY
|
|
|
|
TEST_F(LocalStateMetadataTest, myClientId_TakesLegacyValueIfSpecified) {
|
2018-05-21 01:20:38 +02:00
|
|
|
ofstream file((stateDir.path() / "myClientId").string());
|
2017-09-23 21:17:05 +02:00
|
|
|
file << 12345u;
|
|
|
|
file.close();
|
|
|
|
|
2018-02-08 08:08:01 +01:00
|
|
|
LocalStateMetadata metadata = LocalStateMetadata::loadOrGenerate(stateDir.path(), Data(0), false);
|
2017-09-23 21:17:05 +02:00
|
|
|
EXPECT_EQ(12345u, metadata.myClientId());
|
|
|
|
}
|
|
|
|
#endif
|
2017-09-30 09:49:24 +02:00
|
|
|
|
|
|
|
TEST_F(LocalStateMetadataTest, encryptionKeyHash_whenLoadingWithSameKey_thenDoesntCrash) {
|
2018-02-08 08:08:01 +01:00
|
|
|
LocalStateMetadata::loadOrGenerate(stateDir.path(), DataFixture::generate(1024), false);
|
|
|
|
LocalStateMetadata::loadOrGenerate(stateDir.path(), DataFixture::generate(1024), false);
|
2017-09-30 09:49:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(LocalStateMetadataTest, encryptionKeyHash_whenLoadingWithDifferentKey_thenCrashes) {
|
2018-02-08 08:08:01 +01:00
|
|
|
LocalStateMetadata::loadOrGenerate(stateDir.path(), DataFixture::generate(1024, 1), false);
|
2017-09-30 09:49:24 +02:00
|
|
|
EXPECT_THROW(
|
2018-02-08 08:08:01 +01:00
|
|
|
LocalStateMetadata::loadOrGenerate(stateDir.path(), DataFixture::generate(1024, 2), false),
|
2017-09-30 09:49:24 +02:00
|
|
|
std::runtime_error
|
|
|
|
);
|
|
|
|
}
|