Calin Juravle | dff4729 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 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 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | |
| 20 | #include <android-base/logging.h> |
| 21 | #include <gtest/gtest.h> |
| 22 | |
| 23 | #include "otapreopt_parameters.h" |
| 24 | |
| 25 | namespace android { |
| 26 | namespace installd { |
| 27 | |
| 28 | static bool ParseBool(const char* in) { |
| 29 | if (strcmp(in, "true") == 0) { |
| 30 | return true; |
| 31 | } |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | static const char* ParseNull(const char* arg) { |
| 36 | return (strcmp(arg, "!") == 0) ? nullptr : arg; |
| 37 | } |
| 38 | |
| 39 | class OTAPreoptTest : public testing::Test { |
| 40 | protected: |
| 41 | virtual void SetUp() { |
Calin Juravle | 0c609c2 | 2018-02-12 17:39:37 -0800 | [diff] [blame^] | 42 | setenv("ANDROID_LOG_TAGS", "*:f", 1); |
| 43 | android::base::InitLogging(nullptr, android::base::StderrLogger); |
Calin Juravle | dff4729 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void verifyPackageParameters(const OTAPreoptParameters& params, |
| 47 | uint32_t version, |
| 48 | bool versioned, |
| 49 | const char** args) { |
| 50 | // otapreopt target-slot [version] dexopt {DEXOPT_PARAMETERS} |
| 51 | int i = 0; |
| 52 | if (version > 2 || (version == 2 && versioned)) { |
| 53 | i += 4; |
| 54 | } else { |
| 55 | i += 3; |
| 56 | } |
| 57 | ASSERT_STREQ(params.target_slot.c_str(), args[1]); |
| 58 | ASSERT_STREQ(params.apk_path, args[i++]); |
| 59 | ASSERT_EQ(params.uid, static_cast<uid_t>(atoi(args[i++]))); |
| 60 | ASSERT_STREQ(params.pkgName, args[i++]); |
| 61 | ASSERT_STREQ(params.instruction_set, args[i++]); |
| 62 | ASSERT_EQ(params.dexopt_needed, atoi(args[i++])); |
| 63 | ASSERT_STREQ(params.oat_dir, args[i++]); |
| 64 | ASSERT_EQ(params.dexopt_flags, atoi(args[i++])); |
| 65 | ASSERT_STREQ(params.compiler_filter, args[i++]); |
| 66 | ASSERT_STREQ(params.volume_uuid, ParseNull(args[i++])); |
| 67 | ASSERT_STREQ(params.shared_libraries, ParseNull(args[i++])); |
| 68 | if (version > 1) { |
| 69 | ASSERT_STREQ(params.se_info, ParseNull(args[i++])); |
| 70 | } else { |
Calin Juravle | 0c609c2 | 2018-02-12 17:39:37 -0800 | [diff] [blame^] | 71 | ASSERT_EQ(params.se_info, nullptr); |
Calin Juravle | dff4729 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 72 | } |
| 73 | if (version > 2) { |
| 74 | ASSERT_EQ(params.downgrade, ParseBool(args[i++])); |
| 75 | } else { |
| 76 | ASSERT_FALSE(params.downgrade); |
| 77 | } |
| 78 | if (version > 3) { |
| 79 | ASSERT_EQ(params.target_sdk_version, atoi(args[i++])); |
| 80 | } else { |
| 81 | ASSERT_EQ(params.target_sdk_version, 0); |
| 82 | } |
| 83 | if (version > 4) { |
| 84 | ASSERT_STREQ(params.profile_name, ParseNull(args[i++])); |
| 85 | } else { |
| 86 | ASSERT_STREQ(params.profile_name, "primary.prof"); |
| 87 | } |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 88 | if (version > 5) { |
| 89 | ASSERT_STREQ(params.dex_metadata_path, ParseNull(args[i++])); |
| 90 | } else { |
Calin Juravle | 0c609c2 | 2018-02-12 17:39:37 -0800 | [diff] [blame^] | 91 | ASSERT_EQ(params.dex_metadata_path, nullptr); |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 92 | } |
Calin Juravle | dff4729 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | const char* getVersionCStr(uint32_t version) { |
| 96 | switch (version) { |
| 97 | case 1: return "1"; |
| 98 | case 2: return "2"; |
| 99 | case 3: return "3"; |
| 100 | case 4: return "4"; |
| 101 | case 5: return "5"; |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 102 | case 6: return "6"; |
Calin Juravle | dff4729 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 103 | } |
| 104 | return nullptr; |
| 105 | } |
| 106 | |
| 107 | std::vector<const char*> getArgs(uint32_t version, bool versioned) { |
| 108 | std::vector<const char*> args; |
| 109 | args.push_back("otapreopt"); // "otapreopt" |
| 110 | args.push_back("a"); // slot |
| 111 | if (versioned) { |
| 112 | args.push_back(getVersionCStr(version)); |
| 113 | } |
| 114 | args.push_back("dexopt"); // "dexopt" |
| 115 | args.push_back("foo.apk"); // apk_path |
| 116 | args.push_back("123"); // uid |
| 117 | args.push_back("pkgname"); // pkg |
| 118 | args.push_back("arm"); // isa |
| 119 | args.push_back("1"); // dexopt_needed (DEX2OAT_FROM_SCRATCH) |
| 120 | args.push_back("oat_dir"); // oat_dir |
| 121 | args.push_back("0"); // dexopt_flags |
| 122 | args.push_back("speed"); // filter |
| 123 | args.push_back("!"); // volume |
| 124 | args.push_back("shared.lib"); // libs |
| 125 | |
| 126 | if (version > 1) { |
| 127 | args.push_back("!"); // seinfo |
| 128 | } |
| 129 | if (version > 2) { |
| 130 | args.push_back("true"); // downgrade |
| 131 | } |
| 132 | if (version > 3) { |
| 133 | args.push_back("28"); // sdk_version |
| 134 | } |
| 135 | if (version > 4) { |
| 136 | args.push_back("split_a.prof"); // profile_name |
| 137 | } |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 138 | if (version > 5) { |
| 139 | args.push_back("dex_metadata.dm"); // dex_metadata_path |
| 140 | } |
Calin Juravle | dff4729 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 141 | args.push_back(nullptr); // we have to end with null. |
| 142 | return args; |
| 143 | } |
| 144 | |
| 145 | void VerifyReadArguments(uint32_t version, bool versioned) { |
| 146 | OTAPreoptParameters params; |
| 147 | std::vector<const char*> args = getArgs(version, versioned); |
| 148 | ASSERT_TRUE(params.ReadArguments(args.size() - 1, args.data())); |
| 149 | verifyPackageParameters(params, version, versioned, args.data()); |
| 150 | } |
| 151 | }; |
| 152 | |
| 153 | TEST_F(OTAPreoptTest, ReadArgumentsV1) { |
| 154 | VerifyReadArguments(1, false); |
| 155 | } |
| 156 | |
| 157 | TEST_F(OTAPreoptTest, ReadArgumentsV2Unversioned) { |
| 158 | VerifyReadArguments(2, false); |
| 159 | } |
| 160 | |
| 161 | TEST_F(OTAPreoptTest, ReadArgumentsV2) { |
| 162 | VerifyReadArguments(2, true); |
| 163 | } |
| 164 | |
| 165 | TEST_F(OTAPreoptTest, ReadArgumentsV3) { |
| 166 | VerifyReadArguments(3, true); |
| 167 | } |
| 168 | |
| 169 | TEST_F(OTAPreoptTest, ReadArgumentsV4) { |
| 170 | VerifyReadArguments(4, true); |
| 171 | } |
| 172 | |
| 173 | TEST_F(OTAPreoptTest, ReadArgumentsV5) { |
| 174 | VerifyReadArguments(5, true); |
| 175 | } |
| 176 | |
Calin Juravle | 62c5a37 | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 177 | TEST_F(OTAPreoptTest, ReadArgumentsV6) { |
| 178 | VerifyReadArguments(6, true); |
| 179 | } |
| 180 | |
Calin Juravle | dff4729 | 2018-02-01 14:44:56 +0000 | [diff] [blame] | 181 | TEST_F(OTAPreoptTest, ReadArgumentsFailToManyArgs) { |
| 182 | OTAPreoptParameters params; |
| 183 | std::vector<const char*> args = getArgs(5, true); |
| 184 | args[2] = "3"; // pretend it's version 3. It should fail since there are too many args. |
| 185 | ASSERT_FALSE(params.ReadArguments(args.size() - 1, args.data())); |
| 186 | } |
| 187 | |
| 188 | TEST_F(OTAPreoptTest, ReadArgumentsFailInsufficientArgs) { |
| 189 | OTAPreoptParameters params; |
| 190 | std::vector<const char*> args = getArgs(4, true); |
| 191 | args[2] = "5"; // pretend it's version 5. It should fail since there are insufficient args. |
| 192 | ASSERT_FALSE(params.ReadArguments(args.size() - 1, args.data())); |
| 193 | } |
| 194 | |
| 195 | TEST_F(OTAPreoptTest, ReadArgumentsFailInvalidDexopt) { |
| 196 | OTAPreoptParameters params; |
| 197 | std::vector<const char*> args = getArgs(4, true); |
| 198 | args[3] = "dexopt-invalid"; |
| 199 | ASSERT_FALSE(params.ReadArguments(args.size() - 1, args.data())); |
| 200 | } |
| 201 | |
| 202 | TEST_F(OTAPreoptTest, ReadArgumentsFailInvalidSlot) { |
| 203 | OTAPreoptParameters params; |
| 204 | std::vector<const char*> args = getArgs(3, true); |
| 205 | args[1] = "invalid-slot???"; |
| 206 | ASSERT_FALSE(params.ReadArguments(args.size() - 1, args.data())); |
| 207 | } |
| 208 | |
| 209 | } // namespace installd |
| 210 | } // namespace android |