forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test suite covering both blink::SecurityOrigin and url::Origin.
This CL adds an AbstractOriginTest test suite that covers both blink::SecurityOrigin and url::Origin. Having tests that are shared across Blink and //url will help 1) avoid code duplication and 2) ensure that Blink and //url behave in the same way (at least wrt things covered by the shared tests). Bug: 1164416 Change-Id: I1a4ecb32eba4d31ca07328a43f4392586588aca5 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2606715 Commit-Queue: Łukasz Anforowicz <lukasza@chromium.org> Reviewed-by: Dave Tapuska <dtapuska@chromium.org> Reviewed-by: Tom Sepez <tsepez@chromium.org> Reviewed-by: Daniel Cheng <dcheng@chromium.org> Cr-Commit-Position: refs/heads/master@{#841679}
- Loading branch information
1 parent
5a849c9
commit 0394357
Showing
5 changed files
with
183 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2020 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef URL_ORIGIN_ABSTRACT_TESTS_H_ | ||
#define URL_ORIGIN_ABSTRACT_TESTS_H_ | ||
|
||
#include <string> | ||
#include <type_traits> | ||
|
||
#include "base/strings/string_piece.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
#include "url/url_util.h" | ||
|
||
namespace url { | ||
|
||
// AbstractOriginTest below abstracts away differences between url::Origin and | ||
// blink::SecurityOrigin by parametrizing the tests with a class that has to be | ||
// derived from OriginTraitsBase below. | ||
template <typename TConcreteOriginType> | ||
class OriginTraitsBase { | ||
public: | ||
using OriginType = TConcreteOriginType; | ||
OriginTraitsBase() = default; | ||
|
||
// Constructing an origin. | ||
virtual OriginType CreateOriginFromString(base::StringPiece s) const = 0; | ||
|
||
// Accessors for origin properties. | ||
virtual bool IsOpaque(const OriginType& origin) const = 0; | ||
virtual std::string GetScheme(const OriginType& origin) const = 0; | ||
virtual std::string GetHost(const OriginType& origin) const = 0; | ||
virtual uint16_t GetPort(const OriginType& origin) const = 0; | ||
|
||
// This type is non-copyable and non-moveable. | ||
OriginTraitsBase(const OriginTraitsBase&) = delete; | ||
OriginTraitsBase& operator=(const OriginTraitsBase&) = delete; | ||
}; | ||
|
||
// Test suite for tests that cover both url::Origin and blink::SecurityOrigin. | ||
template <typename TOriginTraits> | ||
class AbstractOriginTest : public testing::Test { | ||
static_assert( | ||
std::is_base_of<OriginTraitsBase<typename TOriginTraits::OriginType>, | ||
TOriginTraits>::value, | ||
"TOriginTraits needs to expose the right members."); | ||
|
||
protected: | ||
// Wrappers that allow tests to ignore presence of `origin_traits_`. | ||
// | ||
// Note that calling the wrappers needs to be prefixed with `this->...` to | ||
// avoid hitting: explicit qualification required to use member 'IsOpaque' | ||
// from dependent base class. | ||
using OriginType = typename TOriginTraits::OriginType; | ||
OriginType CreateOriginFromString(base::StringPiece s) const { | ||
return origin_traits_.CreateOriginFromString(s); | ||
} | ||
bool IsOpaque(const OriginType& origin) const { | ||
return origin_traits_.IsOpaque(origin); | ||
} | ||
std::string GetScheme(const OriginType& origin) const { | ||
return origin_traits_.GetScheme(origin); | ||
} | ||
std::string GetHost(const OriginType& origin) const { | ||
return origin_traits_.GetHost(origin); | ||
} | ||
uint16_t GetPort(const OriginType& origin) const { | ||
return origin_traits_.GetPort(origin); | ||
} | ||
|
||
private: | ||
TOriginTraits origin_traits_; | ||
}; | ||
|
||
TYPED_TEST_SUITE_P(AbstractOriginTest); | ||
|
||
TYPED_TEST_P(AbstractOriginTest, NonStandardSchemeWithAndroidWebViewHack) { | ||
ScopedSchemeRegistryForTests scoped_registry; | ||
EnableNonStandardSchemesForAndroidWebView(); | ||
|
||
// Regression test for https://crbug.com/896059. | ||
auto origin = this->CreateOriginFromString("cow://"); | ||
EXPECT_FALSE(this->IsOpaque(origin)); | ||
EXPECT_EQ("cow", this->GetScheme(origin)); | ||
EXPECT_EQ("", this->GetHost(origin)); | ||
EXPECT_EQ(0, this->GetPort(origin)); | ||
|
||
// about:blank translates into an opaque origin, even in presence of | ||
// EnableNonStandardSchemesForAndroidWebView. | ||
origin = this->CreateOriginFromString("about:blank"); | ||
EXPECT_TRUE(this->IsOpaque(origin)); | ||
} | ||
|
||
REGISTER_TYPED_TEST_SUITE_P(AbstractOriginTest, | ||
NonStandardSchemeWithAndroidWebViewHack); | ||
|
||
} // namespace url | ||
|
||
#endif // URL_ORIGIN_ABSTRACT_TESTS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters