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

fuchsia_web / webengine / browser / url_request_invalidation_browsertest.cc [blame]

// 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 <fuchsia/web/cpp/fidl.h>

#include "base/run_loop.h"
#include "content/public/test/browser_test.h"
#include "fuchsia_web/common/test/frame_for_test.h"
#include "fuchsia_web/common/test/frame_test_util.h"
#include "fuchsia_web/common/test/test_navigation_listener.h"
#include "fuchsia_web/common/test/url_request_rewrite_test_util.h"
#include "fuchsia_web/webengine/test/web_engine_browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

// Test harness for browser tests exercising the URL request rewrite rule
// invalidation logic. Sending incorrect rules should result in the frame
// disconnecting.
class UrlRequestInvalidationTest : public WebEngineBrowserTest {
 public:
  UrlRequestInvalidationTest() = default;
  ~UrlRequestInvalidationTest() override = default;

  UrlRequestInvalidationTest(const UrlRequestInvalidationTest&) = delete;
  UrlRequestInvalidationTest& operator=(const UrlRequestInvalidationTest&) =
      delete;

  // Tests that sending |rewrite| will cause the fuchsia.web.Frame to disconnect
  // with ZX_ERR_INVALID_ARGS.
  void ExpectInvalidArgsForRewrite(fuchsia::web::UrlRequestRewrite rewrite) {
    auto frame = FrameForTest::Create(context(), {});
    base::RunLoop run_loop;
    frame.ptr().set_error_handler([&run_loop](zx_status_t status) {
      EXPECT_EQ(status, ZX_ERR_INVALID_ARGS);
      run_loop.Quit();
    });

    std::vector<fuchsia::web::UrlRequestRewrite> rewrites;
    rewrites.push_back(std::move(rewrite));
    fuchsia::web::UrlRequestRewriteRule rule;
    rule.set_rewrites(std::move(rewrites));
    std::vector<fuchsia::web::UrlRequestRewriteRule> rules;
    rules.push_back(std::move(rule));

    frame->SetUrlRequestRewriteRules(std::move(rules), []() {});
    run_loop.Run();
  }
};

IN_PROC_BROWSER_TEST_F(UrlRequestInvalidationTest, EmptyRewrite) {
  ExpectInvalidArgsForRewrite(fuchsia::web::UrlRequestRewrite());
}

IN_PROC_BROWSER_TEST_F(UrlRequestInvalidationTest, InvalidAddHeaderName) {
  ExpectInvalidArgsForRewrite(CreateRewriteAddHeaders("Te\nst1", "Value"));
}

IN_PROC_BROWSER_TEST_F(UrlRequestInvalidationTest, InvalidAddHeaderValue) {
  ExpectInvalidArgsForRewrite(CreateRewriteAddHeaders("Test1", "Val\nue"));
}

IN_PROC_BROWSER_TEST_F(UrlRequestInvalidationTest, EmptyAddHeader) {
  fuchsia::web::UrlRequestRewrite rewrite;
  rewrite.set_add_headers(fuchsia::web::UrlRequestRewriteAddHeaders());
  ExpectInvalidArgsForRewrite(std::move(rewrite));
}

IN_PROC_BROWSER_TEST_F(UrlRequestInvalidationTest, InvalidRemoveHeaderName) {
  ExpectInvalidArgsForRewrite(CreateRewriteRemoveHeader("Query", "Head\ner"));
}

IN_PROC_BROWSER_TEST_F(UrlRequestInvalidationTest, EmptyRemoveHeader) {
  fuchsia::web::UrlRequestRewrite rewrite;
  rewrite.set_remove_header(fuchsia::web::UrlRequestRewriteRemoveHeader());
  ExpectInvalidArgsForRewrite(std::move(rewrite));
}

IN_PROC_BROWSER_TEST_F(UrlRequestInvalidationTest,
                       EmptySubstituteQueryPattern) {
  fuchsia::web::UrlRequestRewrite rewrite;
  rewrite.set_substitute_query_pattern(
      fuchsia::web::UrlRequestRewriteSubstituteQueryPattern());
  ExpectInvalidArgsForRewrite(std::move(rewrite));
}

IN_PROC_BROWSER_TEST_F(UrlRequestInvalidationTest,
                       InvalidReplaceUrlNewUrlWithNullUrlPath) {
  ExpectInvalidArgsForRewrite(
      CreateRewriteReplaceUrl("some%00thing", "http:site:xyz"));
}

IN_PROC_BROWSER_TEST_F(UrlRequestInvalidationTest, InvalidReplaceUrlNewUrl) {
  ExpectInvalidArgsForRewrite(
      CreateRewriteReplaceUrl("/something", "http:site:xyz"));
}

// TODO(http://crbug.com/4596360): The following cases should succeed, but
// checking for success requires non-trivial work since this
// `UrlRequestInvalidationTest` is designed to test only invalid cases. Someone
// familiar with this code might want to test success cases.
//
// - CreateRewriteReplaceUrl("/something", "http://site.xyz/"));
// - CreateRewriteReplaceUrl("some%00thing", "http://site.xyz/"));
// - CreateRewriteReplaceUrl("/something", "http://site.xyz/%00"));
// - CreateRewriteReplaceUrl("some%00thing", "http://site.xyz/%00"));

IN_PROC_BROWSER_TEST_F(UrlRequestInvalidationTest, EmptyReplaceUrl) {
  fuchsia::web::UrlRequestRewrite rewrite;
  rewrite.set_replace_url(fuchsia::web::UrlRequestRewriteReplaceUrl());
  ExpectInvalidArgsForRewrite(std::move(rewrite));
}

IN_PROC_BROWSER_TEST_F(UrlRequestInvalidationTest, EmptyAppendToQuery) {
  fuchsia::web::UrlRequestRewrite rewrite;
  rewrite.set_append_to_query(fuchsia::web::UrlRequestRewriteAppendToQuery());
  ExpectInvalidArgsForRewrite(std::move(rewrite));
}

}  // namespace