Skip to content

Commit

Permalink
Clean up URL test string conversions.
Browse files Browse the repository at this point in the history
Originally the url test utils had UTF string conversion functions because we
didn't want the library to depend on STL (for embedders). We've not had this
limitation for a long time so the UTF16 <--> UTF8 conversion functions can be
deleted in favor of the ones in base.

The one remaining function which generates possibly-invalid UTF16 (by design)
is renamed and clarified for this purpose.

Review-Url: https://codereview.chromium.org/2469133002
Cr-Commit-Position: refs/heads/master@{#429688}
  • Loading branch information
brettw authored and Commit bot committed Nov 3, 2016
1 parent df00084 commit 1b8582f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 64 deletions.
21 changes: 11 additions & 10 deletions url/gurl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
#include <stddef.h>

#include "base/macros.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/url_canon.h"
#include "url/url_test_utils.h"

namespace url {

using test_utils::WStringToUTF16;
using test_utils::ConvertUTF8ToUTF16;

namespace {

template<typename CHAR>
Expand Down Expand Up @@ -67,11 +65,11 @@ TEST(GURLTest, Types) {
// the parser is already tested and works, so we are mostly interested if the
// object does the right thing with the results.
TEST(GURLTest, Components) {
GURL empty_url(WStringToUTF16(L""));
GURL empty_url(base::UTF8ToUTF16(""));
EXPECT_TRUE(empty_url.is_empty());
EXPECT_FALSE(empty_url.is_valid());

GURL url(WStringToUTF16(L"http://user:pass@google.com:99/foo;bar?q=a#ref"));
GURL url(base::UTF8ToUTF16("http://user:pass@google.com:99/foo;bar?q=a#ref"));
EXPECT_FALSE(url.is_empty());
EXPECT_TRUE(url.is_valid());
EXPECT_TRUE(url.SchemeIs("http"));
Expand Down Expand Up @@ -116,7 +114,8 @@ TEST(GURLTest, Empty) {
}

TEST(GURLTest, Copy) {
GURL url(WStringToUTF16(L"http://user:pass@google.com:99/foo;bar?q=a#ref"));
GURL url(base::UTF8ToUTF16(
"http://user:pass@google.com:99/foo;bar?q=a#ref"));

GURL url2(url);
EXPECT_TRUE(url2.is_valid());
Expand Down Expand Up @@ -149,7 +148,8 @@ TEST(GURLTest, Copy) {
}

TEST(GURLTest, Assign) {
GURL url(WStringToUTF16(L"http://user:pass@google.com:99/foo;bar?q=a#ref"));
GURL url(base::UTF8ToUTF16(
"http://user:pass@google.com:99/foo;bar?q=a#ref"));

GURL url2;
url2 = url;
Expand Down Expand Up @@ -191,7 +191,8 @@ TEST(GURLTest, SelfAssign) {
}

TEST(GURLTest, CopyFileSystem) {
GURL url(WStringToUTF16(L"filesystem:https://user:pass@google.com:99/t/foo;bar?q=a#ref"));
GURL url(base::UTF8ToUTF16(
"filesystem:https://user:pass@google.com:99/t/foo;bar?q=a#ref"));

GURL url2(url);
EXPECT_TRUE(url2.is_valid());
Expand Down Expand Up @@ -313,9 +314,9 @@ TEST(GURLTest, Resolve) {
EXPECT_EQ(output.SchemeIsFileSystem(), output.inner_url() != NULL);

// Wide code path.
GURL inputw(ConvertUTF8ToUTF16(resolve_cases[i].base));
GURL inputw(base::UTF8ToUTF16(resolve_cases[i].base));
GURL outputw =
input.Resolve(ConvertUTF8ToUTF16(resolve_cases[i].relative));
input.Resolve(base::UTF8ToUTF16(resolve_cases[i].relative));
EXPECT_EQ(resolve_cases[i].expected_valid, outputw.is_valid()) << i;
EXPECT_EQ(resolve_cases[i].expected, outputw.spec()) << i;
EXPECT_EQ(outputw.SchemeIsFileSystem(), outputw.inner_url() != NULL);
Expand Down
8 changes: 4 additions & 4 deletions url/url_canon_icu_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

namespace url {

using test_utils::WStringToUTF16;

namespace {

// Wrapper around a UConverter object that managers creation and destruction.
Expand Down Expand Up @@ -64,7 +62,8 @@ TEST(URLCanonIcuTest, ICUCharsetConverter) {
std::string str;
StdStringCanonOutput output(&str);

base::string16 input_str(WStringToUTF16(icu_cases[i].input));
base::string16 input_str(
test_utils::TruncateWStringToUTF16(icu_cases[i].input));
int input_len = static_cast<int>(input_str.length());
converter.ConvertFromUTF16(input_str.c_str(), input_len, &output);
output.Complete();
Expand Down Expand Up @@ -134,7 +133,8 @@ TEST(URLCanonIcuTest, QueryWithConverter) {
}

if (query_cases[i].input16) {
base::string16 input16(WStringToUTF16(query_cases[i].input16));
base::string16 input16(
test_utils::TruncateWStringToUTF16(query_cases[i].input16));
int len = static_cast<int>(input16.length());
Component in_comp(0, len);
std::string out_str;
Expand Down
56 changes: 32 additions & 24 deletions url/url_canon_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <stddef.h>

#include "base/macros.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/third_party/mozilla/url_parse.h"
#include "url/url_canon.h"
Expand All @@ -15,10 +16,6 @@

namespace url {

using test_utils::WStringToUTF16;
using test_utils::ConvertUTF8ToUTF16;
using test_utils::ConvertUTF16ToUTF8;

namespace {

struct ComponentCase {
Expand Down Expand Up @@ -195,7 +192,8 @@ TEST(URLCanonTest, UTF) {
out_str.clear();
StdStringCanonOutput output(&out_str);

base::string16 input_str(WStringToUTF16(utf_cases[i].input16));
base::string16 input_str(
test_utils::TruncateWStringToUTF16(utf_cases[i].input16));
int input_len = static_cast<int>(input_str.length());
bool success = true;
for (int ch = 0; ch < input_len; ch++) {
Expand All @@ -213,11 +211,12 @@ TEST(URLCanonTest, UTF) {

// UTF-16 -> UTF-8
std::string input8_str(utf_cases[i].input8);
base::string16 input16_str(WStringToUTF16(utf_cases[i].input16));
EXPECT_EQ(input8_str, ConvertUTF16ToUTF8(input16_str));
base::string16 input16_str(
test_utils::TruncateWStringToUTF16(utf_cases[i].input16));
EXPECT_EQ(input8_str, base::UTF16ToUTF8(input16_str));

// UTF-8 -> UTF-16
EXPECT_EQ(input16_str, ConvertUTF8ToUTF16(input8_str));
EXPECT_EQ(input16_str, base::UTF8ToUTF16(input8_str));
}
}
}
Expand Down Expand Up @@ -265,7 +264,7 @@ TEST(URLCanonTest, Scheme) {
out_str.clear();
StdStringCanonOutput output2(&out_str);

base::string16 wide_input(ConvertUTF8ToUTF16(scheme_cases[i].input));
base::string16 wide_input(base::UTF8ToUTF16(scheme_cases[i].input));
in_comp.len = static_cast<int>(wide_input.length());
success = CanonicalizeScheme(wide_input.c_str(), in_comp, &output2,
&out_comp);
Expand Down Expand Up @@ -530,7 +529,8 @@ TEST(URLCanonTest, Host) {

// Wide version.
if (host_cases[i].input16) {
base::string16 input16(WStringToUTF16(host_cases[i].input16));
base::string16 input16(
test_utils::TruncateWStringToUTF16(host_cases[i].input16));
int host_len = static_cast<int>(input16.length());
Component in_comp(0, host_len);
Component out_comp;
Expand Down Expand Up @@ -580,7 +580,8 @@ TEST(URLCanonTest, Host) {

// Wide version.
if (host_cases[i].input16) {
base::string16 input16(WStringToUTF16(host_cases[i].input16));
base::string16 input16(
test_utils::TruncateWStringToUTF16(host_cases[i].input16));
int host_len = static_cast<int>(input16.length());
Component in_comp(0, host_len);

Expand Down Expand Up @@ -702,7 +703,8 @@ TEST(URLCanonTest, IPv4) {
}

// 16-bit version.
base::string16 input16(WStringToUTF16(cases[i].input16));
base::string16 input16(
test_utils::TruncateWStringToUTF16(cases[i].input16));
component = Component(0, static_cast<int>(input16.length()));

std::string out_str2;
Expand Down Expand Up @@ -854,7 +856,8 @@ TEST(URLCanonTest, IPv6) {
}

// 16-bit version.
base::string16 input16(WStringToUTF16(cases[i].input16));
base::string16 input16(
test_utils::TruncateWStringToUTF16(cases[i].input16));
component = Component(0, static_cast<int>(input16.length()));

std::string out_str2;
Expand Down Expand Up @@ -906,7 +909,8 @@ TEST(URLCanonTest, CanonicalizeHostSubstring) {
std::string out_str;
StdStringCanonOutput output(&out_str);
EXPECT_FALSE(CanonicalizeHostSubstring(
WStringToUTF16(L"\xfdd0zyx.com").c_str(), Component(0, 8), &output));
test_utils::TruncateWStringToUTF16(L"\xfdd0zyx.com").c_str(),
Component(0, 8), &output));
output.Complete();
EXPECT_EQ("%EF%BF%BDzyx.com", out_str);
}
Expand Down Expand Up @@ -984,7 +988,7 @@ TEST(URLCanonTest, UserInfo) {
// Now try the wide version
out_str.clear();
StdStringCanonOutput output2(&out_str);
base::string16 wide_input(ConvertUTF8ToUTF16(user_info_cases[i].input));
base::string16 wide_input(base::UTF8ToUTF16(user_info_cases[i].input));
success = CanonicalizeUserInfo(wide_input.c_str(),
parsed.username,
wide_input.c_str(),
Expand Down Expand Up @@ -1047,7 +1051,7 @@ TEST(URLCanonTest, Port) {
// Now try the wide version
out_str.clear();
StdStringCanonOutput output2(&out_str);
base::string16 wide_input(ConvertUTF8ToUTF16(port_cases[i].input));
base::string16 wide_input(base::UTF8ToUTF16(port_cases[i].input));
success = CanonicalizePort(wide_input.c_str(),
in_comp,
port_cases[i].default_port,
Expand Down Expand Up @@ -1167,7 +1171,8 @@ TEST(URLCanonTest, Path) {
}

if (path_cases[i].input16) {
base::string16 input16(WStringToUTF16(path_cases[i].input16));
base::string16 input16(
test_utils::TruncateWStringToUTF16(path_cases[i].input16));
int len = static_cast<int>(input16.length());
Component in_comp(0, len);
Component out_comp;
Expand Down Expand Up @@ -1242,7 +1247,8 @@ TEST(URLCanonTest, Query) {
}

if (query_cases[i].input16) {
base::string16 input16(WStringToUTF16(query_cases[i].input16));
base::string16 input16(
test_utils::TruncateWStringToUTF16(query_cases[i].input16));
int len = static_cast<int>(input16.length());
Component in_comp(0, len);
std::string out_str;
Expand Down Expand Up @@ -1304,7 +1310,8 @@ TEST(URLCanonTest, Ref) {

// 16-bit input
if (ref_cases[i].input16) {
base::string16 input16(WStringToUTF16(ref_cases[i].input16));
base::string16 input16(
test_utils::TruncateWStringToUTF16(ref_cases[i].input16));
int len = static_cast<int>(input16.length());
Component in_comp(0, len);
Component out_comp;
Expand Down Expand Up @@ -1940,12 +1947,12 @@ TEST(URLCanonTest, _itow_s) {
const base::char16 fill_char = 0xffff;
memset(buf, fill_mem, sizeof(buf));
EXPECT_EQ(0, _itow_s(12, buf, sizeof(buf) / 2 - 1, 10));
EXPECT_EQ(WStringToUTF16(L"12"), base::string16(buf));
EXPECT_EQ(base::UTF8ToUTF16("12"), base::string16(buf));
EXPECT_EQ(fill_char, buf[3]);

// Test the edge cases - exactly the buffer size and one over
EXPECT_EQ(0, _itow_s(1234, buf, sizeof(buf) / 2 - 1, 10));
EXPECT_EQ(WStringToUTF16(L"1234"), base::string16(buf));
EXPECT_EQ(base::UTF8ToUTF16("1234"), base::string16(buf));
EXPECT_EQ(fill_char, buf[5]);

memset(buf, fill_mem, sizeof(buf));
Expand All @@ -1955,12 +1962,13 @@ TEST(URLCanonTest, _itow_s) {
// Test the template overload (note that this will see the full buffer)
memset(buf, fill_mem, sizeof(buf));
EXPECT_EQ(0, _itow_s(12, buf, 10));
EXPECT_EQ(WStringToUTF16(L"12"), base::string16(buf));
EXPECT_EQ(base::UTF8ToUTF16("12"),
base::string16(buf));
EXPECT_EQ(fill_char, buf[3]);

memset(buf, fill_mem, sizeof(buf));
EXPECT_EQ(0, _itow_s(12345, buf, 10));
EXPECT_EQ(WStringToUTF16(L"12345"), base::string16(buf));
EXPECT_EQ(base::UTF8ToUTF16("12345"), base::string16(buf));

EXPECT_EQ(EINVAL, _itow_s(123456, buf, 10));
}
Expand Down Expand Up @@ -2196,7 +2204,7 @@ TEST(URLCanonTest, ReplacementOverflow) {
for (int i = 0; i < 4800; i++)
new_query.push_back('a');

base::string16 new_path(WStringToUTF16(L"/foo"));
base::string16 new_path(test_utils::TruncateWStringToUTF16(L"/foo"));
repl.SetPath(new_path.c_str(), Component(0, 4));
repl.SetQuery(new_query.c_str(),
Component(0, static_cast<int>(new_query.length())));
Expand Down
30 changes: 7 additions & 23 deletions url/url_test_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@
#include <string>

#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/url_canon_internal.h"

namespace url {

namespace test_utils {

// Converts a UTF-16 string from native wchar_t format to char16, by
// truncating the high 32 bits. This is not meant to handle true UTF-32
// encoded strings.
inline base::string16 WStringToUTF16(const wchar_t* src) {
// Converts a UTF-16 string from native wchar_t format to char16 by
// truncating the high 32 bits. This is different than the conversion function
// in base bacause it passes invalid UTF-16 characters which is important for
// test purposes. As a result, this is not meant to handle true UTF-32 encoded
// strings.
inline base::string16 TruncateWStringToUTF16(const wchar_t* src) {
base::string16 str;
int length = static_cast<int>(wcslen(src));
for (int i = 0; i < length; ++i) {
Expand All @@ -30,25 +33,6 @@ inline base::string16 WStringToUTF16(const wchar_t* src) {
return str;
}

// Converts a string from UTF-8 to UTF-16.
inline base::string16 ConvertUTF8ToUTF16(const std::string& src) {
int length = static_cast<int>(src.length());
EXPECT_LT(length, 1024);
RawCanonOutputW<1024> output;
EXPECT_TRUE(ConvertUTF8ToUTF16(src.data(), length, &output));
return base::string16(output.data(), output.length());
}

// Converts a string from UTF-16 to UTF-8.
inline std::string ConvertUTF16ToUTF8(const base::string16& src) {
std::string str;
StdStringCanonOutput output(&str);
EXPECT_TRUE(ConvertUTF16ToUTF8(src.data(), static_cast<int>(src.length()),
&output));
output.Complete();
return str;
}

} // namespace test_utils

} // namespace url
Expand Down
6 changes: 3 additions & 3 deletions url/url_util_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,15 @@ TEST(URLUtilTest, DecodeURLEscapeSequences) {
RawCanonOutputT<base::char16> output;
DecodeURLEscapeSequences(input, strlen(input), &output);
EXPECT_EQ(decode_cases[i].output,
test_utils::ConvertUTF16ToUTF8(base::string16(output.data(),
output.length())));
base::UTF16ToUTF8(base::string16(output.data(),
output.length())));
}

// Our decode should decode %00
const char zero_input[] = "%00";
RawCanonOutputT<base::char16> zero_output;
DecodeURLEscapeSequences(zero_input, strlen(zero_input), &zero_output);
EXPECT_NE("%00", test_utils::ConvertUTF16ToUTF8(
EXPECT_NE("%00", base::UTF16ToUTF8(
base::string16(zero_output.data(), zero_output.length())));

// Test the error behavior for invalid UTF-8.
Expand Down

0 comments on commit 1b8582f

Please sign in to comment.