OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.cookies; |
| 6 |
| 7 import org.junit.Assert; |
| 8 import org.junit.Test; |
| 9 import org.junit.runner.RunWith; |
| 10 import org.junit.runners.BlockJUnit4ClassRunner; |
| 11 |
| 12 import java.io.ByteArrayInputStream; |
| 13 import java.io.ByteArrayOutputStream; |
| 14 import java.io.DataInputStream; |
| 15 import java.io.DataOutputStream; |
| 16 import java.util.ArrayList; |
| 17 import java.util.List; |
| 18 |
| 19 /** |
| 20 * Unit test serialization code. |
| 21 */ |
| 22 @RunWith(BlockJUnit4ClassRunner.class) |
| 23 public class CanonicalCookieTest { |
| 24 // Name meant to match CanonicalCookie method. |
| 25 private static byte[] saveListToStream(final List<CanonicalCookie> cookies) |
| 26 throws Exception { |
| 27 ByteArrayOutputStream outByteStream = new ByteArrayOutputStream(); |
| 28 DataOutputStream out = new DataOutputStream(outByteStream); |
| 29 try { |
| 30 CanonicalCookie cookiesArray[] = new CanonicalCookie[cookies.size()]
; |
| 31 cookies.toArray(cookiesArray); |
| 32 CanonicalCookie.saveListToStream(out, cookiesArray); |
| 33 } finally { |
| 34 out.close(); |
| 35 outByteStream.close(); |
| 36 } |
| 37 return outByteStream.toByteArray(); |
| 38 } |
| 39 |
| 40 // Name meant to match CanonicalCookie method. |
| 41 private static List<CanonicalCookie> readListFromStream(final byte[] byteArr
ay) |
| 42 throws Exception { |
| 43 ByteArrayInputStream inByteStream = new ByteArrayInputStream(byteArray); |
| 44 DataInputStream in = new DataInputStream(inByteStream); |
| 45 try { |
| 46 return CanonicalCookie.readListFromStream(in); |
| 47 } finally { |
| 48 in.close(); |
| 49 inByteStream.close(); |
| 50 } |
| 51 } |
| 52 |
| 53 private static void assertCookiesEqual(CanonicalCookie lhs, CanonicalCookie
rhs) { |
| 54 Assert.assertEquals(lhs.getUrl(), rhs.getUrl()); |
| 55 Assert.assertEquals(lhs.getName(), rhs.getName()); |
| 56 Assert.assertEquals(lhs.getValue(), rhs.getValue()); |
| 57 Assert.assertEquals(lhs.getDomain(), rhs.getDomain()); |
| 58 Assert.assertEquals(lhs.getPath(), rhs.getPath()); |
| 59 Assert.assertEquals(lhs.getCreationDate(), rhs.getCreationDate()); |
| 60 Assert.assertEquals(lhs.getExpirationDate(), rhs.getExpirationDate()); |
| 61 Assert.assertEquals(lhs.getLastAccessDate(), rhs.getLastAccessDate()); |
| 62 Assert.assertEquals(lhs.isSecure(), rhs.isSecure()); |
| 63 Assert.assertEquals(lhs.isHttpOnly(), rhs.isHttpOnly()); |
| 64 Assert.assertEquals(lhs.getSameSite(), rhs.getSameSite()); |
| 65 Assert.assertEquals(lhs.getPriority(), rhs.getPriority()); |
| 66 } |
| 67 |
| 68 private static void doSaveRestoreCookiesListTest(final List<CanonicalCookie>
cookies) |
| 69 throws Exception { |
| 70 byte[] byteArray = saveListToStream(cookies); |
| 71 List<CanonicalCookie> readCookies = readListFromStream(byteArray); |
| 72 |
| 73 Assert.assertEquals(cookies.size(), readCookies.size()); |
| 74 for (int i = 0; i < cookies.size(); ++i) { |
| 75 assertCookiesEqual(cookies.get(i), readCookies.get(i)); |
| 76 } |
| 77 } |
| 78 |
| 79 @Test |
| 80 public void testSaveRestoreEmptyList() throws Exception { |
| 81 doSaveRestoreCookiesListTest(new ArrayList<CanonicalCookie>()); |
| 82 } |
| 83 |
| 84 @Test |
| 85 public void testSaveRestore() throws Exception { |
| 86 ArrayList<CanonicalCookie> cookies = new ArrayList<>(); |
| 87 cookies.add(new CanonicalCookie("http://url", "name", "value", "domain",
"path", |
| 88 0 /* creation */, 1 /* expiration */, 0 /* lastAccess */, false
/* secure */, |
| 89 true /* httpOnly */, 0 /* sameSite */, 0 /* priority */)); |
| 90 cookies.add(new CanonicalCookie("http://url2", "name2", "value2", "domai
n2", "path2", |
| 91 10 /* creation */, 20 /* expiration */, 15 /* lastAccess */, tru
e /* secure */, |
| 92 false /* httpOnly */, 1 /* sameSite */, 1 /* priority */)); |
| 93 cookies.add(new CanonicalCookie("http://url3", "name3", "value3", "domai
n3", "path3", |
| 94 10 /* creation */, 20 /* expiration */, 15 /* lastAccess */, tru
e /* secure */, |
| 95 false /* httpOnly */, 2 /* sameSite */, 2 /* priority */)); |
| 96 |
| 97 doSaveRestoreCookiesListTest(cookies); |
| 98 } |
| 99 } |
OLD | NEW |