Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / chromeos / attestation / attestation_ca_client_unittest.cc
blobf08d1dbfaee260fad3d4cba92d7cff91f2da24a0
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.
5 #include "base/bind.h"
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"
15 namespace chromeos {
16 namespace attestation {
18 class AttestationCAClientTest : public ::testing::Test {
19 public:
20 AttestationCAClientTest()
21 : io_thread_(content::BrowserThread::IO, &message_loop_),
22 num_invocations_(0),
23 result_(false) {
26 ~AttestationCAClientTest() override {}
28 void DataCallback (bool result, const std::string& data) {
29 ++num_invocations_;
30 result_ = result;
31 data_ = data;
34 void DeleteClientDataCallback (AttestationCAClient* client,
35 bool result,
36 const std::string& data) {
37 delete client;
38 DataCallback(result, data);
41 protected:
42 void SendResponse(net::URLRequestStatus::Status status, int response_code) {
43 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
44 CHECK(fetcher);
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.
56 int num_invocations_;
57 bool result_;
58 std::string data_;
61 TEST_F(AttestationCAClientTest, EnrollRequest) {
62 AttestationCAClient client;
63 client.SendEnrollRequest(
64 "enroll",
65 base::Bind(&AttestationCAClientTest::DataCallback,
66 base::Unretained(this)));
67 SendResponse(net::URLRequestStatus::SUCCESS, net::HTTP_OK);
69 EXPECT_EQ(1, num_invocations_);
70 EXPECT_TRUE(result_);
71 EXPECT_EQ("enroll_response", data_);
74 TEST_F(AttestationCAClientTest, CertificateRequest) {
75 AttestationCAClient client;
76 client.SendCertificateRequest(
77 "certificate",
78 base::Bind(&AttestationCAClientTest::DataCallback,
79 base::Unretained(this)));
80 SendResponse(net::URLRequestStatus::SUCCESS, net::HTTP_OK);
82 EXPECT_EQ(1, num_invocations_);
83 EXPECT_TRUE(result_);
84 EXPECT_EQ("certificate_response", data_);
87 TEST_F(AttestationCAClientTest, CertificateRequestNetworkFailure) {
88 AttestationCAClient client;
89 client.SendCertificateRequest(
90 "certificate",
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_);
97 EXPECT_EQ("", data_);
100 TEST_F(AttestationCAClientTest, CertificateRequestHttpError) {
101 AttestationCAClient client;
102 client.SendCertificateRequest(
103 "certificate",
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(
116 "certificate",
117 base::Bind(&AttestationCAClientTest::DeleteClientDataCallback,
118 base::Unretained(this),
119 client));
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