1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_util.h"
6
7 #include <stdint.h>
8
9 #include <string>
10
11 #include "base/time/time.h"
12 #include "components/data_reduction_proxy/proto/client_config.pb.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace data_reduction_proxy {
16
17 namespace {
18
19 const char kFutureTime[] = "31 Dec 2020 23:59:59.001";
20
21 } // namespace
22
23 class DataReductionProxyClientProtobufParserTest : public testing::Test {
24 protected:
SetUp()25 void SetUp() override {
26 EXPECT_TRUE(base::Time::FromUTCString(kFutureTime, &future_time_));
27 }
28
GetFutureTime() const29 const base::Time& GetFutureTime() const { return future_time_; }
30
31 private:
32 base::Time future_time_;
33 };
34
TEST_F(DataReductionProxyClientProtobufParserTest,TimeDeltaToFromDuration)35 TEST_F(DataReductionProxyClientProtobufParserTest, TimeDeltaToFromDuration) {
36 const struct {
37 std::string test_name;
38 base::TimeDelta time_delta;
39 int64_t seconds;
40 int32_t nanos;
41 } tests[] = {
42 {
43 "Second", base::TimeDelta::FromSeconds(1), 1, 0,
44 },
45 {
46 "-1 Second", base::TimeDelta::FromSeconds(-1), -1, 0,
47 },
48 {
49 "1.5 Seconds", base::TimeDelta::FromMilliseconds(1500), 1,
50 base::Time::kNanosecondsPerSecond / 2,
51 },
52 };
53
54 for (const auto& test : tests) {
55 Duration duration;
56 protobuf_parser::TimeDeltaToDuration(test.time_delta, &duration);
57 EXPECT_EQ(test.seconds, duration.seconds()) << test.test_name;
58 EXPECT_EQ(test.nanos, duration.nanos()) << test.test_name;
59 duration.set_seconds(test.seconds);
60 duration.set_nanos(test.nanos);
61 EXPECT_EQ(test.time_delta, protobuf_parser::DurationToTimeDelta(duration))
62 << test.test_name;
63 }
64 }
65
TEST_F(DataReductionProxyClientProtobufParserTest,TimeStampToFromTime)66 TEST_F(DataReductionProxyClientProtobufParserTest, TimeStampToFromTime) {
67 const struct {
68 std::string test_name;
69 base::Time time;
70 int64_t seconds;
71 int32_t nanos;
72 } tests[] = {
73 {
74 "Second", base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(1),
75 1, 0,
76 },
77 {
78 "1.5 Seconds",
79 base::Time::UnixEpoch() + base::TimeDelta::FromMilliseconds(1500), 1,
80 base::Time::kNanosecondsPerSecond / 2,
81 },
82 };
83
84 for (const auto& test : tests) {
85 Timestamp timestamp;
86 protobuf_parser::TimeToTimestamp(test.time, ×tamp);
87 EXPECT_EQ(test.seconds, timestamp.seconds()) << test.test_name;
88 EXPECT_EQ(test.nanos, timestamp.nanos()) << test.test_name;
89 timestamp.set_seconds(test.seconds);
90 timestamp.set_nanos(test.nanos);
91 EXPECT_EQ(test.time, protobuf_parser::TimestampToTime(timestamp))
92 << test.test_name;
93 }
94 }
95
96 } // namespace data_reduction_proxy
97