Home | History | Annotate | Download | only in net
      1 /* Licensed to the Apache Software Foundation (ASF) under one or more
      2  * contributor license agreements.  See the NOTICE file distributed with
      3  * this work for additional information regarding copyright ownership.
      4  * The ASF licenses this file to You under the Apache License, Version 2.0
      5  * (the "License"); you may not use this file except in compliance with
      6  * the License.  You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package org.apache.harmony.tests.java.net;
     17 
     18 import junit.framework.TestCase;
     19 import java.net.CookieManager;
     20 import java.net.CookieStore;
     21 import java.net.HttpCookie;
     22 import java.net.URI;
     23 import java.net.URISyntaxException;
     24 import java.util.List;
     25 import java.util.RandomAccess;
     26 
     27 public class CookieStoreTest extends TestCase {
     28 
     29     private CookieManager cookieManager;
     30 
     31     private CookieStore cookieStore;
     32 
     33     /**
     34      * java.net.CookieStore#add(URI, HttpCookie)
     35      * @since 1.6
     36      */
     37     public void test_add_LURI_LHttpCookie() throws URISyntaxException {
     38         URI uri = new URI("http://harmony.test.unit.org");
     39         HttpCookie cookie = new HttpCookie("name1", "value1");
     40         cookie.setDiscard(true);
     41 
     42         // This needn't throw. We should use the cookie's domain, if set.
     43         // If no domain is set, this cookie will languish in the store until
     44         // it expires.
     45         cookieStore.add(null, cookie);
     46 
     47         try {
     48             cookieStore.add(uri, null);
     49             fail("should throw NullPointerException");
     50         } catch (NullPointerException e) {
     51             // expected
     52         }
     53 
     54         try {
     55             cookieStore.add(null, null);
     56             fail("should throw NullPointerException");
     57         } catch (NullPointerException e) {
     58             // expected
     59         }
     60 
     61         cookieStore.add(uri, cookie);
     62         List<HttpCookie> list = cookieStore.get(uri);
     63         assertEquals(1, list.size());
     64         assertTrue(list.contains(cookie));
     65 
     66         HttpCookie cookie2 = new HttpCookie("  NaME1   ", "  TESTVALUE1   ");
     67         cookieStore.add(uri, cookie2);
     68         list = cookieStore.get(uri);
     69         assertEquals(1, list.size());
     70         assertEquals("  TESTVALUE1   ", list.get(0).getValue());
     71         assertTrue(list.contains(cookie2));
     72 
     73         // domain and path attributes works
     74         HttpCookie anotherCookie = new HttpCookie("name1", "value1");
     75         anotherCookie.setDomain("domain");
     76         anotherCookie.setPath("Path");
     77         cookieStore.add(uri, anotherCookie);
     78         list = cookieStore.get(uri);
     79         assertEquals(2, list.size());
     80         assertNull(list.get(0).getDomain());
     81         assertEquals("domain", list.get(1).getDomain());
     82         assertEquals("Path", list.get(1).getPath());
     83 
     84         URI uri2 = new URI("http://.test.unit.org");
     85         HttpCookie cookie3 = new HttpCookie("NaME2", "VALUE2");
     86         cookieStore.add(uri2, cookie3);
     87         list = cookieStore.get(uri2);
     88         assertEquals(1, list.size());
     89         assertEquals("VALUE2", list.get(0).getValue());
     90         list = cookieStore.getCookies();
     91         assertEquals(3, list.size());
     92 
     93         // expired cookie won't be selected.
     94         HttpCookie cookie4 = new HttpCookie("cookie4", "value4");
     95         cookie4.setMaxAge(-2);
     96         assertTrue(cookie4.hasExpired());
     97         cookieStore.add(uri2, cookie4);
     98         list = cookieStore.getCookies();
     99         assertEquals(3, list.size());
    100         assertFalse(cookieStore.remove(uri2, cookie4));
    101 
    102         cookie4.setMaxAge(3000);
    103         cookie4.setDomain("domain");
    104         cookie4.setPath("path");
    105         cookieStore.add(uri2, cookie4);
    106         list = cookieStore.get(uri2);
    107         assertEquals(2, list.size());
    108 
    109         cookieStore.add(uri, cookie4);
    110         list = cookieStore.get(uri);
    111         assertEquals(3, list.size());
    112         list = cookieStore.get(uri2);
    113         assertEquals(2, list.size());
    114         list = cookieStore.getCookies();
    115         assertEquals(4, list.size());
    116 
    117         URI baduri = new URI("bad_url");
    118         HttpCookie cookie6 = new HttpCookie("cookie5", "value5");
    119         cookieStore.add(baduri, cookie6);
    120         list = cookieStore.get(baduri);
    121         assertTrue(list.contains(cookie6));
    122     }
    123 
    124     /**
    125      * java.net.CookieStore#get(URI)
    126      * @since 1.6
    127      */
    128     public void test_get_LURI() throws URISyntaxException {
    129         try {
    130             cookieStore.get(null);
    131             fail("should throw NullPointerException");
    132         } catch (NullPointerException e) {
    133             // expected
    134         }
    135 
    136         URI uri1 = new URI("http://get.uri1.test.org");
    137         List<HttpCookie> list = cookieStore.get(uri1);
    138         assertTrue(list.isEmpty());
    139 
    140         HttpCookie cookie1 = new HttpCookie("cookie_name1", "cookie_value1");
    141         HttpCookie cookie2 = new HttpCookie("cookie_name2", "cookie_value2");
    142         cookieStore.add(uri1, cookie1);
    143         cookieStore.add(uri1, cookie2);
    144         URI uri2 = new URI("http://get.uri2.test.org");
    145         HttpCookie cookie3 = new HttpCookie("cookie_name3", "cookie_value3");
    146         cookieStore.add(uri2, cookie3);
    147         list = cookieStore.get(uri1);
    148         assertEquals(2, list.size());
    149         list = cookieStore.get(uri2);
    150         assertEquals(1, list.size());
    151 
    152         // domain-match cookies also be selected.
    153         HttpCookie cookie4 = new HttpCookie("cookie_name4", "cookie_value4");
    154         cookie4.setDomain(".uri1.test.org");
    155         cookieStore.add(uri2, cookie4);
    156         list = cookieStore.get(uri1);
    157         assertEquals(3, list.size());
    158 
    159         cookieStore.add(uri1, cookie4);
    160         list = cookieStore.get(uri1);
    161         assertEquals(3, list.size());
    162         list = cookieStore.get(uri2);
    163         assertEquals(2, list.size());
    164 
    165         // expired cookie won't be selected.
    166         HttpCookie cookie5 = new HttpCookie("cookie_name5", "cookie_value5");
    167         cookie5.setMaxAge(-333);
    168         assertTrue(cookie5.hasExpired());
    169         cookieStore.add(uri1, cookie5);
    170         list = cookieStore.get(uri1);
    171         assertEquals(3, list.size());
    172         assertFalse(cookieStore.remove(uri1, cookie5));
    173         list = cookieStore.getCookies();
    174         assertEquals(4, list.size());
    175 
    176         cookie4.setMaxAge(-123);
    177         list = cookieStore.get(uri1);
    178         assertEquals(2, list.size());
    179         list = cookieStore.getCookies();
    180         assertEquals(3, list.size());
    181         // expired cookies are also deleted even if it domain-matches the URI
    182         HttpCookie cookie6 = new HttpCookie("cookie_name6", "cookie_value6");
    183         cookie6.setMaxAge(-2);
    184         cookie6.setDomain(".uri1.test.org");
    185         cookieStore.add(uri2, cookie6);
    186         list = cookieStore.get(uri1);
    187         assertEquals(2, list.size());
    188         assertFalse(cookieStore.remove(null, cookie6));
    189 
    190         URI uri3 = new URI("http://get.uri3.test.org");
    191         assertTrue(cookieStore.get(uri3).isEmpty());
    192         URI baduri = new URI("invalid_uri");
    193         assertTrue(cookieStore.get(baduri).isEmpty());
    194     }
    195 
    196     /**
    197      * java.net.CookieStore#getCookies()
    198      * @since 1.6
    199      */
    200     public void test_getCookies() throws URISyntaxException {
    201         List<HttpCookie> list = cookieStore.getCookies();
    202         assertTrue(list.isEmpty());
    203         assertTrue(list instanceof RandomAccess);
    204 
    205         HttpCookie cookie1 = new HttpCookie("cookie_name", "cookie_value");
    206         URI uri1 = new URI("http://getcookies1.test.org");
    207         cookieStore.add(uri1, cookie1);
    208         list = cookieStore.getCookies();
    209         assertTrue(list.contains(cookie1));
    210 
    211         HttpCookie cookie2 = new HttpCookie("cookie_name2", "cookie_value2");
    212         URI uri2 = new URI("http://getcookies2.test.org");
    213         cookieStore.add(uri2, cookie2);
    214         list = cookieStore.getCookies();
    215         assertEquals(2, list.size());
    216         assertTrue(list.contains(cookie1));
    217         assertTrue(list.contains(cookie2));
    218 
    219         // duplicated cookie won't be selected.
    220         cookieStore.add(uri2, cookie1);
    221         list = cookieStore.getCookies();
    222         assertEquals(2, list.size());
    223         // expired cookie won't be selected.
    224         HttpCookie cookie3 = new HttpCookie("cookie_name3", "cookie_value3");
    225         cookie3.setMaxAge(-1357);
    226         cookieStore.add(uri1, cookie3);
    227         list = cookieStore.getCookies();
    228         assertEquals(2, list.size());
    229 
    230         try {
    231             list.add(new HttpCookie("readOnlyName", "readOnlyValue"));
    232             fail("should throw UnsupportedOperationException");
    233         } catch (UnsupportedOperationException e) {
    234             // expected
    235         }
    236 
    237         try {
    238             list.remove(new HttpCookie("readOnlyName", "readOnlyValue"));
    239             fail("should throw UnsupportedOperationException");
    240         } catch (UnsupportedOperationException e) {
    241             // expected
    242         }
    243     }
    244 
    245     /**
    246      * java.net.CookieStore#getURIs()
    247      * @since 1.6
    248      */
    249     public void test_getURIs() throws URISyntaxException {
    250         List<URI> list = cookieStore.getURIs();
    251         assertTrue(list.isEmpty());
    252 
    253         URI uri1 = new URI("http://geturis1.test.com");
    254         HttpCookie cookie1 = new HttpCookie("cookie_name1", "cookie_value1");
    255         cookieStore.add(uri1, cookie1);
    256         list = cookieStore.getURIs();
    257         assertEquals("geturis1.test.com", list.get(0).getHost());
    258 
    259         HttpCookie cookie2 = new HttpCookie("cookie_name2", "cookie_value2");
    260         cookieStore.add(uri1, cookie2);
    261         list = cookieStore.getURIs();
    262         assertEquals(1, list.size());
    263 
    264         URI uri2 = new URI("http://geturis2.test.com");
    265         cookieStore.add(uri2, cookie2);
    266         list = cookieStore.getURIs();
    267         assertEquals(2, list.size());
    268         assertTrue(list.contains(uri1));
    269         assertTrue(list.contains(uri2));
    270     }
    271 
    272     /**
    273      * java.net.CookieStore#remove(URI, HttpCookie)
    274      * @since 1.6
    275      */
    276     public void test_remove_LURI_LHttpCookie() throws URISyntaxException {
    277         URI uri1 = new URI("http://remove1.test.com");
    278         HttpCookie cookie1 = new HttpCookie("cookie_name1", "cookie_value1");
    279         try {
    280             cookieStore.remove(uri1, null);
    281             fail("should throw NullPointerException");
    282         } catch (NullPointerException e) {
    283             // expected
    284         }
    285         assertFalse(cookieStore.remove(uri1, cookie1));
    286         assertFalse(cookieStore.remove(null, cookie1));
    287 
    288         cookieStore.add(uri1, cookie1);
    289         URI uri2 = new URI("http://remove2.test.com");
    290         HttpCookie cookie2 = new HttpCookie("cookie_name2", "cookie_value2");
    291         cookieStore.add(uri2, cookie2);
    292         assertTrue(cookieStore.remove(uri1, cookie1));
    293         assertFalse(cookieStore.remove(uri1, cookie1));
    294         assertEquals(2, cookieStore.getURIs().size());
    295         assertEquals(1, cookieStore.getCookies().size());
    296         assertTrue(cookieStore.remove(uri2, cookie2));
    297         assertFalse(cookieStore.remove(uri2, cookie2));
    298         assertEquals(2, cookieStore.getURIs().size());
    299         assertEquals(0, cookieStore.getCookies().size());
    300 
    301         assertTrue(cookieStore.removeAll());
    302         cookieStore.add(uri1, cookie1);
    303         cookieStore.add(uri2, cookie2);
    304         HttpCookie cookie3 = new HttpCookie("cookie_name3", "cookie_value3");
    305         assertFalse(cookieStore.remove(null, cookie3));
    306         // No guarantees on behavior if we call remove with a different
    307         // uri from the one originally associated with the cookie.
    308         assertFalse(cookieStore.remove(null, cookie1));
    309         assertTrue(cookieStore.remove(uri1, cookie1));
    310         assertFalse(cookieStore.remove(uri1, cookie1));
    311 
    312         assertEquals(2, cookieStore.getURIs().size());
    313         assertEquals(1, cookieStore.getCookies().size());
    314         assertTrue(cookieStore.remove(uri2, cookie2));
    315         assertFalse(cookieStore.remove(uri2, cookie2));
    316         assertEquals(2, cookieStore.getURIs().size());
    317         assertEquals(0, cookieStore.getCookies().size());
    318 
    319         cookieStore.removeAll();
    320         // expired cookies can also be deleted.
    321         cookie2.setMaxAge(-34857);
    322         cookieStore.add(uri2, cookie2);
    323         assertTrue(cookieStore.remove(uri2, cookie2));
    324         assertFalse(cookieStore.remove(uri2, cookie2));
    325         assertEquals(0, cookieStore.getCookies().size());
    326 
    327         cookie2.setMaxAge(34857);
    328         cookieStore.add(uri1, cookie1);
    329         cookieStore.add(uri2, cookie1);
    330         cookieStore.add(uri2, cookie2);
    331         assertTrue(cookieStore.remove(uri1, cookie1));
    332         assertFalse(cookieStore.remove(uri1, cookie1));
    333         assertTrue(cookieStore.get(uri2).contains(cookie1));
    334         assertTrue(cookieStore.get(uri2).contains(cookie2));
    335         assertEquals(0, cookieStore.get(uri1).size());
    336         cookieStore.remove(uri2, cookie2);
    337 
    338         cookieStore.removeAll();
    339         cookieStore.add(uri2, cookie2);
    340         cookieStore.add(uri1, cookie1);
    341         assertEquals(2, cookieStore.getCookies().size());
    342         assertFalse(cookieStore.remove(uri2, cookie1));
    343         assertTrue(cookieStore.remove(uri1, cookie1));
    344         assertEquals(2, cookieStore.getURIs().size());
    345         assertEquals(1, cookieStore.getCookies().size());
    346         assertTrue(cookieStore.getCookies().contains(cookie2));
    347 
    348         cookieStore.removeAll();
    349         URI uri3 = new URI("http://remove3.test.com");
    350         URI uri4 = new URI("http://test.com");
    351         HttpCookie cookie4 = new HttpCookie("cookie_name4", "cookie_value4");
    352         cookie4.setDomain(".test.com");
    353         cookie2.setMaxAge(-34857);
    354         cookie3.setMaxAge(-22);
    355         cookie4.setMaxAge(-45);
    356         cookieStore.add(uri1, cookie1);
    357         cookieStore.add(uri2, cookie2);
    358         cookieStore.add(uri3, cookie3);
    359         cookieStore.add(uri4, cookie4);
    360         assertEquals(0, cookieStore.get(uri2).size());
    361         assertFalse(cookieStore.remove(uri2, cookie2));
    362         assertTrue(cookieStore.remove(uri3, cookie3));
    363         assertFalse(cookieStore.remove(uri4, cookie4));
    364     }
    365 
    366     /**
    367      * java.net.CookieStore#test_removeAll()
    368      * @since 1.6
    369      */
    370     public void test_removeAll() throws URISyntaxException {
    371         assertFalse(cookieStore.removeAll());
    372 
    373         URI uri1 = new URI("http://removeAll1.test.com");
    374         HttpCookie cookie1 = new HttpCookie("cookie_name1", "cookie_value1");
    375         cookieStore.add(uri1, cookie1);
    376         URI uri2 = new URI("http://removeAll2.test.com");
    377         HttpCookie cookie2 = new HttpCookie("cookie_name2", "cookie_value2");
    378         cookieStore.add(uri2, cookie2);
    379 
    380         assertTrue(cookieStore.removeAll());
    381         assertTrue(cookieStore.getURIs().isEmpty());
    382         assertTrue(cookieStore.getCookies().isEmpty());
    383 
    384         assertFalse(cookieStore.removeAll());
    385     }
    386 
    387     @Override
    388     protected void setUp() throws Exception {
    389         super.setUp();
    390         cookieManager = new CookieManager();
    391         cookieStore = cookieManager.getCookieStore();
    392     }
    393 
    394     @Override
    395     protected void tearDown() throws Exception {
    396         cookieManager = null;
    397         cookieStore = null;
    398         super.tearDown();
    399     }
    400 
    401 }
    402