summaryrefslogtreecommitdiff
path: root/src/mongo/db/auth/role_graph_test.cpp
blob: 7a91929078d81d5ec55411a9eeb386a2427a7e2f (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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/**
 *    Copyright (C) 2013 10gen Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Unit tests of the RoleGraph type.
 */

#include <algorithm>

#include "mongo/db/auth/role_graph.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/mongoutils/str.h"

namespace mongo {
namespace {

    // Tests adding and removing roles from other roles, the RoleNameIterator, and the
    // getDirectMembers and getDirectSubordinates methods
    TEST(RoleGraphTest, AddRemoveRoles) {
        RoleName roleA("roleA", "dbA");
        RoleName roleB("roleB", "dbB");
        RoleName roleC("roleC", "dbC");
        RoleName roleD("roleD", "dbD");

        RoleGraph graph;
        ASSERT_OK(graph.createRole(roleA));
        ASSERT_OK(graph.createRole(roleB));
        ASSERT_OK(graph.createRole(roleC));
        ASSERT_OK(graph.createRole(roleD));

        RoleNameIterator it;
        it = graph.getDirectSubordinates(roleA);
        ASSERT_FALSE(it.more());
        it = graph.getDirectMembers(roleA);
        ASSERT_FALSE(it.more());

        ASSERT_OK(graph.addRoleToRole(roleA, roleB));

        // A -> B
        it = graph.getDirectSubordinates(roleA);
        ASSERT_TRUE(it.more());
        // should not advance the iterator
        ASSERT_EQUALS(it.get().getFullName(), roleB.getFullName());
        ASSERT_EQUALS(it.get().getFullName(), roleB.getFullName());
        ASSERT_EQUALS(it.next().getFullName(), roleB.getFullName());
        ASSERT_FALSE(it.more());

        it = graph.getDirectMembers(roleA);
        ASSERT_FALSE(it.more());

        it = graph.getDirectMembers(roleB);
        ASSERT_EQUALS(it.next().getFullName(), roleA.getFullName());
        ASSERT_FALSE(it.more());

        it = graph.getDirectSubordinates(roleB);
        ASSERT_FALSE(it.more());

        ASSERT_OK(graph.addRoleToRole(roleA, roleC));
        ASSERT_OK(graph.addRoleToRole(roleB, roleC));
        ASSERT_OK(graph.addRoleToRole(roleB, roleD));

        /*
         * Graph now looks like:
         *   A
         *  / \
         * v   v
         * B -> C
         * |
         * v
         * D
        */


        it = graph.getDirectSubordinates(roleA); // should be roleB and roleC, order doesn't matter
        RoleName cur = it.next();
        if (cur == roleB) {
            ASSERT_EQUALS(it.next().getFullName(), roleC.getFullName());
        } else if (cur == roleC) {
            ASSERT_EQUALS(it.next().getFullName(), roleB.getFullName());
        } else {
            FAIL(mongoutils::str::stream() << "unexpected role returned: " << cur.getFullName());
        }
        ASSERT_FALSE(it.more());

        it = graph.getDirectSubordinates(roleB); // should be roleC and roleD, order doesn't matter
        cur = it.next();
        if (cur == roleC) {
            ASSERT_EQUALS(it.next().getFullName(), roleD.getFullName());
        } else if (cur == roleD) {
            ASSERT_EQUALS(it.next().getFullName(), roleC.getFullName());
        } else {
            FAIL(mongoutils::str::stream() << "unexpected role returned: " << cur.getFullName());
        }
        ASSERT_FALSE(it.more());

        it = graph.getDirectSubordinates(roleC);
        ASSERT_FALSE(it.more());

        it = graph.getDirectMembers(roleA);
        ASSERT_FALSE(it.more());

        it = graph.getDirectMembers(roleB);
        ASSERT_EQUALS(it.next().getFullName(), roleA.getFullName());
        ASSERT_FALSE(it.more());

        it = graph.getDirectMembers(roleC); // should be role A and role B, order doesn't matter
        cur = it.next();
        if (cur == roleA) {
            ASSERT_EQUALS(it.next().getFullName(), roleB.getFullName());
        } else if (cur == roleB) {
            ASSERT_EQUALS(it.next().getFullName(), roleA.getFullName());
        } else {
            FAIL(mongoutils::str::stream() << "unexpected role returned: " << cur.getFullName());
        }
        ASSERT_FALSE(it.more());

        // Now remove roleD from roleB and make sure graph is update correctly
        ASSERT_OK(graph.removeRoleFromRole(roleB, roleD));

        /*
         * Graph now looks like:
         *   A
         *  / \
         * v   v
         * B -> C
         */
        it = graph.getDirectSubordinates(roleB); // should be just roleC
        ASSERT_EQUALS(it.next().getFullName(), roleC.getFullName());
        ASSERT_FALSE(it.more());

        it = graph.getDirectSubordinates(roleD); // should be empty
        ASSERT_FALSE(it.more());


        // Now delete roleB entirely and make sure that the other roles are updated properly
        ASSERT_OK(graph.deleteRole(roleB));
        ASSERT_NOT_OK(graph.deleteRole(roleB));
        it = graph.getDirectSubordinates(roleA);
        ASSERT_EQUALS(it.next().getFullName(), roleC.getFullName());
        ASSERT_FALSE(it.more());
        it = graph.getDirectMembers(roleC);
        ASSERT_EQUALS(it.next().getFullName(), roleA.getFullName());
        ASSERT_FALSE(it.more());
    }

    // Tests that adding multiple privileges on the same resource correctly collapses those to one
    // privilege
    TEST(RoleGraphTest, AddPrivileges) {
        RoleName roleA("roleA", "dbA");

        RoleGraph graph;
        ASSERT_OK(graph.createRole(roleA));

        // Test adding a single privilege
        ActionSet actions;
        actions.addAction(ActionType::find);
        ASSERT_OK(graph.addPrivilegeToRole(roleA, Privilege("dbA", actions)));

        PrivilegeVector privileges = graph.getDirectPrivileges(roleA);
        ASSERT_EQUALS(static_cast<size_t>(1), privileges.size());
        ASSERT_EQUALS("dbA", privileges[0].getResource());
        ASSERT_EQUALS(actions.toString(), privileges[0].getActions().toString());

        // Add a privilege on a different resource
        ASSERT_OK(graph.addPrivilegeToRole(roleA, Privilege("dbA.foo", actions)));
        privileges = graph.getDirectPrivileges(roleA);
        ASSERT_EQUALS(static_cast<size_t>(2), privileges.size());
        ASSERT_EQUALS("dbA", privileges[0].getResource());
        ASSERT_EQUALS(actions.toString(), privileges[0].getActions().toString());
        ASSERT_EQUALS("dbA.foo", privileges[1].getResource());
        ASSERT_EQUALS(actions.toString(), privileges[1].getActions().toString());


        // Add different privileges on an existing resource and make sure they get de-duped
        actions.removeAllActions();
        actions.addAction(ActionType::insert);

        PrivilegeVector privilegesToAdd;
        privilegesToAdd.push_back(Privilege("dbA", actions));

        actions.removeAllActions();
        actions.addAction(ActionType::update);
        privilegesToAdd.push_back(Privilege("dbA", actions));

        ASSERT_OK(graph.addPrivilegesToRole(roleA, privilegesToAdd));

        privileges = graph.getDirectPrivileges(roleA);
        ASSERT_EQUALS(static_cast<size_t>(2), privileges.size());
        ASSERT_EQUALS("dbA", privileges[0].getResource());
        ASSERT_NOT_EQUALS(actions.toString(), privileges[0].getActions().toString());
        actions.addAction(ActionType::find);
        actions.addAction(ActionType::insert);
        ASSERT_EQUALS(actions.toString(), privileges[0].getActions().toString());
        actions.removeAction(ActionType::insert);
        actions.removeAction(ActionType::update);
        ASSERT_EQUALS("dbA.foo", privileges[1].getResource());
        ASSERT_EQUALS(actions.toString(), privileges[1].getActions().toString());
    }

    // Tests that recomputePrivilegeData correctly detects cycles in the graph.
    TEST(RoleGraphTest, DetectCycles) {
        RoleName roleA("roleA", "dbA");
        RoleName roleB("roleB", "dbB");
        RoleName roleC("roleC", "dbC");
        RoleName roleD("roleD", "dbD");

        RoleGraph graph;
        ASSERT_OK(graph.createRole(roleA));
        ASSERT_OK(graph.createRole(roleB));
        ASSERT_OK(graph.createRole(roleC));
        ASSERT_OK(graph.createRole(roleD));

        ASSERT_OK(graph.recomputePrivilegeData());
        ASSERT_OK(graph.addRoleToRole(roleA, roleB));
        ASSERT_OK(graph.recomputePrivilegeData());
        ASSERT_OK(graph.addRoleToRole(roleA, roleC));
        ASSERT_OK(graph.addRoleToRole(roleB, roleC));
        ASSERT_OK(graph.recomputePrivilegeData());
        /*
         * Graph now looks like:
         *    A
         *   / \
         *  v   v
         *  B -> C
         */
        ASSERT_OK(graph.addRoleToRole(roleC, roleD));
        ASSERT_OK(graph.addRoleToRole(roleD, roleB)); // Add a cycle
        /*
         * Graph now looks like:
         *    A
         *   / \
         *  v   v
         *  B -> C
         *  ^   /
         *   \ v
         *    D
         */
        ASSERT_NOT_OK(graph.recomputePrivilegeData());
        ASSERT_OK(graph.removeRoleFromRole(roleD, roleB));
        ASSERT_OK(graph.recomputePrivilegeData());
    }

    // Tests that recomputePrivilegeData correctly updates transitive privilege data for all roles.
    TEST(RoleGraphTest, RecomputePrivilegeData) {
        // We create 4 roles and give each of them a unique privilege.  After that the direct
        // privileges for all the roles are not touched.  The only thing that is changed is the
        // role membership graph, and we test how that affects the set of all transitive privileges
        // for each role.
        RoleName roleA("roleA", "dbA");
        RoleName roleB("roleB", "dbB");
        RoleName roleC("roleC", "dbC");
        RoleName roleD("roleD", "dbD");

        ActionSet actions;
        actions.addAllActions();

        RoleGraph graph;
        ASSERT_OK(graph.createRole(roleA));
        ASSERT_OK(graph.createRole(roleB));
        ASSERT_OK(graph.createRole(roleC));
        ASSERT_OK(graph.createRole(roleD));

        ASSERT_OK(graph.addPrivilegeToRole(roleA, Privilege("dbA", actions)));
        ASSERT_OK(graph.addPrivilegeToRole(roleB, Privilege("dbB", actions)));
        ASSERT_OK(graph.addPrivilegeToRole(roleC, Privilege("dbC", actions)));
        ASSERT_OK(graph.addPrivilegeToRole(roleD, Privilege("dbD", actions)));

        ASSERT_OK(graph.recomputePrivilegeData());

        PrivilegeVector privileges = graph.getAllPrivileges(roleA);
        ASSERT_EQUALS(static_cast<size_t>(1), privileges.size());
        ASSERT_EQUALS("dbA", privileges[0].getResource());

        // At this point we have all 4 roles set up, each with their own privilege, but no
        // roles have been granted to each other.

        ASSERT_OK(graph.addRoleToRole(roleA, roleB));
        // Role graph: A->B
        ASSERT_OK(graph.recomputePrivilegeData());
        privileges = graph.getAllPrivileges(roleA);
        ASSERT_EQUALS(static_cast<size_t>(2), privileges.size());
        ASSERT_EQUALS("dbA", privileges[0].getResource());
        ASSERT_EQUALS("dbB", privileges[1].getResource());

        // Add's roleC's privileges to roleB and make sure roleA gets them as well.
        ASSERT_OK(graph.addRoleToRole(roleB, roleC));
        // Role graph: A->B->C
        ASSERT_OK(graph.recomputePrivilegeData());
        privileges = graph.getAllPrivileges(roleA);
        ASSERT_EQUALS(static_cast<size_t>(3), privileges.size());
        ASSERT_EQUALS("dbA", privileges[0].getResource());
        ASSERT_EQUALS("dbB", privileges[1].getResource());
        ASSERT_EQUALS("dbC", privileges[2].getResource());
        privileges = graph.getAllPrivileges(roleB);
        ASSERT_EQUALS(static_cast<size_t>(2), privileges.size());
        ASSERT_EQUALS("dbB", privileges[0].getResource());
        ASSERT_EQUALS("dbC", privileges[1].getResource());

        // Add's roleD's privileges to roleC and make sure that roleA and roleB get them as well.
        ASSERT_OK(graph.addRoleToRole(roleC, roleD));
        // Role graph: A->B->C->D
        ASSERT_OK(graph.recomputePrivilegeData());
        privileges = graph.getAllPrivileges(roleA);
        ASSERT_EQUALS(static_cast<size_t>(4), privileges.size());
        ASSERT_EQUALS("dbA", privileges[0].getResource());
        ASSERT_EQUALS("dbB", privileges[1].getResource());
        ASSERT_EQUALS("dbC", privileges[2].getResource());
        ASSERT_EQUALS("dbD", privileges[3].getResource());
        privileges = graph.getAllPrivileges(roleB);
        ASSERT_EQUALS(static_cast<size_t>(3), privileges.size());
        ASSERT_EQUALS("dbB", privileges[0].getResource());
        ASSERT_EQUALS("dbC", privileges[1].getResource());
        ASSERT_EQUALS("dbD", privileges[2].getResource());
        privileges = graph.getAllPrivileges(roleC);
        ASSERT_EQUALS(static_cast<size_t>(2), privileges.size());
        ASSERT_EQUALS("dbC", privileges[0].getResource());
        ASSERT_EQUALS("dbD", privileges[1].getResource());
        privileges = graph.getAllPrivileges(roleD);
        ASSERT_EQUALS(static_cast<size_t>(1), privileges.size());
        ASSERT_EQUALS("dbD", privileges[0].getResource());

        // Remove roleC from roleB, make sure that roleA then loses both roleC's and roleD's
        // privileges
        ASSERT_OK(graph.removeRoleFromRole(roleB, roleC));
        // Role graph: A->B     C->D
        ASSERT_OK(graph.recomputePrivilegeData());
        privileges = graph.getAllPrivileges(roleA);
        ASSERT_EQUALS(static_cast<size_t>(2), privileges.size());
        ASSERT_EQUALS("dbA", privileges[0].getResource());
        ASSERT_EQUALS("dbB", privileges[1].getResource());
        privileges = graph.getAllPrivileges(roleB);
        ASSERT_EQUALS(static_cast<size_t>(1), privileges.size());
        ASSERT_EQUALS("dbB", privileges[0].getResource());
        privileges = graph.getAllPrivileges(roleC);
        ASSERT_EQUALS(static_cast<size_t>(2), privileges.size());
        ASSERT_EQUALS("dbC", privileges[0].getResource());
        ASSERT_EQUALS("dbD", privileges[1].getResource());
        privileges = graph.getAllPrivileges(roleD);
        ASSERT_EQUALS(static_cast<size_t>(1), privileges.size());
        ASSERT_EQUALS("dbD", privileges[0].getResource());

        // Make sure direct privileges were untouched
        privileges = graph.getDirectPrivileges(roleA);
        ASSERT_EQUALS(static_cast<size_t>(1), privileges.size());
        ASSERT_EQUALS("dbA", privileges[0].getResource());
        privileges = graph.getDirectPrivileges(roleB);
        ASSERT_EQUALS(static_cast<size_t>(1), privileges.size());
        ASSERT_EQUALS("dbB", privileges[0].getResource());
        privileges = graph.getDirectPrivileges(roleC);
        ASSERT_EQUALS(static_cast<size_t>(1), privileges.size());
        ASSERT_EQUALS("dbC", privileges[0].getResource());
        privileges = graph.getDirectPrivileges(roleD);
        ASSERT_EQUALS(static_cast<size_t>(1), privileges.size());
        ASSERT_EQUALS("dbD", privileges[0].getResource());
    }

    // Test that if you grant 1 role to another, then remove it and change it's privileges, then
    // re-grant it, the receiving role sees the new privileges and not the old ones.
    TEST(RoleGraphTest, ReAddRole) {
        RoleName roleA("roleA", "dbA");
        RoleName roleB("roleB", "dbB");
        RoleName roleC("roleC", "dbC");

        ActionSet actionsA, actionsB, actionsC;
        actionsA.addAction(ActionType::find);
        actionsB.addAction(ActionType::insert);
        actionsC.addAction(ActionType::update);

        RoleGraph graph;
        ASSERT_OK(graph.createRole(roleA));
        ASSERT_OK(graph.createRole(roleB));
        ASSERT_OK(graph.createRole(roleC));

        ASSERT_OK(graph.addPrivilegeToRole(roleA, Privilege("db", actionsA)));
        ASSERT_OK(graph.addPrivilegeToRole(roleB, Privilege("db", actionsB)));
        ASSERT_OK(graph.addPrivilegeToRole(roleC, Privilege("db", actionsC)));

        ASSERT_OK(graph.addRoleToRole(roleA, roleB));
        ASSERT_OK(graph.addRoleToRole(roleB, roleC)); // graph: A <- B <- C

        ASSERT_OK(graph.recomputePrivilegeData());

        // roleA should have privileges from roleB and roleC
        PrivilegeVector privileges = graph.getAllPrivileges(roleA);
        ASSERT_EQUALS(static_cast<size_t>(1), privileges.size());
        ASSERT_TRUE(privileges[0].getActions().contains(ActionType::find));
        ASSERT_TRUE(privileges[0].getActions().contains(ActionType::insert));
        ASSERT_TRUE(privileges[0].getActions().contains(ActionType::update));

        // Now remove roleB from roleA.  B still is a member of C, but A no longer should have
        // privileges from B or C.
        ASSERT_OK(graph.removeRoleFromRole(roleA, roleB));
        ASSERT_OK(graph.recomputePrivilegeData());

        // roleA should no longer have the privileges from roleB or roleC
        privileges = graph.getAllPrivileges(roleA);
        ASSERT_EQUALS(static_cast<size_t>(1), privileges.size());
        ASSERT_TRUE(privileges[0].getActions().contains(ActionType::find));
        ASSERT_FALSE(privileges[0].getActions().contains(ActionType::insert));
        ASSERT_FALSE(privileges[0].getActions().contains(ActionType::update));

        // Change the privileges that roleB grants
        ASSERT_OK(graph.removeAllPrivilegesFromRole(roleB));
        ActionSet newActionsB;
        newActionsB.addAction(ActionType::remove);
        ASSERT_OK(graph.addPrivilegeToRole(roleB, Privilege("db", newActionsB)));

        // Grant roleB back to roleA, make sure roleA has roleB's new privilege but not its old one.
        ASSERT_OK(graph.addRoleToRole(roleA, roleB));
        ASSERT_OK(graph.recomputePrivilegeData());

        privileges = graph.getAllPrivileges(roleA);
        ASSERT_EQUALS(static_cast<size_t>(1), privileges.size());
        ASSERT_TRUE(privileges[0].getActions().contains(ActionType::find));
        ASSERT_TRUE(privileges[0].getActions().contains(ActionType::update)); // should get roleC's actions again
        ASSERT_TRUE(privileges[0].getActions().contains(ActionType::remove)); // roleB should grant this to roleA
        ASSERT_FALSE(privileges[0].getActions().contains(ActionType::insert)); // no roles have this action anymore

        // Now delete roleB completely.  A should once again lose the privileges from both B and C.
        ASSERT_OK(graph.deleteRole(roleB));
        ASSERT_OK(graph.recomputePrivilegeData());

        // roleA should no longer have the privileges from roleB or roleC
        privileges = graph.getAllPrivileges(roleA);
        ASSERT_EQUALS(static_cast<size_t>(1), privileges.size());
        ASSERT_TRUE(privileges[0].getActions().contains(ActionType::find));
        ASSERT_FALSE(privileges[0].getActions().contains(ActionType::update));
        ASSERT_FALSE(privileges[0].getActions().contains(ActionType::remove));
        ASSERT_FALSE(privileges[0].getActions().contains(ActionType::insert));

        // Now re-create roleB and give it a new privilege, then grant it back to roleA.
        // RoleA should get its new privilege but not roleC's privilege this time nor either of
        // roleB's old privileges.
        ASSERT_OK(graph.createRole(roleB));
        actionsB.removeAllActions();
        actionsB.addAction(ActionType::shutdown);
        ASSERT_OK(graph.addPrivilegeToRole(roleB, Privilege("db", actionsB)));
        ASSERT_OK(graph.addRoleToRole(roleA, roleB));
        ASSERT_OK(graph.recomputePrivilegeData());

        privileges = graph.getAllPrivileges(roleA);
        ASSERT_EQUALS(static_cast<size_t>(1), privileges.size());
        ASSERT_TRUE(privileges[0].getActions().contains(ActionType::find));
        ASSERT_TRUE(privileges[0].getActions().contains(ActionType::shutdown));
        ASSERT_FALSE(privileges[0].getActions().contains(ActionType::update));
        ASSERT_FALSE(privileges[0].getActions().contains(ActionType::remove));
        ASSERT_FALSE(privileges[0].getActions().contains(ActionType::insert));
    }

    // Tests copy constructor and swap functionality.
    TEST(RoleGraphTest, CopySwap) {
        RoleName roleA("roleA", "dbA");
        RoleName roleB("roleB", "dbB");
        RoleName roleC("roleC", "dbC");

        RoleGraph graph;
        ASSERT_OK(graph.createRole(roleA));
        ASSERT_OK(graph.createRole(roleB));
        ASSERT_OK(graph.createRole(roleC));

        ActionSet actions;
        actions.addAction(ActionType::find);
        ASSERT_OK(graph.addPrivilegeToRole(roleA, Privilege("dbA", actions)));
        ASSERT_OK(graph.addPrivilegeToRole(roleB, Privilege("dbB", actions)));
        ASSERT_OK(graph.addPrivilegeToRole(roleC, Privilege("dbC", actions)));

        ASSERT_OK(graph.addRoleToRole(roleA, roleB));

        // Make a copy of the graph to do further modifications on.
        RoleGraph tempGraph(graph);
        ASSERT_OK(tempGraph.addRoleToRole(roleB, roleC));
        tempGraph.recomputePrivilegeData();

        // Now swap the copy back with the original graph and make sure the original was updated
        // properly.
        swap(tempGraph, graph);

        RoleNameIterator it = graph.getDirectSubordinates(roleB);
        ASSERT_TRUE(it.more());
        ASSERT_EQUALS(it.next().getFullName(), roleC.getFullName());
        ASSERT_FALSE(it.more());

        graph.getAllPrivileges(roleA); // should have privileges from roleB *and* role C
        PrivilegeVector privileges = graph.getAllPrivileges(roleA);
        ASSERT_EQUALS(static_cast<size_t>(3), privileges.size());
        ASSERT_EQUALS("dbA", privileges[0].getResource());
        ASSERT_EQUALS("dbB", privileges[1].getResource());
        ASSERT_EQUALS("dbC", privileges[2].getResource());
    }

    // Tests error handling
    TEST(RoleGraphTest, ErrorHandling) {
        RoleName roleA("roleA", "dbA");
        RoleName roleB("roleB", "dbB");
        RoleName roleC("roleC", "dbC");

        ActionSet actions;
        actions.addAction(ActionType::find);
        Privilege privilege1("db1", actions);
        Privilege privilege2("db2", actions);
        PrivilegeVector privileges;
        privileges.push_back(privilege1);
        privileges.push_back(privilege2);

        RoleGraph graph;
        // None of the roles exist yet.
        ASSERT_NOT_OK(graph.addPrivilegeToRole(roleA, privilege1));
        ASSERT_NOT_OK(graph.addPrivilegesToRole(roleA, privileges));
        ASSERT_NOT_OK(graph.removePrivilegeFromRole(roleA, privilege1));
        ASSERT_NOT_OK(graph.removePrivilegesFromRole(roleA, privileges));
        ASSERT_NOT_OK(graph.removeAllPrivilegesFromRole(roleA));
        ASSERT_NOT_OK(graph.addRoleToRole(roleA, roleB));
        ASSERT_NOT_OK(graph.removeRoleFromRole(roleA, roleB));

        // One of the roles exists
        ASSERT_OK(graph.createRole(roleA));
        ASSERT_NOT_OK(graph.createRole(roleA)); // Can't create same role twice
        ASSERT_NOT_OK(graph.addRoleToRole(roleA, roleB));
        ASSERT_NOT_OK(graph.addRoleToRole(roleB, roleA));
        ASSERT_NOT_OK(graph.removeRoleFromRole(roleA, roleB));
        ASSERT_NOT_OK(graph.removeRoleFromRole(roleB, roleA));

        // Should work now that both exist.
        ASSERT_OK(graph.createRole(roleB));
        ASSERT_OK(graph.addRoleToRole(roleA, roleB));
        ASSERT_OK(graph.removeRoleFromRole(roleA, roleB));
        ASSERT_NOT_OK(graph.removeRoleFromRole(roleA, roleB)); // roleA isn't actually a member of roleB

        // Can't remove a privilege from a role that doesn't have it.
        ASSERT_NOT_OK(graph.removePrivilegeFromRole(roleA, privilege1));
        ASSERT_OK(graph.addPrivilegeToRole(roleA, privilege1));
        ASSERT_OK(graph.removePrivilegeFromRole(roleA, privilege1)); // now should work

        // Test that removing a vector of privileges fails if *any* of the privileges are missing.
        ASSERT_OK(graph.addPrivilegeToRole(roleA, privilege1));
        ASSERT_OK(graph.addPrivilegeToRole(roleA, privilege2));
        // Removing both privileges should work since it has both
        ASSERT_OK(graph.removePrivilegesFromRole(roleA, privileges));
        // Now add only 1 back and this time removing both should fail
        ASSERT_OK(graph.addPrivilegeToRole(roleA, privilege1));
        ASSERT_NOT_OK(graph.removePrivilegesFromRole(roleA, privileges));
    }

}  // namespace
}  // namespace mongo