OLD | NEW |
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/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
7 #include "chrome/browser/chromeos/attestation/attestation_ca_client.h" | 7 #include "chrome/browser/chromeos/attestation/attestation_ca_client.h" |
8 #include "content/public/test/test_browser_thread.h" | 8 #include "content/public/test/test_browser_thread.h" |
| 9 #include "net/base/net_errors.h" |
9 #include "net/http/http_status_code.h" | 10 #include "net/http/http_status_code.h" |
10 #include "net/url_request/test_url_fetcher_factory.h" | 11 #include "net/url_request/test_url_fetcher_factory.h" |
11 #include "net/url_request/url_fetcher.h" | 12 #include "net/url_request/url_fetcher.h" |
12 #include "net/url_request/url_request_status.h" | 13 #include "net/url_request/url_request_status.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 namespace chromeos { | 16 namespace chromeos { |
16 namespace attestation { | 17 namespace attestation { |
17 | 18 |
18 class AttestationCAClientTest : public ::testing::Test { | 19 class AttestationCAClientTest : public ::testing::Test { |
(...skipping 13 matching lines...) Expand all Loading... |
32 } | 33 } |
33 | 34 |
34 void DeleteClientDataCallback (AttestationCAClient* client, | 35 void DeleteClientDataCallback (AttestationCAClient* client, |
35 bool result, | 36 bool result, |
36 const std::string& data) { | 37 const std::string& data) { |
37 delete client; | 38 delete client; |
38 DataCallback(result, data); | 39 DataCallback(result, data); |
39 } | 40 } |
40 | 41 |
41 protected: | 42 protected: |
42 void SendResponse(net::URLRequestStatus::Status status, int response_code) { | 43 void SendResponse(net::Error error, int response_code) { |
43 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 44 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
44 CHECK(fetcher); | 45 CHECK(fetcher); |
45 fetcher->set_status(net::URLRequestStatus(status, 0)); | 46 fetcher->set_status(net::URLRequestStatus::FromError(error)); |
46 fetcher->set_response_code(response_code); | 47 fetcher->set_response_code(response_code); |
47 fetcher->SetResponseString(fetcher->upload_data() + "_response"); | 48 fetcher->SetResponseString(fetcher->upload_data() + "_response"); |
48 fetcher->delegate()->OnURLFetchComplete(fetcher); | 49 fetcher->delegate()->OnURLFetchComplete(fetcher); |
49 } | 50 } |
50 | 51 |
51 base::MessageLoop message_loop_; | 52 base::MessageLoop message_loop_; |
52 content::TestBrowserThread io_thread_; | 53 content::TestBrowserThread io_thread_; |
53 net::TestURLFetcherFactory url_fetcher_factory_; | 54 net::TestURLFetcherFactory url_fetcher_factory_; |
54 | 55 |
55 // For use with DataCallback. | 56 // For use with DataCallback. |
56 int num_invocations_; | 57 int num_invocations_; |
57 bool result_; | 58 bool result_; |
58 std::string data_; | 59 std::string data_; |
59 }; | 60 }; |
60 | 61 |
61 TEST_F(AttestationCAClientTest, EnrollRequest) { | 62 TEST_F(AttestationCAClientTest, EnrollRequest) { |
62 AttestationCAClient client; | 63 AttestationCAClient client; |
63 client.SendEnrollRequest( | 64 client.SendEnrollRequest( |
64 "enroll", | 65 "enroll", |
65 base::Bind(&AttestationCAClientTest::DataCallback, | 66 base::Bind(&AttestationCAClientTest::DataCallback, |
66 base::Unretained(this))); | 67 base::Unretained(this))); |
67 SendResponse(net::URLRequestStatus::SUCCESS, net::HTTP_OK); | 68 SendResponse(net::OK, net::HTTP_OK); |
68 | 69 |
69 EXPECT_EQ(1, num_invocations_); | 70 EXPECT_EQ(1, num_invocations_); |
70 EXPECT_TRUE(result_); | 71 EXPECT_TRUE(result_); |
71 EXPECT_EQ("enroll_response", data_); | 72 EXPECT_EQ("enroll_response", data_); |
72 } | 73 } |
73 | 74 |
74 TEST_F(AttestationCAClientTest, CertificateRequest) { | 75 TEST_F(AttestationCAClientTest, CertificateRequest) { |
75 AttestationCAClient client; | 76 AttestationCAClient client; |
76 client.SendCertificateRequest( | 77 client.SendCertificateRequest( |
77 "certificate", | 78 "certificate", |
78 base::Bind(&AttestationCAClientTest::DataCallback, | 79 base::Bind(&AttestationCAClientTest::DataCallback, |
79 base::Unretained(this))); | 80 base::Unretained(this))); |
80 SendResponse(net::URLRequestStatus::SUCCESS, net::HTTP_OK); | 81 SendResponse(net::OK, net::HTTP_OK); |
81 | 82 |
82 EXPECT_EQ(1, num_invocations_); | 83 EXPECT_EQ(1, num_invocations_); |
83 EXPECT_TRUE(result_); | 84 EXPECT_TRUE(result_); |
84 EXPECT_EQ("certificate_response", data_); | 85 EXPECT_EQ("certificate_response", data_); |
85 } | 86 } |
86 | 87 |
87 TEST_F(AttestationCAClientTest, CertificateRequestNetworkFailure) { | 88 TEST_F(AttestationCAClientTest, CertificateRequestNetworkFailure) { |
88 AttestationCAClient client; | 89 AttestationCAClient client; |
89 client.SendCertificateRequest( | 90 client.SendCertificateRequest( |
90 "certificate", | 91 "certificate", |
91 base::Bind(&AttestationCAClientTest::DataCallback, | 92 base::Bind(&AttestationCAClientTest::DataCallback, |
92 base::Unretained(this))); | 93 base::Unretained(this))); |
93 SendResponse(net::URLRequestStatus::FAILED, net::HTTP_OK); | 94 SendResponse(net::ERR_FAILED, net::HTTP_OK); |
94 | 95 |
95 EXPECT_EQ(1, num_invocations_); | 96 EXPECT_EQ(1, num_invocations_); |
96 EXPECT_FALSE(result_); | 97 EXPECT_FALSE(result_); |
97 EXPECT_EQ("", data_); | 98 EXPECT_EQ("", data_); |
98 } | 99 } |
99 | 100 |
100 TEST_F(AttestationCAClientTest, CertificateRequestHttpError) { | 101 TEST_F(AttestationCAClientTest, CertificateRequestHttpError) { |
101 AttestationCAClient client; | 102 AttestationCAClient client; |
102 client.SendCertificateRequest( | 103 client.SendCertificateRequest( |
103 "certificate", | 104 "certificate", |
104 base::Bind(&AttestationCAClientTest::DataCallback, | 105 base::Bind(&AttestationCAClientTest::DataCallback, |
105 base::Unretained(this))); | 106 base::Unretained(this))); |
106 SendResponse(net::URLRequestStatus::SUCCESS, net::HTTP_NOT_FOUND); | 107 SendResponse(net::OK, net::HTTP_NOT_FOUND); |
107 | 108 |
108 EXPECT_EQ(1, num_invocations_); | 109 EXPECT_EQ(1, num_invocations_); |
109 EXPECT_FALSE(result_); | 110 EXPECT_FALSE(result_); |
110 EXPECT_EQ("", data_); | 111 EXPECT_EQ("", data_); |
111 } | 112 } |
112 | 113 |
113 TEST_F(AttestationCAClientTest, DeleteOnCallback) { | 114 TEST_F(AttestationCAClientTest, DeleteOnCallback) { |
114 AttestationCAClient* client = new AttestationCAClient(); | 115 AttestationCAClient* client = new AttestationCAClient(); |
115 client->SendCertificateRequest( | 116 client->SendCertificateRequest( |
116 "certificate", | 117 "certificate", |
117 base::Bind(&AttestationCAClientTest::DeleteClientDataCallback, | 118 base::Bind(&AttestationCAClientTest::DeleteClientDataCallback, |
118 base::Unretained(this), | 119 base::Unretained(this), |
119 client)); | 120 client)); |
120 SendResponse(net::URLRequestStatus::SUCCESS, net::HTTP_OK); | 121 SendResponse(net::OK, net::HTTP_OK); |
121 | 122 |
122 EXPECT_EQ(1, num_invocations_); | 123 EXPECT_EQ(1, num_invocations_); |
123 EXPECT_TRUE(result_); | 124 EXPECT_TRUE(result_); |
124 EXPECT_EQ("certificate_response", data_); | 125 EXPECT_EQ("certificate_response", data_); |
125 } | 126 } |
126 | 127 |
127 } // namespace attestation | 128 } // namespace attestation |
128 } // namespace chromeos | 129 } // namespace chromeos |
OLD | NEW |