| 1 |
/* |
| 2 |
* Copyright (c) 2011-2022, The DART development contributors |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* The list of contributors can be found at: |
| 6 |
* https://github.com/dartsim/dart/blob/master/LICENSE |
| 7 |
* |
| 8 |
* This file is provided under the following "BSD-style" License: |
| 9 |
* Redistribution and use in source and binary forms, with or |
| 10 |
* without modification, are permitted provided that the following |
| 11 |
* conditions are met: |
| 12 |
* * Redistributions of source code must retain the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer. |
| 14 |
* * Redistributions in binary form must reproduce the above |
| 15 |
* copyright notice, this list of conditions and the following |
| 16 |
* disclaimer in the documentation and/or other materials provided |
| 17 |
* with the distribution. |
| 18 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 19 |
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 20 |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 23 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 26 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 27 |
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 29 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 |
* POSSIBILITY OF SUCH DAMAGE. |
| 31 |
*/ |
| 32 |
|
| 33 |
#include <gtest/gtest.h> |
| 34 |
|
| 35 |
#include "dart/common/LocalResourceRetriever.hpp" |
| 36 |
|
| 37 |
#include "TestHelpers.hpp" |
| 38 |
|
| 39 |
using dart::common::LocalResourceRetriever; |
| 40 |
using dart::common::Resource; |
| 41 |
using dart::common::Uri; |
| 42 |
|
| 43 |
#ifdef _WIN32 |
| 44 |
#define FILE_SCHEME "file:///" |
| 45 |
#else |
| 46 |
#define FILE_SCHEME "file://" |
| 47 |
#endif |
| 48 |
|
| 49 |
TEST(LocalResourceRetriever, exists_UnsupportedUri_ReturnsFalse) |
| 50 |
{ |
| 51 |
LocalResourceRetriever retriever; |
| 52 |
EXPECT_FALSE(retriever.exists("unknown://test")); |
| 53 |
} |
| 54 |
|
| 55 |
TEST(LocalResourceRetriever, exists_FileUriDoesNotExist_ReturnsFalse) |
| 56 |
{ |
| 57 |
LocalResourceRetriever retriever; |
| 58 |
EXPECT_FALSE(retriever.exists(FILE_SCHEME DART_DATA_PATH "does/not/exist")); |
| 59 |
} |
| 60 |
|
| 61 |
TEST(LocalResourceRetriever, exists_PathDoesNotExist_ReturnsFalse) |
| 62 |
{ |
| 63 |
LocalResourceRetriever retriever; |
| 64 |
EXPECT_FALSE(retriever.exists(DART_DATA_PATH "does/not/exist")); |
| 65 |
} |
| 66 |
|
| 67 |
TEST(LocalResourceRetriever, exists_FileUriDoesExists_ReturnsTrue) |
| 68 |
{ |
| 69 |
LocalResourceRetriever retriever; |
| 70 |
EXPECT_TRUE(retriever.exists(FILE_SCHEME DART_DATA_PATH "skel/cube.skel")); |
| 71 |
} |
| 72 |
|
| 73 |
TEST(LocalResourceRetriever, exists_PathDoesExists_ReturnsTrue) |
| 74 |
{ |
| 75 |
LocalResourceRetriever retriever; |
| 76 |
EXPECT_TRUE(retriever.exists(DART_DATA_PATH "skel/cube.skel")); |
| 77 |
} |
| 78 |
|
| 79 |
TEST(LocalResourceRetriever, getFilePath_UnsupportedUri_ReturnsEmptyString) |
| 80 |
{ |
| 81 |
LocalResourceRetriever retriever; |
| 82 |
EXPECT_EQ(retriever.getFilePath("unknown://test"), ""); |
| 83 |
} |
| 84 |
|
| 85 |
TEST(LocalResourceRetriever, getFilePath_FileUriDoesNotExist_ReturnsEmptyString) |
| 86 |
{ |
| 87 |
LocalResourceRetriever retriever; |
| 88 |
EXPECT_EQ( |
| 89 |
retriever.getFilePath(FILE_SCHEME DART_DATA_PATH "does/not/exist"), ""); |
| 90 |
} |
| 91 |
|
| 92 |
TEST(LocalResourceRetriever, getFilePath_PathDoesNotExist_ReturnsEmptyString) |
| 93 |
{ |
| 94 |
LocalResourceRetriever retriever; |
| 95 |
EXPECT_EQ(retriever.getFilePath(DART_DATA_PATH "does/not/exist"), ""); |
| 96 |
} |
| 97 |
|
| 98 |
TEST(LocalResourceRetriever, getFilePath_FileUriDoesExists_ReturnsPath) |
| 99 |
{ |
| 100 |
LocalResourceRetriever retriever; |
| 101 |
EXPECT_EQ( |
| 102 |
retriever.getFilePath(FILE_SCHEME DART_DATA_PATH "skel/cube.skel"), |
| 103 |
DART_DATA_PATH "skel/cube.skel"); |
| 104 |
} |
| 105 |
|
| 106 |
TEST(LocalResourceRetriever, getFilePath_PathDoesExists_ReturnsPath) |
| 107 |
{ |
| 108 |
LocalResourceRetriever retriever; |
| 109 |
EXPECT_EQ( |
| 110 |
retriever.getFilePath(DART_DATA_PATH "skel/cube.skel"), |
| 111 |
DART_DATA_PATH "skel/cube.skel"); |
| 112 |
} |
| 113 |
|
| 114 |
TEST(LocalResourceRetriever, retrieve_UnsupportedUri_ReturnsNull) |
| 115 |
{ |
| 116 |
LocalResourceRetriever retriever; |
| 117 |
EXPECT_EQ(nullptr, retriever.retrieve("unknown://test")); |
| 118 |
} |
| 119 |
|
| 120 |
TEST(LocalResourceRetriever, retrieve_FileUriDoesNotExist_ReturnsNull) |
| 121 |
{ |
| 122 |
LocalResourceRetriever retriever; |
| 123 |
EXPECT_EQ( |
| 124 |
nullptr, retriever.retrieve(FILE_SCHEME DART_DATA_PATH "does/not/exist")); |
| 125 |
} |
| 126 |
|
| 127 |
TEST(LocalResourceRetriever, retrieve_PathDoesNotExist_ReturnsNull) |
| 128 |
{ |
| 129 |
LocalResourceRetriever retriever; |
| 130 |
EXPECT_EQ(nullptr, retriever.retrieve(DART_DATA_PATH "does/not/exist")); |
| 131 |
} |
| 132 |
|
| 133 |
TEST(LocalResourceRetriever, retrieve_FileUri) |
| 134 |
{ |
| 135 |
LocalResourceRetriever retriever; |
| 136 |
auto resource |
| 137 |
= retriever.retrieve(FILE_SCHEME DART_DATA_PATH "test/hello_world.txt"); |
| 138 |
ASSERT_TRUE(resource != nullptr); |
| 139 |
} |
| 140 |
|
| 141 |
TEST(LocalResourceRetriever, retrieve_Path) |
| 142 |
{ |
| 143 |
LocalResourceRetriever retriever; |
| 144 |
auto resource = retriever.retrieve(DART_DATA_PATH "test/hello_world.txt"); |
| 145 |
ASSERT_TRUE(resource != nullptr); |
| 146 |
} |
| 147 |
|
| 148 |
TEST(LocalResourceRetriever, readAll) |
| 149 |
{ |
| 150 |
LocalResourceRetriever retriever; |
| 151 |
auto resource = retriever.retrieve(DART_DATA_PATH "test/hello_world.txt"); |
| 152 |
ASSERT_TRUE(resource != nullptr); |
| 153 |
|
| 154 |
auto content = resource->readAll(); |
| 155 |
ASSERT_TRUE(content == std::string("Hello World")); |
| 156 |
|
| 157 |
ASSERT_TRUE( |
| 158 |
retriever.readAll(DART_DATA_PATH "test/hello_world.txt") |
| 159 |
== std::string("Hello World")); |
| 160 |
} |
| 161 |
|
| 162 |
TEST(LocalResourceRetriever, retrieve_ResourceOperations) |
| 163 |
{ |
| 164 |
const std::string content = "Hello World"; |
| 165 |
|
| 166 |
std::vector<char> buffer(100, '\0'); |
| 167 |
|
| 168 |
LocalResourceRetriever retriever; |
| 169 |
auto resource = retriever.retrieve(DART_DATA_PATH "test/hello_world.txt"); |
| 170 |
ASSERT_TRUE(resource != nullptr); |
| 171 |
|
| 172 |
EXPECT_EQ(content.size(), resource->getSize()); |
| 173 |
|
| 174 |
// Relative seek. |
| 175 |
ASSERT_TRUE(resource->seek(2, Resource::SEEKTYPE_CUR)); |
| 176 |
EXPECT_EQ(2u, resource->tell()); |
| 177 |
|
| 178 |
// Absolute seek. |
| 179 |
ASSERT_TRUE(resource->seek(5, Resource::SEEKTYPE_SET)); |
| 180 |
EXPECT_EQ(5u, resource->tell()); |
| 181 |
|
| 182 |
// Seek to the end of the file. |
| 183 |
ASSERT_TRUE(resource->seek(0, Resource::SEEKTYPE_END)); |
| 184 |
EXPECT_EQ(content.size(), resource->tell()); |
| 185 |
|
| 186 |
// TODO: SEEKTYPE_END should require negative input. |
| 187 |
ASSERT_TRUE(resource->seek(-3, Resource::SEEKTYPE_END)); |
| 188 |
EXPECT_EQ(content.size() - 3, resource->tell()); |
| 189 |
|
| 190 |
// Reading a block that's too large should do nothing. |
| 191 |
ASSERT_TRUE(resource->seek(0, Resource::SEEKTYPE_SET)); |
| 192 |
ASSERT_EQ(0u, resource->read(buffer.data(), content.size() + 1, 1)); |
| 193 |
|
| 194 |
// Reading should only return full blocks. |
| 195 |
buffer.assign(buffer.size(), '\0'); |
| 196 |
ASSERT_TRUE(resource->seek(0, Resource::SEEKTYPE_SET)); |
| 197 |
ASSERT_EQ(1u, resource->read(buffer.data(), 8, 1)); |
| 198 |
EXPECT_STREQ(content.substr(0, 8).c_str(), buffer.data()); |
| 199 |
|
| 200 |
// Reading multiple blocks |
| 201 |
buffer.assign(buffer.size(), '\0'); |
| 202 |
ASSERT_TRUE(resource->seek(0, Resource::SEEKTYPE_SET)); |
| 203 |
ASSERT_EQ(2u, resource->read(buffer.data(), 4, 2)); |
| 204 |
EXPECT_STREQ(content.substr(0, 8).c_str(), buffer.data()); |
| 205 |
|
| 206 |
// Reading the whole file at once. |
| 207 |
buffer.assign(buffer.size(), '\0'); |
| 208 |
ASSERT_TRUE(resource->seek(0, Resource::SEEKTYPE_SET)); |
| 209 |
ASSERT_EQ(1u, resource->read(buffer.data(), content.size(), 1)); |
| 210 |
EXPECT_STREQ(content.c_str(), buffer.data()); |
| 211 |
} |