0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/cctest/test_url.cc
Timothy Gu e96ca62480
src: avoid dereference without existence check
Currently the URL API is only used from the JS binding, which always
initializes `base` regardless of `has_base`. Therefore, there is no
actual security risk right now, but would be had we made other C++ parts
of Node.js use this API.

An earlier version of this patch was created by Bradley Farias
<bradley.meck@gmail.com>.

PR-URL: https://github.com/nodejs/node/pull/14591
Refs: https://github.com/nodejs/node/pull/14369#discussion_r128767221
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
2017-08-06 15:10:59 +08:00

82 lines
2.1 KiB
C++

#include "node_url.h"
#include "node_i18n.h"
#include "gtest/gtest.h"
using node::url::URL;
using node::url::URL_FLAGS_FAILED;
class URLTest : public ::testing::Test {
protected:
void SetUp() override {
#if defined(NODE_HAVE_I18N_SUPPORT)
std::string icu_data_dir;
node::i18n::InitializeICUDirectory(icu_data_dir);
#endif
}
void TearDown() override {}
};
TEST_F(URLTest, Simple) {
URL simple("https://example.org:81/a/b/c?query#fragment");
EXPECT_FALSE(simple.flags() & URL_FLAGS_FAILED);
EXPECT_EQ(simple.protocol(), "https:");
EXPECT_EQ(simple.host(), "example.org");
EXPECT_EQ(simple.port(), 81);
EXPECT_EQ(simple.path(), "/a/b/c");
EXPECT_EQ(simple.query(), "query");
EXPECT_EQ(simple.fragment(), "fragment");
}
TEST_F(URLTest, Simple2) {
const char* input = "https://example.org:81/a/b/c?query#fragment";
URL simple(input, strlen(input));
EXPECT_FALSE(simple.flags() & URL_FLAGS_FAILED);
EXPECT_EQ(simple.protocol(), "https:");
EXPECT_EQ(simple.host(), "example.org");
EXPECT_EQ(simple.port(), 81);
EXPECT_EQ(simple.path(), "/a/b/c");
EXPECT_EQ(simple.query(), "query");
EXPECT_EQ(simple.fragment(), "fragment");
}
TEST_F(URLTest, NoBase1) {
URL error("123noscheme");
EXPECT_TRUE(error.flags() & URL_FLAGS_FAILED);
}
TEST_F(URLTest, Base1) {
URL base("http://example.org/foo/bar");
ASSERT_FALSE(base.flags() & URL_FLAGS_FAILED);
URL simple("../baz", &base);
EXPECT_FALSE(simple.flags() & URL_FLAGS_FAILED);
EXPECT_EQ(simple.protocol(), "http:");
EXPECT_EQ(simple.host(), "example.org");
EXPECT_EQ(simple.path(), "/baz");
}
TEST_F(URLTest, Base2) {
URL simple("../baz", "http://example.org/foo/bar");
EXPECT_FALSE(simple.flags() & URL_FLAGS_FAILED);
EXPECT_EQ(simple.protocol(), "http:");
EXPECT_EQ(simple.host(), "example.org");
EXPECT_EQ(simple.path(), "/baz");
}
TEST_F(URLTest, Base3) {
const char* input = "../baz";
const char* base = "http://example.org/foo/bar";
URL simple(input, strlen(input), base, strlen(base));
EXPECT_FALSE(simple.flags() & URL_FLAGS_FAILED);
EXPECT_EQ(simple.protocol(), "http:");
EXPECT_EQ(simple.host(), "example.org");
EXPECT_EQ(simple.path(), "/baz");
}