2016-11-15 03:08:45 -05:00
|
|
|
#include "storage/io.hpp"
|
2017-04-04 18:00:17 -04:00
|
|
|
#include "storage/serialization.hpp"
|
2016-11-30 22:08:01 -05:00
|
|
|
#include "util/exception.hpp"
|
2017-03-04 05:52:40 -05:00
|
|
|
#include "util/fingerprint.hpp"
|
2016-01-07 04:33:47 -05:00
|
|
|
#include "util/typedefs.hpp"
|
2017-01-06 16:45:08 -05:00
|
|
|
#include "util/version.hpp"
|
2016-01-07 04:33:47 -05:00
|
|
|
|
|
|
|
#include <boost/test/test_case_template.hpp>
|
2016-05-27 15:05:04 -04:00
|
|
|
#include <boost/test/unit_test.hpp>
|
2016-01-07 04:33:47 -05:00
|
|
|
|
2016-11-30 22:08:01 -05:00
|
|
|
#include <exception>
|
|
|
|
#include <numeric>
|
2016-01-07 04:33:47 -05:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
const static std::string IO_TMP_FILE = "test_io.tmp";
|
2016-11-30 22:08:01 -05:00
|
|
|
const static std::string IO_NONEXISTENT_FILE = "non_existent_test_io.tmp";
|
|
|
|
const static std::string IO_TOO_SMALL_FILE = "file_too_small_test_io.tmp";
|
|
|
|
const static std::string IO_CORRUPT_FINGERPRINT_FILE = "corrupt_fingerprint_file_test_io.tmp";
|
2017-01-06 16:45:08 -05:00
|
|
|
const static std::string IO_INCOMPATIBLE_FINGERPRINT_FILE =
|
|
|
|
"incompatible_fingerprint_file_test_io.tmp";
|
2016-11-30 22:08:01 -05:00
|
|
|
const static std::string IO_TEXT_FILE = "plain_text_file.tmp";
|
2016-01-07 04:33:47 -05:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(osrm_io)
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(io_data)
|
|
|
|
{
|
2016-11-30 22:08:01 -05:00
|
|
|
std::vector<int> data_in(53), data_out;
|
|
|
|
std::iota(begin(data_in), end(data_in), 0);
|
2016-01-07 04:33:47 -05:00
|
|
|
|
2017-03-04 05:52:40 -05:00
|
|
|
{
|
|
|
|
osrm::storage::io::FileWriter outfile(IO_TMP_FILE,
|
|
|
|
osrm::storage::io::FileWriter::GenerateFingerprint);
|
2017-04-04 18:00:17 -04:00
|
|
|
osrm::storage::serialization::write(outfile, data_in);
|
2017-03-04 05:52:40 -05:00
|
|
|
}
|
2016-11-15 03:08:45 -05:00
|
|
|
|
2016-11-15 17:49:28 -05:00
|
|
|
osrm::storage::io::FileReader infile(IO_TMP_FILE,
|
|
|
|
osrm::storage::io::FileReader::VerifyFingerprint);
|
2017-04-04 18:00:17 -04:00
|
|
|
osrm::storage::serialization::read(infile, data_out);
|
2016-01-07 04:33:47 -05:00
|
|
|
|
|
|
|
BOOST_REQUIRE_EQUAL(data_in.size(), data_out.size());
|
2016-03-03 08:26:13 -05:00
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(data_out.begin(), data_out.end(), data_in.begin(), data_in.end());
|
2016-01-07 04:33:47 -05:00
|
|
|
}
|
|
|
|
|
2016-11-30 22:08:01 -05:00
|
|
|
BOOST_AUTO_TEST_CASE(io_nonexistent_file)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
osrm::storage::io::FileReader infile(IO_NONEXISTENT_FILE,
|
|
|
|
osrm::storage::io::FileReader::VerifyFingerprint);
|
|
|
|
BOOST_REQUIRE_MESSAGE(false, "Should not get here");
|
|
|
|
}
|
2017-05-27 00:16:20 -04:00
|
|
|
catch (const osrm::util::RuntimeError &e)
|
2016-11-30 22:08:01 -05:00
|
|
|
{
|
2017-05-27 00:16:20 -04:00
|
|
|
const std::string expected("Problem opening file: " + IO_NONEXISTENT_FILE);
|
2016-12-06 15:30:46 -05:00
|
|
|
const std::string got(e.what());
|
|
|
|
BOOST_REQUIRE(std::equal(expected.begin(), expected.end(), got.begin()));
|
2017-05-27 00:16:20 -04:00
|
|
|
BOOST_REQUIRE(e.GetCode() == osrm::ErrorCode::FileOpenError);
|
2016-11-30 22:08:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(file_too_small)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::vector<int> v(53);
|
|
|
|
std::iota(begin(v), end(v), 0);
|
|
|
|
|
2017-03-04 05:52:40 -05:00
|
|
|
{
|
|
|
|
osrm::storage::io::FileWriter outfile(
|
|
|
|
IO_TOO_SMALL_FILE, osrm::storage::io::FileWriter::GenerateFingerprint);
|
2017-04-04 18:00:17 -04:00
|
|
|
osrm::storage::serialization::write(outfile, v);
|
2017-03-04 05:52:40 -05:00
|
|
|
}
|
2016-11-30 22:08:01 -05:00
|
|
|
|
2017-01-06 16:45:08 -05:00
|
|
|
std::fstream f(IO_TOO_SMALL_FILE);
|
|
|
|
f.seekp(sizeof(osrm::util::FingerPrint), std::ios_base::beg);
|
|
|
|
std::uint64_t badcount = 100;
|
|
|
|
f.write(reinterpret_cast<char *>(&badcount), sizeof(badcount));
|
2016-11-30 22:08:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
osrm::storage::io::FileReader infile(IO_TOO_SMALL_FILE,
|
|
|
|
osrm::storage::io::FileReader::VerifyFingerprint);
|
2017-01-06 16:45:08 -05:00
|
|
|
std::vector<int> buffer;
|
2017-04-04 18:00:17 -04:00
|
|
|
osrm::storage::serialization::read(infile, buffer);
|
2016-11-30 22:08:01 -05:00
|
|
|
BOOST_REQUIRE_MESSAGE(false, "Should not get here");
|
|
|
|
}
|
2017-05-27 00:16:20 -04:00
|
|
|
catch (const osrm::util::RuntimeError &e)
|
2016-11-30 22:08:01 -05:00
|
|
|
{
|
2017-05-27 00:16:20 -04:00
|
|
|
const std::string expected("Unexpected end of file: " + IO_TOO_SMALL_FILE);
|
2016-12-06 15:30:46 -05:00
|
|
|
const std::string got(e.what());
|
|
|
|
BOOST_REQUIRE(std::equal(expected.begin(), expected.end(), got.begin()));
|
2017-05-27 00:16:20 -04:00
|
|
|
BOOST_REQUIRE(e.GetCode() == osrm::ErrorCode::UnexpectedEndOfFile);
|
2016-11-30 22:08:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(io_corrupt_fingerprint)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::vector<int> v(153);
|
|
|
|
std::iota(begin(v), end(v), 0);
|
|
|
|
|
2017-03-04 05:52:40 -05:00
|
|
|
osrm::storage::io::FileWriter outfile(IO_CORRUPT_FINGERPRINT_FILE,
|
|
|
|
osrm::storage::io::FileWriter::HasNoFingerprint);
|
|
|
|
|
|
|
|
outfile.WriteOne(0xDEADBEEFCAFEFACE);
|
2017-04-04 18:00:17 -04:00
|
|
|
osrm::storage::serialization::write(outfile, v);
|
2016-11-30 22:08:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
osrm::storage::io::FileReader infile(IO_CORRUPT_FINGERPRINT_FILE,
|
|
|
|
osrm::storage::io::FileReader::VerifyFingerprint);
|
|
|
|
BOOST_REQUIRE_MESSAGE(false, "Should not get here");
|
|
|
|
}
|
2017-05-27 00:16:20 -04:00
|
|
|
catch (const osrm::util::RuntimeError &e)
|
2016-11-30 22:08:01 -05:00
|
|
|
{
|
2017-05-27 00:16:20 -04:00
|
|
|
const std::string expected("Fingerprint did not match the expected value: " +
|
|
|
|
IO_CORRUPT_FINGERPRINT_FILE);
|
2016-12-06 15:30:46 -05:00
|
|
|
const std::string got(e.what());
|
|
|
|
BOOST_REQUIRE(std::equal(expected.begin(), expected.end(), got.begin()));
|
2017-05-27 00:16:20 -04:00
|
|
|
BOOST_REQUIRE(e.GetCode() == osrm::ErrorCode::InvalidFingerprint);
|
2016-11-30 22:08:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-06 16:45:08 -05:00
|
|
|
BOOST_AUTO_TEST_CASE(io_incompatible_fingerprint)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::vector<int> v(153);
|
|
|
|
std::iota(begin(v), end(v), 0);
|
2017-03-04 05:52:40 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
osrm::storage::io::FileWriter outfile(IO_INCOMPATIBLE_FINGERPRINT_FILE,
|
|
|
|
osrm::storage::io::FileWriter::HasNoFingerprint);
|
|
|
|
|
|
|
|
const auto fingerprint = osrm::util::FingerPrint::GetValid();
|
|
|
|
outfile.WriteOne(fingerprint);
|
2017-04-04 18:00:17 -04:00
|
|
|
osrm::storage::serialization::write(outfile, v);
|
2017-03-04 05:52:40 -05:00
|
|
|
}
|
2017-01-06 16:45:08 -05:00
|
|
|
|
|
|
|
std::fstream f(IO_INCOMPATIBLE_FINGERPRINT_FILE);
|
|
|
|
f.seekp(5, std::ios_base::beg); // Seek past `OSRN` and Major version byte
|
|
|
|
std::uint8_t incompatibleminor = static_cast<std::uint8_t>(OSRM_VERSION_MAJOR) + 1;
|
|
|
|
f.write(reinterpret_cast<char *>(&incompatibleminor), sizeof(incompatibleminor));
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
osrm::storage::io::FileReader infile(IO_INCOMPATIBLE_FINGERPRINT_FILE,
|
|
|
|
osrm::storage::io::FileReader::VerifyFingerprint);
|
|
|
|
BOOST_REQUIRE_MESSAGE(false, "Should not get here");
|
|
|
|
}
|
2017-05-27 00:16:20 -04:00
|
|
|
catch (const osrm::util::RuntimeError &e)
|
2017-01-06 16:45:08 -05:00
|
|
|
{
|
2017-05-27 00:16:20 -04:00
|
|
|
const std::string expected("Fingerprint did not match the expected value: " +
|
|
|
|
IO_INCOMPATIBLE_FINGERPRINT_FILE);
|
2017-01-06 16:45:08 -05:00
|
|
|
const std::string got(e.what());
|
|
|
|
BOOST_REQUIRE(std::equal(expected.begin(), expected.end(), got.begin()));
|
2017-05-27 00:16:20 -04:00
|
|
|
BOOST_REQUIRE(e.GetCode() == osrm::ErrorCode::InvalidFingerprint);
|
2017-01-06 16:45:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-07 04:33:47 -05:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|