File: ipp_endpoint_token_fetcher_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (151 lines) | stat: -rw-r--r-- 5,860 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/printing/oauth2/ipp_endpoint_token_fetcher.h"

#include <memory>
#include <string>

#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"
#include "base/values.h"
#include "chrome/browser/ash/printing/oauth2/constants.h"
#include "chrome/browser/ash/printing/oauth2/status_code.h"
#include "chrome/browser/ash/printing/oauth2/test_authorization_server.h"
#include "net/http/http_status_code.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace ash {
namespace printing {
namespace oauth2 {
namespace {

class PrintingOAuth2IppEndpointTokenFetcherTest : public testing::Test {
 protected:
  // Initialize the `session_` field.
  void CreateSession(std::string&& endpoint_uri,
                     base::flat_set<std::string>&& scope) {
    GURL gurl(token_url_);
    CHECK(gurl.is_valid());
    chromeos::Uri uri(endpoint_uri);
    CHECK(uri.GetLastParsingError().status ==
          chromeos::Uri::ParserStatus::kNoErrors);
    session_ = std::make_unique<IppEndpointTokenFetcher>(
        server_.GetURLLoaderFactory(), gurl, uri, std::move(scope));
  }

  // URL of the endpoint at the server.
  const std::string token_url_ = "https://a.b/c";
  // The object simulating the Authorization Server.
  FakeAuthorizationServer server_;
  // The tested session, it is created by the method CreateSession(...).
  std::unique_ptr<IppEndpointTokenFetcher> session_;
};

TEST_F(PrintingOAuth2IppEndpointTokenFetcherTest, InitialState) {
  CreateSession("ipp://my.printer/print", {"ala", "ma", "kota"});
  EXPECT_EQ(session_->ipp_endpoint_uri().GetNormalized(false),
            "ipp://my.printer/print");
  EXPECT_EQ(session_->scope(),
            base::flat_set<std::string>({"ala", "ma", "kota"}));
  EXPECT_TRUE(session_->endpoint_access_token().empty());
  EXPECT_TRUE(session_->TakeWaitingList().empty());
}

TEST_F(PrintingOAuth2IppEndpointTokenFetcherTest, WaitingList) {
  CreateSession("ipp://my.printer/print", {"ala", "ma", "kota"});
  CallbackResult cr1;
  CallbackResult cr2;
  CallbackResult cr3;
  session_->AddToWaitingList(BindResult(cr1));
  session_->AddToWaitingList(BindResult(cr2));
  session_->AddToWaitingList(BindResult(cr3));
  auto callbacks = session_->TakeWaitingList();
  ASSERT_EQ(callbacks.size(), 3u);
  EXPECT_TRUE(session_->TakeWaitingList().empty());
  std::move(callbacks[0]).Run(StatusCode::kOK, "1");
  std::move(callbacks[1]).Run(StatusCode::kAccessDenied, "2");
  std::move(callbacks[2]).Run(StatusCode::kServerError, "3");
  EXPECT_EQ(cr1.status, StatusCode::kOK);
  EXPECT_EQ(cr1.data, "1");
  EXPECT_EQ(cr2.status, StatusCode::kAccessDenied);
  EXPECT_EQ(cr2.data, "2");
  EXPECT_EQ(cr3.status, StatusCode::kServerError);
  EXPECT_EQ(cr3.data, "3");
}

TEST_F(PrintingOAuth2IppEndpointTokenFetcherTest, TokenExchangeRequest) {
  CreateSession("ipp://my.printer:123/print", {});
  CallbackResult cr;
  session_->SendTokenExchangeRequest("access_token_s%!", BindResult(cr));

  // Verify the request.
  base::flat_map<std::string, std::string> params;
  ASSERT_EQ("", server_.ReceivePOSTWithURLParams(token_url_, params));
  EXPECT_EQ(params["grant_type"],
            "urn:ietf:params:oauth:grant-type:token-exchange");
  EXPECT_EQ(params["resource"], "ipp://my.printer:123/print");
  EXPECT_EQ(params["subject_token"], "access_token_s%!");
  EXPECT_EQ(params["subject_token_type"],
            "urn:ietf:params:oauth:token-type:access_token");

  // Prepare and send the response.
  base::Value::Dict fields;
  fields.Set("access_token", "endpoint_access_token_swD");
  fields.Set("issued_token_type",
             "urn:ietf:params:oauth:token-type:access_token");
  fields.Set("token_type", "bearer");
  server_.ResponseWithJSON(net::HttpStatusCode::HTTP_OK, fields);

  // Verify the response.
  EXPECT_EQ(cr.status, StatusCode::kOK);
  EXPECT_EQ(cr.data, "endpoint_access_token_swD");
  EXPECT_EQ(session_->endpoint_access_token(), "endpoint_access_token_swD");
}

TEST_F(PrintingOAuth2IppEndpointTokenFetcherTest, TokenExchangeRequestError) {
  CreateSession("ipp://my.printer:123/print", {});
  CallbackResult cr;
  session_->SendTokenExchangeRequest("access_token_s%!", BindResult(cr));

  // Receive the request and send the response.
  base::flat_map<std::string, std::string> params;
  ASSERT_EQ("", server_.ReceivePOSTWithURLParams(token_url_, params));
  base::Value::Dict fields;
  fields.Set("access_token", "endpoint_access_token_swD");
  fields.Set("issued_token_type",
             "urn:ietf:params:oauth:token-type:access_token");
  // Missing field "token_type".
  server_.ResponseWithJSON(net::HttpStatusCode::HTTP_OK, fields);

  // Verify the response.
  EXPECT_EQ(cr.status, StatusCode::kInvalidResponse);
  // The error message contains "token_type".
  EXPECT_NE(cr.data.find("token_type"), std::string::npos);
  EXPECT_TRUE(session_->endpoint_access_token().empty());
}

TEST_F(PrintingOAuth2IppEndpointTokenFetcherTest, InvalidAccessToken) {
  CreateSession("ipp://my.printer:123/print", {});
  CallbackResult cr;
  session_->SendTokenExchangeRequest("access_token_s%!", BindResult(cr));

  // Receive the request and send the response.
  base::flat_map<std::string, std::string> params;
  ASSERT_EQ("", server_.ReceivePOSTWithURLParams(token_url_, params));
  base::Value::Dict fields;
  fields.Set("error", "invalid_grant");
  server_.ResponseWithJSON(net::HttpStatusCode::HTTP_BAD_REQUEST, fields);

  // Verify the response.
  EXPECT_EQ(cr.status, StatusCode::kInvalidAccessToken);
  EXPECT_EQ(cr.data, "access_token_s%!");
  EXPECT_TRUE(session_->endpoint_access_token().empty());
}

}  // namespace
}  // namespace oauth2
}  // namespace printing
}  // namespace ash