Chromium Code Reviews| Index: chrome/android/junit/src/org/chromium/chrome/browser/cookies/CanonicalCookieTest.java |
| diff --git a/chrome/android/junit/src/org/chromium/chrome/browser/cookies/CanonicalCookieTest.java b/chrome/android/junit/src/org/chromium/chrome/browser/cookies/CanonicalCookieTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..be56a08cadacbe439d224f710a77feaafffa29ff |
| --- /dev/null |
| +++ b/chrome/android/junit/src/org/chromium/chrome/browser/cookies/CanonicalCookieTest.java |
| @@ -0,0 +1,254 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.cookies; |
| + |
| +import org.junit.Assert; |
| +import org.junit.Test; |
| +import org.junit.runner.RunWith; |
| +import org.junit.runners.BlockJUnit4ClassRunner; |
| + |
| +import java.io.ByteArrayInputStream; |
| +import java.io.ByteArrayOutputStream; |
| +import java.io.DataInputStream; |
| +import java.io.DataOutputStream; |
| +import java.io.File; |
| +import java.io.FileOutputStream; |
| +import java.util.ArrayList; |
| +import java.util.List; |
| + |
| +import javax.crypto.Cipher; |
| +import javax.crypto.CipherOutputStream; |
| +import javax.crypto.KeyGenerator; |
| +import javax.crypto.SecretKey; |
| + |
| + |
| +/** |
| + * Unit test serialization code. |
| + */ |
| +@RunWith(BlockJUnit4ClassRunner.class) |
| +public class CanonicalCookieTest { |
| + // Name meant to match CanonicalCookie method. |
| + private static byte[] saveToStream(final List<CanonicalCookie> cookies) |
|
Yaron
2016/04/28 03:43:10
reuse CanonicalCookies.writeToByteArray instead of
boliu
2016/04/28 17:49:26
Disagree. writeToByteArray also does encryption, w
|
| + throws Exception { |
| + ByteArrayOutputStream outByteStream = new ByteArrayOutputStream(); |
| + DataOutputStream out = new DataOutputStream(outByteStream); |
| + try { |
| + CanonicalCookie cookiesArray[] = new CanonicalCookie[cookies.size()]; |
| + cookies.toArray(cookiesArray); |
| + CanonicalCookie.saveToStream(out, cookiesArray); |
| + } finally { |
| + out.close(); |
| + outByteStream.close(); |
| + } |
| + return outByteStream.toByteArray(); |
| + } |
| + |
| + // Name meant to match CanonicalCookie method. |
| + private static List<CanonicalCookie> createFromStream(final byte[] byteArray) |
| + throws Exception { |
| + ByteArrayInputStream inByteStream = new ByteArrayInputStream(byteArray); |
| + DataInputStream in = new DataInputStream(inByteStream); |
| + try { |
| + return CanonicalCookie.readFromStream(in); |
| + } finally { |
| + in.close(); |
| + inByteStream.close(); |
| + } |
| + } |
| + |
| + private static void assertCookiesEqual(CanonicalCookie lhs, CanonicalCookie rhs) { |
| + Assert.assertEquals(lhs.getUrl(), rhs.getUrl()); |
| + Assert.assertEquals(lhs.getName(), rhs.getName()); |
| + Assert.assertEquals(lhs.getValue(), rhs.getValue()); |
| + Assert.assertEquals(lhs.getDomain(), rhs.getDomain()); |
| + Assert.assertEquals(lhs.getPath(), rhs.getPath()); |
| + Assert.assertEquals(lhs.getCreationDate(), rhs.getCreationDate()); |
| + Assert.assertEquals(lhs.getExpirationDate(), rhs.getExpirationDate()); |
| + Assert.assertEquals(lhs.getLastAccessDate(), rhs.getLastAccessDate()); |
| + Assert.assertEquals(lhs.isSecure(), rhs.isSecure()); |
| + Assert.assertEquals(lhs.isHttpOnly(), rhs.isHttpOnly()); |
| + Assert.assertEquals(lhs.getSameSite(), rhs.getSameSite()); |
| + Assert.assertEquals(lhs.getPriority(), rhs.getPriority()); |
| + } |
| + |
| + private static void doSaveRestoreCookiesListTest(final List<CanonicalCookie> cookies) |
| + throws Exception { |
| + byte[] byteArray = saveToStream(cookies); |
| + List<CanonicalCookie> readCookies = createFromStream(byteArray); |
| + |
| + Assert.assertEquals(cookies.size(), readCookies.size()); |
| + for (int i = 0; i < cookies.size(); ++i) { |
| + assertCookiesEqual(cookies.get(i), readCookies.get(i)); |
| + } |
| + } |
| + |
| + @Test |
| + public void testSaveRestoreEmptyList() throws Exception { |
| + doSaveRestoreCookiesListTest(new ArrayList<CanonicalCookie>()); |
| + } |
| + |
| + @Test |
| + public void testSaveRestore() throws Exception { |
| + ArrayList<CanonicalCookie> cookies = new ArrayList<>(); |
| + cookies.add(new CanonicalCookie("http://url", "name", "value", "domain", "path", |
|
boliu
2016/04/28 00:00:43
you can blame weird formatting here on clang..
|
| + 0 /* creation */, 1 /* expiration */, 0 /* lastAccess */, false /* secure */, |
| + true /* httpOnly */, 0 /* sameSite */, 0 /* priority */)); |
| + cookies.add(new CanonicalCookie("http://url2", "name2", "value2", "domain2", "path2", |
| + 10 /* creation */, 20 /* expiration */, 15 /* lastAccess */, true /* secure */, |
| + false /* httpOnly */, 1 /* sameSite */, 1 /* priority */)); |
| + cookies.add(new CanonicalCookie("http://url3", "name3", "value3", "domain3", "path3", |
| + 10 /* creation */, 20 /* expiration */, 15 /* lastAccess */, true /* secure */, |
| + false /* httpOnly */, 2 /* sameSite */, 2 /* priority */)); |
| + |
| + doSaveRestoreCookiesListTest(cookies); |
| + } |
| + |
| + private static byte[] saveToStreamLegacyFormat(final List<CanonicalCookie> cookies) |
| + throws Exception { |
| + ByteArrayOutputStream outByteStream = new ByteArrayOutputStream(); |
| + DataOutputStream out = new DataOutputStream(outByteStream); |
| + try { |
| + // Don't change logic here. This is legacy code maintained in test. |
| + out.writeUTF(CanonicalCookie.LEGACY_FORMAT_MAGIC_STRING); |
| + for (CanonicalCookie cookie : cookies) { |
| + out.writeUTF(cookie.getUrl()); |
| + out.writeUTF(cookie.getName()); |
| + out.writeUTF(cookie.getValue()); |
| + out.writeUTF(cookie.getDomain()); |
| + out.writeUTF(cookie.getPath()); |
| + out.writeLong(cookie.getCreationDate()); |
| + out.writeLong(cookie.getExpirationDate()); |
| + out.writeLong(cookie.getLastAccessDate()); |
| + out.writeBoolean(cookie.isSecure()); |
| + out.writeBoolean(cookie.isHttpOnly()); |
| + out.writeBoolean(cookie.getSameSite() > 0); |
| + out.writeInt(cookie.getPriority()); |
| + } |
| + } finally { |
| + out.close(); |
| + outByteStream.close(); |
| + } |
| + return outByteStream.toByteArray(); |
| + } |
| + |
| + // Name meant to match CanonicalCookie method. |
| + private static List<CanonicalCookie> readFromLegacyStream(final byte[] byteArray) |
| + throws Exception { |
| + ByteArrayInputStream inByteStream = new ByteArrayInputStream(byteArray); |
| + DataInputStream in = new DataInputStream(inByteStream); |
| + try { |
| + return CanonicalCookie.readFromLegacyStream(in); |
| + } finally { |
| + in.close(); |
| + inByteStream.close(); |
| + } |
| + } |
| + |
| + @Test |
| + public void testReadFromLegacyStream() throws Exception { |
| + ArrayList<CanonicalCookie> cookies = new ArrayList<>(); |
| + cookies.add(new CanonicalCookie("http://url", "name", "value", "domain", "path", |
| + 0 /* creation */, 1 /* expiration */, 0 /* lastAccess */, false /* secure */, |
| + true /* httpOnly */, 0 /* sameSite */, 0 /* priority */)); |
| + cookies.add(new CanonicalCookie("http://url2", "name2", "value2", "domain2", "path2", |
| + 10 /* creation */, 20 /* expiration */, 15 /* lastAccess */, true /* secure */, |
| + false /* httpOnly */, 1 /* sameSite */, 1 /* priority */)); |
| + // Doesn't support sameSite == 2. |
| + byte[] byteArray = saveToStreamLegacyFormat(cookies); |
| + List<CanonicalCookie> readCookies = readFromLegacyStream(byteArray); |
| + |
| + Assert.assertEquals(cookies.size(), readCookies.size()); |
| + for (int i = 0; i < cookies.size(); ++i) { |
| + assertCookiesEqual(cookies.get(i), readCookies.get(i)); |
| + } |
| + } |
| + |
| + @Test |
| + public void testLegacySourceMergeSameSite() throws Exception { |
| + ArrayList<CanonicalCookie> cookies = new ArrayList<>(); |
| + cookies.add(new CanonicalCookie("http://url", "name", "value", "domain", "path", |
| + 0 /* creation */, 1 /* expiration */, 0 /* lastAccess */, false /* secure */, |
| + true /* httpOnly */, 2 /* sameSite */, 0 /* priority */)); |
|
Yaron
2016/04/28 03:43:10
This seems a little strange. 2 couldn't exist in l
boliu
2016/04/28 17:49:26
Oh good point. true -> 1 case is covered above alr
|
| + |
| + byte[] byteArray = saveToStreamLegacyFormat(cookies); |
| + List<CanonicalCookie> readCookies = readFromLegacyStream(byteArray); |
| + |
| + Assert.assertEquals(cookies.size(), readCookies.size()); |
| + Assert.assertEquals(1, readCookies.get(0).getSameSite()); |
| + } |
| + |
| + @Test |
| + public void testReadWriteToFile() throws Exception { |
| + ArrayList<CanonicalCookie> cookies = new ArrayList<>(); |
| + cookies.add(new CanonicalCookie("http://url", "name", "value", "domain", "path", |
| + 0 /* creation */, 1 /* expiration */, 0 /* lastAccess */, false /* secure */, |
| + true /* httpOnly */, 0 /* sameSite */, 0 /* priority */)); |
| + cookies.add(new CanonicalCookie("http://url2", "name2", "value2", "domain2", "path2", |
| + 10 /* creation */, 20 /* expiration */, 15 /* lastAccess */, true /* secure */, |
| + false /* httpOnly */, 1 /* sameSite */, 1 /* priority */)); |
| + CanonicalCookie cookiesArray[] = new CanonicalCookie[cookies.size()]; |
| + cookies.toArray(cookiesArray); |
| + |
| + KeyGenerator keyGen = KeyGenerator.getInstance("AES"); |
| + keyGen.init(128); |
| + final SecretKey key = keyGen.generateKey(); |
| + Cipher encryptCipher = Cipher.getInstance("AES"); |
| + encryptCipher.init(Cipher.ENCRYPT_MODE, key); |
| + |
| + byte[] byteArray = CanonicalCookie.writeToByteArray(encryptCipher, cookiesArray); |
| + File tempFile = File.createTempFile("cookies_junit_test", null); |
| + tempFile.deleteOnExit(); |
| + FileOutputStream outStream = new FileOutputStream(tempFile); |
| + outStream.write(byteArray); |
| + outStream.close(); |
| + |
| + Cipher decryptCipher = Cipher.getInstance("AES"); |
| + decryptCipher.init(Cipher.DECRYPT_MODE, key); |
| + List<CanonicalCookie> readCookies = |
| + CanonicalCookie.readFromFileUnknownFormat(tempFile, decryptCipher); |
| + Assert.assertEquals(cookies.size(), readCookies.size()); |
| + for (int i = 0; i < cookies.size(); ++i) { |
| + assertCookiesEqual(cookies.get(i), readCookies.get(i)); |
| + } |
| + } |
| + |
| + @Test |
| + public void testReadFromLegacyFormat() throws Exception { |
| + ArrayList<CanonicalCookie> cookies = new ArrayList<>(); |
| + cookies.add(new CanonicalCookie("http://url", "name", "value", "domain", "path", |
| + 0 /* creation */, 1 /* expiration */, 0 /* lastAccess */, false /* secure */, |
| + true /* httpOnly */, 0 /* sameSite */, 0 /* priority */)); |
| + cookies.add(new CanonicalCookie("http://url2", "name2", "value2", "domain2", "path2", |
| + 10 /* creation */, 20 /* expiration */, 15 /* lastAccess */, true /* secure */, |
| + false /* httpOnly */, 1 /* sameSite */, 1 /* priority */)); |
| + cookies.add(new CanonicalCookie("http://url2", "name2", "value2", "domain2", "path2", |
| + 10 /* creation */, 20 /* expiration */, 15 /* lastAccess */, true /* secure */, |
| + false /* httpOnly */, 2 /* sameSite */, 1 /* priority */)); |
| + |
| + KeyGenerator keyGen = KeyGenerator.getInstance("AES"); |
| + keyGen.init(128); |
| + final SecretKey key = keyGen.generateKey(); |
| + Cipher encryptCipher = Cipher.getInstance("AES"); |
| + encryptCipher.init(Cipher.ENCRYPT_MODE, key); |
| + |
| + byte[] byteArray = saveToStreamLegacyFormat(cookies); |
| + File tempFile = File.createTempFile("cookies_junit_test", null); |
| + tempFile.deleteOnExit(); |
| + CipherOutputStream outStream = |
| + new CipherOutputStream(new FileOutputStream(tempFile), encryptCipher); |
| + outStream.write(byteArray); |
| + outStream.close(); |
| + |
| + Cipher decryptCipher = Cipher.getInstance("AES"); |
| + decryptCipher.init(Cipher.DECRYPT_MODE, key); |
| + List<CanonicalCookie> readCookies = |
| + CanonicalCookie.readFromFileUnknownFormat(tempFile, decryptCipher); |
| + Assert.assertEquals(cookies.size(), readCookies.size()); |
| + for (int i = 0; i < 2; ++i) { |
| + assertCookiesEqual(cookies.get(i), readCookies.get(i)); |
| + } |
| + Assert.assertEquals(1, readCookies.get(2).getSameSite()); |
| + } |
| +} |