1 /*
2  * Copyright (C) 2019 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 #include <iostream>
18 
19 #include <gtest/gtest.h>
20 
21 #include "apk_archive.h"
22 
23 // Friend test to get around private scope of ApkArchive private functions.
24 class ApkArchiveTester {
25   public:
26     ApkArchiveTester(const std::string& path) : archive_(path) {}
27 
28     bool ready() { return archive_.ready(); }
29 
30     auto ExtractMetadata() { return archive_.ExtractMetadata(); }
31 
32     ApkArchive::Location GetCDLocation() { return archive_.GetCDLocation(); }
33     ApkArchive::Location GetSignatureLocation(size_t start) {
34         return archive_.GetSignatureLocation(start);
35     }
36 
37   private:
38     ApkArchive archive_;
39 };
40 
41 TEST(ApkArchiveTest, TestApkArchiveSizes) {
42     ApkArchiveTester archiveTester("fastdeploy/testdata/sample.apk");
43     EXPECT_TRUE(archiveTester.ready());
44 
45     ApkArchive::Location cdLoc = archiveTester.GetCDLocation();
46     EXPECT_TRUE(cdLoc.valid);
47     ASSERT_EQ(cdLoc.offset, 2044145u);
48     ASSERT_EQ(cdLoc.size, 49390u);
49 
50     // Check that block can be retrieved
51     ApkArchive::Location sigLoc = archiveTester.GetSignatureLocation(cdLoc.offset);
52     EXPECT_TRUE(sigLoc.valid);
53     ASSERT_EQ(sigLoc.offset, 2040049u);
54     ASSERT_EQ(sigLoc.size, 4088u);
55 }
56 
57 TEST(ApkArchiveTest, TestApkArchiveDump) {
58     ApkArchiveTester archiveTester("fastdeploy/testdata/sample.apk");
59     EXPECT_TRUE(archiveTester.ready());
60 
61     auto dump = archiveTester.ExtractMetadata();
62     ASSERT_EQ(dump.cd().size(), 49390u);
63     ASSERT_EQ(dump.signature().size(), 4088u);
64 }
65 
66 TEST(ApkArchiveTest, WrongApk) {
67     ApkArchiveTester archiveTester("fastdeploy/testdata/sample.cd");
68     EXPECT_TRUE(archiveTester.ready());
69 
70     auto dump = archiveTester.ExtractMetadata();
71     ASSERT_EQ(dump.cd().size(), 0u);
72     ASSERT_EQ(dump.signature().size(), 0u);
73 }
74