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
3 // found in the LICENSE file.
6 #include "base/message_loop/message_loop.h"
7 #include "chrome/browser/chromeos/attestation/attestation_ca_client.h"
8 #include "content/public/test/test_browser_thread.h"
9 #include "net/http/http_status_code.h"
10 #include "net/url_request/test_url_fetcher_factory.h"
11 #include "net/url_request/url_fetcher.h"
12 #include "net/url_request/url_request_status.h"
13 #include "testing/gtest/include/gtest/gtest.h"
16 namespace attestation
{
18 class AttestationCAClientTest
: public ::testing::Test
{
20 AttestationCAClientTest()
21 : io_thread_(content::BrowserThread::IO
, &message_loop_
),
26 ~AttestationCAClientTest() override
{}
28 void DataCallback (bool result
, const std::string
& data
) {
34 void DeleteClientDataCallback (AttestationCAClient
* client
,
36 const std::string
& data
) {
38 DataCallback(result
, data
);
42 void SendResponse(net::URLRequestStatus::Status status
, int response_code
) {
43 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
45 fetcher
->set_status(net::URLRequestStatus(status
, 0));
46 fetcher
->set_response_code(response_code
);
47 fetcher
->SetResponseString(fetcher
->upload_data() + "_response");
48 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
51 base::MessageLoop message_loop_
;
52 content::TestBrowserThread io_thread_
;
53 net::TestURLFetcherFactory url_fetcher_factory_
;
55 // For use with DataCallback.
61 TEST_F(AttestationCAClientTest
, EnrollRequest
) {
62 AttestationCAClient client
;
63 client
.SendEnrollRequest(
65 base::Bind(&AttestationCAClientTest::DataCallback
,
66 base::Unretained(this)));
67 SendResponse(net::URLRequestStatus::SUCCESS
, net::HTTP_OK
);
69 EXPECT_EQ(1, num_invocations_
);
71 EXPECT_EQ("enroll_response", data_
);
74 TEST_F(AttestationCAClientTest
, CertificateRequest
) {
75 AttestationCAClient client
;
76 client
.SendCertificateRequest(
78 base::Bind(&AttestationCAClientTest::DataCallback
,
79 base::Unretained(this)));
80 SendResponse(net::URLRequestStatus::SUCCESS
, net::HTTP_OK
);
82 EXPECT_EQ(1, num_invocations_
);
84 EXPECT_EQ("certificate_response", data_
);
87 TEST_F(AttestationCAClientTest
, CertificateRequestNetworkFailure
) {
88 AttestationCAClient client
;
89 client
.SendCertificateRequest(
91 base::Bind(&AttestationCAClientTest::DataCallback
,
92 base::Unretained(this)));
93 SendResponse(net::URLRequestStatus::FAILED
, net::HTTP_OK
);
95 EXPECT_EQ(1, num_invocations_
);
96 EXPECT_FALSE(result_
);
100 TEST_F(AttestationCAClientTest
, CertificateRequestHttpError
) {
101 AttestationCAClient client
;
102 client
.SendCertificateRequest(
104 base::Bind(&AttestationCAClientTest::DataCallback
,
105 base::Unretained(this)));
106 SendResponse(net::URLRequestStatus::SUCCESS
, net::HTTP_NOT_FOUND
);
108 EXPECT_EQ(1, num_invocations_
);
109 EXPECT_FALSE(result_
);
110 EXPECT_EQ("", data_
);
113 TEST_F(AttestationCAClientTest
, DeleteOnCallback
) {
114 AttestationCAClient
* client
= new AttestationCAClient();
115 client
->SendCertificateRequest(
117 base::Bind(&AttestationCAClientTest::DeleteClientDataCallback
,
118 base::Unretained(this),
120 SendResponse(net::URLRequestStatus::SUCCESS
, net::HTTP_OK
);
122 EXPECT_EQ(1, num_invocations_
);
123 EXPECT_TRUE(result_
);
124 EXPECT_EQ("certificate_response", data_
);
127 } // namespace attestation
128 } // namespace chromeos