1 /*
2  * Copyright (C) 2018 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 com.android.cts.releaseparser;
18 
19 import com.android.cts.releaseparser.ReleaseProto.*;
20 import com.google.protobuf.TextFormat;
21 
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.junit.runners.JUnit4;
25 
26 import java.io.File;
27 
28 import static org.junit.Assert.*;
29 
30 /** Unit tests for {@link ApkParser} */
31 @RunWith(JUnit4.class)
32 public class ApkParserTest {
33     private static final String PB_TXT = ".pb.txt";
34     // HelloActivity.apk's source code: android/cts/tests/tests/jni/AndroidManifest.xml
35     private static final String TEST_SIMPLE_APK = "HelloActivity.apk";
36 
37     // Shell.apk's source code:
38     // android/frameworks/base/packages/Shell/AndroidManifest.xml
39     private static final String TEST_SYS_APK = "Shell.apk";
40 
41     // CtsJniTestCases.apk's source code:
42     // android/development/samples/HelloActivity/AndroidManifest.xml
43     private static final String TEST_SO_APK = "CtsJniTestCases.apk";
44 
45     /**
46      * Test {@link ApkParser} with an simple APK
47      *
48      * @throws Exception
49      */
50     @Test
testSimpleApk()51     public void testSimpleApk() throws Exception {
52         testApkParser(TEST_SIMPLE_APK);
53     }
54 
55     /**
56      * Test {@link ApkParser} with an Sys(priv-app) APK
57      *
58      * @throws Exception
59      */
60     @Test
testSysApk()61     public void testSysApk() throws Exception {
62         testApkParser(TEST_SYS_APK);
63     }
64 
65     /**
66      * Test {@link ApkParser} with an APK with Shared Objects/Nactive Code
67      *
68      * @throws Exception
69      */
70     @Test
testSoApk()71     public void testSoApk() throws Exception {
72         testApkParser(TEST_SO_APK);
73     }
74 
testApkParser(String fileName)75     private void testApkParser(String fileName) throws Exception {
76         File apkFile = ClassUtils.getResrouceFile(getClass(), fileName);
77         ApkParser aParser = new ApkParser(apkFile);
78         Entry.Builder fileEntryBuilder = aParser.getFileEntryBuilder();
79         fileEntryBuilder.setName(fileName);
80 
81         Entry fileEntry = fileEntryBuilder.build();
82         Entry.Builder expectedfileEntryBuilder = Entry.newBuilder();
83 
84         String txtProtobufFileName = fileName + PB_TXT;
85         TextFormat.getParser()
86                 .merge(
87                         ClassUtils.openResourceAsStreamReader(getClass(), txtProtobufFileName),
88                         expectedfileEntryBuilder);
89         assertTrue(
90                 String.format(
91                         "ApkParser does not return the same Entry of %s as %s.\n%s",
92                         fileName, txtProtobufFileName, TextFormat.printToString(fileEntry)),
93                 fileEntry.equals(expectedfileEntryBuilder.build()));
94     }
95 }
96