File: TestAclAceRange.cpp

package info (click to toggle)
firefox 147.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 4,683,324 kB
  • sloc: cpp: 7,607,156; javascript: 6,532,492; ansic: 3,775,158; python: 1,415,368; xml: 634,556; asm: 438,949; java: 186,241; sh: 62,751; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (249 lines) | stat: -rw-r--r-- 7,252 bytes parent folder | download | duplicates (2)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* -*- 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 "WinHeaderOnlyUtils.h"

#include <algorithm>

#include "gtest/gtest.h"
#include "mozilla/gtest/MozHelpers.h"

using namespace mozilla;

struct TestAcl {
  ACL acl{ACL_REVISION, 0, sizeof(TestAcl), 3, 0};
  ACCESS_ALLOWED_ACE ace1{
      {ACCESS_ALLOWED_ACE_TYPE, OBJECT_INHERIT_ACE, sizeof(ACCESS_ALLOWED_ACE)},
      GENERIC_READ,
      0};
  ACCESS_ALLOWED_OBJECT_ACE ace2{{ACCESS_ALLOWED_OBJECT_ACE_TYPE, INHERITED_ACE,
                                  sizeof(ACCESS_ALLOWED_OBJECT_ACE)},
                                 GENERIC_READ,
                                 0};
  ACCESS_DENIED_ACE ace3{
      {ACCESS_DENIED_ACE_TYPE, INHERITED_ACE, sizeof(ACCESS_DENIED_ACE)},
      GENERIC_READ,
      0};
  NotNull<ACL*> AsAclPtr() { return WrapNotNull(reinterpret_cast<ACL*>(this)); }
};

TEST(AclAceRange, SimpleCount)
{
  TestAcl testAcl;
  int aceCount = 0;
  for (const auto& aceHeader : AclAceRange(testAcl.AsAclPtr())) {
    (void)aceHeader;
    ++aceCount;
  }

  ASSERT_EQ(aceCount, 3);
}

TEST(AclAceRange, SameAsGetAce)
{
  TestAcl testAcl;
  int aceIdx = 0;
  for (const auto& aceHeader : AclAceRange(testAcl.AsAclPtr())) {
    VOID* pGetAceHeader = nullptr;
    EXPECT_TRUE(::GetAce(testAcl.AsAclPtr(), aceIdx, &pGetAceHeader));
    auto* getAceHeader = static_cast<ACE_HEADER*>(pGetAceHeader);
    EXPECT_EQ(getAceHeader->AceType, aceHeader.AceType);
    EXPECT_EQ(getAceHeader->AceFlags, aceHeader.AceFlags);
    EXPECT_EQ(getAceHeader->AceSize, aceHeader.AceSize);
    ++aceIdx;
  }
}

TEST(AclAceRange, WithFlagCount)
{
  TestAcl testAcl;
  int aceCount = 0;
  for (const auto& aceHeader : AclAceRange(testAcl.AsAclPtr())) {
    if (aceHeader.AceFlags & INHERITED_ACE) {
      ++aceCount;
    }
  }

  ASSERT_EQ(aceCount, 2);
}

TEST(AclAceRange, AclSizeCheckedAsWellAsCount)
{
  TestAcl testAcl;
  testAcl.acl.AclSize -= sizeof(ACCESS_DENIED_ACE);
  int aceCount = 0;
  for (const auto& aceHeader : AclAceRange(testAcl.AsAclPtr())) {
    if (aceHeader.AceFlags & INHERITED_ACE) {
      ++aceCount;
    }
  }

  ASSERT_EQ(aceCount, 1);
}

TEST(AclAceRange, ChecksAceHeaderSizeInAclSize)
{
  TestAcl testAcl;
  testAcl.acl.AclSize -= 1;
  int aceCount = 0;
  for (const auto& aceHeader : AclAceRange(testAcl.AsAclPtr())) {
    if (aceHeader.AceFlags & INHERITED_ACE) {
      ++aceCount;
    }
  }

  ASSERT_EQ(aceCount, 1);
}

TEST(AclAceRange, AceCountOfZeroResultsInNoIterations)
{
  TestAcl testAcl;
  testAcl.acl.AceCount = 0;
  int aceCount = 0;
  for (const auto& aceHeader : AclAceRange(testAcl.AsAclPtr())) {
    (void)aceHeader;
    ++aceCount;
  }

  ASSERT_EQ(aceCount, 0);
}

TEST(AclAceRange, AclSizeTooSmallForAnyAcesResultsInNoIterations)
{
  TestAcl testAcl;
  testAcl.acl.AclSize = sizeof(ACCESS_ALLOWED_ACE) - 1;
  int aceCount = 0;
  for (const auto& aceHeader : AclAceRange(testAcl.AsAclPtr())) {
    (void)aceHeader;
    ++aceCount;
  }

  ASSERT_EQ(aceCount, 0);
}

TEST(AclAceRange, weakly_incrementable)
{
  TestAcl testAcl;
  AclAceRange aclAceRange(testAcl.AsAclPtr());
  auto iter = aclAceRange.begin();

  EXPECT_TRUE(std::addressof(++iter) == std::addressof(iter))
      << "addressof pre-increment result should match iterator";

  // pre and post increment advance iterator.
  EXPECT_EQ(iter->AceType, testAcl.ace2.Header.AceType);
  EXPECT_EQ(iter->AceFlags, testAcl.ace2.Header.AceFlags);
  EXPECT_EQ(iter->AceSize, testAcl.ace2.Header.AceSize);
  iter++;
  EXPECT_EQ(iter->AceType, testAcl.ace3.Header.AceType);
  EXPECT_EQ(iter->AceFlags, testAcl.ace3.Header.AceFlags);
  EXPECT_EQ(iter->AceSize, testAcl.ace3.Header.AceSize);

  // Moveable.
  auto moveConstructedIter(std::move(iter));
  EXPECT_EQ(moveConstructedIter->AceType, testAcl.ace3.Header.AceType);
  EXPECT_EQ(moveConstructedIter->AceFlags, testAcl.ace3.Header.AceFlags);
  EXPECT_EQ(moveConstructedIter->AceSize, testAcl.ace3.Header.AceSize);
  auto moveAssignedIter = std::move(iter);
  EXPECT_EQ(moveAssignedIter->AceType, testAcl.ace3.Header.AceType);
  EXPECT_EQ(moveAssignedIter->AceFlags, testAcl.ace3.Header.AceFlags);
  EXPECT_EQ(moveAssignedIter->AceSize, testAcl.ace3.Header.AceSize);
}

TEST(AclAceRange, incrementable)
{
  TestAcl testAcl;
  AclAceRange aclAceRange1(testAcl.AsAclPtr());
  AclAceRange aclAceRange2(testAcl.AsAclPtr());
  auto it1 = aclAceRange1.begin();
  auto it2 = aclAceRange2.begin();

  // bool(a == b) implies bool(a++ == b)
  EXPECT_TRUE(it1 == it2) << "begin iterators for same ACL should be equal";
  EXPECT_TRUE(it1++ == it2);
  EXPECT_FALSE(it1 == it2);
  EXPECT_FALSE(it1++ == it2);

  // bool(a == b) implies bool(((void)a++, a) == ++b)
  it1 = aclAceRange1.begin();
  EXPECT_TRUE(it1 == it2);
  EXPECT_TRUE(((void)it1++, it1) == ++it2);
  it1 = aclAceRange1.begin();
  EXPECT_FALSE(it1 == it2);
  EXPECT_FALSE(((void)it1++, it1) == ++it2);

  // Copyable.
  auto copyConstructedIter(it2);
  EXPECT_EQ(copyConstructedIter->AceType, testAcl.ace3.Header.AceType);
  EXPECT_EQ(copyConstructedIter->AceFlags, testAcl.ace3.Header.AceFlags);
  EXPECT_EQ(copyConstructedIter->AceSize, testAcl.ace3.Header.AceSize);
  auto copyAssignedIter = it2;
  EXPECT_EQ(copyAssignedIter->AceType, testAcl.ace3.Header.AceType);
  EXPECT_EQ(copyAssignedIter->AceFlags, testAcl.ace3.Header.AceFlags);
  EXPECT_EQ(copyAssignedIter->AceSize, testAcl.ace3.Header.AceSize);

  // Default constructable.
  AclAceRange::Iterator defaultConstructed;
  EXPECT_TRUE(defaultConstructed == aclAceRange1.end());
}

TEST(AclAceRange, AlgorithmCountIf)
{
  TestAcl testAcl;
  AclAceRange aclAceRange(testAcl.AsAclPtr());
  auto aceCount = std::count_if(
      aclAceRange.begin(), aclAceRange.end(),
      [](const auto& hdr) { return hdr.AceFlags & INHERITED_ACE; });

  ASSERT_EQ(aceCount, 2);
}

TEST(AclAceRange, AlgorithmAnyOf)
{
  TestAcl testAcl;
  AclAceRange aclAceRange(testAcl.AsAclPtr());
  auto anyInherited =
      std::any_of(aclAceRange.begin(), aclAceRange.end(),
                  [](const auto& hdr) { return hdr.AceFlags & INHERITED_ACE; });

  ASSERT_TRUE(anyInherited);
}

TEST(AclAceRange, DereferenceAtEndIsFatal)
{
#if DEBUG
  const auto* msg =
      "Assertion failure: mAceCount \\(Trying to dereference past end of "
      "AclAceRange\\)";
#else
  const auto* msg = "";
#endif

  EXPECT_DEATH_WRAP(
      {
        TestAcl testAcl;
        AclAceRange aclAceRange(testAcl.AsAclPtr());
        auto aceItCurrent = aclAceRange.begin();
        for (; aceItCurrent != aclAceRange.end(); ++aceItCurrent) {
        }
        *aceItCurrent;
      },
      msg);
}

TEST(AclAceRange, DebugAssertForIteratingPastEnd)
{
  EXPECT_DEBUG_DEATH_WRAP(
      {
        TestAcl testAcl;
        AclAceRange aclAceRange(testAcl.AsAclPtr());
        auto aceItCurrent = aclAceRange.begin();
        for (; aceItCurrent != aclAceRange.end(); ++aceItCurrent) {
        }
        ++aceItCurrent;
      },
      "Assertion failure: mAceCount \\(Iterating past end of AclAceRange\\)");
}