Adding subframe_origin to NetworkIsolationKey.

This CL adds the subframe_origin member to NetworkIsolationKey, as
well as adding a flag to enable it.  When enabled, it is used by the
NetworkIsolationKey, when disabled, it is unused and the field is
always empty.

Change-Id: I97a65eefaaa50c7e5931589db5e38cb2161104aa
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1644895
Commit-Queue: Eric Robinson <[email protected]>
Reviewed-by: Shivani Sharma <[email protected]>
Reviewed-by: Matt Menke <[email protected]>
Cr-Commit-Position: refs/heads/master@{#671310}
diff --git a/net/base/network_isolation_key_unittest.cc b/net/base/network_isolation_key_unittest.cc
index 74549a8..d73931f 100644
--- a/net/base/network_isolation_key_unittest.cc
+++ b/net/base/network_isolation_key_unittest.cc
@@ -5,6 +5,8 @@
 #include "net/base/network_isolation_key.h"
 
 #include "base/stl_util.h"
+#include "base/test/scoped_feature_list.h"
+#include "net/base/features.h"
 #include "testing/gtest/include/gtest/gtest.h"
 #include "url/gurl.h"
 #include "url/origin.h"
@@ -104,4 +106,125 @@
   EXPECT_TRUE(!(key1 < key2) || !(key2 < key1));
 }
 
+TEST(NetworkIsolationKeyTest, WithInitiatingFrame) {
+  NetworkIsolationKey key1(url::Origin::Create(GURL("http://b.test")));
+  NetworkIsolationKey key2(url::Origin::Create(GURL("http://b.test")),
+                           url::Origin::Create(GURL("http://a.test/")));
+  EXPECT_TRUE(key2.IsFullyPopulated());
+  EXPECT_FALSE(key2.IsTransient());
+  EXPECT_EQ("http://b.test", key2.ToString());
+  EXPECT_EQ("http://b.test", key2.ToDebugString());
+
+  EXPECT_TRUE(key1 == key2);
+  EXPECT_FALSE(key1 != key2);
+  EXPECT_FALSE(key1 < key2);
+  EXPECT_FALSE(key2 < key1);
+}
+
+TEST(NetworkIsolationKeyTest, OpaqueOriginKeyWithInitiatingFrame) {
+  url::Origin origin_data =
+      url::Origin::Create(GURL("data:text/html,<body>Hello World</body>"));
+
+  NetworkIsolationKey key1(url::Origin::Create(GURL("http://a.test")),
+                           origin_data);
+  EXPECT_TRUE(key1.IsFullyPopulated());
+  EXPECT_FALSE(key1.IsTransient());
+  EXPECT_EQ("http://a.test", key1.ToString());
+  EXPECT_EQ("http://a.test", key1.ToDebugString());
+
+  NetworkIsolationKey key2(origin_data,
+                           url::Origin::Create(GURL("http://a.test")));
+  EXPECT_TRUE(key2.IsFullyPopulated());
+  EXPECT_TRUE(key2.IsTransient());
+  EXPECT_EQ("", key2.ToString());
+  EXPECT_EQ(origin_data.GetDebugString(), key2.ToDebugString());
+  EXPECT_NE(origin_data.DeriveNewOpaqueOrigin().GetDebugString(),
+            key2.ToDebugString());
+}
+
+class NetworkIsolationKeyWithInitiatingFrameOriginTest : public testing::Test {
+ public:
+  NetworkIsolationKeyWithInitiatingFrameOriginTest() {
+    feature_list_.InitAndEnableFeature(
+        net::features::kAppendInitiatingFrameOriginToNetworkIsolationKey);
+  }
+
+ private:
+  base::test::ScopedFeatureList feature_list_;
+};
+
+TEST_F(NetworkIsolationKeyWithInitiatingFrameOriginTest, WithInitiatingFrame) {
+  NetworkIsolationKey key(url::Origin::Create(GURL("http://b.test")),
+                          url::Origin::Create(GURL("http://a.test/")));
+  EXPECT_TRUE(key.IsFullyPopulated());
+  EXPECT_FALSE(key.IsTransient());
+  EXPECT_EQ("http://b.test http://a.test", key.ToString());
+  EXPECT_EQ("http://b.test http://a.test", key.ToDebugString());
+
+  EXPECT_TRUE(key == key);
+  EXPECT_FALSE(key != key);
+  EXPECT_FALSE(key < key);
+}
+
+TEST_F(NetworkIsolationKeyWithInitiatingFrameOriginTest, OpaqueOriginKey) {
+  url::Origin origin_data =
+      url::Origin::Create(GURL("data:text/html,<body>Hello World</body>"));
+
+  NetworkIsolationKey key1(url::Origin::Create(GURL("http://a.test")),
+                           origin_data);
+  EXPECT_TRUE(key1.IsFullyPopulated());
+  EXPECT_TRUE(key1.IsTransient());
+  EXPECT_EQ("", key1.ToString());
+  EXPECT_EQ("http://a.test " + origin_data.GetDebugString(),
+            key1.ToDebugString());
+  EXPECT_NE(
+      "http://a.test " + origin_data.DeriveNewOpaqueOrigin().GetDebugString(),
+      key1.ToDebugString());
+
+  NetworkIsolationKey key2(origin_data,
+                           url::Origin::Create(GURL("http://a.test")));
+  EXPECT_TRUE(key2.IsFullyPopulated());
+  EXPECT_TRUE(key2.IsTransient());
+  EXPECT_EQ("", key2.ToString());
+  EXPECT_EQ(origin_data.GetDebugString() + " http://a.test",
+            key2.ToDebugString());
+  EXPECT_NE(
+      origin_data.DeriveNewOpaqueOrigin().GetDebugString() + " http://a.test",
+      key2.ToDebugString());
+}
+
+TEST_F(NetworkIsolationKeyWithInitiatingFrameOriginTest, OpaqueOriginKeyBoth) {
+  url::Origin origin_data_1 =
+      url::Origin::Create(GURL("data:text/html,<body>Hello World</body>"));
+  url::Origin origin_data_2 =
+      url::Origin::Create(GURL("data:text/html,<body>Hello Universe</body>"));
+  url::Origin origin_data_3 =
+      url::Origin::Create(GURL("data:text/html,<body>Hello Cosmos</body>"));
+
+  NetworkIsolationKey key1(origin_data_1, origin_data_2);
+  NetworkIsolationKey key2(origin_data_1, origin_data_2);
+  NetworkIsolationKey key3(origin_data_1, origin_data_3);
+
+  // All the keys should be fully populated and transient.
+  EXPECT_TRUE(key1.IsFullyPopulated());
+  EXPECT_TRUE(key2.IsFullyPopulated());
+  EXPECT_TRUE(key3.IsFullyPopulated());
+  EXPECT_TRUE(key1.IsTransient());
+  EXPECT_TRUE(key2.IsTransient());
+  EXPECT_TRUE(key3.IsTransient());
+
+  // Test the equality/comparisons of the various keys
+  EXPECT_TRUE(key1 == key2);
+  EXPECT_FALSE(key1 == key3);
+  EXPECT_FALSE(key1 < key2 || key2 < key1);
+  EXPECT_TRUE(key1 < key3 || key3 < key1);
+
+  // Test the ToString and ToDebugString
+  EXPECT_EQ(key1.ToDebugString(), key2.ToDebugString());
+  EXPECT_NE(key1.ToDebugString(), key3.ToDebugString());
+  EXPECT_EQ("", key1.ToString());
+  EXPECT_EQ("", key2.ToString());
+  EXPECT_EQ("", key3.ToString());
+}
+
 }  // namespace net