/test/storage/checksum/checksumvalidator.cpp

https://gitlab.com/admin-github-cloud/cynara · C++ · 154 lines · 81 code · 27 blank · 46 comment · 4 complexity · 1ab3b1645ec25e33e32a72a3d62fbc35 MD5 · raw file

  1. /*
  2. * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License
  15. */
  16. /**
  17. * @file test/storage/checksum/checksumvalidator.cpp
  18. * @author Pawel Wieczorek <p.wieczorek2@samsung.com>
  19. * @version 1.0
  20. * @brief Tests of ChecksumValidator
  21. */
  22. #include <gmock/gmock.h>
  23. #include <gtest/gtest.h>
  24. #include <cstddef>
  25. #include <memory>
  26. #include <sstream>
  27. #include <string>
  28. #include <unordered_map>
  29. #include <config/PathConfig.h>
  30. #include <exceptions/ChecksumRecordCorruptedException.h>
  31. #include <storage/ChecksumValidator.h>
  32. #include "checksumvalidatorfixture.h"
  33. #include "fakechecksumvalidator.h"
  34. using namespace Cynara;
  35. const std::string ChecksumValidatorFixture::m_dbPath("/fake/path/");
  36. const std::string ChecksumValidatorFixture::m_filename("fakeFilename");
  37. const std::string ChecksumValidatorFixture::m_checksum("$1$$fakeChecksum");
  38. const char ChecksumValidatorFixture::m_fieldSeparator(PathConfig::StoragePath::fieldSeparator);
  39. const char ChecksumValidatorFixture::m_recordSeparator(PathConfig::StoragePath::recordSeparator);
  40. const size_t ChecksumValidatorFixture::m_firstLine(1);
  41. /**
  42. * @brief Verify if generate() can use systems' crypt(3) implementation
  43. * @test Expected result: no exceptions are thrown and non-empty string is returned
  44. */
  45. TEST_F(ChecksumValidatorFixture, generateChecksum) {
  46. for (const auto &data : { std::string("test-data"), std::string() }) {
  47. SCOPED_TRACE(data);
  48. std::string returnedChecksum;
  49. ASSERT_NO_THROW(returnedChecksum = ChecksumValidator::generate(data));
  50. ASSERT_NE(std::string(), returnedChecksum);
  51. }
  52. }
  53. /**
  54. * @brief Verify if load() can successfully parse sample checksum record
  55. * @test Expected result:
  56. * - no exceptions are thrown from load()
  57. * - proper checksums are stored for corresponding files
  58. */
  59. TEST_F(ChecksumValidatorFixture, parseCorrectRecords) {
  60. std::istringstream checksums(m_filename + m_fieldSeparator + m_checksum);
  61. FakeChecksumValidator validator(m_dbPath);
  62. ASSERT_NO_THROW(validator.load(checksums));
  63. ASSERT_EQ(m_checksum, validator.sums().at(m_filename));
  64. }
  65. /**
  66. * @brief Verify if load() rejects storing corrupted checksum records
  67. * @test Expected result:
  68. * - load() throws ChecksumRecordCorruptedException
  69. * - no data is stored (there was no correct data to insert)
  70. */
  71. TEST_F(ChecksumValidatorFixture, parseCorruptedRecords) {
  72. const auto badLines = { m_filename + m_fieldSeparator,
  73. m_filename + m_checksum,
  74. m_fieldSeparator + m_checksum };
  75. FakeChecksumValidator validator(m_dbPath);
  76. for (const auto &badLine : badLines) {
  77. SCOPED_TRACE(badLine);
  78. std::istringstream checksums(badLine);
  79. ASSERT_THROW(validator.load(checksums), ChecksumRecordCorruptedException);
  80. ASSERT_TRUE(validator.sums().empty());
  81. }
  82. }
  83. /**
  84. * @brief Verify if compare() can successfully check sample database file
  85. * @test Expected result: no exceptions are thrown and file contents are still readable
  86. */
  87. TEST_F(ChecksumValidatorFixture, compareBasicAndBackup) {
  88. FakeChecksumValidator validator(m_dbPath);
  89. std::string checksumsContents;
  90. simpleChecksumsContents(checksumsContents);
  91. std::istringstream checksums(checksumsContents);
  92. validator.load(checksums);
  93. std::unordered_map<std::string, std::string> files = { { "_", std::string() },
  94. { "buckets", ";0x0;\n" } };
  95. for (const auto &file : files) {
  96. const auto filename = m_dbPath + file.first;
  97. const auto contents = file.second;
  98. std::istringstream fakeFile(contents);
  99. SCOPED_TRACE(filename);
  100. ASSERT_NO_THROW(validator.compare(fakeFile, filename, false));
  101. ASSERT_NO_THROW(validator.compare(fakeFile, filename +
  102. PathConfig::StoragePath::backupFilenameSuffix, true));
  103. ASSERT_EQ(contents, fakeFile.str());
  104. }
  105. }
  106. /**
  107. * @brief Verify if compare() throws an exception when checksum mismatch is detected
  108. * @test Expected result: ChecksumRecordCorruptedException is thrown
  109. */
  110. TEST_F(ChecksumValidatorFixture, checksumMismatch) {
  111. FakeChecksumValidator validator(m_dbPath);
  112. std::string checksumsContents;
  113. simpleChecksumsContents(checksumsContents);
  114. std::istringstream checksums(checksumsContents);
  115. validator.load(checksums);
  116. // Please note that default policy is set to ALLOW instead of DENY
  117. std::unordered_map<std::string, std::string> files = { { "_", "client;user;privilege;0x0;" },
  118. { "buckets", ";0xFFFF;\n" } };
  119. for (const auto &file : files) {
  120. const auto filename = m_dbPath + file.first;
  121. const auto contents = file.second;
  122. std::istringstream fakeFile(contents);
  123. SCOPED_TRACE(filename);
  124. ASSERT_THROW(validator.compare(fakeFile, filename, false),
  125. ChecksumRecordCorruptedException);
  126. }
  127. }