Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Unified Diff: components/data_reduction_proxy/core/common/client_config_parser_unittest.cc

Issue 1017853003: Add ClientConfig proto, and JSON generation/parsing. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Formatting and unit test fix. Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/data_reduction_proxy/core/common/client_config_parser_unittest.cc
diff --git a/components/data_reduction_proxy/core/common/client_config_parser_unittest.cc b/components/data_reduction_proxy/core/common/client_config_parser_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..74eaba9d76a626d797c71e68d3e364f0ef711b81
--- /dev/null
+++ b/components/data_reduction_proxy/core/common/client_config_parser_unittest.cc
@@ -0,0 +1,228 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/data_reduction_proxy/core/common/client_config_parser.h"
+
+#include <string>
+
+#include "base/time/time.h"
+#include "components/data_reduction_proxy/proto/client_config.pb.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace data_reduction_proxy {
+
+namespace {
+
+const char kValidPreamble[] =
+ "{ \"sessionKey\": \"foobar\", "
+ "\"expireTime\": \"2100-12-31T23:59:59.999999Z\", "
+ "\"proxyConfig\": { \"httpProxyServers\": [";
+const char kValidPostamble[] = "] } }";
+
+} // namespace
+
+TEST(ClientConfigParserTest, TimeToISO8601) {
+ const struct {
+ base::Time time;
+ std::string expected;
+ } tests[] = {
+ {
+ base::Time(),
+ "1601-01-01T00:00:00.000Z",
+ },
+ {
+ base::Time::UnixEpoch(),
+ "1970-01-01T00:00:00.000Z",
+ },
+ };
+
+ for (size_t i = 0; i < arraysize(tests); ++i) {
sclittle 2015/03/18 20:31:51 Use range-based for loops; e.g. for (const auto& t
jeremyim 2015/03/20 16:21:11 Done.
+ EXPECT_EQ(tests[i].expected, TimeToISO8601(tests[i].time));
+ }
+}
+
+TEST(ClientConfigParserTest, ISO8601ToTimestamp) {
+ const struct {
+ std::string time_string;
+ int64 epoch_seconds;
+ } tests[] = {
+ {
+ "1970-01-01T00:00:00.000Z",
+ 0,
+ },
+ {
+ "1970-01-01T00:00:00.999Z",
+ 0,
+ },
+ {
+ "1900-01-01T00:00:00.000Z",
+ -2208988800,
+ },
+ {
+ "1900-01-01T00:00:00.500Z",
+ -2208988799, // Rounding of negative fractional values causes this.
+ },
+ };
+
+ for (size_t i = 0; i < arraysize(tests); ++i) {
+ Timestamp timestamp;
+ EXPECT_TRUE(ISO8601ToTimestamp(tests[i].time_string, &timestamp));
+ EXPECT_EQ(tests[i].epoch_seconds, timestamp.seconds());
+ EXPECT_EQ(0, timestamp.nanos());
+ }
+}
+
+TEST(ClientConfigParserTest, ISO8601ToTimestampTestFailures) {
+ const std::string inputs[] = {
+ "",
+ "Not a time",
+ "1234",
+ "2099-43-12",
+ "2099-11-52",
+ };
+
+ for (size_t i = 0; i < arraysize(inputs); ++i) {
+ Timestamp timestamp;
+ EXPECT_FALSE(ISO8601ToTimestamp(inputs[i], &timestamp));
+ }
+}
+
+TEST(ClientConfigParserTest, TimestampToTime) {
+ base::Time::Exploded future = {
+ 2100,
+ 12,
+ 5,
+ 31,
+ 23,
+ 59,
+ 59,
+ 0,
+ };
+ const struct {
+ int64 timestamp_seconds;
+ int32 timestamp_nanos;
+ base::Time expected_time;
+ } tests[] = {
+ {
+ 0,
+ 0,
+ base::Time::UnixEpoch(),
+ },
+ {
+ 24 * 60 * 60 - 1,
+ base::Time::kNanosecondsPerSecond - 1,
+ base::Time::UnixEpoch() + base::TimeDelta::FromDays(1) -
+ base::TimeDelta::FromMicroseconds(1),
+ },
+ {
+ // 2100-12-31T23:59:59.000Z
+ 4133980799,
+ 0,
+ base::Time::FromUTCExploded(future),
+ },
+ };
+
+ for (size_t i = 0; i < arraysize(tests); ++i) {
+ Timestamp ts;
+ ts.set_seconds(tests[i].timestamp_seconds);
+ ts.set_nanos(tests[i].timestamp_nanos);
+ EXPECT_EQ(tests[i].expected_time, TimestampToTime(ts));
+ }
+}
+
+TEST(ClientConfigParserTest, SingleServer) {
+ const std::string inputs[] = {
+ "{ \"scheme\": \"HTTP\", \"host\": \"foo.com\", \"port\": 80 }",
+ "{ \"scheme\": \"HTTPS\", \"host\": \"foo.com\", \"port\": 443 }",
+ "{ \"scheme\": \"QUIC\", \"host\": \"foo.com\", \"port\": 443 }",
+ };
+
+ for (size_t i = 0; i < arraysize(inputs); ++i) {
+ std::string input = kValidPreamble + inputs[i] + kValidPostamble;
+ ClientConfig config;
+ EXPECT_TRUE(ParseClientConfig(input, &config));
+ EXPECT_EQ(1, config.proxy_config().http_proxy_servers_size());
+ }
+}
+
+TEST(ClientConfigParserTest, MultipleServers) {
+ std::string input =
+ "{ \"scheme\": \"HTTP\", ""\"host\": \"foo.com\", \"port\": 80 }, "
+ "{ \"scheme\": \"HTTPS\", \"host\": \"foo.com\", \"port\": 443 }, "
+ "{ \"scheme\": \"QUIC\", \"host\": \"foo.com\", \"port\": 443 }";
+ ClientConfig config;
+ EXPECT_TRUE(ParseClientConfig(
+ kValidPreamble + input + kValidPostamble, &config));
+ EXPECT_EQ(3, config.proxy_config().http_proxy_servers_size());
+}
+
+TEST(ClientConfigParserTest, FailureCases) {
+ const std::string inputs[] = {
+ "",
+ "invalid json",
+ // No sessionKey
+ "{ \"expireTime\": \"2100-12-31T23:59:59.999999Z\","
+ "\"proxyConfig\": { \"httpProxyServers\": [ ] } }",
+ // No expireTime
+ "{ \"sessionKey\": \"foobar\","
+ "\"proxyConfig\": { \"httpProxyServers\": [ ] } }",
+ // No proxyConfig
+ "{ \"sessionKey\": \"foobar\", "
+ "\"expireTime\": \"2100-12-31T23:59:59.999999Z\" }",
+ // No proxyConfig.httpProxyServers
+ "{ \"sessionKey\": \"foobar\", "
+ "\"expireTime\": \"2100-12-31T23:59:59.999999Z\", \"proxyConfig\": { } }",
+ // Invalid sessionKey
+ "{ \"sessionKey\": 12345, "
+ "\"expireTime\": \"2100-12-31T23:59:59.999999Z\","
+ " \"proxyConfig\": { \"httpProxyServers\": [ ] } }",
+ // Invalid sessionKey
+ "{ \"sessionKey\": { }, "
+ "\"expireTime\": \"2100-12-31T23:59:59.999999Z\","
+ " \"proxyConfig\": { \"httpProxyServers\": [ ] } }",
+ // Invalid sessionKey
+ "{ \"sessionKey\": [ ], "
+ "\"expireTime\": \"2100-12-31T23:59:59.999999Z\","
+ " \"proxyConfig\": { \"httpProxyServers\": [ ] } }",
+ // Invalid expireTime
+ "{ \"sessionKey\": \"foobar\", "
+ "\"expireTime\": \"abcd\","
+ " \"proxyConfig\": { \"httpProxyServers\": [ ] } }",
+ // Invalid expireTime
+ "{ \"sessionKey\": \"foobar\", "
+ "\"expireTime\": [ ],"
+ " \"proxyConfig\": { \"httpProxyServers\": [ ] } }",
+ // Invalid expireTime
+ "{ \"sessionKey\": \"foobar\", "
+ "\"expireTime\": { },"
+ " \"proxyConfig\": { \"httpProxyServers\": [ ] } }",
+ };
+
+ for (size_t i = 0; i < arraysize(inputs); ++i) {
+ const std::string& input = inputs[i];
+ ClientConfig config;
+ EXPECT_FALSE(ParseClientConfig(input, &config));
+ }
+}
+
+TEST(ClientConfigParserTest, EmptyServerLists) {
+ const std::string inputs[] = {
+ "",
+ "{ }",
+ "{ \"scheme\": \"foo\", \"host\": \"foo.com\", \"port\": 80 }",
+ "{ \"scheme\": \"HTTP\", \"port\": 80 }",
+ "{ \"scheme\": \"HTTP\", \"host\": \"foo.com\" }",
+ "{ \"scheme\": \"HTTP\", \"host\": \"foo.com\", \"port\": \"bar\" }",
+ "{ \"scheme\": \"HTTP\", \"host\": 12345, \"port\": 80 }",
+ };
+
+ for (size_t i = 0; i < arraysize(inputs); ++i) {
+ std::string input = kValidPreamble + inputs[i] + kValidPostamble;
+ ClientConfig config;
+ EXPECT_TRUE(ParseClientConfig(input, &config));
+ EXPECT_EQ(0, config.proxy_config().http_proxy_servers_size());
+ }
+}
+
+} // namespace data_reduction_proxy

Powered by Google App Engine
This is Rietveld 408576698