about summary refs log tree commit diff
path: root/third_party/nix/src/tests/references_test.cc
blob: 8dcb3ed37a8bfa7b36c0620653dca7ab1852ac1f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "libstore/references.hh"

#include <cstdio>
#include <fstream>
#include <ostream>
#include <unordered_set>

#include <absl/strings/str_format.h>
#include <gtest/gtest.h>
#include <rapidcheck.h>
#include <rapidcheck/gtest.h>

#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<std::string> 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