summaryrefslogtreecommitdiffstats
path: root/dom/fs/test/gtest/parent/TestFileSystemHashSource.cpp
blob: 2a680838a8f17f59c71b37c80ae6c842cbcb7d5a (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "FileSystemHashSource.h"
#include "TestHelpers.h"
#include "gtest/gtest.h"
#include "mozilla/Array.h"
#include "mozilla/dom/FileSystemTypes.h"
#include "nsContentUtils.h"
#include "nsLiteralString.h"
#include "nsString.h"
#include "nsStringFwd.h"
#include "nsTArray.h"
#include "nsTHashSet.h"

namespace mozilla::dom::fs::test {

using mozilla::dom::fs::data::FileSystemHashSource;

namespace {

constexpr size_t sha256ByteLength = 32u;

constexpr size_t kExpectedLength = 52u;

std::wstring asWide(const nsString& aStr) {
  std::wstring result;
  result.reserve(aStr.Length());
  for (const auto* it = aStr.BeginReading(); it != aStr.EndReading(); ++it) {
    result.push_back(static_cast<wchar_t>(*it));
  }
  return result;
}

}  // namespace

TEST(TestFileSystemHashSource, isHashLengthAsExpected)
{
  EntryId parent = "a"_ns;
  Name name = u"b"_ns;
  TEST_TRY_UNWRAP(EntryId result,
                  FileSystemHashSource::GenerateHash(parent, name));
  ASSERT_EQ(sha256ByteLength, result.Length());
};

TEST(TestFileSystemHashSource, areNestedNameHashesValidAndUnequal)
{
  EntryId emptyParent = ""_ns;
  Name name = u"a"_ns;
  const size_t nestingNumber = 500u;

  nsTHashSet<EntryId> results;
  nsTHashSet<Name> names;

  auto previousParent = emptyParent;
  for (size_t i = 0; i < nestingNumber; ++i) {
    TEST_TRY_UNWRAP(EntryId result,
                    FileSystemHashSource::GenerateHash(previousParent, name));

    TEST_TRY_UNWRAP(Name encoded, FileSystemHashSource::EncodeHash(result));

    // Validity checks
    ASSERT_TRUE(mozilla::IsAscii(encoded))
    << encoded;
    Name upperCaseVersion;
    nsContentUtils::ASCIIToUpper(encoded, upperCaseVersion);
    ASSERT_STREQ(asWide(upperCaseVersion).c_str(), asWide(encoded).c_str());

    // Is the same hash encountered?
    ASSERT_FALSE(results.Contains(result));
    ASSERT_TRUE(results.Insert(result, mozilla::fallible));

    // Is the same name encountered?
    ASSERT_FALSE(names.Contains(encoded));
    ASSERT_TRUE(names.Insert(encoded, mozilla::fallible));

    previousParent = result;
  }
};

TEST(TestFileSystemHashSource, areNameCombinationHashesUnequal)
{
  EntryId emptyParent = ""_ns;

  mozilla::Array<Name, 2> inputs = {u"a"_ns, u"b"_ns};
  nsTArray<EntryId> results;
  nsTArray<Name> names;

  for (const auto& name : inputs) {
    TEST_TRY_UNWRAP(EntryId result,
                    FileSystemHashSource::GenerateHash(emptyParent, name));
    TEST_TRY_UNWRAP(Name encoded, FileSystemHashSource::EncodeHash(result));

    // Validity checks
    ASSERT_TRUE(mozilla::IsAscii(encoded))
    << encoded;
    Name upperCaseVersion;
    nsContentUtils::ASCIIToUpper(encoded, upperCaseVersion);
    ASSERT_STREQ(asWide(upperCaseVersion).c_str(), asWide(encoded).c_str());

    results.AppendElement(result);
    names.AppendElement(encoded);
  }

  nsTArray<EntryId> more_results;
  nsTArray<Name> more_names;
  for (const auto& parent : results) {
    for (const auto& name : inputs) {
      TEST_TRY_UNWRAP(EntryId result,
                      FileSystemHashSource::GenerateHash(parent, name));
      TEST_TRY_UNWRAP(Name encoded, FileSystemHashSource::EncodeHash(result));

      // Validity checks
      ASSERT_TRUE(mozilla::IsAscii(encoded))
      << encoded;
      Name upperCaseVersion;
      nsContentUtils::ASCIIToUpper(encoded, upperCaseVersion);
      ASSERT_STREQ(asWide(upperCaseVersion).c_str(), asWide(encoded).c_str());

      more_results.AppendElement(result);
      more_names.AppendElement(encoded);
    }
  }

  results.AppendElements(more_results);
  names.AppendElements(more_names);

  // Is the same hash encountered?
  for (size_t i = 0; i < results.Length(); ++i) {
    for (size_t j = i + 1; j < results.Length(); ++j) {
      ASSERT_STRNE(results[i].get(), results[j].get());
    }
  }

  // Is the same name encountered?
  for (size_t i = 0; i < names.Length(); ++i) {
    for (size_t j = i + 1; j < names.Length(); ++j) {
      ASSERT_STRNE(asWide(names[i]).c_str(), asWide(names[j]).c_str());
    }
  }
};

TEST(TestFileSystemHashSource, encodeGeneratedHash)
{
  Name expected = u"HF6FOFV72G3NMDEJKYMVRIFJO4X5ZNZCF2GM7Q4Y5Q3E7NPQKSLA"_ns;
  ASSERT_EQ(kExpectedLength, expected.Length());

  EntryId parent = "a"_ns;
  Name name = u"b"_ns;
  TEST_TRY_UNWRAP(EntryId entry,
                  FileSystemHashSource::GenerateHash(parent, name));
  ASSERT_EQ(sha256ByteLength, entry.Length());

  TEST_TRY_UNWRAP(Name result, FileSystemHashSource::EncodeHash(entry));
  ASSERT_EQ(kExpectedLength, result.Length());
  ASSERT_STREQ(asWide(expected).c_str(), asWide(result).c_str());

  // Generate further hashes
  TEST_TRY_UNWRAP(entry, FileSystemHashSource::GenerateHash(entry, result));
  ASSERT_EQ(sha256ByteLength, entry.Length());

  TEST_TRY_UNWRAP(result, FileSystemHashSource::EncodeHash(entry));

  // Always the same length
  ASSERT_EQ(kExpectedLength, result.Length());

  // Encoded versions should differ
  ASSERT_STRNE(asWide(expected).c_str(), asWide(result).c_str());

  // Padding length should have been stripped
  char16_t padding = u"="_ns[0];
  const int32_t paddingStart = result.FindChar(padding);
  ASSERT_EQ(-1, paddingStart);
};

}  // namespace mozilla::dom::fs::test