Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 1 | #include <gtest/gtest.h> |
| 2 | |
| 3 | #include "node-inl.h" |
| 4 | |
| 5 | #include <algorithm> |
Nikita Ioffe | 890e6f2 | 2020-06-18 17:35:08 +0100 | [diff] [blame] | 6 | #include <limits> |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 7 | #include <memory> |
| 8 | #include <mutex> |
| 9 | |
| 10 | using mediaprovider::fuse::dirhandle; |
| 11 | using mediaprovider::fuse::handle; |
| 12 | using mediaprovider::fuse::node; |
Narayan Kamath | 568f17a | 2020-02-19 13:45:29 +0000 | [diff] [blame] | 13 | using mediaprovider::fuse::NodeTracker; |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 14 | |
| 15 | // Listed as a friend class to struct node so it can observe implementation |
| 16 | // details if required. The only implementation detail that is worth writing |
| 17 | // tests around at the moment is the reference count. |
| 18 | class NodeTest : public ::testing::Test { |
| 19 | public: |
Narayan Kamath | 568f17a | 2020-02-19 13:45:29 +0000 | [diff] [blame] | 20 | NodeTest() : tracker_(NodeTracker(&lock_)) {} |
| 21 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 22 | uint32_t GetRefCount(node* node) { return node->refcount_; } |
| 23 | |
| 24 | std::recursive_mutex lock_; |
Narayan Kamath | 568f17a | 2020-02-19 13:45:29 +0000 | [diff] [blame] | 25 | NodeTracker tracker_; |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 26 | |
| 27 | // Forward destruction here, as NodeTest is a friend class. |
| 28 | static void destroy(node* node) { delete node; } |
| 29 | |
Narayan Kamath | eca3425 | 2020-02-11 13:08:37 +0000 | [diff] [blame] | 30 | static void acquire(node* node) { node->Acquire(); } |
| 31 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 32 | typedef std::unique_ptr<node, decltype(&NodeTest::destroy)> unique_node_ptr; |
| 33 | |
Zim | bb7c376 | 2020-09-23 13:50:08 +0100 | [diff] [blame] | 34 | unique_node_ptr CreateNode(node* parent, const std::string& path, const int transforms = 0) { |
| 35 | return unique_node_ptr(node::Create(parent, path, "", true, transforms, &lock_, &tracker_), |
| 36 | &NodeTest::destroy); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 37 | } |
Nikita Ioffe | 890e6f2 | 2020-06-18 17:35:08 +0100 | [diff] [blame] | 38 | |
Zim | 53c2d70 | 2020-09-23 12:18:27 +0100 | [diff] [blame] | 39 | static class node* ForChild(class node* node, const std::string& name, |
| 40 | const std::function<bool(class node*)>& callback) { |
| 41 | return node->ForChild(name, callback); |
| 42 | } |
| 43 | |
Nikita Ioffe | 890e6f2 | 2020-06-18 17:35:08 +0100 | [diff] [blame] | 44 | // Expose NodeCompare for testing. |
| 45 | node::NodeCompare cmp; |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | TEST_F(NodeTest, TestCreate) { |
| 49 | unique_node_ptr node = CreateNode(nullptr, "/path"); |
| 50 | |
| 51 | ASSERT_EQ("/path", node->GetName()); |
| 52 | ASSERT_EQ(1, GetRefCount(node.get())); |
| 53 | ASSERT_FALSE(node->HasCachedHandle()); |
| 54 | } |
| 55 | |
| 56 | TEST_F(NodeTest, TestCreate_withParent) { |
| 57 | unique_node_ptr parent = CreateNode(nullptr, "/path"); |
| 58 | ASSERT_EQ(1, GetRefCount(parent.get())); |
| 59 | |
| 60 | // Adding a child to a parent node increments its refcount. |
| 61 | unique_node_ptr child = CreateNode(parent.get(), "subdir"); |
| 62 | ASSERT_EQ(2, GetRefCount(parent.get())); |
| 63 | |
| 64 | // Make sure the node has been added to the parents list of children. |
Narayan Kamath | eca3425 | 2020-02-11 13:08:37 +0000 | [diff] [blame] | 65 | ASSERT_EQ(child.get(), parent->LookupChildByName("subdir", false /* acquire */)); |
| 66 | ASSERT_EQ(1, GetRefCount(child.get())); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | TEST_F(NodeTest, TestRelease) { |
Zim | bb7c376 | 2020-09-23 13:50:08 +0100 | [diff] [blame] | 70 | node* node = node::Create(nullptr, "/path", "", true, 0, &lock_, &tracker_); |
Narayan Kamath | eca3425 | 2020-02-11 13:08:37 +0000 | [diff] [blame] | 71 | acquire(node); |
| 72 | acquire(node); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 73 | ASSERT_EQ(3, GetRefCount(node)); |
| 74 | |
| 75 | ASSERT_FALSE(node->Release(1)); |
| 76 | ASSERT_EQ(2, GetRefCount(node)); |
| 77 | |
| 78 | // A Release that makes refcount go negative should be a no-op. |
| 79 | ASSERT_FALSE(node->Release(10000)); |
| 80 | ASSERT_EQ(2, GetRefCount(node)); |
| 81 | |
| 82 | // Finally, let the refcount go to zero. |
| 83 | ASSERT_TRUE(node->Release(2)); |
| 84 | } |
| 85 | |
Zim | 53c2d70 | 2020-09-23 12:18:27 +0100 | [diff] [blame] | 86 | TEST_F(NodeTest, TestRenameName) { |
Zim | 87c7bf8 | 2020-01-07 18:11:27 +0000 | [diff] [blame] | 87 | unique_node_ptr parent = CreateNode(nullptr, "/path"); |
| 88 | |
| 89 | unique_node_ptr child = CreateNode(parent.get(), "subdir"); |
| 90 | ASSERT_EQ(2, GetRefCount(parent.get())); |
Narayan Kamath | eca3425 | 2020-02-11 13:08:37 +0000 | [diff] [blame] | 91 | ASSERT_EQ(child.get(), parent->LookupChildByName("subdir", false /* acquire */)); |
Zim | 87c7bf8 | 2020-01-07 18:11:27 +0000 | [diff] [blame] | 92 | |
| 93 | child->Rename("subdir_new", parent.get()); |
| 94 | |
| 95 | ASSERT_EQ(2, GetRefCount(parent.get())); |
Narayan Kamath | eca3425 | 2020-02-11 13:08:37 +0000 | [diff] [blame] | 96 | ASSERT_EQ(nullptr, parent->LookupChildByName("subdir", false /* acquire */)); |
| 97 | ASSERT_EQ(child.get(), parent->LookupChildByName("subdir_new", false /* acquire */)); |
Zim | 87c7bf8 | 2020-01-07 18:11:27 +0000 | [diff] [blame] | 98 | |
| 99 | ASSERT_EQ("/path/subdir_new", child->BuildPath()); |
Narayan Kamath | eca3425 | 2020-02-11 13:08:37 +0000 | [diff] [blame] | 100 | ASSERT_EQ(1, GetRefCount(child.get())); |
Zim | 87c7bf8 | 2020-01-07 18:11:27 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Zim | 53c2d70 | 2020-09-23 12:18:27 +0100 | [diff] [blame] | 103 | TEST_F(NodeTest, TestRenameParent) { |
Zim | 87c7bf8 | 2020-01-07 18:11:27 +0000 | [diff] [blame] | 104 | unique_node_ptr parent1 = CreateNode(nullptr, "/path1"); |
| 105 | unique_node_ptr parent2 = CreateNode(nullptr, "/path2"); |
| 106 | |
| 107 | unique_node_ptr child = CreateNode(parent1.get(), "subdir"); |
| 108 | ASSERT_EQ(2, GetRefCount(parent1.get())); |
Narayan Kamath | eca3425 | 2020-02-11 13:08:37 +0000 | [diff] [blame] | 109 | ASSERT_EQ(child.get(), parent1->LookupChildByName("subdir", false /* acquire */)); |
Zim | 87c7bf8 | 2020-01-07 18:11:27 +0000 | [diff] [blame] | 110 | |
| 111 | child->Rename("subdir", parent2.get()); |
| 112 | ASSERT_EQ(1, GetRefCount(parent1.get())); |
Narayan Kamath | eca3425 | 2020-02-11 13:08:37 +0000 | [diff] [blame] | 113 | ASSERT_EQ(nullptr, parent1->LookupChildByName("subdir", false /* acquire */)); |
Zim | 87c7bf8 | 2020-01-07 18:11:27 +0000 | [diff] [blame] | 114 | |
| 115 | ASSERT_EQ(2, GetRefCount(parent2.get())); |
Narayan Kamath | eca3425 | 2020-02-11 13:08:37 +0000 | [diff] [blame] | 116 | ASSERT_EQ(child.get(), parent2->LookupChildByName("subdir", false /* acquire */)); |
Zim | 87c7bf8 | 2020-01-07 18:11:27 +0000 | [diff] [blame] | 117 | |
| 118 | ASSERT_EQ("/path2/subdir", child->BuildPath()); |
Narayan Kamath | eca3425 | 2020-02-11 13:08:37 +0000 | [diff] [blame] | 119 | ASSERT_EQ(1, GetRefCount(child.get())); |
Zim | 87c7bf8 | 2020-01-07 18:11:27 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Zim | 53c2d70 | 2020-09-23 12:18:27 +0100 | [diff] [blame] | 122 | TEST_F(NodeTest, TestRenameNameAndParent) { |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 123 | unique_node_ptr parent1 = CreateNode(nullptr, "/path1"); |
| 124 | unique_node_ptr parent2 = CreateNode(nullptr, "/path2"); |
| 125 | |
| 126 | unique_node_ptr child = CreateNode(parent1.get(), "subdir"); |
| 127 | ASSERT_EQ(2, GetRefCount(parent1.get())); |
Narayan Kamath | eca3425 | 2020-02-11 13:08:37 +0000 | [diff] [blame] | 128 | ASSERT_EQ(child.get(), parent1->LookupChildByName("subdir", false /* acquire */)); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 129 | |
| 130 | child->Rename("subdir_new", parent2.get()); |
| 131 | ASSERT_EQ(1, GetRefCount(parent1.get())); |
Narayan Kamath | eca3425 | 2020-02-11 13:08:37 +0000 | [diff] [blame] | 132 | ASSERT_EQ(nullptr, parent1->LookupChildByName("subdir", false /* acquire */)); |
| 133 | ASSERT_EQ(nullptr, parent1->LookupChildByName("subdir_new", false /* acquire */)); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 134 | |
| 135 | ASSERT_EQ(2, GetRefCount(parent2.get())); |
Narayan Kamath | eca3425 | 2020-02-11 13:08:37 +0000 | [diff] [blame] | 136 | ASSERT_EQ(child.get(), parent2->LookupChildByName("subdir_new", false /* acquire */)); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 137 | |
| 138 | ASSERT_EQ("/path2/subdir_new", child->BuildPath()); |
Narayan Kamath | eca3425 | 2020-02-11 13:08:37 +0000 | [diff] [blame] | 139 | ASSERT_EQ(1, GetRefCount(child.get())); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Zim | 53c2d70 | 2020-09-23 12:18:27 +0100 | [diff] [blame] | 142 | TEST_F(NodeTest, TestRenameNameForChild) { |
| 143 | unique_node_ptr parent = CreateNode(nullptr, "/path"); |
| 144 | |
Zim | bb7c376 | 2020-09-23 13:50:08 +0100 | [diff] [blame] | 145 | unique_node_ptr child0 = CreateNode(parent.get(), "subdir", 0 /* transforms */); |
| 146 | unique_node_ptr child1 = CreateNode(parent.get(), "subdir", 1 /* transforms */); |
| 147 | ASSERT_EQ(3, GetRefCount(parent.get())); |
| 148 | ASSERT_EQ(child0.get(), |
| 149 | parent->LookupChildByName("subdir", false /* acquire */, 0 /* transforms */)); |
| 150 | ASSERT_EQ(child1.get(), |
| 151 | parent->LookupChildByName("subdir", false /* acquire */, 1 /* transforms */)); |
Zim | 53c2d70 | 2020-09-23 12:18:27 +0100 | [diff] [blame] | 152 | |
| 153 | parent->RenameChild("subdir", "subdir_new", parent.get()); |
| 154 | |
Zim | bb7c376 | 2020-09-23 13:50:08 +0100 | [diff] [blame] | 155 | ASSERT_EQ(3, GetRefCount(parent.get())); |
| 156 | ASSERT_EQ(nullptr, |
| 157 | parent->LookupChildByName("subdir", false /* acquire */, 0 /* transforms */)); |
| 158 | ASSERT_EQ(nullptr, |
| 159 | parent->LookupChildByName("subdir", false /* acquire */, 1 /* transforms */)); |
| 160 | ASSERT_EQ(child0.get(), |
| 161 | parent->LookupChildByName("subdir_new", false /* acquire */, 0 /* transforms */)); |
| 162 | ASSERT_EQ(child1.get(), |
| 163 | parent->LookupChildByName("subdir_new", false /* acquire */, 1 /* transforms */)); |
Zim | 53c2d70 | 2020-09-23 12:18:27 +0100 | [diff] [blame] | 164 | |
Zim | bb7c376 | 2020-09-23 13:50:08 +0100 | [diff] [blame] | 165 | ASSERT_EQ("/path/subdir_new", child0->BuildPath()); |
| 166 | ASSERT_EQ("/path/subdir_new", child1->BuildPath()); |
| 167 | ASSERT_EQ(1, GetRefCount(child0.get())); |
| 168 | ASSERT_EQ(1, GetRefCount(child1.get())); |
Zim | 53c2d70 | 2020-09-23 12:18:27 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | TEST_F(NodeTest, TestRenameParentForChild) { |
| 172 | unique_node_ptr parent1 = CreateNode(nullptr, "/path1"); |
| 173 | unique_node_ptr parent2 = CreateNode(nullptr, "/path2"); |
| 174 | |
Zim | bb7c376 | 2020-09-23 13:50:08 +0100 | [diff] [blame] | 175 | unique_node_ptr child0 = CreateNode(parent1.get(), "subdir", 0 /* transforms */); |
| 176 | unique_node_ptr child1 = CreateNode(parent1.get(), "subdir", 1 /* transforms */); |
| 177 | ASSERT_EQ(3, GetRefCount(parent1.get())); |
| 178 | ASSERT_EQ(child0.get(), |
| 179 | parent1->LookupChildByName("subdir", false /* acquire */, 0 /* transforms */)); |
| 180 | ASSERT_EQ(child1.get(), |
| 181 | parent1->LookupChildByName("subdir", false /* acquire */, 1 /* transforms */)); |
Zim | 53c2d70 | 2020-09-23 12:18:27 +0100 | [diff] [blame] | 182 | |
| 183 | parent1->RenameChild("subdir", "subdir", parent2.get()); |
| 184 | ASSERT_EQ(1, GetRefCount(parent1.get())); |
Zim | bb7c376 | 2020-09-23 13:50:08 +0100 | [diff] [blame] | 185 | ASSERT_EQ(nullptr, |
| 186 | parent1->LookupChildByName("subdir", false /* acquire */, 0 /* transforms */)); |
| 187 | ASSERT_EQ(nullptr, |
| 188 | parent1->LookupChildByName("subdir", false /* acquire */, 1 /* transforms */)); |
Zim | 53c2d70 | 2020-09-23 12:18:27 +0100 | [diff] [blame] | 189 | |
Zim | bb7c376 | 2020-09-23 13:50:08 +0100 | [diff] [blame] | 190 | ASSERT_EQ(3, GetRefCount(parent2.get())); |
| 191 | ASSERT_EQ(child0.get(), |
| 192 | parent2->LookupChildByName("subdir", false /* acquire */, 0 /* transforms */)); |
| 193 | ASSERT_EQ(child1.get(), |
| 194 | parent2->LookupChildByName("subdir", false /* acquire */, 1 /* transforms */)); |
Zim | 53c2d70 | 2020-09-23 12:18:27 +0100 | [diff] [blame] | 195 | |
Zim | bb7c376 | 2020-09-23 13:50:08 +0100 | [diff] [blame] | 196 | ASSERT_EQ("/path2/subdir", child0->BuildPath()); |
| 197 | ASSERT_EQ("/path2/subdir", child1->BuildPath()); |
| 198 | ASSERT_EQ(1, GetRefCount(child0.get())); |
| 199 | ASSERT_EQ(1, GetRefCount(child1.get())); |
Zim | 53c2d70 | 2020-09-23 12:18:27 +0100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | TEST_F(NodeTest, TestRenameNameAndParentForChild) { |
| 203 | unique_node_ptr parent1 = CreateNode(nullptr, "/path1"); |
| 204 | unique_node_ptr parent2 = CreateNode(nullptr, "/path2"); |
| 205 | |
Zim | bb7c376 | 2020-09-23 13:50:08 +0100 | [diff] [blame] | 206 | unique_node_ptr child0 = CreateNode(parent1.get(), "subdir", 0 /* transforms */); |
| 207 | unique_node_ptr child1 = CreateNode(parent1.get(), "subdir", 1 /* transforms */); |
| 208 | ASSERT_EQ(3, GetRefCount(parent1.get())); |
| 209 | ASSERT_EQ(child0.get(), |
| 210 | parent1->LookupChildByName("subdir", false /* acquire */, 0 /* transforms */)); |
| 211 | ASSERT_EQ(child1.get(), |
| 212 | parent1->LookupChildByName("subdir", false /* acquire */, 1 /* transforms */)); |
Zim | 53c2d70 | 2020-09-23 12:18:27 +0100 | [diff] [blame] | 213 | |
| 214 | parent1->RenameChild("subdir", "subdir_new", parent2.get()); |
| 215 | ASSERT_EQ(1, GetRefCount(parent1.get())); |
Zim | bb7c376 | 2020-09-23 13:50:08 +0100 | [diff] [blame] | 216 | ASSERT_EQ(nullptr, |
| 217 | parent1->LookupChildByName("subdir", false /* acquire */, 0 /* transforms */)); |
| 218 | ASSERT_EQ(nullptr, |
| 219 | parent1->LookupChildByName("subdir_new", false /* acquire */, 0 /* transforms */)); |
| 220 | ASSERT_EQ(nullptr, |
| 221 | parent1->LookupChildByName("subdir", false /* acquire */, 1 /* transforms */)); |
| 222 | ASSERT_EQ(nullptr, |
| 223 | parent1->LookupChildByName("subdir_new", false /* acquire */, 1 /* transforms */)); |
Zim | 53c2d70 | 2020-09-23 12:18:27 +0100 | [diff] [blame] | 224 | |
Zim | bb7c376 | 2020-09-23 13:50:08 +0100 | [diff] [blame] | 225 | ASSERT_EQ(3, GetRefCount(parent2.get())); |
| 226 | ASSERT_EQ(nullptr, |
| 227 | parent1->LookupChildByName("subdir_new", false /* acquire */, 0 /* transforms */)); |
| 228 | ASSERT_EQ(nullptr, |
| 229 | parent1->LookupChildByName("subdir_new", false /* acquire */, 1 /* transforms */)); |
Zim | 53c2d70 | 2020-09-23 12:18:27 +0100 | [diff] [blame] | 230 | |
Zim | bb7c376 | 2020-09-23 13:50:08 +0100 | [diff] [blame] | 231 | ASSERT_EQ("/path2/subdir_new", child0->BuildPath()); |
| 232 | ASSERT_EQ("/path2/subdir_new", child1->BuildPath()); |
| 233 | ASSERT_EQ(1, GetRefCount(child0.get())); |
| 234 | ASSERT_EQ(1, GetRefCount(child1.get())); |
Zim | 53c2d70 | 2020-09-23 12:18:27 +0100 | [diff] [blame] | 235 | } |
| 236 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 237 | TEST_F(NodeTest, TestBuildPath) { |
| 238 | unique_node_ptr parent = CreateNode(nullptr, "/path"); |
| 239 | ASSERT_EQ("/path", parent->BuildPath()); |
| 240 | |
| 241 | unique_node_ptr child = CreateNode(parent.get(), "subdir"); |
| 242 | ASSERT_EQ("/path/subdir", child->BuildPath()); |
| 243 | |
| 244 | unique_node_ptr child2 = CreateNode(parent.get(), "subdir2"); |
| 245 | ASSERT_EQ("/path/subdir2", child2->BuildPath()); |
| 246 | |
| 247 | unique_node_ptr subchild = CreateNode(child2.get(), "subsubdir"); |
| 248 | ASSERT_EQ("/path/subdir2/subsubdir", subchild->BuildPath()); |
| 249 | } |
| 250 | |
| 251 | TEST_F(NodeTest, TestSetDeleted) { |
| 252 | unique_node_ptr parent = CreateNode(nullptr, "/path"); |
| 253 | unique_node_ptr child = CreateNode(parent.get(), "subdir"); |
| 254 | |
Narayan Kamath | eca3425 | 2020-02-11 13:08:37 +0000 | [diff] [blame] | 255 | ASSERT_EQ(child.get(), parent->LookupChildByName("subdir", false /* acquire */)); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 256 | child->SetDeleted(); |
Narayan Kamath | eca3425 | 2020-02-11 13:08:37 +0000 | [diff] [blame] | 257 | ASSERT_EQ(nullptr, parent->LookupChildByName("subdir", false /* acquire */)); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Zim | 53c2d70 | 2020-09-23 12:18:27 +0100 | [diff] [blame] | 260 | TEST_F(NodeTest, TestSetDeletedForChild) { |
| 261 | unique_node_ptr parent = CreateNode(nullptr, "/path"); |
Zim | bb7c376 | 2020-09-23 13:50:08 +0100 | [diff] [blame] | 262 | unique_node_ptr child0 = CreateNode(parent.get(), "subdir", 0 /* transforms */); |
| 263 | unique_node_ptr child1 = CreateNode(parent.get(), "subdir", 1 /* transforms */); |
Zim | 53c2d70 | 2020-09-23 12:18:27 +0100 | [diff] [blame] | 264 | |
Zim | bb7c376 | 2020-09-23 13:50:08 +0100 | [diff] [blame] | 265 | ASSERT_EQ(child0.get(), |
| 266 | parent->LookupChildByName("subdir", false /* acquire */, 0 /* transforms */)); |
| 267 | ASSERT_EQ(child1.get(), |
| 268 | parent->LookupChildByName("subdir", false /* acquire */, 1 /* transforms */)); |
Zim | 53c2d70 | 2020-09-23 12:18:27 +0100 | [diff] [blame] | 269 | parent->SetDeletedForChild("subdir"); |
Zim | bb7c376 | 2020-09-23 13:50:08 +0100 | [diff] [blame] | 270 | ASSERT_EQ(nullptr, |
| 271 | parent->LookupChildByName("subdir", false /* acquire */, 0 /* transforms */)); |
| 272 | ASSERT_EQ(nullptr, |
| 273 | parent->LookupChildByName("subdir", false /* acquire */, 1 /* transforms */)); |
Zim | 53c2d70 | 2020-09-23 12:18:27 +0100 | [diff] [blame] | 274 | } |
| 275 | |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 276 | TEST_F(NodeTest, DeleteTree) { |
| 277 | unique_node_ptr parent = CreateNode(nullptr, "/path"); |
| 278 | |
| 279 | // This is the tree that we intend to delete. |
Zim | bb7c376 | 2020-09-23 13:50:08 +0100 | [diff] [blame] | 280 | node* child = node::Create(parent.get(), "subdir", "", true, 0, &lock_, &tracker_); |
| 281 | node::Create(child, "s1", "", true, 0, &lock_, &tracker_); |
| 282 | node* subchild2 = node::Create(child, "s2", "", true, 0, &lock_, &tracker_); |
| 283 | node::Create(subchild2, "sc2", "", true, 0, &lock_, &tracker_); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 284 | |
Narayan Kamath | eca3425 | 2020-02-11 13:08:37 +0000 | [diff] [blame] | 285 | ASSERT_EQ(child, parent->LookupChildByName("subdir", false /* acquire */)); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 286 | node::DeleteTree(child); |
Narayan Kamath | eca3425 | 2020-02-11 13:08:37 +0000 | [diff] [blame] | 287 | ASSERT_EQ(nullptr, parent->LookupChildByName("subdir", false /* acquire */)); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | TEST_F(NodeTest, LookupChildByName_empty) { |
| 291 | unique_node_ptr parent = CreateNode(nullptr, "/path"); |
| 292 | unique_node_ptr child = CreateNode(parent.get(), "subdir"); |
| 293 | |
Narayan Kamath | eca3425 | 2020-02-11 13:08:37 +0000 | [diff] [blame] | 294 | ASSERT_EQ(child.get(), parent->LookupChildByName("subdir", false /* acquire */)); |
| 295 | ASSERT_EQ(nullptr, parent->LookupChildByName("", false /* acquire */)); |
| 296 | } |
| 297 | |
Zim | bb7c376 | 2020-09-23 13:50:08 +0100 | [diff] [blame] | 298 | TEST_F(NodeTest, LookupChildByName_transforms) { |
| 299 | unique_node_ptr parent = CreateNode(nullptr, "/path"); |
| 300 | unique_node_ptr child0 = CreateNode(parent.get(), "subdir", 0 /* transforms */); |
| 301 | unique_node_ptr child1 = CreateNode(parent.get(), "subdir", 1 /* transforms */); |
| 302 | |
| 303 | ASSERT_EQ(child0.get(), parent->LookupChildByName("subdir", false /* acquire */)); |
| 304 | ASSERT_EQ(child0.get(), |
| 305 | parent->LookupChildByName("subdir", false /* acquire */, 0 /* transforms */)); |
| 306 | ASSERT_EQ(child1.get(), |
| 307 | parent->LookupChildByName("subdir", false /* acquire */, 1 /* transforms */)); |
| 308 | ASSERT_EQ(nullptr, |
| 309 | parent->LookupChildByName("subdir", false /* acquire */, 2 /* transforms */)); |
| 310 | } |
| 311 | |
Narayan Kamath | eca3425 | 2020-02-11 13:08:37 +0000 | [diff] [blame] | 312 | TEST_F(NodeTest, LookupChildByName_refcounts) { |
| 313 | unique_node_ptr parent = CreateNode(nullptr, "/path"); |
| 314 | unique_node_ptr child = CreateNode(parent.get(), "subdir"); |
| 315 | |
| 316 | ASSERT_EQ(child.get(), parent->LookupChildByName("subdir", false /* acquire */)); |
| 317 | ASSERT_EQ(1, GetRefCount(child.get())); |
| 318 | |
| 319 | ASSERT_EQ(child.get(), parent->LookupChildByName("subdir", true /* acquire */)); |
| 320 | ASSERT_EQ(2, GetRefCount(child.get())); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | TEST_F(NodeTest, LookupAbsolutePath) { |
| 324 | unique_node_ptr parent = CreateNode(nullptr, "/path"); |
| 325 | unique_node_ptr child = CreateNode(parent.get(), "subdir"); |
| 326 | unique_node_ptr child2 = CreateNode(parent.get(), "subdir2"); |
| 327 | unique_node_ptr subchild = CreateNode(child2.get(), "subsubdir"); |
| 328 | |
| 329 | ASSERT_EQ(parent.get(), node::LookupAbsolutePath(parent.get(), "/path")); |
| 330 | ASSERT_EQ(parent.get(), node::LookupAbsolutePath(parent.get(), "/path/")); |
| 331 | ASSERT_EQ(nullptr, node::LookupAbsolutePath(parent.get(), "/path2")); |
| 332 | |
| 333 | ASSERT_EQ(child.get(), node::LookupAbsolutePath(parent.get(), "/path/subdir")); |
| 334 | ASSERT_EQ(child.get(), node::LookupAbsolutePath(parent.get(), "/path/subdir/")); |
| 335 | // TODO(narayan): Are the two cases below intentional behaviour ? |
| 336 | ASSERT_EQ(child.get(), node::LookupAbsolutePath(parent.get(), "/path//subdir")); |
| 337 | ASSERT_EQ(child.get(), node::LookupAbsolutePath(parent.get(), "/path///subdir")); |
| 338 | |
| 339 | ASSERT_EQ(child2.get(), node::LookupAbsolutePath(parent.get(), "/path/subdir2")); |
| 340 | ASSERT_EQ(child2.get(), node::LookupAbsolutePath(parent.get(), "/path/subdir2/")); |
| 341 | |
| 342 | ASSERT_EQ(nullptr, node::LookupAbsolutePath(parent.get(), "/path/subdir3/")); |
| 343 | |
| 344 | ASSERT_EQ(subchild.get(), node::LookupAbsolutePath(parent.get(), "/path/subdir2/subsubdir")); |
| 345 | ASSERT_EQ(nullptr, node::LookupAbsolutePath(parent.get(), "/path/subdir/subsubdir")); |
| 346 | } |
| 347 | |
| 348 | TEST_F(NodeTest, AddDestroyHandle) { |
| 349 | unique_node_ptr node = CreateNode(nullptr, "/path"); |
| 350 | |
Zim | 148cbe2 | 2020-11-17 15:58:29 +0000 | [diff] [blame] | 351 | handle* h = new handle(-1, new mediaprovider::fuse::RedactionInfo, true /* cached */, |
| 352 | false /* passthrough */, 0 /* uid */); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 353 | node->AddHandle(h); |
| 354 | ASSERT_TRUE(node->HasCachedHandle()); |
| 355 | |
| 356 | node->DestroyHandle(h); |
| 357 | ASSERT_FALSE(node->HasCachedHandle()); |
| 358 | |
| 359 | // Should all crash the process as the handle is no longer associated with |
| 360 | // the node in question. |
| 361 | EXPECT_DEATH(node->DestroyHandle(h), ""); |
| 362 | EXPECT_DEATH(node->DestroyHandle(nullptr), ""); |
Zim | 148cbe2 | 2020-11-17 15:58:29 +0000 | [diff] [blame] | 363 | std::unique_ptr<handle> h2(new handle(-1, new mediaprovider::fuse::RedactionInfo, |
| 364 | true /* cached */, false /* passthrough */, 0 /* uid */)); |
Narayan Kamath | aef84a1 | 2020-01-02 15:20:13 +0000 | [diff] [blame] | 365 | EXPECT_DEATH(node->DestroyHandle(h2.get()), ""); |
| 366 | } |
Zim | 8996231 | 2020-04-22 21:21:53 +0100 | [diff] [blame] | 367 | |
| 368 | TEST_F(NodeTest, CaseInsensitive) { |
| 369 | unique_node_ptr parent = CreateNode(nullptr, "/path"); |
Zim | 8996231 | 2020-04-22 21:21:53 +0100 | [diff] [blame] | 370 | unique_node_ptr mixed_child = CreateNode(parent.get(), "cHiLd"); |
| 371 | |
Zim | 0852ba7 | 2020-06-09 11:50:33 +0100 | [diff] [blame] | 372 | node* upper_child = parent->LookupChildByName("CHILD", false /* acquire */); |
| 373 | node* lower_child = parent->LookupChildByName("child", false /* acquire */); |
Zim | 8996231 | 2020-04-22 21:21:53 +0100 | [diff] [blame] | 374 | |
Zim | 0852ba7 | 2020-06-09 11:50:33 +0100 | [diff] [blame] | 375 | ASSERT_EQ(mixed_child.get(), lower_child); |
| 376 | ASSERT_EQ(mixed_child.get(), upper_child); |
Zim | 8996231 | 2020-04-22 21:21:53 +0100 | [diff] [blame] | 377 | } |
Nikita Ioffe | 890e6f2 | 2020-06-18 17:35:08 +0100 | [diff] [blame] | 378 | |
| 379 | TEST_F(NodeTest, RenameSameNameSameParent) { |
| 380 | unique_node_ptr parent = CreateNode(nullptr, "/path1"); |
| 381 | unique_node_ptr child = CreateNode(parent.get(), "subdir"); |
| 382 | |
| 383 | ASSERT_EQ(child.get(), parent->LookupChildByName("SuBdIr", false /* acquire */)); |
| 384 | ASSERT_EQ(2, GetRefCount(parent.get())); |
| 385 | |
| 386 | child->Rename("subdir", parent.get()); |
| 387 | |
| 388 | ASSERT_EQ(child.get(), parent->LookupChildByName("SuBdIr", false /* acquire */)); |
| 389 | ASSERT_EQ(2, GetRefCount(parent.get())); |
| 390 | } |
| 391 | |
| 392 | TEST_F(NodeTest, RenameRoot) { |
| 393 | unique_node_ptr root = CreateNode(nullptr, "/root"); |
| 394 | ASSERT_EQ(1, GetRefCount(root.get())); |
| 395 | |
| 396 | root->Rename("/i-am-root!", nullptr); |
| 397 | |
| 398 | ASSERT_EQ("/i-am-root!", root->GetName()); |
| 399 | ASSERT_EQ(1, GetRefCount(root.get())); |
| 400 | } |
| 401 | |
| 402 | TEST_F(NodeTest, NodeCompareDefinesLinearOrder) { |
| 403 | unique_node_ptr node_a = CreateNode(nullptr, "a"); |
| 404 | unique_node_ptr node_b = CreateNode(nullptr, "B"); |
| 405 | unique_node_ptr node_c = CreateNode(nullptr, "c"); |
| 406 | |
| 407 | ASSERT_FALSE(cmp.operator()(node_a.get(), node_a.get())); |
| 408 | ASSERT_FALSE(cmp.operator()(node_b.get(), node_b.get())); |
| 409 | ASSERT_FALSE(cmp.operator()(node_c.get(), node_c.get())); |
| 410 | |
| 411 | auto check_fn = [&](const node* lhs_node, const node* rhs_node) { |
| 412 | ASSERT_TRUE(cmp.operator()(lhs_node, rhs_node)); |
| 413 | ASSERT_FALSE(cmp.operator()(rhs_node, lhs_node)); |
| 414 | }; |
| 415 | |
| 416 | check_fn(node_a.get(), node_b.get()); |
| 417 | check_fn(node_b.get(), node_c.get()); |
| 418 | check_fn(node_a.get(), node_c.get()); |
| 419 | |
| 420 | // ("A", 0) < node_a < ("a", max_uintptr_t) < node_b |
| 421 | ASSERT_TRUE(cmp.operator()(std::make_pair("A", 0), node_a.get())); |
| 422 | ASSERT_TRUE(cmp.operator()(node_a.get(), |
| 423 | std::make_pair("A", std::numeric_limits<uintptr_t>::max()))); |
| 424 | ASSERT_TRUE(cmp.operator()(std::make_pair("A", std::numeric_limits<uintptr_t>::max()), |
| 425 | node_b.get())); |
| 426 | } |
| 427 | |
| 428 | TEST_F(NodeTest, LookupChildByName_ChildrenWithSameName) { |
| 429 | unique_node_ptr parent = CreateNode(nullptr, "/path"); |
| 430 | unique_node_ptr foo1 = CreateNode(parent.get(), "FoO"); |
| 431 | unique_node_ptr foo2 = CreateNode(parent.get(), "fOo"); |
| 432 | unique_node_ptr bar1 = CreateNode(parent.get(), "BAR"); |
| 433 | unique_node_ptr bar2 = CreateNode(parent.get(), "bar"); |
| 434 | unique_node_ptr baz1 = CreateNode(parent.get(), "baZ"); |
| 435 | unique_node_ptr baz2 = CreateNode(parent.get(), "Baz"); |
| 436 | |
| 437 | auto test_fn = [&](const std::string& name, node* first, node* second) { |
| 438 | auto node1 = parent->LookupChildByName(name, false /* acquire */); |
| 439 | ASSERT_EQ(std::min(first, second), node1); |
| 440 | node1->SetDeleted(); |
| 441 | |
| 442 | auto node2 = parent->LookupChildByName(name, false /* acquire */); |
| 443 | ASSERT_EQ(std::max(first, second), node2); |
| 444 | node2->SetDeleted(); |
| 445 | |
| 446 | ASSERT_EQ(nullptr, parent->LookupChildByName(name, false /* acquire */)); |
| 447 | }; |
| 448 | |
| 449 | test_fn("foo", foo1.get(), foo2.get()); |
| 450 | test_fn("bAr", bar1.get(), bar2.get()); |
| 451 | test_fn("BaZ", baz1.get(), baz2.get()); |
| 452 | } |
Zim | 53c2d70 | 2020-09-23 12:18:27 +0100 | [diff] [blame] | 453 | |
| 454 | TEST_F(NodeTest, ForChild) { |
| 455 | unique_node_ptr parent = CreateNode(nullptr, "/path"); |
| 456 | unique_node_ptr foo1 = CreateNode(parent.get(), "FoO"); |
| 457 | unique_node_ptr foo2 = CreateNode(parent.get(), "fOo"); |
| 458 | unique_node_ptr foo3 = CreateNode(parent.get(), "foo"); |
| 459 | foo3->SetDeleted(); |
| 460 | |
| 461 | std::vector<node*> match_all; |
| 462 | auto test_fn_match_all = [&](node* child) { |
| 463 | match_all.push_back(child); |
| 464 | return false; |
| 465 | }; |
| 466 | |
| 467 | std::vector<node*> match_first; |
| 468 | auto test_fn_match_first = [&](node* child) { |
| 469 | match_first.push_back(child); |
| 470 | return true; |
| 471 | }; |
| 472 | |
| 473 | std::vector<node*> match_none; |
| 474 | auto test_fn_match_none = [&](node* child) { |
| 475 | match_none.push_back(child); |
| 476 | return false; |
| 477 | }; |
| 478 | |
| 479 | node* node_all = ForChild(parent.get(), "foo", test_fn_match_all); |
| 480 | ASSERT_EQ(nullptr, node_all); |
| 481 | ASSERT_EQ(2, match_all.size()); |
| 482 | ASSERT_EQ(std::min(foo1.get(), foo2.get()), match_all[0]); |
| 483 | ASSERT_EQ(std::max(foo1.get(), foo2.get()), match_all[1]); |
| 484 | |
| 485 | node* node_first = ForChild(parent.get(), "foo", test_fn_match_first); |
| 486 | ASSERT_EQ(std::min(foo1.get(), foo2.get()), node_first); |
| 487 | ASSERT_EQ(1, match_first.size()); |
| 488 | ASSERT_EQ(std::min(foo1.get(), foo2.get()), match_first[0]); |
| 489 | |
| 490 | node* node_none = ForChild(parent.get(), "bar", test_fn_match_none); |
| 491 | ASSERT_EQ(nullptr, node_none); |
| 492 | ASSERT_TRUE(match_none.empty()); |
| 493 | } |