diff options
Diffstat (limited to 'chromium/third_party/blink/renderer/platform/storage/blink_storage_key_test.cc')
-rw-r--r-- | chromium/third_party/blink/renderer/platform/storage/blink_storage_key_test.cc | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/chromium/third_party/blink/renderer/platform/storage/blink_storage_key_test.cc b/chromium/third_party/blink/renderer/platform/storage/blink_storage_key_test.cc new file mode 100644 index 00000000000..1808328f1b0 --- /dev/null +++ b/chromium/third_party/blink/renderer/platform/storage/blink_storage_key_test.cc @@ -0,0 +1,135 @@ +// Copyright 2021 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. + +#include "third_party/blink/renderer/platform/storage/blink_storage_key.h" + +#include "base/memory/scoped_refptr.h" +#include "base/unguessable_token.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "third_party/blink/renderer/platform/weborigin/security_origin.h" +#include "url/gurl.h" + +namespace blink { + +TEST(BlinkStorageKeyTest, OpaqueOriginsDistinct) { + // Test that two opaque origins give distinct BlinkStorageKeys. + BlinkStorageKey unique_opaque1; + EXPECT_TRUE(unique_opaque1.GetSecurityOrigin()); + EXPECT_TRUE(unique_opaque1.GetSecurityOrigin()->IsOpaque()); + BlinkStorageKey unique_opaque2; + EXPECT_FALSE(unique_opaque2.GetSecurityOrigin()->IsSameOriginWith( + unique_opaque1.GetSecurityOrigin().get())); + EXPECT_NE(unique_opaque1, unique_opaque2); +} + +TEST(BlinkStorageKeyTest, EqualityWithNonce) { + // Test that BlinkStorageKeys with different nonces are different. + scoped_refptr<const SecurityOrigin> origin = + SecurityOrigin::CreateFromString("https://example.com"); + base::UnguessableToken token1 = base::UnguessableToken::Create(); + base::UnguessableToken token2 = base::UnguessableToken::Create(); + BlinkStorageKey key1 = BlinkStorageKey::CreateWithNonce(origin, token1); + BlinkStorageKey key2 = BlinkStorageKey::CreateWithNonce(origin, token1); + BlinkStorageKey key3 = BlinkStorageKey::CreateWithNonce(origin, token2); + + EXPECT_TRUE(key1.GetSecurityOrigin()->IsSameOriginWith( + key2.GetSecurityOrigin().get())); + EXPECT_TRUE(key1.GetSecurityOrigin()->IsSameOriginWith( + key3.GetSecurityOrigin().get())); + EXPECT_TRUE(key2.GetSecurityOrigin()->IsSameOriginWith( + key3.GetSecurityOrigin().get())); + + EXPECT_EQ(key1, key2); + EXPECT_NE(key1, key3); +} + +TEST(BlinkStorageKeyTest, OpaqueOriginRetained) { + // Test that a StorageKey made from an opaque origin retains the origin. + scoped_refptr<const SecurityOrigin> opaque_origin = + SecurityOrigin::CreateUniqueOpaque(); + scoped_refptr<const SecurityOrigin> opaque_copied = + opaque_origin->IsolatedCopy(); + BlinkStorageKey from_opaque(std::move(opaque_origin)); + EXPECT_TRUE( + from_opaque.GetSecurityOrigin()->IsSameOriginWith(opaque_copied.get())); +} + +TEST(BlinkStorageKeyTest, CreateFromNonOpaqueOrigin) { + struct { + const char* origin; + } kTestCases[] = { + {"http://example.site"}, + {"https://example.site"}, + {"file:///path/to/file"}, + }; + + for (const auto& test : kTestCases) { + scoped_refptr<const SecurityOrigin> origin = + SecurityOrigin::CreateFromString(test.origin); + ASSERT_FALSE(origin->IsOpaque()); + scoped_refptr<const SecurityOrigin> copied = origin->IsolatedCopy(); + + // Test that the origin is retained. + BlinkStorageKey storage_key(std::move(origin)); + EXPECT_TRUE( + storage_key.GetSecurityOrigin()->IsSameOriginWith(copied.get())); + + // Test that two StorageKeys from the same origin are the same. + BlinkStorageKey storage_key_from_copy(std::move(copied)); + EXPECT_EQ(storage_key, storage_key_from_copy); + } +} + +// Tests that the conversion BlinkStorageKey -> StorageKey -> BlinkStorageKey is +// the identity. +TEST(BlinkStorageKeyTest, BlinkStorageKeyRoundTripConversion) { + scoped_refptr<const SecurityOrigin> origin1 = + SecurityOrigin::CreateUniqueOpaque(); + scoped_refptr<const SecurityOrigin> origin2 = + SecurityOrigin::CreateFromString("http://example.site"); + scoped_refptr<const SecurityOrigin> origin3 = + SecurityOrigin::CreateFromString("https://example.site"); + scoped_refptr<const SecurityOrigin> origin4 = + SecurityOrigin::CreateFromString("file:///path/to/file"); + base::UnguessableToken nonce = base::UnguessableToken::Create(); + + Vector<BlinkStorageKey> keys = { + BlinkStorageKey(), + BlinkStorageKey(origin1), + BlinkStorageKey(origin2), + BlinkStorageKey(origin3), + BlinkStorageKey(origin4), + BlinkStorageKey::CreateWithNonce(origin1, nonce), + BlinkStorageKey::CreateWithNonce(origin2, nonce), + }; + + for (BlinkStorageKey& key : keys) { + EXPECT_EQ(key, BlinkStorageKey(StorageKey(key))); + } +} + +// Tests that the conversion StorageKey -> BlinkStorageKey -> StorageKey is the +// identity. +TEST(BlinkStorageKey, StorageKeyRoundTripConversion) { + url::Origin url_origin1; + url::Origin url_origin2 = url::Origin::Create(GURL("http://example.site")); + url::Origin url_origin3 = url::Origin::Create(GURL("https://example.site")); + url::Origin url_origin4 = url::Origin::Create(GURL("file:///path/to/file")); + base::UnguessableToken nonce = base::UnguessableToken::Create(); + + Vector<StorageKey> storage_keys = { + StorageKey(url_origin1), + StorageKey(url_origin2), + StorageKey(url_origin3), + StorageKey(url_origin4), + StorageKey::CreateWithNonce(url_origin1, nonce), + StorageKey::CreateWithNonce(url_origin2, nonce), + }; + + for (const auto& key : storage_keys) { + EXPECT_EQ(key, StorageKey(BlinkStorageKey(key))); + } +} + +} // namespace blink |