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/base/net_errors.h"
10 #include "net/http/http_status_code.h"
11 #include "net/url_request/test_url_fetcher_factory.h"
12 #include "net/url_request/url_fetcher.h"
13 #include "net/url_request/url_request_status.h"
14 #include "testing/gtest/include/gtest/gtest.h"
17 namespace attestation
{
19 class AttestationCAClientTest
: public ::testing::Test
{
21 AttestationCAClientTest()
22 : io_thread_(content::BrowserThread::IO
, &message_loop_
),
27 ~AttestationCAClientTest() override
{}
29 void DataCallback (bool result
, const std::string
& data
) {
35 void DeleteClientDataCallback (AttestationCAClient
* client
,
37 const std::string
& data
) {
39 DataCallback(result
, data
);
43 void SendResponse(net::Error error
, int response_code
) {
44 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
46 fetcher
->set_status(net::URLRequestStatus::FromError(error
));
47 fetcher
->set_response_code(response_code
);
48 fetcher
->SetResponseString(fetcher
->upload_data() + "_response");
49 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
52 base::MessageLoop message_loop_
;
53 content::TestBrowserThread io_thread_
;
54 net::TestURLFetcherFactory url_fetcher_factory_
;
56 // For use with DataCallback.
62 TEST_F(AttestationCAClientTest
, EnrollRequest
) {
63 AttestationCAClient client
;
64 client
.SendEnrollRequest(
66 base::Bind(&AttestationCAClientTest::DataCallback
,
67 base::Unretained(this)));
68 SendResponse(net::OK
, net::HTTP_OK
);
70 EXPECT_EQ(1, num_invocations_
);
72 EXPECT_EQ("enroll_response", data_
);
75 TEST_F(AttestationCAClientTest
, CertificateRequest
) {
76 AttestationCAClient client
;
77 client
.SendCertificateRequest(
79 base::Bind(&AttestationCAClientTest::DataCallback
,
80 base::Unretained(this)));
81 SendResponse(net::OK
, net::HTTP_OK
);
83 EXPECT_EQ(1, num_invocations_
);
85 EXPECT_EQ("certificate_response", data_
);
88 TEST_F(AttestationCAClientTest
, CertificateRequestNetworkFailure
) {
89 AttestationCAClient client
;
90 client
.SendCertificateRequest(
92 base::Bind(&AttestationCAClientTest::DataCallback
,
93 base::Unretained(this)));
94 SendResponse(net::ERR_FAILED
, net::HTTP_OK
);
96 EXPECT_EQ(1, num_invocations_
);
97 EXPECT_FALSE(result_
);
101 TEST_F(AttestationCAClientTest
, CertificateRequestHttpError
) {
102 AttestationCAClient client
;
103 client
.SendCertificateRequest(
105 base::Bind(&AttestationCAClientTest::DataCallback
,
106 base::Unretained(this)));
107 SendResponse(net::OK
, net::HTTP_NOT_FOUND
);
109 EXPECT_EQ(1, num_invocations_
);
110 EXPECT_FALSE(result_
);
111 EXPECT_EQ("", data_
);
114 TEST_F(AttestationCAClientTest
, DeleteOnCallback
) {
115 AttestationCAClient
* client
= new AttestationCAClient();
116 client
->SendCertificateRequest(
118 base::Bind(&AttestationCAClientTest::DeleteClientDataCallback
,
119 base::Unretained(this),
121 SendResponse(net::OK
, net::HTTP_OK
);
123 EXPECT_EQ(1, num_invocations_
);
124 EXPECT_TRUE(result_
);
125 EXPECT_EQ("certificate_response", data_
);
128 } // namespace attestation
129 } // namespace chromeos