2016-03-14 10:11:38 -04:00
|
|
|
#include "engine/base64.hpp"
|
2016-03-15 09:00:32 -04:00
|
|
|
#include "mocks/mock_datafacade.hpp"
|
2016-05-27 15:05:04 -04:00
|
|
|
#include "engine/hint.hpp"
|
2016-03-14 10:11:38 -04:00
|
|
|
|
|
|
|
#include <boost/test/test_case_template.hpp>
|
2016-05-27 15:05:04 -04:00
|
|
|
#include <boost/test/unit_test.hpp>
|
2016-03-14 10:11:38 -04:00
|
|
|
|
2016-03-15 09:00:32 -04:00
|
|
|
#include <algorithm>
|
2016-05-27 15:05:04 -04:00
|
|
|
#include <iostream>
|
2016-03-15 09:00:32 -04:00
|
|
|
|
2016-03-14 10:25:36 -04:00
|
|
|
// RFC 4648 "The Base16, Base32, and Base64 Data Encodings"
|
2016-03-14 10:11:38 -04:00
|
|
|
BOOST_AUTO_TEST_SUITE(base64)
|
|
|
|
|
2016-03-14 10:25:36 -04:00
|
|
|
// For test vectors see section 10: https://tools.ietf.org/html/rfc4648#section-10
|
|
|
|
BOOST_AUTO_TEST_CASE(rfc4648_test_vectors)
|
|
|
|
{
|
|
|
|
using namespace osrm::engine;
|
|
|
|
|
|
|
|
BOOST_CHECK_EQUAL(encodeBase64("f"), "Zg==");
|
|
|
|
BOOST_CHECK_EQUAL(encodeBase64("fo"), "Zm8=");
|
|
|
|
BOOST_CHECK_EQUAL(encodeBase64("foo"), "Zm9v");
|
|
|
|
BOOST_CHECK_EQUAL(encodeBase64("foob"), "Zm9vYg==");
|
|
|
|
BOOST_CHECK_EQUAL(encodeBase64("fooba"), "Zm9vYmE=");
|
|
|
|
BOOST_CHECK_EQUAL(encodeBase64("foobar"), "Zm9vYmFy");
|
|
|
|
}
|
|
|
|
|
2016-03-15 09:00:32 -04:00
|
|
|
BOOST_AUTO_TEST_CASE(rfc4648_test_vectors_roundtrip)
|
|
|
|
{
|
|
|
|
using namespace osrm::engine;
|
|
|
|
|
|
|
|
BOOST_CHECK_EQUAL(decodeBase64(encodeBase64("f")), "f");
|
|
|
|
BOOST_CHECK_EQUAL(decodeBase64(encodeBase64("fo")), "fo");
|
|
|
|
BOOST_CHECK_EQUAL(decodeBase64(encodeBase64("foo")), "foo");
|
|
|
|
BOOST_CHECK_EQUAL(decodeBase64(encodeBase64("foob")), "foob");
|
|
|
|
BOOST_CHECK_EQUAL(decodeBase64(encodeBase64("fooba")), "fooba");
|
|
|
|
BOOST_CHECK_EQUAL(decodeBase64(encodeBase64("foobar")), "foobar");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(hint_encoding_decoding_roundtrip)
|
|
|
|
{
|
|
|
|
using namespace osrm::engine;
|
2016-03-15 09:14:22 -04:00
|
|
|
using namespace osrm::util;
|
2016-03-15 09:00:32 -04:00
|
|
|
|
2016-03-15 09:14:22 -04:00
|
|
|
const Coordinate coordinate;
|
|
|
|
const PhantomNode phantom;
|
2017-04-05 11:54:18 -04:00
|
|
|
const osrm::test::MockDataFacade<osrm::engine::routing_algorithms::ch::Algorithm> facade{};
|
2016-03-15 09:00:32 -04:00
|
|
|
|
2016-03-20 13:03:15 -04:00
|
|
|
const Hint hint{phantom, facade.GetCheckSum()};
|
2016-03-15 09:00:32 -04:00
|
|
|
|
|
|
|
const auto base64 = hint.ToBase64();
|
|
|
|
|
|
|
|
BOOST_CHECK(0 == std::count(begin(base64), end(base64), '+'));
|
|
|
|
BOOST_CHECK(0 == std::count(begin(base64), end(base64), '/'));
|
|
|
|
|
|
|
|
const auto decoded = Hint::FromBase64(base64);
|
|
|
|
|
|
|
|
BOOST_CHECK_EQUAL(hint, decoded);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(hint_encoding_decoding_roundtrip_bytewise)
|
|
|
|
{
|
|
|
|
using namespace osrm::engine;
|
2016-03-15 09:14:22 -04:00
|
|
|
using namespace osrm::util;
|
2016-03-15 09:00:32 -04:00
|
|
|
|
2016-03-15 09:14:22 -04:00
|
|
|
const Coordinate coordinate;
|
|
|
|
const PhantomNode phantom;
|
2017-04-05 11:54:18 -04:00
|
|
|
const osrm::test::MockDataFacade<osrm::engine::routing_algorithms::ch::Algorithm> facade{};
|
2016-03-15 09:00:32 -04:00
|
|
|
|
2016-03-20 13:03:15 -04:00
|
|
|
const Hint hint{phantom, facade.GetCheckSum()};
|
2016-03-14 10:11:38 -04:00
|
|
|
|
2016-03-15 09:00:32 -04:00
|
|
|
const auto decoded = Hint::FromBase64(hint.ToBase64());
|
|
|
|
|
|
|
|
BOOST_CHECK(std::equal(reinterpret_cast<const unsigned char *>(&hint),
|
|
|
|
reinterpret_cast<const unsigned char *>(&hint) + sizeof(Hint),
|
|
|
|
reinterpret_cast<const unsigned char *>(&decoded)));
|
|
|
|
}
|
2016-03-14 10:11:38 -04:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|