File: plugin_response_writer_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 (246 lines) | stat: -rw-r--r-- 8,214 bytes parent folder | download | duplicates (3)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/pdf/browser/plugin_response_writer.h"

#include <stddef.h>

#include <memory>
#include <string>
#include <utility>

#include "base/containers/span.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "base/strings/string_view_util.h"
#include "base/test/mock_callback.h"
#include "base/test/task_environment.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/system/data_pipe.h"
#include "mojo/public/cpp/system/data_pipe_drainer.h"
#include "net/base/net_errors.h"
#include "net/http/http_response_headers.h"
#include "services/network/public/cpp/url_loader_completion_status.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "services/network/test/mock_url_loader_client.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace pdf {

namespace {

using ::network::MockURLLoaderClient;

using ::testing::HasSubstr;
using ::testing::NiceMock;
using ::testing::Not;

class BodyDrainer {
 public:
  explicit BodyDrainer(mojo::ScopedDataPipeConsumerHandle body)
      : drainer_(&drainer_client_, std::move(body)) {}

  const std::string& content() const { return drainer_client_.content; }

  void WaitComplete() {
    if (drainer_client_.is_complete)
      return;

    base::RunLoop run_loop;
    ASSERT_FALSE(drainer_client_.quit_closure);
    drainer_client_.quit_closure = run_loop.QuitClosure();
    run_loop.Run();

    EXPECT_TRUE(drainer_client_.is_complete);
  }

 private:
  struct DrainerClient : public mojo::DataPipeDrainer::Client {
    void OnDataAvailable(base::span<const uint8_t> data) override {
      content.append(base::as_string_view(data));
    }

    void OnDataComplete() override {
      is_complete = true;
      if (quit_closure)
        std::move(quit_closure).Run();
    }

    std::string content;
    bool is_complete = false;
    base::OnceClosure quit_closure;
  };

  DrainerClient drainer_client_;
  mojo::DataPipeDrainer drainer_;
};

class PluginResponseWriterTest : public testing::Test {
 protected:
  PluginResponseWriterTest() {
    ON_CALL(mock_client_, OnReceiveResponse)
        .WillByDefault(
            [this](network::mojom::URLResponseHeadPtr head,
                   mojo::ScopedDataPipeConsumerHandle body,
                   std::optional<mojo_base::BigBuffer> cached_metadata) {
              body_drainer_ = std::make_unique<BodyDrainer>(std::move(body));
            });
  }

  std::unique_ptr<PluginResponseWriter> NewPluginResponseWriter(
      const PdfStreamDelegate::StreamInfo& stream_info) {
    return std::make_unique<PluginResponseWriter>(
        stream_info, client_receiver_.BindNewPipeAndPassRemote());
  }

  // Returns the generated response after sending it.
  std::string GenerateResponse(const PdfStreamDelegate::StreamInfo& stream) {
    auto response_writer = NewPluginResponseWriter(stream);

    base::RunLoop run_loop;
    EXPECT_CALL(mock_client_, OnComplete).WillOnce([&run_loop]() {
      run_loop.Quit();
    });
    response_writer->Start(base::DoNothing());
    run_loop.Run();

    // Waiting for `URLLoaderClient::OnComplete()` ensures `body_drainer_` is
    // set, but the data pipe may still have unread data.
    body_drainer_->WaitComplete();

    return body_drainer_->content();
  }

  base::test::TaskEnvironment task_environment_;
  NiceMock<MockURLLoaderClient> mock_client_;
  mojo::Receiver<network::mojom::URLLoaderClient> client_receiver_{
      &mock_client_};

  std::unique_ptr<BodyDrainer> body_drainer_;
};

}  // namespace

TEST_F(PluginResponseWriterTest, Start) {
  const std::string kFakeScript = "fake-script";

  PdfStreamDelegate::StreamInfo stream;
  stream.stream_url = GURL("chrome-extension://id/stream-url");
  stream.original_url = GURL("https://example.test/fake.pdf");
  stream.injected_script = &kFakeScript;
  stream.background_color = SK_ColorGREEN;
  stream.full_frame = true;
  stream.allow_javascript = true;
  auto response_writer = NewPluginResponseWriter(stream);

  base::RunLoop run_loop;

  {
    // Note that `URLLoaderClient` operations are received on a separate
    // sequence, and only are ordered with respect to each other.
    testing::InSequence in_sequence;

    EXPECT_CALL(mock_client_, OnReceiveResponse)
        .WillOnce([this](network::mojom::URLResponseHeadPtr head,
                         mojo::ScopedDataPipeConsumerHandle body,
                         std::optional<mojo_base::BigBuffer> cached_metadata) {
          EXPECT_EQ(200, head->headers->response_code());
          EXPECT_EQ("text/html", head->mime_type);
          body_drainer_ = std::make_unique<BodyDrainer>(std::move(body));
        });

    EXPECT_CALL(mock_client_, OnComplete)
        .WillOnce(
            [&run_loop](const network::URLLoaderCompletionStatus& status) {
              EXPECT_EQ(net::OK, status.error_code);
              run_loop.Quit();
            });
  }

  // Note that `done_callback` is not ordered with respect to the
  // `URLLoaderClient` operations.
  base::MockCallback<base::OnceClosure> done_callback;
  EXPECT_CALL(done_callback, Run);

  response_writer->Start(done_callback.Get());
  run_loop.Run();

  // Waiting for `URLLoaderClient::OnComplete()` ensures `body_drainer_` is set,
  // but the data pipe may still have unread data.
  ASSERT_TRUE(body_drainer_);
  body_drainer_->WaitComplete();

  EXPECT_THAT(body_drainer_->content(),
              HasSubstr("src=\"chrome-extension://id/stream-url\""));
  EXPECT_THAT(body_drainer_->content(),
              HasSubstr("original-url=\"https://example.test/fake.pdf\""));
  EXPECT_THAT(body_drainer_->content(), HasSubstr(kFakeScript));
  EXPECT_THAT(body_drainer_->content(),
              HasSubstr("background-color=\"4278255360\""));
  EXPECT_THAT(body_drainer_->content(), HasSubstr("javascript=\"allow\""));
  EXPECT_THAT(body_drainer_->content(), HasSubstr("full-frame"));
}

TEST_F(PluginResponseWriterTest, StartWithUnescapedUrls) {
  PdfStreamDelegate::StreamInfo stream;
  stream.stream_url = GURL("chrome-extension://id/stream-url\"");
  stream.original_url = GURL("https://example.test/\"fake.pdf");
  std::string response = GenerateResponse(stream);

  EXPECT_THAT(response,
              HasSubstr("src=\"chrome-extension://id/stream-url%22\""));
  EXPECT_THAT(response,
              HasSubstr("original-url=\"https://example.test/%22fake.pdf\""));
}

TEST_F(PluginResponseWriterTest, StartForPrintPreview) {
  PdfStreamDelegate::StreamInfo stream;
  stream.stream_url = GURL("chrome-untrusted://print/1/0/print.pdf");
  stream.original_url = GURL("chrome-untrusted://print/1/0/print.pdf");
  std::string response = GenerateResponse(stream);

  EXPECT_THAT(response,
              HasSubstr("src=\"chrome-untrusted://print/1/0/print.pdf\""));
  EXPECT_THAT(
      response,
      HasSubstr("original-url=\"chrome-untrusted://print/1/0/print.pdf\""));
}

TEST_F(PluginResponseWriterTest, StartWithoutInjectedScript) {
  PdfStreamDelegate::StreamInfo stream;
  stream.injected_script = nullptr;
  std::string response = GenerateResponse(stream);

  EXPECT_THAT(response, Not(HasSubstr("fake-script")));
}

TEST_F(PluginResponseWriterTest, StartWithBackgroundColor) {
  PdfStreamDelegate::StreamInfo stream;
  stream.background_color = SK_ColorBLUE;
  std::string response = GenerateResponse(stream);

  EXPECT_THAT(response, HasSubstr("background-color=\"4278190335\""));
}

TEST_F(PluginResponseWriterTest, StartWithNonFullFrame) {
  PdfStreamDelegate::StreamInfo stream;
  stream.full_frame = false;
  std::string response = GenerateResponse(stream);

  EXPECT_THAT(response, Not(HasSubstr("full-frame")));
}

TEST_F(PluginResponseWriterTest, StartWithJavaScriptDisabled) {
  PdfStreamDelegate::StreamInfo stream;
  stream.allow_javascript = false;
  std::string response = GenerateResponse(stream);

  EXPECT_THAT(response, HasSubstr("javascript=\"block\""));
}

}  // namespace pdf