nigori_storage_impl_unittest.cc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/sync/nigori/nigori_storage_impl.h"
  5. #include "base/files/file_util.h"
  6. #include "base/files/scoped_temp_dir.h"
  7. #include "components/os_crypt/os_crypt_mocker.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace syncer {
  10. namespace {
  11. sync_pb::NigoriLocalData MakeSomeNigoriLocalData() {
  12. sync_pb::NigoriLocalData result;
  13. result.mutable_model_type_state()->set_initial_sync_done(true);
  14. result.mutable_entity_metadata()->set_sequence_number(1);
  15. result.mutable_nigori_model()->set_encrypt_everything(true);
  16. return result;
  17. }
  18. class NigoriStorageImplTest : public testing::Test {
  19. protected:
  20. NigoriStorageImplTest() = default;
  21. ~NigoriStorageImplTest() override = default;
  22. void SetUp() override {
  23. ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
  24. OSCryptMocker::SetUp();
  25. }
  26. void TearDown() override { OSCryptMocker::TearDown(); }
  27. base::FilePath GetFilePath() {
  28. return temp_dir_.GetPath().Append(
  29. base::FilePath(FILE_PATH_LITERAL("some_file")));
  30. }
  31. private:
  32. base::ScopedTempDir temp_dir_;
  33. };
  34. TEST_F(NigoriStorageImplTest, ShouldBeAbleToRestoreAfterWrite) {
  35. NigoriStorageImpl writer_storage(GetFilePath());
  36. sync_pb::NigoriLocalData write_data = MakeSomeNigoriLocalData();
  37. writer_storage.StoreData(write_data);
  38. // Use different NigoriStorageImpl when reading to avoid dependency on its
  39. // state and emulate browser restart.
  40. NigoriStorageImpl reader_storage(GetFilePath());
  41. absl::optional<sync_pb::NigoriLocalData> read_data =
  42. reader_storage.RestoreData();
  43. EXPECT_NE(read_data, absl::nullopt);
  44. EXPECT_EQ(read_data->SerializeAsString(), write_data.SerializeAsString());
  45. }
  46. TEST_F(NigoriStorageImplTest, ShouldReturnNulloptWhenFileNotExists) {
  47. NigoriStorageImpl storage(GetFilePath());
  48. EXPECT_EQ(storage.RestoreData(), absl::nullopt);
  49. }
  50. TEST_F(NigoriStorageImplTest, ShouldRemoveFile) {
  51. NigoriStorageImpl storage(GetFilePath());
  52. sync_pb::NigoriLocalData data = MakeSomeNigoriLocalData();
  53. storage.StoreData(data);
  54. ASSERT_TRUE(base::PathExists(GetFilePath()));
  55. storage.ClearData();
  56. EXPECT_FALSE(base::PathExists(GetFilePath()));
  57. }
  58. } // namespace
  59. } // namespace syncer