86d73d410a829556f88794b340abaa987bd0c8b1
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WebCore / SecurityOrigin.cpp
1 /*
2  * Copyright (C) 2016 Igalia S.L.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "WTFStringUtilities.h"
28 #include <WebCore/SecurityOrigin.h>
29 #include <WebCore/URL.h>
30 #include <wtf/MainThread.h>
31
32 using namespace WebCore;
33
34 namespace TestWebKitAPI {
35
36 class SecurityOriginTest : public testing::Test {
37 public:
38     void SetUp() final {
39         WTF::initializeMainThread();
40     }
41 };
42
43 TEST_F(SecurityOriginTest, SecurityOriginConstructors)
44 {
45     Ref<SecurityOrigin> o1 = SecurityOrigin::create("http", "example.com", std::optional<uint16_t>(80));
46     Ref<SecurityOrigin> o2 = SecurityOrigin::create("http", "example.com", std::optional<uint16_t>());
47     Ref<SecurityOrigin> o3 = SecurityOrigin::createFromString("http://example.com");
48     Ref<SecurityOrigin> o4 = SecurityOrigin::createFromString("http://example.com:80");
49     Ref<SecurityOrigin> o5 = SecurityOrigin::create(URL(URL(), "http://example.com"));
50     Ref<SecurityOrigin> o6 = SecurityOrigin::create(URL(URL(), "http://example.com:80"));
51
52     EXPECT_EQ(String("http"), o1->protocol());
53     EXPECT_EQ(String("http"), o2->protocol());
54     EXPECT_EQ(String("http"), o3->protocol());
55     EXPECT_EQ(String("http"), o4->protocol());
56     EXPECT_EQ(String("http"), o5->protocol());
57     EXPECT_EQ(String("http"), o6->protocol());
58
59     EXPECT_EQ(String("example.com"), o1->host());
60     EXPECT_EQ(String("example.com"), o2->host());
61     EXPECT_EQ(String("example.com"), o3->host());
62     EXPECT_EQ(String("example.com"), o4->host());
63     EXPECT_EQ(String("example.com"), o5->host());
64     EXPECT_EQ(String("example.com"), o6->host());
65
66     EXPECT_FALSE(o1->port());
67     EXPECT_FALSE(o2->port());
68     EXPECT_FALSE(o3->port());
69     EXPECT_FALSE(o4->port());
70     EXPECT_FALSE(o5->port());
71     EXPECT_FALSE(o6->port());
72
73     EXPECT_EQ("http://example.com", o1->toString());
74     EXPECT_EQ("http://example.com", o2->toString());
75     EXPECT_EQ("http://example.com", o3->toString());
76     EXPECT_EQ("http://example.com", o4->toString());
77     EXPECT_EQ("http://example.com", o5->toString());
78     EXPECT_EQ("http://example.com", o6->toString());
79
80     EXPECT_TRUE(o1->isSameOriginAs(o2.ptr()));
81     EXPECT_TRUE(o1->isSameOriginAs(o3.ptr()));
82     EXPECT_TRUE(o1->isSameOriginAs(o4.ptr()));
83     EXPECT_TRUE(o1->isSameOriginAs(o5.ptr()));
84     EXPECT_TRUE(o1->isSameOriginAs(o6.ptr()));
85 }
86
87 } // namespace TestWebKitAPI