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
|
/**
* @file sdk_test_file_path.cpp
* @brief This file defines tests related to path handling functions
*/
#include "sdk_test_utils.h"
#include "SdkTestNodesSetUp.h"
#include <gmock/gmock.h>
#include <regex>
using namespace sdk_test;
/**
* @class SdkTestPath
* @brief Test suite for path handling functions
*
*/
class SdkTestPath: public SdkTestNodesSetUp
{
public:
const std::vector<NodeInfo>& getElements() const override
{
static const std::vector<NodeInfo> testNodes{
FileNodeInfo("rootTestFile"),
DirNodeInfo("dir1")
.addChild(FileNodeInfo("testFile1"))
#if !defined(WIN32) // Windows does not allow ':' character in file names
.addChild(FileNodeInfo("testFile1:"))
.addChild(FileNodeInfo("test:File1"))
.addChild(FileNodeInfo(":testFile1")),
FileNodeInfo("rootTestFile:"),
FileNodeInfo("rootTest:File"),
FileNodeInfo(":rootTestFile"),
DirNodeInfo("dir2:")
.addChild(DirNodeInfo("dir3:").addChild(FileNodeInfo("testFile3:")))
.addChild(FileNodeInfo("testFile2:"))
.addChild(FileNodeInfo("test:File2"))
.addChild(FileNodeInfo(":testFile2"))
.addChild(FileNodeInfo("testFile2")),
#endif
};
return testNodes;
}
const std::string& getRootTestDir() const override
{
static const std::string dirName{"SDK_TEST_PATH"};
return dirName;
}
const std::vector<MegaHandle> getAllNodesHandles()
{
std::vector<MegaHandle> result;
std::function<void(MegaNode*)> collectHandles = [&](MegaNode* node)
{
if (!node)
return;
result.push_back(node->getHandle());
std::unique_ptr<MegaNodeList> children(megaApi[0].get()->getChildren(node));
if (children)
{
for (int i = 0; i < children->size(); ++i)
{
collectHandles(children->get(i));
}
}
};
collectHandles(getRootTestDirectory());
return result;
}
};
/**
* @brief SdkTestPath.GetNodeByPathResolvesPathFromGetNodePath
*
* Verifies that a node retrieved by handle can return its path using getNodePath
* and then resolved back to the original handle using getNodeByPath.
*
* Steps for each handle defined in the suite:
* 1. Get a node by handle.
* 2. Get the node path by using getNodePath.
* 3. Escape colons in the path (required to use getNodeByPath when the paths has colons).
* 4. Use getNodeByPath to resolve the escaped path.
* 5. Confirm that the resolved node has the same handle as the original.
*/
TEST_F(SdkTestPath, GetNodeByPathResolvesPathFromGetNodePath)
{
auto handles = getAllNodesHandles();
for (auto handle: handles)
{
std::unique_ptr<MegaNode> node(megaApi[0]->getNodeByHandle(handle));
ASSERT_NE(node, nullptr) << "Failed to retrieve node by handle.";
auto path{megaApi[0]->getNodePath(node.get())};
ASSERT_NE(path, nullptr) << "Failed to retrieve node by node handle.";
auto escapedPath{std::regex_replace(path, std::regex(":"), "\\:")};
std::unique_ptr<MegaNode> fromPath(megaApi[0]->getNodeByPath(escapedPath.c_str()));
ASSERT_NE(fromPath, nullptr) << "Failed to retrieve node by path.";
EXPECT_EQ(fromPath->getHandle(), handle) << escapedPath;
}
}
/**
* @brief SdkTestPath.GetNodeByPathResolvesPathFromGetNodePathByNodeHandle
*
* Verifies that a path obtained from a handle using getNodePathByNodeHandle
* can be resolved back to the original handle using getNodeByPath.
*
* Steps for each handle defined in the suite:
* 1. Get the node path by using getNodePathByNodeHandle.
* 2. Escape colons in the path (required to use getNodeByPath when the path has colons).
* 3. Use getNodeByPath to resolve the escaped path.
* 4. Confirm that the resolved node has the same handle as the original.
*/
TEST_F(SdkTestPath, GetNodeByPathResolvesPathFromGetNodePathByNodeHandle)
{
auto handles = getAllNodesHandles();
for (auto handle: handles)
{
auto path{megaApi[0]->getNodePathByNodeHandle(handle)};
ASSERT_NE(path, nullptr) << "Failed to retrieve node by handle.";
auto escapedPath{std::regex_replace(path, std::regex(":"), "\\:")};
std::unique_ptr<MegaNode> fromPath(megaApi[0]->getNodeByPath(escapedPath.c_str()));
ASSERT_NE(fromPath, nullptr) << "Failed to retrieve node by path.";
EXPECT_EQ(fromPath->getHandle(), handle) << escapedPath;
}
}
/**
* @brief SdkTestPath.GetNodeByPathOfTypeResolvesPathFromGetNodePath
*
* Verifies that a node retrieved by handle can return its path using getNodePath
* and then be resolved back to the original handle using getNodeByPathOfType.
*
* Steps for each handle defined in the suite:
* 1. Get a node by handle.
* 2. Get the node path by using getNodePath.
* 3. Escape colons in the path (required to use getNodeByPathOfType when the path has colons).
* 4. Use getNodeByPathOfType to resolve the escaped path, providing the node's type.
* 5. Confirm that the resolved node has the same handle as the original.
*/
TEST_F(SdkTestPath, GetNodeByPathOfTypeResolvesPathFromGetNodePath)
{
auto handles = getAllNodesHandles();
for (auto handle: handles)
{
std::unique_ptr<MegaNode> node(megaApi[0]->getNodeByHandle(handle));
ASSERT_NE(node, nullptr) << "Failed to retrieve node by handle.";
auto path{megaApi[0]->getNodePath(node.get())};
ASSERT_NE(path, nullptr) << "Failed to retrieve node by node handle.";
auto escapedPath{std::regex_replace(path, std::regex(":"), "\\:")};
std::unique_ptr<MegaNode> fromPath(
megaApi[0]->getNodeByPathOfType(escapedPath.c_str(), nullptr, node->getType()));
ASSERT_NE(fromPath, nullptr) << "Failed to retrieve node by path and type.";
EXPECT_EQ(fromPath->getHandle(), handle) << escapedPath;
}
}
/**
* @brief SdkTestPath.GetNodeByPathOfTypeResolvesPathFromGetNodePathByNodeHandle
*
* Verifies that a path obtained from a handle using getNodePathByNodeHandle
* can be resolved back to the original handle using getNodeByPathOfType.
*
* Steps for each handle defined in the suite:
* 1. Get a node by handle (to retrieve the type).
* 2. Get the node path by using getNodePathByNodeHandle.
* 3. Escape colons in the path (required to use getNodeByPathOfType when the path has colons).
* 4. Use getNodeByPathOfType to resolve the escaped path, providing the node's type.
* 5. Confirm that the resolved node has the same handle as the original.
*/
TEST_F(SdkTestPath, GetNodeByPathOfTypeResolvesPathFromGetNodePathByNodeHandle)
{
auto handles = getAllNodesHandles();
for (auto handle: handles)
{
std::unique_ptr<MegaNode> node(megaApi[0]->getNodeByHandle(handle));
ASSERT_NE(node, nullptr) << "Failed to retrieve node by handle.";
auto path{megaApi[0]->getNodePathByNodeHandle(handle)};
ASSERT_NE(path, nullptr) << "Failed to retrieve path by handle.";
auto escapedPath{std::regex_replace(path, std::regex(":"), "\\:")};
std::unique_ptr<MegaNode> fromPath(
megaApi[0]->getNodeByPathOfType(escapedPath.c_str(), nullptr, node->getType()));
ASSERT_NE(fromPath, nullptr) << "Failed to retrieve node by path and type.";
EXPECT_EQ(fromPath->getHandle(), handle) << escapedPath;
}
}
|