1cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath/* Licensed to the Apache Software Foundation (ASF) under one or more
2cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * contributor license agreements.  See the NOTICE file distributed with
3cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * this work for additional information regarding copyright ownership.
4cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * The ASF licenses this file to You under the Apache License, Version 2.0
5cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * (the "License"); you may not use this file except in compliance with
6cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * the License.  You may obtain a copy of the License at
7cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *
8cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *     http://www.apache.org/licenses/LICENSE-2.0
9cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath *
10cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * Unless required by applicable law or agreed to in writing, software
11cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * distributed under the License is distributed on an "AS IS" BASIS,
12cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * See the License for the specific language governing permissions and
14cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * limitations under the License.
15cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath */
16cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
17ab762bb740405d0fefcccf4a0899a234f995be13Narayan Kamathpackage org.apache.harmony.tests.java.net;
18cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
19cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.net.CookiePolicy;
20cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.net.HttpCookie;
21cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.net.URI;
22cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.net.URISyntaxException;
23cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
24cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport junit.framework.TestCase;
25cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
26cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathpublic class CookiePolicyTest extends TestCase {
27cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
28cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    /**
29cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * java.net.CookiePolicy#shouldAccept(java.net.URI,
30cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     *java.net.HttpCookie).
31cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     * @since 1.6
32cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath     */
33cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    public void test_ShouldAccept_LURI_LHttpCookie() throws URISyntaxException {
34cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        HttpCookie cookie = new HttpCookie("Harmony_6", "ongoing");
35cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        URI uri = new URI("");
36cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        try {
37cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(null, cookie);
38cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            fail("Should throw NullPointerException");
39cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        } catch (NullPointerException e) {
40cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            // expected
41cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        }
42cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
43cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        try {
44cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(uri, null);
45cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            fail("Should throw NullPointerException");
46cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        } catch (NullPointerException e) {
47cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            // expected
48cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        }
49cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
50cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        try {
51cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(null, null);
52cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            fail("Should throw NullPointerException");
53cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        } catch (NullPointerException e) {
54cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath            // expected
55cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        }
56cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
57cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // Policy: ACCEPT_ALL, always returns true
58cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        boolean accept = CookiePolicy.ACCEPT_ALL.shouldAccept(null, cookie);
59cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue(accept);
60cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
61cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        accept = CookiePolicy.ACCEPT_ALL.shouldAccept(null, null);
62cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue(accept);
63cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
64cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        accept = CookiePolicy.ACCEPT_ALL.shouldAccept(uri, null);
65cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue(accept);
66cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
67cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // Policy: ACCEPT_NONE, always returns false
68cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        accept = CookiePolicy.ACCEPT_NONE.shouldAccept(null, cookie);
69cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertFalse(accept);
70cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
71cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        accept = CookiePolicy.ACCEPT_NONE.shouldAccept(null, null);
72cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertFalse(accept);
73cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
74cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        accept = CookiePolicy.ACCEPT_NONE.shouldAccept(uri, null);
75cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertFalse(accept);
76cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
77cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        // Policy: ACCEPT_ORIGINAL_SERVER
78cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(uri, cookie);
79cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertFalse(accept);
80cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
81cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        cookie.setDomain(".b.c");
82cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
83cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                "schema://a.b.c"), cookie);
84cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue(accept);
85cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
86cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        cookie.setDomain(".b.c");
87cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
88cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                "s://a.b.c.d"), cookie);
89cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertFalse(accept);
90cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
91cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        cookie.setDomain("b.c");
92cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
93cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                "s://a.b.c.d"), cookie);
94cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertFalse(accept);
95cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
96cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        cookie.setDomain("a.b.c.d");
97cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
98cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                "s://a.b.c.d"), cookie);
99cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertTrue(accept);
100cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
101cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        cookie.setDomain(".");
102cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
103cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                "s://a.b.c.d"), cookie);
104cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertFalse(accept);
105cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
106cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        cookie.setDomain("");
107cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        accept = CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(new URI(
108cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath                "s://a.b.c.d"), cookie);
109cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath        assertFalse(accept);
110cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath    }
111cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath
112cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath}
113