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.io.File; | |
17 import java.io.FileOutputStream; | |
18 import java.util.ArrayList; | |
19 import java.util.List; | |
20 | |
21 import javax.crypto.Cipher; | |
22 import javax.crypto.CipherOutputStream; | |
23 import javax.crypto.KeyGenerator; | |
24 import javax.crypto.SecretKey; | |
25 | |
26 | |
27 /** | |
28 * Unit test serialization code. | |
29 */ | |
30 @RunWith(BlockJUnit4ClassRunner.class) | |
31 public class CanonicalCookieTest { | |
32 // Name meant to match CanonicalCookie method. | |
33 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
| |
34 throws Exception { | |
35 ByteArrayOutputStream outByteStream = new ByteArrayOutputStream(); | |
36 DataOutputStream out = new DataOutputStream(outByteStream); | |
37 try { | |
38 CanonicalCookie cookiesArray[] = new CanonicalCookie[cookies.size()] ; | |
39 cookies.toArray(cookiesArray); | |
40 CanonicalCookie.saveToStream(out, cookiesArray); | |
41 } finally { | |
42 out.close(); | |
43 outByteStream.close(); | |
44 } | |
45 return outByteStream.toByteArray(); | |
46 } | |
47 | |
48 // Name meant to match CanonicalCookie method. | |
49 private static List<CanonicalCookie> createFromStream(final byte[] byteArray ) | |
50 throws Exception { | |
51 ByteArrayInputStream inByteStream = new ByteArrayInputStream(byteArray); | |
52 DataInputStream in = new DataInputStream(inByteStream); | |
53 try { | |
54 return CanonicalCookie.readFromStream(in); | |
55 } finally { | |
56 in.close(); | |
57 inByteStream.close(); | |
58 } | |
59 } | |
60 | |
61 private static void assertCookiesEqual(CanonicalCookie lhs, CanonicalCookie rhs) { | |
62 Assert.assertEquals(lhs.getUrl(), rhs.getUrl()); | |
63 Assert.assertEquals(lhs.getName(), rhs.getName()); | |
64 Assert.assertEquals(lhs.getValue(), rhs.getValue()); | |
65 Assert.assertEquals(lhs.getDomain(), rhs.getDomain()); | |
66 Assert.assertEquals(lhs.getPath(), rhs.getPath()); | |
67 Assert.assertEquals(lhs.getCreationDate(), rhs.getCreationDate()); | |
68 Assert.assertEquals(lhs.getExpirationDate(), rhs.getExpirationDate()); | |
69 Assert.assertEquals(lhs.getLastAccessDate(), rhs.getLastAccessDate()); | |
70 Assert.assertEquals(lhs.isSecure(), rhs.isSecure()); | |
71 Assert.assertEquals(lhs.isHttpOnly(), rhs.isHttpOnly()); | |
72 Assert.assertEquals(lhs.getSameSite(), rhs.getSameSite()); | |
73 Assert.assertEquals(lhs.getPriority(), rhs.getPriority()); | |
74 } | |
75 | |
76 private static void doSaveRestoreCookiesListTest(final List<CanonicalCookie> cookies) | |
77 throws Exception { | |
78 byte[] byteArray = saveToStream(cookies); | |
79 List<CanonicalCookie> readCookies = createFromStream(byteArray); | |
80 | |
81 Assert.assertEquals(cookies.size(), readCookies.size()); | |
82 for (int i = 0; i < cookies.size(); ++i) { | |
83 assertCookiesEqual(cookies.get(i), readCookies.get(i)); | |
84 } | |
85 } | |
86 | |
87 @Test | |
88 public void testSaveRestoreEmptyList() throws Exception { | |
89 doSaveRestoreCookiesListTest(new ArrayList<CanonicalCookie>()); | |
90 } | |
91 | |
92 @Test | |
93 public void testSaveRestore() throws Exception { | |
94 ArrayList<CanonicalCookie> cookies = new ArrayList<>(); | |
95 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..
| |
96 0 /* creation */, 1 /* expiration */, 0 /* lastAccess */, false /* secure */, | |
97 true /* httpOnly */, 0 /* sameSite */, 0 /* priority */)); | |
98 cookies.add(new CanonicalCookie("http://url2", "name2", "value2", "domai n2", "path2", | |
99 10 /* creation */, 20 /* expiration */, 15 /* lastAccess */, tru e /* secure */, | |
100 false /* httpOnly */, 1 /* sameSite */, 1 /* priority */)); | |
101 cookies.add(new CanonicalCookie("http://url3", "name3", "value3", "domai n3", "path3", | |
102 10 /* creation */, 20 /* expiration */, 15 /* lastAccess */, tru e /* secure */, | |
103 false /* httpOnly */, 2 /* sameSite */, 2 /* priority */)); | |
104 | |
105 doSaveRestoreCookiesListTest(cookies); | |
106 } | |
107 | |
108 private static byte[] saveToStreamLegacyFormat(final List<CanonicalCookie> c ookies) | |
109 throws Exception { | |
110 ByteArrayOutputStream outByteStream = new ByteArrayOutputStream(); | |
111 DataOutputStream out = new DataOutputStream(outByteStream); | |
112 try { | |
113 // Don't change logic here. This is legacy code maintained in test. | |
114 out.writeUTF(CanonicalCookie.LEGACY_FORMAT_MAGIC_STRING); | |
115 for (CanonicalCookie cookie : cookies) { | |
116 out.writeUTF(cookie.getUrl()); | |
117 out.writeUTF(cookie.getName()); | |
118 out.writeUTF(cookie.getValue()); | |
119 out.writeUTF(cookie.getDomain()); | |
120 out.writeUTF(cookie.getPath()); | |
121 out.writeLong(cookie.getCreationDate()); | |
122 out.writeLong(cookie.getExpirationDate()); | |
123 out.writeLong(cookie.getLastAccessDate()); | |
124 out.writeBoolean(cookie.isSecure()); | |
125 out.writeBoolean(cookie.isHttpOnly()); | |
126 out.writeBoolean(cookie.getSameSite() > 0); | |
127 out.writeInt(cookie.getPriority()); | |
128 } | |
129 } finally { | |
130 out.close(); | |
131 outByteStream.close(); | |
132 } | |
133 return outByteStream.toByteArray(); | |
134 } | |
135 | |
136 // Name meant to match CanonicalCookie method. | |
137 private static List<CanonicalCookie> readFromLegacyStream(final byte[] byteA rray) | |
138 throws Exception { | |
139 ByteArrayInputStream inByteStream = new ByteArrayInputStream(byteArray); | |
140 DataInputStream in = new DataInputStream(inByteStream); | |
141 try { | |
142 return CanonicalCookie.readFromLegacyStream(in); | |
143 } finally { | |
144 in.close(); | |
145 inByteStream.close(); | |
146 } | |
147 } | |
148 | |
149 @Test | |
150 public void testReadFromLegacyStream() throws Exception { | |
151 ArrayList<CanonicalCookie> cookies = new ArrayList<>(); | |
152 cookies.add(new CanonicalCookie("http://url", "name", "value", "domain", "path", | |
153 0 /* creation */, 1 /* expiration */, 0 /* lastAccess */, false /* secure */, | |
154 true /* httpOnly */, 0 /* sameSite */, 0 /* priority */)); | |
155 cookies.add(new CanonicalCookie("http://url2", "name2", "value2", "domai n2", "path2", | |
156 10 /* creation */, 20 /* expiration */, 15 /* lastAccess */, tru e /* secure */, | |
157 false /* httpOnly */, 1 /* sameSite */, 1 /* priority */)); | |
158 // Doesn't support sameSite == 2. | |
159 byte[] byteArray = saveToStreamLegacyFormat(cookies); | |
160 List<CanonicalCookie> readCookies = readFromLegacyStream(byteArray); | |
161 | |
162 Assert.assertEquals(cookies.size(), readCookies.size()); | |
163 for (int i = 0; i < cookies.size(); ++i) { | |
164 assertCookiesEqual(cookies.get(i), readCookies.get(i)); | |
165 } | |
166 } | |
167 | |
168 @Test | |
169 public void testLegacySourceMergeSameSite() throws Exception { | |
170 ArrayList<CanonicalCookie> cookies = new ArrayList<>(); | |
171 cookies.add(new CanonicalCookie("http://url", "name", "value", "domain", "path", | |
172 0 /* creation */, 1 /* expiration */, 0 /* lastAccess */, false /* secure */, | |
173 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
| |
174 | |
175 byte[] byteArray = saveToStreamLegacyFormat(cookies); | |
176 List<CanonicalCookie> readCookies = readFromLegacyStream(byteArray); | |
177 | |
178 Assert.assertEquals(cookies.size(), readCookies.size()); | |
179 Assert.assertEquals(1, readCookies.get(0).getSameSite()); | |
180 } | |
181 | |
182 @Test | |
183 public void testReadWriteToFile() throws Exception { | |
184 ArrayList<CanonicalCookie> cookies = new ArrayList<>(); | |
185 cookies.add(new CanonicalCookie("http://url", "name", "value", "domain", "path", | |
186 0 /* creation */, 1 /* expiration */, 0 /* lastAccess */, false /* secure */, | |
187 true /* httpOnly */, 0 /* sameSite */, 0 /* priority */)); | |
188 cookies.add(new CanonicalCookie("http://url2", "name2", "value2", "domai n2", "path2", | |
189 10 /* creation */, 20 /* expiration */, 15 /* lastAccess */, tru e /* secure */, | |
190 false /* httpOnly */, 1 /* sameSite */, 1 /* priority */)); | |
191 CanonicalCookie cookiesArray[] = new CanonicalCookie[cookies.size()]; | |
192 cookies.toArray(cookiesArray); | |
193 | |
194 KeyGenerator keyGen = KeyGenerator.getInstance("AES"); | |
195 keyGen.init(128); | |
196 final SecretKey key = keyGen.generateKey(); | |
197 Cipher encryptCipher = Cipher.getInstance("AES"); | |
198 encryptCipher.init(Cipher.ENCRYPT_MODE, key); | |
199 | |
200 byte[] byteArray = CanonicalCookie.writeToByteArray(encryptCipher, cooki esArray); | |
201 File tempFile = File.createTempFile("cookies_junit_test", null); | |
202 tempFile.deleteOnExit(); | |
203 FileOutputStream outStream = new FileOutputStream(tempFile); | |
204 outStream.write(byteArray); | |
205 outStream.close(); | |
206 | |
207 Cipher decryptCipher = Cipher.getInstance("AES"); | |
208 decryptCipher.init(Cipher.DECRYPT_MODE, key); | |
209 List<CanonicalCookie> readCookies = | |
210 CanonicalCookie.readFromFileUnknownFormat(tempFile, decryptCiphe r); | |
211 Assert.assertEquals(cookies.size(), readCookies.size()); | |
212 for (int i = 0; i < cookies.size(); ++i) { | |
213 assertCookiesEqual(cookies.get(i), readCookies.get(i)); | |
214 } | |
215 } | |
216 | |
217 @Test | |
218 public void testReadFromLegacyFormat() throws Exception { | |
219 ArrayList<CanonicalCookie> cookies = new ArrayList<>(); | |
220 cookies.add(new CanonicalCookie("http://url", "name", "value", "domain", "path", | |
221 0 /* creation */, 1 /* expiration */, 0 /* lastAccess */, false /* secure */, | |
222 true /* httpOnly */, 0 /* sameSite */, 0 /* priority */)); | |
223 cookies.add(new CanonicalCookie("http://url2", "name2", "value2", "domai n2", "path2", | |
224 10 /* creation */, 20 /* expiration */, 15 /* lastAccess */, tru e /* secure */, | |
225 false /* httpOnly */, 1 /* sameSite */, 1 /* priority */)); | |
226 cookies.add(new CanonicalCookie("http://url2", "name2", "value2", "domai n2", "path2", | |
227 10 /* creation */, 20 /* expiration */, 15 /* lastAccess */, tru e /* secure */, | |
228 false /* httpOnly */, 2 /* sameSite */, 1 /* priority */)); | |
229 | |
230 KeyGenerator keyGen = KeyGenerator.getInstance("AES"); | |
231 keyGen.init(128); | |
232 final SecretKey key = keyGen.generateKey(); | |
233 Cipher encryptCipher = Cipher.getInstance("AES"); | |
234 encryptCipher.init(Cipher.ENCRYPT_MODE, key); | |
235 | |
236 byte[] byteArray = saveToStreamLegacyFormat(cookies); | |
237 File tempFile = File.createTempFile("cookies_junit_test", null); | |
238 tempFile.deleteOnExit(); | |
239 CipherOutputStream outStream = | |
240 new CipherOutputStream(new FileOutputStream(tempFile), encryptCi pher); | |
241 outStream.write(byteArray); | |
242 outStream.close(); | |
243 | |
244 Cipher decryptCipher = Cipher.getInstance("AES"); | |
245 decryptCipher.init(Cipher.DECRYPT_MODE, key); | |
246 List<CanonicalCookie> readCookies = | |
247 CanonicalCookie.readFromFileUnknownFormat(tempFile, decryptCiphe r); | |
248 Assert.assertEquals(cookies.size(), readCookies.size()); | |
249 for (int i = 0; i < 2; ++i) { | |
250 assertCookiesEqual(cookies.get(i), readCookies.get(i)); | |
251 } | |
252 Assert.assertEquals(1, readCookies.get(2).getSameSite()); | |
253 } | |
254 } | |
OLD | NEW |