From d1653533a6f6f70d1d95001abf3f80665ba135e7 Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Sun, 9 Aug 2020 00:27:55 -0400 Subject: test(tvix): Cover scanForReferences in a test Aded a few test cases covering the scanForReferences function, which had been accidentally broken in 976a36c (which is now partially-reverted). As part of this, since the test needed to generate hashes for store paths, the logic in MakeStorePath to compress a sha256 hash down to 20 bytes and convert it to base32 has been extracted to a member function on the Hash class. Fixes: #34 Change-Id: Ie2d914688a80f42d0234d351a7cc0714fd15709e Reviewed-on: https://cl.tvl.fyi/c/depot/+/1698 Tested-by: BuildkiteCI Reviewed-by: kanepyork --- third_party/nix/src/tests/CMakeLists.txt | 10 ++++ third_party/nix/src/tests/references_test.cc | 74 ++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 third_party/nix/src/tests/references_test.cc (limited to 'third_party/nix/src/tests') diff --git a/third_party/nix/src/tests/CMakeLists.txt b/third_party/nix/src/tests/CMakeLists.txt index 929acc5ea671..cda8f5da8483 100644 --- a/third_party/nix/src/tests/CMakeLists.txt +++ b/third_party/nix/src/tests/CMakeLists.txt @@ -30,6 +30,16 @@ target_link_libraries(hash_test gtest_discover_tests(hash_test) +add_executable(references_test references_test.cc) +target_link_libraries(references_test + nixstore + rapidcheck + rapidcheck_gtest + GTest::gtest_main +) + +gtest_discover_tests(references_test) + add_executable(store_test store_tests.cc) target_link_libraries(store_test nixstore diff --git a/third_party/nix/src/tests/references_test.cc b/third_party/nix/src/tests/references_test.cc new file mode 100644 index 000000000000..8dcb3ed37a8b --- /dev/null +++ b/third_party/nix/src/tests/references_test.cc @@ -0,0 +1,74 @@ +#include "libstore/references.hh" + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "libutil/hash.hh" + +class ReferencesTest : public ::testing::Test {}; + +namespace nix { + +TEST(ReferencesTest, ScanForOneReferenceNotFound) { + char path[] = "store_XXXXXXX"; + auto f = mkstemp(path); + + auto hash = hashString(htSHA256, "foo"); + auto ref = absl::StrFormat("/nix/store/%s-foo", hash.ToStorePathHash()); + + HashResult hr; + auto result = scanForReferences(path, {ref}, hr); + + ASSERT_EQ(result.find(ref), result.end()); + + EXPECT_EQ(close(f), 0); +} + +TEST(ReferencesTest, ScanForOneReferenceFound) { + char path[] = "store_XXXXXXX"; + auto f = mkstemp(path); + + auto hash = hashString(htSHA256, "foo"); + auto ref = absl::StrFormat("/nix/store/%s-foo", hash.ToStorePathHash()); + + EXPECT_GT(write(f, ref.c_str(), sizeof(char) * ref.size()), 0); + + HashResult hr; + auto result = scanForReferences(path, {ref}, hr); + + ASSERT_NE(result.find(ref), result.end()); + + ASSERT_EQ(close(f), 0); +} + +RC_GTEST_PROP(ReferencesTest, ScanForReferences, + (std::unordered_set strs)) { + char path[] = "store_XXXXXXX"; + auto f = mkstemp(path); + + PathSet refs; + for (const auto& s : strs) { + auto hash = hashString(htSHA256, s); + auto ref = absl::StrFormat("/nix/store/%s-foo", hash.ToStorePathHash()); + refs.insert(ref); + RC_ASSERT(write(f, ref.c_str(), sizeof(char) * ref.size()) > 0); + } + + HashResult hr; + auto result = scanForReferences(path, refs, hr); + + for (const auto& ref : refs) { + RC_ASSERT(result.find(ref) != result.end()); + } + + RC_ASSERT(close(f) == 0); +} + +} // namespace nix -- cgit 1.4.1