OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 "net/reporting/reporting_serializer.h" |
| 6 |
| 7 #include "base/json/json_writer.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/test/simple_test_clock.h" |
| 10 #include "base/test/simple_test_tick_clock.h" |
| 11 #include "base/time/time.h" |
| 12 #include "base/values.h" |
| 13 #include "net/reporting/reporting_cache.h" |
| 14 #include "net/reporting/reporting_client.h" |
| 15 #include "net/reporting/reporting_policy.h" |
| 16 #include "net/reporting/reporting_report.h" |
| 17 #include "net/reporting/reporting_test_util.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 namespace net { |
| 21 namespace { |
| 22 |
| 23 class ReportingSerializerTest : public ReportingTestBase { |
| 24 protected: |
| 25 ReportingSerializerTest() : new_context(ReportingPolicy()) {} |
| 26 |
| 27 const GURL kUrl_ = GURL("https://origin/path"); |
| 28 const url::Origin kOrigin_ = url::Origin(kUrl_); |
| 29 const GURL kEndpoint_ = GURL("https://endpoint/"); |
| 30 const std::string kGroup_ = "group"; |
| 31 const std::string kType_ = "default"; |
| 32 |
| 33 TestReportingContext new_context; |
| 34 }; |
| 35 |
| 36 TEST_F(ReportingSerializerTest, Test) { |
| 37 ReportingPolicy policy; |
| 38 policy.persist_reports_across_restarts = true; |
| 39 policy.persist_clients_across_restarts = true; |
| 40 UsePolicy(policy); |
| 41 |
| 42 static const int kAttempts = 3; |
| 43 |
| 44 base::DictionaryValue body; |
| 45 body.SetString("key", "value"); |
| 46 |
| 47 cache()->AddReport(kUrl_, kGroup_, kType_, body.CreateDeepCopy(), |
| 48 tick_clock()->NowTicks(), kAttempts); |
| 49 cache()->SetClient(kOrigin_, kEndpoint_, ReportingClient::Subdomains::EXCLUDE, |
| 50 kGroup_, |
| 51 tick_clock()->NowTicks() + base::TimeDelta::FromDays(1)); |
| 52 |
| 53 auto serialized = ReportingSerializer::SerializeToValue(context()); |
| 54 |
| 55 std::string json; |
| 56 ASSERT_TRUE(base::JSONWriter::Write(*serialized, &json)); |
| 57 |
| 58 // Ticks can go backwards! |
| 59 new_context.test_tick_clock()->SetNowTicks(tick_clock()->NowTicks() - |
| 60 base::TimeDelta::FromHours(3)); |
| 61 new_context.test_clock()->Advance(base::TimeDelta::FromHours(1)); |
| 62 |
| 63 bool deserialized = |
| 64 ReportingSerializer::DeserializeFromValue(*serialized, &new_context); |
| 65 ASSERT_TRUE(deserialized); |
| 66 |
| 67 std::vector<const ReportingReport*> reports; |
| 68 new_context.cache()->GetReports(&reports); |
| 69 ASSERT_EQ(1u, reports.size()); |
| 70 EXPECT_EQ(kUrl_, reports[0]->url); |
| 71 EXPECT_EQ(kGroup_, reports[0]->group); |
| 72 EXPECT_EQ(kType_, reports[0]->type); |
| 73 EXPECT_TRUE(base::Value::Equals(&body, reports[0]->body.get())); |
| 74 EXPECT_EQ( |
| 75 new_context.tick_clock()->NowTicks() - base::TimeDelta::FromHours(1), |
| 76 reports[0]->queued); |
| 77 EXPECT_EQ(kAttempts, reports[0]->attempts); |
| 78 |
| 79 const ReportingClient* client = |
| 80 FindClientInCache(new_context.cache(), kOrigin_, kEndpoint_); |
| 81 ASSERT_TRUE(client); |
| 82 EXPECT_EQ(ReportingClient::Subdomains::EXCLUDE, client->subdomains); |
| 83 EXPECT_EQ(kGroup_, client->group); |
| 84 EXPECT_EQ(new_context.tick_clock()->NowTicks() + |
| 85 base::TimeDelta::FromDays(1) - base::TimeDelta::FromHours(1), |
| 86 client->expires); |
| 87 } |
| 88 |
| 89 } // namespace |
| 90 } // namespace net |
OLD | NEW |