summaryrefslogtreecommitdiff
path: root/jni/node_test.cpp
blob: 6afdd75b37640ca0a8e2ae78af6c5d56f9469818 (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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#include <gtest/gtest.h>

#include "node-inl.h"

#include <algorithm>
#include <limits>
#include <memory>
#include <mutex>

using mediaprovider::fuse::handle;
using mediaprovider::fuse::node;
using mediaprovider::fuse::NodeTracker;

// Listed as a friend class to struct node so it can observe implementation
// details if required. The only implementation detail that is worth writing
// tests around at the moment is the reference count.
class NodeTest : public ::testing::Test {
  public:
    NodeTest() : tracker_(NodeTracker(&lock_)) {}

    uint32_t GetRefCount(node* node) { return node->refcount_; }

    std::recursive_mutex lock_;
    NodeTracker tracker_;

    // Forward destruction here, as NodeTest is a friend class.
    static void destroy(node* node) { delete node; }

    static void acquire(node* node) { node->Acquire(); }

    typedef std::unique_ptr<node, decltype(&NodeTest::destroy)> unique_node_ptr;

    unique_node_ptr CreateNode(node* parent, const std::string& path, const int transforms = 0) {
        return unique_node_ptr(
                node::Create(parent, path, "", true, transforms, 0, &lock_, 0, &tracker_),
                &NodeTest::destroy);
    }

    static class node* ForChild(class node* node, const std::string& name,
                                const std::function<bool(class node*)>& callback) {
        return node->ForChild(name, callback);
    }

    // Expose NodeCompare for testing.
    node::NodeCompare cmp;
};

TEST_F(NodeTest, TestCreate) {
    unique_node_ptr node = CreateNode(nullptr, "/path");

    ASSERT_EQ("/path", node->GetName());
    ASSERT_EQ(1, GetRefCount(node.get()));
    ASSERT_FALSE(node->HasCachedHandle());
}

TEST_F(NodeTest, TestCreate_withParent) {
    unique_node_ptr parent = CreateNode(nullptr, "/path");
    ASSERT_EQ(1, GetRefCount(parent.get()));

    // Adding a child to a parent node increments its refcount.
    unique_node_ptr child = CreateNode(parent.get(), "subdir");
    ASSERT_EQ(2, GetRefCount(parent.get()));

    // Make sure the node has been added to the parents list of children.
    ASSERT_EQ(child.get(), parent->LookupChildByName("subdir", false /* acquire */));
    ASSERT_EQ(1, GetRefCount(child.get()));
}

TEST_F(NodeTest, TestRelease) {
    node* node = node::Create(nullptr, "/path", "", true, 0, 0, &lock_, 0, &tracker_);
    acquire(node);
    acquire(node);
    ASSERT_EQ(3, GetRefCount(node));

    ASSERT_FALSE(node->Release(1));
    ASSERT_EQ(2, GetRefCount(node));

    // A Release that makes refcount go negative should be a no-op.
    ASSERT_FALSE(node->Release(10000));
    ASSERT_EQ(2, GetRefCount(node));

    // Finally, let the refcount go to zero.
    ASSERT_TRUE(node->Release(2));
}

TEST_F(NodeTest, TestRenameName) {
    unique_node_ptr parent = CreateNode(nullptr, "/path");

    unique_node_ptr child = CreateNode(parent.get(), "subdir");
    ASSERT_EQ(2, GetRefCount(parent.get()));
    ASSERT_EQ(child.get(), parent->LookupChildByName("subdir", false /* acquire */));

    child->Rename("subdir_new", parent.get());

    ASSERT_EQ(2, GetRefCount(parent.get()));
    ASSERT_EQ(nullptr, parent->LookupChildByName("subdir", false /* acquire */));
    ASSERT_EQ(child.get(), parent->LookupChildByName("subdir_new", false /* acquire */));

    ASSERT_EQ("/path/subdir_new", child->BuildPath());
    ASSERT_EQ(1, GetRefCount(child.get()));
}

TEST_F(NodeTest, TestRenameParent) {
    unique_node_ptr parent1 = CreateNode(nullptr, "/path1");
    unique_node_ptr parent2 = CreateNode(nullptr, "/path2");

    unique_node_ptr child = CreateNode(parent1.get(), "subdir");
    ASSERT_EQ(2, GetRefCount(parent1.get()));
    ASSERT_EQ(child.get(), parent1->LookupChildByName("subdir", false /* acquire */));

    child->Rename("subdir", parent2.get());
    ASSERT_EQ(1, GetRefCount(parent1.get()));
    ASSERT_EQ(nullptr, parent1->LookupChildByName("subdir", false /* acquire */));

    ASSERT_EQ(2, GetRefCount(parent2.get()));
    ASSERT_EQ(child.get(), parent2->LookupChildByName("subdir", false /* acquire */));

    ASSERT_EQ("/path2/subdir", child->BuildPath());
    ASSERT_EQ(1, GetRefCount(child.get()));
}

TEST_F(NodeTest, TestRenameNameAndParent) {
    unique_node_ptr parent1 = CreateNode(nullptr, "/path1");
    unique_node_ptr parent2 = CreateNode(nullptr, "/path2");

    unique_node_ptr child = CreateNode(parent1.get(), "subdir");
    ASSERT_EQ(2, GetRefCount(parent1.get()));
    ASSERT_EQ(child.get(), parent1->LookupChildByName("subdir", false /* acquire */));

    child->Rename("subdir_new", parent2.get());
    ASSERT_EQ(1, GetRefCount(parent1.get()));
    ASSERT_EQ(nullptr, parent1->LookupChildByName("subdir", false /* acquire */));
    ASSERT_EQ(nullptr, parent1->LookupChildByName("subdir_new", false /* acquire */));

    ASSERT_EQ(2, GetRefCount(parent2.get()));
    ASSERT_EQ(child.get(), parent2->LookupChildByName("subdir_new", false /* acquire */));

    ASSERT_EQ("/path2/subdir_new", child->BuildPath());
    ASSERT_EQ(1, GetRefCount(child.get()));
}

TEST_F(NodeTest, TestRenameNameForChild) {
    unique_node_ptr parent = CreateNode(nullptr, "/path");

    unique_node_ptr child0 = CreateNode(parent.get(), "subdir", 0 /* transforms */);
    unique_node_ptr child1 = CreateNode(parent.get(), "subdir", 1 /* transforms */);
    ASSERT_EQ(3, GetRefCount(parent.get()));
    ASSERT_EQ(child0.get(),
              parent->LookupChildByName("subdir", false /* acquire */, 0 /* transforms */));
    ASSERT_EQ(child1.get(),
              parent->LookupChildByName("subdir", false /* acquire */, 1 /* transforms */));

    parent->RenameChild("subdir", "subdir_new", parent.get());

    ASSERT_EQ(3, GetRefCount(parent.get()));
    ASSERT_EQ(nullptr,
              parent->LookupChildByName("subdir", false /* acquire */, 0 /* transforms */));
    ASSERT_EQ(nullptr,
              parent->LookupChildByName("subdir", false /* acquire */, 1 /* transforms */));
    ASSERT_EQ(child0.get(),
              parent->LookupChildByName("subdir_new", false /* acquire */, 0 /* transforms */));
    ASSERT_EQ(child1.get(),
              parent->LookupChildByName("subdir_new", false /* acquire */, 1 /* transforms */));

    ASSERT_EQ("/path/subdir_new", child0->BuildPath());
    ASSERT_EQ("/path/subdir_new", child1->BuildPath());
    ASSERT_EQ(1, GetRefCount(child0.get()));
    ASSERT_EQ(1, GetRefCount(child1.get()));
}

TEST_F(NodeTest, TestRenameParentForChild) {
    unique_node_ptr parent1 = CreateNode(nullptr, "/path1");
    unique_node_ptr parent2 = CreateNode(nullptr, "/path2");

    unique_node_ptr child0 = CreateNode(parent1.get(), "subdir", 0 /* transforms */);
    unique_node_ptr child1 = CreateNode(parent1.get(), "subdir", 1 /* transforms */);
    ASSERT_EQ(3, GetRefCount(parent1.get()));
    ASSERT_EQ(child0.get(),
              parent1->LookupChildByName("subdir", false /* acquire */, 0 /* transforms */));
    ASSERT_EQ(child1.get(),
              parent1->LookupChildByName("subdir", false /* acquire */, 1 /* transforms */));

    parent1->RenameChild("subdir", "subdir", parent2.get());
    ASSERT_EQ(1, GetRefCount(parent1.get()));
    ASSERT_EQ(nullptr,
              parent1->LookupChildByName("subdir", false /* acquire */, 0 /* transforms */));
    ASSERT_EQ(nullptr,
              parent1->LookupChildByName("subdir", false /* acquire */, 1 /* transforms */));

    ASSERT_EQ(3, GetRefCount(parent2.get()));
    ASSERT_EQ(child0.get(),
              parent2->LookupChildByName("subdir", false /* acquire */, 0 /* transforms */));
    ASSERT_EQ(child1.get(),
              parent2->LookupChildByName("subdir", false /* acquire */, 1 /* transforms */));

    ASSERT_EQ("/path2/subdir", child0->BuildPath());
    ASSERT_EQ("/path2/subdir", child1->BuildPath());
    ASSERT_EQ(1, GetRefCount(child0.get()));
    ASSERT_EQ(1, GetRefCount(child1.get()));
}

TEST_F(NodeTest, TestRenameNameAndParentForChild) {
    unique_node_ptr parent1 = CreateNode(nullptr, "/path1");
    unique_node_ptr parent2 = CreateNode(nullptr, "/path2");

    unique_node_ptr child0 = CreateNode(parent1.get(), "subdir", 0 /* transforms */);
    unique_node_ptr child1 = CreateNode(parent1.get(), "subdir", 1 /* transforms */);
    ASSERT_EQ(3, GetRefCount(parent1.get()));
    ASSERT_EQ(child0.get(),
              parent1->LookupChildByName("subdir", false /* acquire */, 0 /* transforms */));
    ASSERT_EQ(child1.get(),
              parent1->LookupChildByName("subdir", false /* acquire */, 1 /* transforms */));

    parent1->RenameChild("subdir", "subdir_new", parent2.get());
    ASSERT_EQ(1, GetRefCount(parent1.get()));
    ASSERT_EQ(nullptr,
              parent1->LookupChildByName("subdir", false /* acquire */, 0 /* transforms */));
    ASSERT_EQ(nullptr,
              parent1->LookupChildByName("subdir_new", false /* acquire */, 0 /* transforms */));
    ASSERT_EQ(nullptr,
              parent1->LookupChildByName("subdir", false /* acquire */, 1 /* transforms */));
    ASSERT_EQ(nullptr,
              parent1->LookupChildByName("subdir_new", false /* acquire */, 1 /* transforms */));

    ASSERT_EQ(3, GetRefCount(parent2.get()));
    ASSERT_EQ(nullptr,
              parent1->LookupChildByName("subdir_new", false /* acquire */, 0 /* transforms */));
    ASSERT_EQ(nullptr,
              parent1->LookupChildByName("subdir_new", false /* acquire */, 1 /* transforms */));

    ASSERT_EQ("/path2/subdir_new", child0->BuildPath());
    ASSERT_EQ("/path2/subdir_new", child1->BuildPath());
    ASSERT_EQ(1, GetRefCount(child0.get()));
    ASSERT_EQ(1, GetRefCount(child1.get()));
}

TEST_F(NodeTest, TestBuildPath) {
    unique_node_ptr parent = CreateNode(nullptr, "/path");
    ASSERT_EQ("/path", parent->BuildPath());

    unique_node_ptr child = CreateNode(parent.get(), "subdir");
    ASSERT_EQ("/path/subdir", child->BuildPath());

    unique_node_ptr child2 = CreateNode(parent.get(), "subdir2");
    ASSERT_EQ("/path/subdir2", child2->BuildPath());

    unique_node_ptr subchild = CreateNode(child2.get(), "subsubdir");
    ASSERT_EQ("/path/subdir2/subsubdir", subchild->BuildPath());
}

TEST_F(NodeTest, TestSetDeleted) {
    unique_node_ptr parent = CreateNode(nullptr, "/path");
    unique_node_ptr child = CreateNode(parent.get(), "subdir");

    ASSERT_EQ(child.get(), parent->LookupChildByName("subdir", false /* acquire */));
    child->SetDeleted();
    ASSERT_EQ(nullptr, parent->LookupChildByName("subdir", false /* acquire */));
}

TEST_F(NodeTest, TestSetDeletedForChild) {
    unique_node_ptr parent = CreateNode(nullptr, "/path");
    unique_node_ptr child0 = CreateNode(parent.get(), "subdir", 0 /* transforms */);
    unique_node_ptr child1 = CreateNode(parent.get(), "subdir", 1 /* transforms */);

    ASSERT_EQ(child0.get(),
              parent->LookupChildByName("subdir", false /* acquire */, 0 /* transforms */));
    ASSERT_EQ(child1.get(),
              parent->LookupChildByName("subdir", false /* acquire */, 1 /* transforms */));
    parent->SetDeletedForChild("subdir");
    ASSERT_EQ(nullptr,
              parent->LookupChildByName("subdir", false /* acquire */, 0 /* transforms */));
    ASSERT_EQ(nullptr,
              parent->LookupChildByName("subdir", false /* acquire */, 1 /* transforms */));
}

TEST_F(NodeTest, DeleteTree) {
    unique_node_ptr parent = CreateNode(nullptr, "/path");

    // This is the tree that we intend to delete.
    node* child = node::Create(parent.get(), "subdir", "", true, 0, 0, &lock_, 0, &tracker_);
    node::Create(child, "s1", "", true, 0, 0, &lock_, 0, &tracker_);
    node* subchild2 = node::Create(child, "s2", "", true, 0, 0, &lock_, 0, &tracker_);
    node::Create(subchild2, "sc2", "", true, 0, 0, &lock_, 0, &tracker_);

    ASSERT_EQ(child, parent->LookupChildByName("subdir", false /* acquire */));
    node::DeleteTree(child);
    ASSERT_EQ(nullptr, parent->LookupChildByName("subdir", false /* acquire */));
}

TEST_F(NodeTest, LookupChildByName_empty) {
    unique_node_ptr parent = CreateNode(nullptr, "/path");
    unique_node_ptr child = CreateNode(parent.get(), "subdir");

    ASSERT_EQ(child.get(), parent->LookupChildByName("subdir", false /* acquire */));
    ASSERT_EQ(nullptr, parent->LookupChildByName("", false /* acquire */));
}

TEST_F(NodeTest, LookupChildByName_transforms) {
    unique_node_ptr parent = CreateNode(nullptr, "/path");
    unique_node_ptr child0 = CreateNode(parent.get(), "subdir", 0 /* transforms */);
    unique_node_ptr child1 = CreateNode(parent.get(), "subdir", 1 /* transforms */);

    ASSERT_EQ(child0.get(), parent->LookupChildByName("subdir", false /* acquire */));
    ASSERT_EQ(child0.get(),
              parent->LookupChildByName("subdir", false /* acquire */, 0 /* transforms */));
    ASSERT_EQ(child1.get(),
              parent->LookupChildByName("subdir", false /* acquire */, 1 /* transforms */));
    ASSERT_EQ(nullptr,
              parent->LookupChildByName("subdir", false /* acquire */, 2 /* transforms */));
}

TEST_F(NodeTest, LookupChildByName_refcounts) {
    unique_node_ptr parent = CreateNode(nullptr, "/path");
    unique_node_ptr child = CreateNode(parent.get(), "subdir");

    ASSERT_EQ(child.get(), parent->LookupChildByName("subdir", false /* acquire */));
    ASSERT_EQ(1, GetRefCount(child.get()));

    ASSERT_EQ(child.get(), parent->LookupChildByName("subdir", true /* acquire */));
    ASSERT_EQ(2, GetRefCount(child.get()));
}

TEST_F(NodeTest, LookupAbsolutePath) {
    unique_node_ptr parent = CreateNode(nullptr, "/path");
    unique_node_ptr child = CreateNode(parent.get(), "subdir");
    unique_node_ptr child2 = CreateNode(parent.get(), "subdir2");
    unique_node_ptr subchild = CreateNode(child2.get(), "subsubdir");

    ASSERT_EQ(parent.get(), node::LookupAbsolutePath(parent.get(), "/path"));
    ASSERT_EQ(parent.get(), node::LookupAbsolutePath(parent.get(), "/path/"));
    ASSERT_EQ(nullptr, node::LookupAbsolutePath(parent.get(), "/path2"));

    ASSERT_EQ(child.get(), node::LookupAbsolutePath(parent.get(), "/path/subdir"));
    ASSERT_EQ(child.get(), node::LookupAbsolutePath(parent.get(), "/path/subdir/"));
    // TODO(narayan): Are the two cases below intentional behaviour ?
    ASSERT_EQ(child.get(), node::LookupAbsolutePath(parent.get(), "/path//subdir"));
    ASSERT_EQ(child.get(), node::LookupAbsolutePath(parent.get(), "/path///subdir"));

    ASSERT_EQ(child2.get(), node::LookupAbsolutePath(parent.get(), "/path/subdir2"));
    ASSERT_EQ(child2.get(), node::LookupAbsolutePath(parent.get(), "/path/subdir2/"));

    ASSERT_EQ(nullptr, node::LookupAbsolutePath(parent.get(), "/path/subdir3/"));

    ASSERT_EQ(subchild.get(), node::LookupAbsolutePath(parent.get(), "/path/subdir2/subsubdir"));
    ASSERT_EQ(nullptr, node::LookupAbsolutePath(parent.get(), "/path/subdir/subsubdir"));
}

TEST_F(NodeTest, AddDestroyHandle) {
    unique_node_ptr node = CreateNode(nullptr, "/path");

    handle* h = new handle(-1, new mediaprovider::fuse::RedactionInfo, true /* cached */,
                           false /* passthrough */, 0 /* uid */, 0 /* transforms_uid */);
    node->AddHandle(h);
    ASSERT_TRUE(node->HasCachedHandle());

    node->DestroyHandle(h);
    ASSERT_FALSE(node->HasCachedHandle());

    // Should all crash the process as the handle is no longer associated with
    // the node in question.
    EXPECT_DEATH(node->DestroyHandle(h), "");
    EXPECT_DEATH(node->DestroyHandle(nullptr), "");
    std::unique_ptr<handle> h2(new handle(-1, new mediaprovider::fuse::RedactionInfo,
                                          true /* cached */, false /* passthrough */, 0 /* uid */,
                                          0 /* transforms_uid */));
    EXPECT_DEATH(node->DestroyHandle(h2.get()), "");
}

TEST_F(NodeTest, CheckHandleForUid_foundSingle_shouldRedact) {
    unique_node_ptr node = CreateNode(nullptr, "/path");

    off64_t ranges[2] = {0, 1};
    mediaprovider::fuse::RedactionInfo* infoWithLocation =
            new mediaprovider::fuse::RedactionInfo(1, ranges);

    handle* h = new handle(-1, infoWithLocation, true /* cached */, false /* passthrough */,
                           1 /* uid */, 0 /* transforms_uid */);

    node->AddHandle(h);
    std::unique_ptr<mediaprovider::fuse::FdAccessResult> res(node->CheckHandleForUid(1));
    ASSERT_TRUE(res->should_redact);
    ASSERT_EQ(res->file_path, "/path");
}

TEST_F(NodeTest, CheckHandleForUid_foundSingle_shouldNotRedact) {
    unique_node_ptr node = CreateNode(nullptr, "/path");

    mediaprovider::fuse::RedactionInfo* infoWithoutLocation = new mediaprovider::fuse::RedactionInfo;

    handle* h = new handle(-1, infoWithoutLocation, true /* cached */, false /* passthrough */,
                           1 /* uid */, 0 /* transforms_uid */);

    node->AddHandle(h);
    std::unique_ptr<mediaprovider::fuse::FdAccessResult> res(node->CheckHandleForUid(1));
    ASSERT_FALSE(res->should_redact);
    ASSERT_EQ(res->file_path, "/path");
}

TEST_F(NodeTest, CheckHandleForUid_foundMultiple_shouldNotRedact) {
    unique_node_ptr node = CreateNode(nullptr, "/path");

    off64_t ranges[2] = {0, 1};
    mediaprovider::fuse::RedactionInfo* infoWithLocation =
            new mediaprovider::fuse::RedactionInfo(1, ranges);
    mediaprovider::fuse::RedactionInfo* infoWithoutLocation = new mediaprovider::fuse::RedactionInfo;

    handle* h1 = new handle(-1, infoWithLocation, true /* cached */, false /* passthrough */,
                            1 /* uid */, 0 /* transforms_uid */);
    handle* h2 = new handle(-1, infoWithoutLocation, true /* cached */, false /* passthrough */,
                            1 /* uid */, 0 /* transforms_uid */);

    node->AddHandle(h1);
    node->AddHandle(h2);
    std::unique_ptr<mediaprovider::fuse::FdAccessResult> res(node->CheckHandleForUid(1));
    ASSERT_FALSE(res->should_redact);
    ASSERT_EQ(res->file_path, "/path");
}

TEST_F(NodeTest, CheckHandleForUid_notFound_differentUid) {
    unique_node_ptr node = CreateNode(nullptr, "/path");

    off64_t ranges[2] = {0, 1};
    mediaprovider::fuse::RedactionInfo* infoWithLocation =
            new mediaprovider::fuse::RedactionInfo(1, ranges);

    handle* h = new handle(-1, infoWithLocation, true /* cached */, false /* passthrough */,
                           2 /* uid */, 0 /* transforms_uid */);

    node->AddHandle(h);
    std::unique_ptr<mediaprovider::fuse::FdAccessResult> res(node->CheckHandleForUid(1));
    ASSERT_FALSE(res->should_redact);
    ASSERT_EQ(res->file_path, "");
}

TEST_F(NodeTest, CheckHandleForUid_notFound_noHandle) {
    unique_node_ptr node = CreateNode(nullptr, "/path");

    std::unique_ptr<mediaprovider::fuse::FdAccessResult> res(node->CheckHandleForUid(1));
    ASSERT_FALSE(res->should_redact);
    ASSERT_EQ(res->file_path, "");
}

TEST_F(NodeTest, CaseInsensitive) {
    unique_node_ptr parent = CreateNode(nullptr, "/path");
    unique_node_ptr mixed_child = CreateNode(parent.get(), "cHiLd");

    node* upper_child = parent->LookupChildByName("CHILD", false /* acquire */);
    node* lower_child = parent->LookupChildByName("child", false /* acquire */);

    ASSERT_EQ(mixed_child.get(), lower_child);
    ASSERT_EQ(mixed_child.get(), upper_child);
}

TEST_F(NodeTest, RenameSameNameSameParent) {
    unique_node_ptr parent = CreateNode(nullptr, "/path1");
    unique_node_ptr child = CreateNode(parent.get(), "subdir");

    ASSERT_EQ(child.get(), parent->LookupChildByName("SuBdIr", false /* acquire */));
    ASSERT_EQ(2, GetRefCount(parent.get()));

    child->Rename("subdir", parent.get());

    ASSERT_EQ(child.get(), parent->LookupChildByName("SuBdIr", false /* acquire */));
    ASSERT_EQ(2, GetRefCount(parent.get()));
}

TEST_F(NodeTest, RenameRoot) {
    unique_node_ptr root = CreateNode(nullptr, "/root");
    ASSERT_EQ(1, GetRefCount(root.get()));

    root->Rename("/i-am-root!", nullptr);

    ASSERT_EQ("/i-am-root!", root->GetName());
    ASSERT_EQ(1, GetRefCount(root.get()));
}

TEST_F(NodeTest, NodeCompareDefinesLinearOrder) {
    unique_node_ptr node_a = CreateNode(nullptr, "a");
    unique_node_ptr node_b = CreateNode(nullptr, "B");
    unique_node_ptr node_c = CreateNode(nullptr, "c");

    ASSERT_FALSE(cmp.operator()(node_a.get(), node_a.get()));
    ASSERT_FALSE(cmp.operator()(node_b.get(), node_b.get()));
    ASSERT_FALSE(cmp.operator()(node_c.get(), node_c.get()));

    auto check_fn = [&](const node* lhs_node, const node* rhs_node) {
        ASSERT_TRUE(cmp.operator()(lhs_node, rhs_node));
        ASSERT_FALSE(cmp.operator()(rhs_node, lhs_node));
    };

    check_fn(node_a.get(), node_b.get());
    check_fn(node_b.get(), node_c.get());
    check_fn(node_a.get(), node_c.get());

    // ("A", 0) < node_a < ("a", max_uintptr_t) < node_b
    ASSERT_TRUE(cmp.operator()(std::make_pair("A", 0), node_a.get()));
    ASSERT_TRUE(cmp.operator()(node_a.get(),
                               std::make_pair("A", std::numeric_limits<uintptr_t>::max())));
    ASSERT_TRUE(cmp.operator()(std::make_pair("A", std::numeric_limits<uintptr_t>::max()),
                               node_b.get()));
}

TEST_F(NodeTest, LookupChildByName_ChildrenWithSameName) {
    unique_node_ptr parent = CreateNode(nullptr, "/path");
    unique_node_ptr foo1 = CreateNode(parent.get(), "FoO");
    unique_node_ptr foo2 = CreateNode(parent.get(), "fOo");
    unique_node_ptr bar1 = CreateNode(parent.get(), "BAR");
    unique_node_ptr bar2 = CreateNode(parent.get(), "bar");
    unique_node_ptr baz1 = CreateNode(parent.get(), "baZ");
    unique_node_ptr baz2 = CreateNode(parent.get(), "Baz");

    auto test_fn = [&](const std::string& name, node* first, node* second) {
        auto node1 = parent->LookupChildByName(name, false /* acquire */);
        ASSERT_EQ(std::min(first, second), node1);
        node1->SetDeleted();

        auto node2 = parent->LookupChildByName(name, false /* acquire */);
        ASSERT_EQ(std::max(first, second), node2);
        node2->SetDeleted();

        ASSERT_EQ(nullptr, parent->LookupChildByName(name, false /* acquire */));
    };

    test_fn("foo", foo1.get(), foo2.get());
    test_fn("bAr", bar1.get(), bar2.get());
    test_fn("BaZ", baz1.get(), baz2.get());
}

TEST_F(NodeTest, DestroyDoesntDoubleFree) {
    node* root = node::Create(nullptr, "root", "", true, 0, 0, &lock_, 0, &tracker_);
    node* child = node::Create(root, "child", "", true, 0, 0, &lock_, 0, &tracker_);
    node* grandchild = node::Create(child, "grandchild", "", true, 0, 0, &lock_, 0, &tracker_);

    // 'child' is referenced by itself and by 'grandchild'
    ASSERT_EQ(2, GetRefCount(child));
    // Kernel forgets about child only
    ASSERT_FALSE(child->Release(1));
    // Child only referenced by 'grandchild'
    ASSERT_EQ(1, GetRefCount(child));

    // Now, destroying the filesystem shouldn't result in a double free
    node::DeleteTree(root);
}

TEST_F(NodeTest, ForChild) {
    unique_node_ptr parent = CreateNode(nullptr, "/path");
    unique_node_ptr foo1 = CreateNode(parent.get(), "FoO");
    unique_node_ptr foo2 = CreateNode(parent.get(), "fOo");
    unique_node_ptr foo3 = CreateNode(parent.get(), "foo");
    foo3->SetDeleted();

    std::vector<node*> match_all;
    auto test_fn_match_all = [&](node* child) {
        match_all.push_back(child);
        return false;
    };

    std::vector<node*> match_first;
    auto test_fn_match_first = [&](node* child) {
        match_first.push_back(child);
        return true;
    };

    std::vector<node*> match_none;
    auto test_fn_match_none = [&](node* child) {
        match_none.push_back(child);
        return false;
    };

    node* node_all = ForChild(parent.get(), "foo", test_fn_match_all);
    ASSERT_EQ(nullptr, node_all);
    ASSERT_EQ(2, match_all.size());
    ASSERT_EQ(std::min(foo1.get(), foo2.get()), match_all[0]);
    ASSERT_EQ(std::max(foo1.get(), foo2.get()), match_all[1]);

    node* node_first = ForChild(parent.get(), "foo", test_fn_match_first);
    ASSERT_EQ(std::min(foo1.get(), foo2.get()), node_first);
    ASSERT_EQ(1, match_first.size());
    ASSERT_EQ(std::min(foo1.get(), foo2.get()), match_first[0]);

    node* node_none = ForChild(parent.get(), "bar", test_fn_match_none);
    ASSERT_EQ(nullptr, node_none);
    ASSERT_TRUE(match_none.empty());
}