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

Side by Side Diff: chrome/browser/chromeos/attestation/attestation_ca_client_unittest.cc

Issue 2464333002: Support different Google attestation (Privacy CA) servers. (Closed)
Patch Set: Fixed memory issues. Created 4 years, 1 month 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind.h" 5 #include "base/bind.h"
6 #include "base/command_line.h"
6 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
7 #include "chrome/browser/chromeos/attestation/attestation_ca_client.h" 8 #include "chrome/browser/chromeos/attestation/attestation_ca_client.h"
9 #include "chromeos/chromeos_switches.h"
8 #include "content/public/test/test_browser_thread.h" 10 #include "content/public/test/test_browser_thread.h"
9 #include "net/base/net_errors.h" 11 #include "net/base/net_errors.h"
10 #include "net/http/http_status_code.h" 12 #include "net/http/http_status_code.h"
11 #include "net/url_request/test_url_fetcher_factory.h" 13 #include "net/url_request/test_url_fetcher_factory.h"
12 #include "net/url_request/url_fetcher.h" 14 #include "net/url_request/url_fetcher.h"
13 #include "net/url_request/url_request_status.h" 15 #include "net/url_request/url_request_status.h"
14 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
15 17
16 namespace chromeos { 18 namespace chromeos {
17 namespace attestation { 19 namespace attestation {
18 20
19 class AttestationCAClientTest : public ::testing::Test { 21 class AttestationCAClientTest : public ::testing::Test {
20 public: 22 public:
21 AttestationCAClientTest() 23 AttestationCAClientTest()
22 : io_thread_(content::BrowserThread::IO, &message_loop_), 24 : io_thread_(content::BrowserThread::IO, &message_loop_),
23 num_invocations_(0), 25 num_invocations_(0),
24 result_(false) { 26 result_(false) {
25 } 27 }
26 28
27 ~AttestationCAClientTest() override {} 29 ~AttestationCAClientTest() override {}
28 30
29 void DataCallback (bool result, const std::string& data) { 31 void DataCallback(bool result, const std::string& data) {
30 ++num_invocations_; 32 ++num_invocations_;
31 result_ = result; 33 result_ = result;
32 data_ = data; 34 data_ = data;
33 } 35 }
34 36
35 void DeleteClientDataCallback (AttestationCAClient* client, 37 void DeleteClientDataCallback(AttestationCAClient* client,
36 bool result, 38 bool result,
37 const std::string& data) { 39 const std::string& data) {
38 delete client; 40 delete client;
39 DataCallback(result, data); 41 DataCallback(result, data);
40 } 42 }
41 43
42 protected: 44 protected:
45 void CheckURLAndSendResponse(GURL expected_url,
46 net::Error error,
47 int response_code) {
48 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
49 CHECK(fetcher);
50 EXPECT_EQ(expected_url, fetcher->GetOriginalURL());
51 SendFetcherResponse(fetcher, error, response_code);
52 }
53
43 void SendResponse(net::Error error, int response_code) { 54 void SendResponse(net::Error error, int response_code) {
44 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 55 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
45 CHECK(fetcher); 56 CHECK(fetcher);
57 SendFetcherResponse(fetcher, error, response_code);
58 }
59
60 void SendFetcherResponse(net::TestURLFetcher* fetcher,
61 net::Error error,
62 int response_code) {
46 fetcher->set_status(net::URLRequestStatus::FromError(error)); 63 fetcher->set_status(net::URLRequestStatus::FromError(error));
47 fetcher->set_response_code(response_code); 64 fetcher->set_response_code(response_code);
48 fetcher->SetResponseString(fetcher->upload_data() + "_response"); 65 fetcher->SetResponseString(fetcher->upload_data() + "_response");
49 fetcher->delegate()->OnURLFetchComplete(fetcher); 66 fetcher->delegate()->OnURLFetchComplete(fetcher);
50 } 67 }
51 68
52 base::MessageLoop message_loop_; 69 base::MessageLoop message_loop_;
53 content::TestBrowserThread io_thread_; 70 content::TestBrowserThread io_thread_;
54 net::TestURLFetcherFactory url_fetcher_factory_; 71 net::TestURLFetcherFactory url_fetcher_factory_;
55 72
56 // For use with DataCallback. 73 // For use with DataCallback.
57 int num_invocations_; 74 int num_invocations_;
58 bool result_; 75 bool result_;
59 std::string data_; 76 std::string data_;
60 }; 77 };
61 78
62 TEST_F(AttestationCAClientTest, EnrollRequest) { 79 TEST_F(AttestationCAClientTest, EnrollRequest) {
63 AttestationCAClient client; 80 AttestationCAClient client;
64 client.SendEnrollRequest( 81 client.SendEnrollRequest(
65 "enroll", 82 "enroll",
66 base::Bind(&AttestationCAClientTest::DataCallback, 83 base::Bind(&AttestationCAClientTest::DataCallback,
67 base::Unretained(this))); 84 base::Unretained(this)));
68 SendResponse(net::OK, net::HTTP_OK); 85 CheckURLAndSendResponse(GURL("https://chromeos-ca.gstatic.com/enroll"),
86 net::OK, net::HTTP_OK);
69 87
70 EXPECT_EQ(1, num_invocations_); 88 EXPECT_EQ(1, num_invocations_);
71 EXPECT_TRUE(result_); 89 EXPECT_TRUE(result_);
72 EXPECT_EQ("enroll_response", data_); 90 EXPECT_EQ("enroll_response", data_);
73 } 91 }
74 92
75 TEST_F(AttestationCAClientTest, CertificateRequest) { 93 TEST_F(AttestationCAClientTest, CertificateRequest) {
76 AttestationCAClient client; 94 AttestationCAClient client;
77 client.SendCertificateRequest( 95 client.SendCertificateRequest(
78 "certificate", 96 "certificate",
79 base::Bind(&AttestationCAClientTest::DataCallback, 97 base::Bind(&AttestationCAClientTest::DataCallback,
80 base::Unretained(this))); 98 base::Unretained(this)));
81 SendResponse(net::OK, net::HTTP_OK); 99 CheckURLAndSendResponse(GURL("https://chromeos-ca.gstatic.com/sign"), net::OK,
100 net::HTTP_OK);
82 101
83 EXPECT_EQ(1, num_invocations_); 102 EXPECT_EQ(1, num_invocations_);
84 EXPECT_TRUE(result_); 103 EXPECT_TRUE(result_);
85 EXPECT_EQ("certificate_response", data_); 104 EXPECT_EQ("certificate_response", data_);
86 } 105 }
87 106
88 TEST_F(AttestationCAClientTest, CertificateRequestNetworkFailure) { 107 TEST_F(AttestationCAClientTest, CertificateRequestNetworkFailure) {
89 AttestationCAClient client; 108 AttestationCAClient client;
90 client.SendCertificateRequest( 109 client.SendCertificateRequest(
91 "certificate", 110 "certificate",
(...skipping 26 matching lines...) Expand all
118 base::Bind(&AttestationCAClientTest::DeleteClientDataCallback, 137 base::Bind(&AttestationCAClientTest::DeleteClientDataCallback,
119 base::Unretained(this), 138 base::Unretained(this),
120 client)); 139 client));
121 SendResponse(net::OK, net::HTTP_OK); 140 SendResponse(net::OK, net::HTTP_OK);
122 141
123 EXPECT_EQ(1, num_invocations_); 142 EXPECT_EQ(1, num_invocations_);
124 EXPECT_TRUE(result_); 143 EXPECT_TRUE(result_);
125 EXPECT_EQ("certificate_response", data_); 144 EXPECT_EQ("certificate_response", data_);
126 } 145 }
127 146
147 class AttestationCAClientAttestationServerTest
148 : public AttestationCAClientTest {};
149
150 TEST_F(AttestationCAClientAttestationServerTest, DefaultEnrollRequest) {
151 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
152 chromeos::switches::kAttestationServer, "default");
153 AttestationCAClient client;
154 client.SendEnrollRequest("enroll",
155 base::Bind(&AttestationCAClientTest::DataCallback,
156 base::Unretained(this)));
157 CheckURLAndSendResponse(GURL("https://chromeos-ca.gstatic.com/enroll"),
158 net::OK, net::HTTP_OK);
159
160 EXPECT_EQ(1, num_invocations_);
161 EXPECT_TRUE(result_);
162 EXPECT_EQ("enroll_response", data_);
163 }
164
165 TEST_F(AttestationCAClientAttestationServerTest, DefaultCertificateRequest) {
166 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
167 chromeos::switches::kAttestationServer, "default");
168 AttestationCAClient client;
169 client.SendCertificateRequest(
170 "certificate", base::Bind(&AttestationCAClientTest::DataCallback,
171 base::Unretained(this)));
172 CheckURLAndSendResponse(GURL("https://chromeos-ca.gstatic.com/sign"), net::OK,
173 net::HTTP_OK);
174
175 EXPECT_EQ(1, num_invocations_);
176 EXPECT_TRUE(result_);
177 EXPECT_EQ("certificate_response", data_);
178 }
179
180 TEST_F(AttestationCAClientAttestationServerTest, TestEnrollRequest) {
181 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
182 chromeos::switches::kAttestationServer, "test");
183 AttestationCAClient client;
184 client.SendEnrollRequest("enroll",
185 base::Bind(&AttestationCAClientTest::DataCallback,
186 base::Unretained(this)));
187 CheckURLAndSendResponse(GURL("https://asbestos-qa.corp.google.com/enroll"),
188 net::OK, net::HTTP_OK);
189
190 EXPECT_EQ(1, num_invocations_);
191 EXPECT_TRUE(result_);
192 EXPECT_EQ("enroll_response", data_);
193 }
194
195 TEST_F(AttestationCAClientAttestationServerTest, TestCertificateRequest) {
196 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
197 chromeos::switches::kAttestationServer, "test");
198 AttestationCAClient client;
199 client.SendCertificateRequest(
200 "certificate", base::Bind(&AttestationCAClientTest::DataCallback,
201 base::Unretained(this)));
202 CheckURLAndSendResponse(GURL("https://asbestos-qa.corp.google.com/sign"),
203 net::OK, net::HTTP_OK);
204
205 EXPECT_EQ(1, num_invocations_);
206 EXPECT_TRUE(result_);
207 EXPECT_EQ("certificate_response", data_);
208 }
209
128 } // namespace attestation 210 } // namespace attestation
129 } // namespace chromeos 211 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/attestation/attestation_ca_client.cc ('k') | chromeos/attestation/attestation_constants.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698