Kenny Root | bb0f3f4 | 2015-05-28 09:27:08 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.conscrypt; |
| 18 | |
| 19 | import java.io.ByteArrayInputStream; |
| 20 | import java.io.ByteArrayOutputStream; |
| 21 | import java.io.IOException; |
| 22 | import java.io.ObjectInputStream; |
| 23 | import java.io.ObjectOutputStream; |
| 24 | import java.io.ObjectStreamClass; |
| 25 | import java.lang.reflect.Field; |
| 26 | |
| 27 | import junit.framework.TestCase; |
| 28 | |
| 29 | public 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 | } |