summaryrefslogtreecommitdiffstats
path: root/components/update_client/test/ping_manager_unittest.cc
blob: 95539f0313b1d8292b24584c6089c441e6720712 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2013 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 "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/version.h"
#include "components/update_client/crx_update_item.h"
#include "components/update_client/ping_manager.h"
#include "components/update_client/test/test_configurator.h"
#include "components/update_client/test/url_request_post_interceptor.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"

using std::string;

namespace update_client {

class ComponentUpdaterPingManagerTest : public testing::Test {
 public:
  ComponentUpdaterPingManagerTest();
  ~ComponentUpdaterPingManagerTest() override {}

  void RunThreadsUntilIdle();

  // Overrides from testing::Test.
  void SetUp() override;
  void TearDown() override;

 protected:
  scoped_ptr<TestConfigurator> config_;
  scoped_ptr<PingManager> ping_manager_;

 private:
  base::MessageLoopForIO loop_;
};

ComponentUpdaterPingManagerTest::ComponentUpdaterPingManagerTest()
    : config_(new TestConfigurator(base::MessageLoopProxy::current(),
                                   base::MessageLoopProxy::current())) {
}

void ComponentUpdaterPingManagerTest::SetUp() {
  ping_manager_.reset(new PingManager(*config_));
}

void ComponentUpdaterPingManagerTest::TearDown() {
  ping_manager_.reset();
  config_.reset();
}

void ComponentUpdaterPingManagerTest::RunThreadsUntilIdle() {
  base::RunLoop().RunUntilIdle();
}

// Test is flaky: http://crbug.com/349547
TEST_F(ComponentUpdaterPingManagerTest, DISABLED_PingManagerTest) {
  scoped_ptr<InterceptorFactory> interceptor_factory(
      new InterceptorFactory(base::MessageLoopProxy::current()));
  URLRequestPostInterceptor* interceptor =
      interceptor_factory->CreateInterceptor();
  EXPECT_TRUE(interceptor);

  // Test eventresult="1" is sent for successful updates.
  CrxUpdateItem item;
  item.id = "abc";
  item.status = CrxUpdateItem::kUpdated;
  item.previous_version = base::Version("1.0");
  item.next_version = base::Version("2.0");

  ping_manager_->OnUpdateComplete(&item);
  base::RunLoop().RunUntilIdle();

  EXPECT_EQ(1, interceptor->GetCount()) << interceptor->GetRequestsAsString();
  EXPECT_NE(string::npos,
            interceptor->GetRequests()[0].find(
                "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">"
                "<event eventtype=\"3\" eventresult=\"1\"/></app>"))
      << interceptor->GetRequestsAsString();
  interceptor->Reset();

  // Test eventresult="0" is sent for failed updates.
  item = CrxUpdateItem();
  item.id = "abc";
  item.status = CrxUpdateItem::kNoUpdate;
  item.previous_version = base::Version("1.0");
  item.next_version = base::Version("2.0");

  ping_manager_->OnUpdateComplete(&item);
  base::RunLoop().RunUntilIdle();

  EXPECT_EQ(1, interceptor->GetCount()) << interceptor->GetRequestsAsString();
  EXPECT_NE(string::npos,
            interceptor->GetRequests()[0].find(
                "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">"
                "<event eventtype=\"3\" eventresult=\"0\"/></app>"))
      << interceptor->GetRequestsAsString();
  interceptor->Reset();

  // Test the error values and the fingerprints.
  item = CrxUpdateItem();
  item.id = "abc";
  item.status = CrxUpdateItem::kNoUpdate;
  item.previous_version = base::Version("1.0");
  item.next_version = base::Version("2.0");
  item.previous_fp = "prev fp";
  item.next_fp = "next fp";
  item.error_category = 1;
  item.error_code = 2;
  item.extra_code1 = -1;
  item.diff_error_category = 10;
  item.diff_error_code = 20;
  item.diff_extra_code1 = -10;
  item.diff_update_failed = true;
  item.crx_diffurls.push_back(GURL("http://host/path"));

  ping_manager_->OnUpdateComplete(&item);
  base::RunLoop().RunUntilIdle();

  EXPECT_EQ(1, interceptor->GetCount()) << interceptor->GetRequestsAsString();
  EXPECT_NE(string::npos,
            interceptor->GetRequests()[0].find(
                "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">"
                "<event eventtype=\"3\" eventresult=\"0\" errorcat=\"1\" "
                "errorcode=\"2\" extracode1=\"-1\" diffresult=\"0\" "
                "differrorcat=\"10\" "
                "differrorcode=\"20\" diffextracode1=\"-10\" "
                "previousfp=\"prev fp\" nextfp=\"next fp\"/></app>"))
      << interceptor->GetRequestsAsString();
  interceptor->Reset();

  // Test the download metrics.
  item = CrxUpdateItem();
  item.id = "abc";
  item.status = CrxUpdateItem::kUpdated;
  item.previous_version = base::Version("1.0");
  item.next_version = base::Version("2.0");

  CrxDownloader::DownloadMetrics download_metrics;
  download_metrics.url = GURL("http://host1/path1");
  download_metrics.downloader = CrxDownloader::DownloadMetrics::kUrlFetcher;
  download_metrics.error = -1;
  download_metrics.downloaded_bytes = 123;
  download_metrics.total_bytes = 456;
  download_metrics.download_time_ms = 987;
  item.download_metrics.push_back(download_metrics);

  download_metrics = CrxDownloader::DownloadMetrics();
  download_metrics.url = GURL("http://host2/path2");
  download_metrics.downloader = CrxDownloader::DownloadMetrics::kBits;
  download_metrics.error = 0;
  download_metrics.downloaded_bytes = 1230;
  download_metrics.total_bytes = 4560;
  download_metrics.download_time_ms = 9870;
  item.download_metrics.push_back(download_metrics);

  ping_manager_->OnUpdateComplete(&item);
  base::RunLoop().RunUntilIdle();

  EXPECT_EQ(1, interceptor->GetCount()) << interceptor->GetRequestsAsString();
  EXPECT_NE(
      string::npos,
      interceptor->GetRequests()[0].find(
          "<app appid=\"abc\" version=\"1.0\" nextversion=\"2.0\">"
          "<event eventtype=\"3\" eventresult=\"1\"/>"
          "<event eventtype=\"14\" eventresult=\"0\" downloader=\"direct\" "
          "errorcode=\"-1\" url=\"http://host1/path1\" downloaded=\"123\" "
          "total=\"456\" download_time_ms=\"987\"/>"
          "<event eventtype=\"14\" eventresult=\"1\" downloader=\"bits\" "
          "url=\"http://host2/path2\" downloaded=\"1230\" total=\"4560\" "
          "download_time_ms=\"9870\"/></app>"))
      << interceptor->GetRequestsAsString();
  interceptor->Reset();
}

}  // namespace update_client