#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