• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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 android.content.pm.cts;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertNotSame;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24 
25 import android.content.pm.Signature;
26 import android.os.Parcel;
27 import android.platform.test.annotations.AppModeFull;
28 
29 import androidx.test.runner.AndroidJUnit4;
30 
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 
34 import java.util.Arrays;
35 
36 @RunWith(AndroidJUnit4.class)
37 @AppModeFull // TODO(Instant) Figure out which APIs should work.
38 public class SignatureTest {
39 
40     private static final String SIGNATURE_STRING = "1234567890abcdef";
41     // SIGNATURE_BYTE_ARRAY is the byte code of SIGNATURE_STRING.
42     private static final byte[] SIGNATURE_BYTE_ARRAY = { (byte) 0x12, (byte) 0x34, (byte) 0x56,
43             (byte) 0x78, (byte) 0x90, (byte) 0xab, (byte) 0xcd, (byte) 0xef };
44     // DIFF_BYTE_ARRAY has different content to SIGNATURE_STRING.
45     private static final byte[] DIFF_BYTE_ARRAY = { (byte) 0xfe, (byte) 0xdc, (byte) 0xba,
46             (byte) 0x09, (byte) 0x87, (byte) 0x65, (byte) 0x43, (byte) 0x21 };
47 
48     @Test
testSignatureStringConstructorValid()49     public void testSignatureStringConstructorValid() {
50         Signature signature = new Signature(SIGNATURE_STRING);
51         byte[] actualByteArray = signature.toByteArray();
52         assertTrue("Output byte array should match constructor byte array.",
53                 Arrays.equals(SIGNATURE_BYTE_ARRAY, actualByteArray));
54     }
55 
56     @Test
testSignatureStringConstructorNull()57     public void testSignatureStringConstructorNull() {
58         String sig = null;
59 
60         try {
61             Signature signature = new Signature(sig);
62             fail("Should throw NullPointerException on null input");
63         } catch (NullPointerException e) {
64             // pass
65         }
66     }
67 
68     @Test
testSignatureStringConstructorInvalidLength()69     public void testSignatureStringConstructorInvalidLength() {
70         try {
71             Signature signature = new Signature("123");
72             fail("Should throw IllegalArgumentException on odd-sized input");
73         } catch (IllegalArgumentException e) {
74             // pass
75         }
76     }
77 
78     @Test
testSignatureByteArrayToCharsString()79     public void testSignatureByteArrayToCharsString() {
80         Signature signature = new Signature(SIGNATURE_BYTE_ARRAY);
81         String actualString = signature.toCharsString();
82         assertEquals(SIGNATURE_STRING, actualString);
83     }
84 
85     @Test
testSignatureByteArrayConstructorNull()86     public void testSignatureByteArrayConstructorNull() {
87         byte[] sig = null;
88 
89         try {
90             Signature signature = new Signature(sig);
91             fail("Should throw NullPointerException on null input");
92         } catch (NullPointerException e) {
93             // pass
94         }
95     }
96 
97     @Test
testSignatureToChars()98     public void testSignatureToChars() {
99         Signature signature = new Signature(SIGNATURE_BYTE_ARRAY);
100         char[] charArray = signature.toChars();
101         String actualString = new String(charArray);
102         assertEquals(SIGNATURE_STRING, actualString);
103     }
104 
105     @Test
testSignatureToCharsExistingArrayCorrectlySized()106     public void testSignatureToCharsExistingArrayCorrectlySized() {
107         char[] existingCharArray = new char[SIGNATURE_STRING.length()];
108         int[] intArray = new int[1];
109 
110         Signature signature = new Signature(SIGNATURE_BYTE_ARRAY);
111 
112         char[] charArray = signature.toChars(existingCharArray, intArray);
113 
114         assertTrue("Should return the same object since it's correctly sized.",
115                 existingCharArray == charArray);
116 
117         String actualString = new String(charArray);
118         assertEquals("The re-encoded Signature should match the constructor input",
119                 SIGNATURE_STRING, actualString);
120 
121         // intArray[0] represents the length of array.
122         assertEquals(intArray[0], SIGNATURE_BYTE_ARRAY.length);
123     }
124 
125     @Test
testSignatureToCharsExistingArrayTooSmall()126     public void testSignatureToCharsExistingArrayTooSmall() {
127         char[] existingCharArray = new char[0];
128         int[] intArray = new int[1];
129 
130         Signature signature = new Signature(SIGNATURE_BYTE_ARRAY);
131         char[] charArray = signature.toChars(existingCharArray, intArray);
132 
133         assertFalse("Should return a new array since the existing one is too small",
134                 existingCharArray == charArray);
135 
136         String actualString = new String(charArray);
137         assertEquals("The re-encoded Signature should match the constructor input",
138                 SIGNATURE_STRING, actualString);
139 
140         // intArray[0] represents the length of array.
141         assertEquals(intArray[0], SIGNATURE_BYTE_ARRAY.length);
142     }
143 
144     @Test
testSignatureToCharsNullArrays()145     public void testSignatureToCharsNullArrays() {
146         char[] existingCharArray = null;
147         int[] intArray = null;
148 
149         Signature signature = new Signature(SIGNATURE_BYTE_ARRAY);
150         char[] charArray = signature.toChars(existingCharArray, intArray);
151 
152         assertFalse("Should return a new array since the existing one is too small",
153                 existingCharArray == charArray);
154 
155         String actualString = new String(charArray);
156         assertEquals("The re-encoded Signature should match the constructor input",
157                 SIGNATURE_STRING, actualString);
158     }
159 
160     @Test
testSignatureStringToByteArray()161     public void testSignatureStringToByteArray() {
162         Signature signature = new Signature(SIGNATURE_BYTE_ARRAY);
163         byte[] actualByteArray = signature.toByteArray();
164 
165         assertFalse("Should return a different array to avoid modification",
166                 SIGNATURE_BYTE_ARRAY == actualByteArray);
167 
168         assertTrue("Output byte array should match constructor byte array.",
169                 Arrays.equals(SIGNATURE_BYTE_ARRAY, actualByteArray));
170     }
171 
172     @Test
testTools()173     public void testTools() {
174         Signature byteSignature = new Signature(SIGNATURE_BYTE_ARRAY);
175         Signature stringSignature = new Signature(SIGNATURE_STRING);
176 
177         // Test describeContents, equals
178         assertEquals(0, byteSignature.describeContents());
179         assertTrue(byteSignature.equals(stringSignature));
180 
181         // Test hashCode
182         byteSignature = new Signature(DIFF_BYTE_ARRAY);
183         assertNotSame(byteSignature.hashCode(), stringSignature.hashCode());
184 
185         // Test writeToParcel
186         Parcel p = Parcel.obtain();
187         byteSignature.writeToParcel(p, 0);
188         p.setDataPosition(0);
189         Signature signatureFromParcel = Signature.CREATOR.createFromParcel(p);
190         assertTrue(signatureFromParcel.equals(byteSignature));
191         p.recycle();
192     }
193 
194     @Test
testSignatureHashCodeEquals_doesNotIncludeFlags()195     public void testSignatureHashCodeEquals_doesNotIncludeFlags() {
196         // Some classes rely on the hash code and equals not including the flags / capabilities
197         // for the signer. This test verifies two signers with the same signature but different
198         // flags have the same hash code and are still equal.
199         Signature signatureWithAllCaps = new Signature(SIGNATURE_STRING);
200         signatureWithAllCaps.setFlags(31);
201         Signature signatureWithNoCaps = new Signature(SIGNATURE_STRING);
202         signatureWithNoCaps.setFlags(0);
203 
204         assertEquals(signatureWithAllCaps.hashCode(), signatureWithNoCaps.hashCode());
205         assertEquals(signatureWithAllCaps, signatureWithNoCaps);
206     }
207 }
208