blob: 626b76c4fa47eb68ab8c67876a1de51fd63712eb [file] [log] [blame]
Kenny Rootbb0f3f42015-05-28 09:27:08 -07001/*
2 * Copyright 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * 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
17package org.conscrypt;
18
19import java.io.ByteArrayInputStream;
20import java.io.ByteArrayOutputStream;
21import java.io.IOException;
22import java.io.ObjectInputStream;
23import java.io.ObjectOutputStream;
24import java.io.ObjectStreamClass;
25import java.lang.reflect.Field;
26
27import junit.framework.TestCase;
28
29public class OpenSSLX509CertificateTest extends TestCase {
30 public void testSerialization_NoContextDeserialization() throws Exception {
31 // Set correct serialVersionUID
32 {
33 ObjectStreamClass clDesc = ObjectStreamClass.lookup(OpenSSLX509Certificate.class);
34 assertNotNull(clDesc);
35
36 // Set our fake class's serialization UID.
37 Field targetUID = ZpenSSLX509Certificate.class.getDeclaredField("serialVersionUID");
38 targetUID.setAccessible(true);
39 targetUID.set(null, clDesc.getSerialVersionUID());
40 }
41
42 final byte[] impostorBytes;
43 // Serialization
44 {
45 ByteArrayOutputStream baos = new ByteArrayOutputStream();
46 ObjectOutputStream oos = new ObjectOutputStream(baos);
47 oos.writeObject(new ZpenSSLX509Certificate(0xA5A5A5A5A5A5A5A5L));
48 oos.close();
49 impostorBytes = baos.toByteArray();
50 }
51
52 // Fix class name
53 {
54 boolean fixed = false;
55 for (int i = 0; i < impostorBytes.length - 4; i++) {
56 if (impostorBytes[i] == 'Z' && impostorBytes[i + 1] == 'p'
57 && impostorBytes[i + 2] == 'e' && impostorBytes[i + 3] == 'n') {
58 impostorBytes[i] = 'O';
59 fixed = true;
60 break;
61 }
62 }
63 assertTrue(fixed);
64 }
65
66 // Deserialization
67 {
68 ByteArrayInputStream bais = new ByteArrayInputStream(impostorBytes);
69 ObjectInputStream ois = new ObjectInputStream(bais);
70 OpenSSLX509Certificate cert = (OpenSSLX509Certificate) ois.readObject();
71 ois.close();
72 assertEquals(0L, cert.getContext());
73 }
74 }
75}