Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

mapbox: URL containing query string causes infinite loop #5554

Merged
merged 4 commits into from
Jul 4, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/mbgl/util/mapbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ bool isMapboxURL(const std::string& url) {
std::vector<std::string> getMapboxURLPathname(const std::string& url) {
std::vector<std::string> pathname;
std::size_t startIndex = protocol.length();
while (startIndex < url.length()) {
std::size_t end = url.find_first_of("?#");
if (end == std::string::npos) {
end = url.length();
}
while (startIndex < end) {
std::size_t endIndex = url.find("/", startIndex);
if (endIndex == std::string::npos) {
endIndex = url.find_first_of("?#");
}
if (endIndex == std::string::npos) {
endIndex = url.length();
endIndex = end;
}
pathname.push_back(url.substr(startIndex, endIndex - startIndex));
startIndex = endIndex + 1;
Expand Down
65 changes: 48 additions & 17 deletions test/util/mapbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,65 @@

using namespace mbgl;

// TODO: correct all EXPECT_EQ(actual, expected) to EXPECT_EQ(expected, actual)

TEST(Mapbox, SourceURL) {
EXPECT_EQ(mbgl::util::mapbox::normalizeSourceURL("mapbox://user.map", "key"), "https://api.mapbox.com/v4/user.map.json?access_token=key&secure");
EXPECT_EQ(mbgl::util::mapbox::normalizeSourceURL("mapbox://user.map", "token"), "https://api.mapbox.com/v4/user.map.json?access_token=token&secure");
EXPECT_THROW(mbgl::util::mapbox::normalizeSourceURL("mapbox://user.map", ""), std::runtime_error);
EXPECT_EQ(
"https://api.mapbox.com/v4/user.map.json?access_token=key&secure",
mbgl::util::mapbox::normalizeSourceURL("mapbox://user.map", "key"));
EXPECT_EQ(
"http://path",
mbgl::util::mapbox::normalizeSourceURL("http://path", "key");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is missing a closing parenthesis.

EXPECT_THROW(
std::runtime_error,
mbgl::util::mapbox::normalizeSourceURL("mapbox://user.map", ""));
Copy link
Contributor

@1ec5 1ec5 Jul 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike EXPECT_EQ, the order of the parameters in EXPECT_THROW matters: the error type needs to be in the second parameter, not the first. With this change, the tests fail to compile.

}

TEST(Mapbox, GlyphsURL) {
EXPECT_EQ(mbgl::util::mapbox::normalizeGlyphsURL("mapbox://fonts/boxmap/Comic%20Sans/0-255.pbf", "key"), "https://api.mapbox.com/fonts/v1/boxmap/Comic%20Sans/0-255.pbf?access_token=key");
EXPECT_EQ(mbgl::util::mapbox::normalizeGlyphsURL("mapbox://fonts/boxmap/{fontstack}/{range}.pbf", "key"), "https://api.mapbox.com/fonts/v1/boxmap/{fontstack}/{range}.pbf?access_token=key");
EXPECT_EQ(mbgl::util::mapbox::normalizeGlyphsURL("http://path", "key"), "http://path");
EXPECT_EQ(mbgl::util::mapbox::normalizeGlyphsURL("mapbox://path", "key"), "mapbox://path");
EXPECT_EQ(
"https://api.mapbox.com/fonts/v1/boxmap/Comic%20Sans/0-255.pbf?access_token=key",
mbgl::util::mapbox::normalizeGlyphsURL("mapbox://fonts/boxmap/Comic%20Sans/0-255.pbf", "key"));
EXPECT_EQ(
"https://api.mapbox.com/fonts/v1/boxmap/{fontstack}/{range}.pbf?access_token=key",
mbgl::util::mapbox::normalizeGlyphsURL("mapbox://fonts/boxmap/{fontstack}/{range}.pbf", "key"));
EXPECT_EQ(
"http://path",
mbgl::util::mapbox::normalizeGlyphsURL("http://path", "key"));
EXPECT_EQ(
"mapbox://path",
mbgl::util::mapbox::normalizeGlyphsURL("mapbox://path", "key"));
}

TEST(Mapbox, StyleURL) {
EXPECT_EQ(mbgl::util::mapbox::normalizeStyleURL("mapbox://foo", "key"), "mapbox://foo");
EXPECT_EQ(mbgl::util::mapbox::normalizeStyleURL("mapbox://styles/user/style", "key"), "https://api.mapbox.com/styles/v1/user/style?access_token=key");
EXPECT_EQ(mbgl::util::mapbox::normalizeStyleURL("mapbox://styles/user/style/draft", "key"), "https://api.mapbox.com/styles/v1/user/style/draft?access_token=key");
EXPECT_EQ(mbgl::util::mapbox::normalizeStyleURL("http://path", "key"), "http://path");
EXPECT_EQ(
"mapbox://foo",
mbgl::util::mapbox::normalizeStyleURL("mapbox://foo", "key"));
EXPECT_EQ(
"https://api.mapbox.com/styles/v1/user/style?access_token=key",
mbgl::util::mapbox::normalizeStyleURL("mapbox://styles/user/style", "key"));
EXPECT_EQ(
"https://api.mapbox.com/styles/v1/user/style/draft?access_token=key",
mbgl::util::mapbox::normalizeStyleURL("mapbox://styles/user/style/draft", "key"));
EXPECT_EQ(
"http://path",
mbgl::util::mapbox::normalizeStyleURL("http://path", "key"));
}

TEST(Mapbox, SpriteURL) {
EXPECT_EQ(mbgl::util::mapbox::normalizeSpriteURL("map/box/sprites@2x.json", "key"), "map/box/sprites@2x.json");
EXPECT_EQ(mbgl::util::mapbox::normalizeSpriteURL("mapbox://foo", "key"), "mapbox://foo");
EXPECT_EQ(mbgl::util::mapbox::normalizeSpriteURL("mapbox://sprites/mapbox/streets-v8.json", "key"), "https://api.mapbox.com/styles/v1/mapbox/streets-v8/sprite.json?access_token=key");
EXPECT_EQ(mbgl::util::mapbox::normalizeSpriteURL("mapbox://sprites/mapbox/streets-v8@2x.png", "key"), "https://api.mapbox.com/styles/v1/mapbox/streets-v8/sprite@2x.png?access_token=key");
EXPECT_EQ(mbgl::util::mapbox::normalizeSpriteURL("mapbox://sprites/mapbox/streets-v8/draft@2x.png", "key"), "https://api.mapbox.com/styles/v1/mapbox/streets-v8/draft/sprite@2x.png?access_token=key");
EXPECT_EQ(
"map/box/sprites@2x.json",
mbgl::util::mapbox::normalizeSpriteURL("map/box/sprites@2x.json", "key"));
EXPECT_EQ(
"mapbox://foo",
mbgl::util::mapbox::normalizeSpriteURL("mapbox://foo", "key"));
EXPECT_EQ(
"https://api.mapbox.com/styles/v1/mapbox/streets-v8/sprite.json?access_token=key",
mbgl::util::mapbox::normalizeSpriteURL("mapbox://sprites/mapbox/streets-v8.json", "key"));
EXPECT_EQ(
"https://api.mapbox.com/styles/v1/mapbox/streets-v8/sprite@2x.png?access_token=key",
mbgl::util::mapbox::normalizeSpriteURL("mapbox://sprites/mapbox/streets-v8@2x.png", "key"));
EXPECT_EQ(
"https://api.mapbox.com/styles/v1/mapbox/streets-v8/draft/sprite@2x.png?access_token=key",
mbgl::util::mapbox::normalizeSpriteURL("mapbox://sprites/mapbox/streets-v8/draft@2x.png", "key"));
}

TEST(Mapbox, TileURL) {
Expand Down