summaryrefslogtreecommitdiff
path: root/cmds/installd/run_dex2oat_test.cpp
blob: 0a638cd51be43d48020afe6557cd43fa9086b515 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <map>
#include <memory>
#include <string>

#include <android-base/logging.h>

#include <gtest/gtest.h>

#include "execv_helper.h"
#include "run_dex2oat.h"
#include "unique_file.h"

namespace android {
namespace installd {

class RunDex2OatTest : public testing::Test {
  public:
    static constexpr const char* INPUT_PATH = "/dir/input/basename.apk";
    static constexpr const char* OUTPUT_PATH = "/dir/output/basename.oat";
    static constexpr const char* FLAG_UNUSED = "{{FLAG_UNUSED}}";

    // UniqueFile closes FD. Avoid using standard I/O since the test is expected to print gtest
    // results. Alternatively, mock out UniqueFile to avoid the side effect of close(2).
    static constexpr int ZIP_FD = 3;
    static constexpr int OAT_FD = 4;
    static constexpr int INPUT_VDEX_FD = 5;
    static constexpr int OUTPUT_VDEX_FD = 6;
    static constexpr int IMAGE_FD = 7;
    static constexpr int PROFILE_FD = 8;
    static constexpr int DEX_METADATA_FD = 9;
    static constexpr int SWAP_FD = 10;

    using FakeSystemProperties = std::map<std::string, std::string>;

    // A fake RunDex2Oat that allows to override (fake) system properties and starts with none.
    class FakeRunDex2Oat : public RunDex2Oat {
      private:
        static constexpr const char* TRUE_STR = "true";
        static constexpr const char* FALSE_STR = "false";

      public:
        FakeRunDex2Oat(ExecVHelper* execv_helper, FakeSystemProperties* properties)
          : RunDex2Oat("/dir/bin/dex2oat", execv_helper), properties_(properties) { }

        virtual ~FakeRunDex2Oat() {}

        virtual std::string GetProperty(const std::string& key,
                                        const std::string& default_value) override {
            if (!properties_) {
                return default_value;
            }
            auto iter = properties_->find(key);
            if (iter == properties_->end()) {
                return default_value;
            }
            return iter->second;
        }

        virtual bool GetBoolProperty(const std::string& key, bool default_value) override {
            std::string value = GetProperty(key, "");
            if (value == "") {
                return default_value;
            }
            return value == TRUE_STR;
        }

      private:
        FakeSystemProperties* properties_;
    };

    struct RunDex2OatArgs {
        static std::unique_ptr<RunDex2OatArgs> MakeDefaultTestArgs() {
            auto args = std::make_unique<RunDex2OatArgs>();
            args->input_dex.reset(ZIP_FD, INPUT_PATH);
            args->output_oat.reset(OAT_FD, OUTPUT_PATH);
            args->input_vdex.reset(INPUT_VDEX_FD, "UNUSED_PATH");
            args->output_vdex.reset(OUTPUT_VDEX_FD, "UNUSED_PATH");
            args->instruction_set = "arm64";
            args->compilation_reason = "rundex2oattest";
            return args;
        }

        UniqueFile output_oat;
        UniqueFile output_vdex;
        UniqueFile output_image;
        UniqueFile input_dex;
        UniqueFile input_vdex;
        UniqueFile dex_metadata;
        UniqueFile profile;
        int swap_fd = -1;
        const char* instruction_set = nullptr;
        const char* compiler_filter = "extract";
        bool debuggable = false;
        bool post_bootcomplete = false;
        bool for_restore = false;
        const char* class_loader_context = nullptr;
        std::string class_loader_context_fds;
        int target_sdk_version = 0;
        bool enable_hidden_api_checks = false;
        bool generate_compact_dex = true;
        bool use_jitzygote_image = false;
        const char* compilation_reason = nullptr;
    };

    class FakeExecVHelper : public ExecVHelper {
      public:
        bool HasArg(const std::string& arg) const {
            auto end = argv_.end() - 1;  // To exclude the terminating nullptr
            return find(argv_.begin(), end, arg) != end;
        }

        bool FlagNotUsed(const std::string& flag) const {
            auto has_prefix = [flag](const char* arg) {
                return strncmp(arg, flag.c_str(), flag.size()) == 0;
            };
            auto end = argv_.end() - 1;  // To exclude the terminating nullptr
            return find_if(argv_.begin(), end, has_prefix) == end;
        }

        virtual void Exec(int exit_code) override {
            std::string cmd;
            for (auto arg : argv_) {
                if (arg == nullptr) {
                  continue;
                }
                cmd += arg;
                cmd += " ";
            }
            LOG(DEBUG) << "FakeExecVHelper exit_code: " << exit_code << " cmd: " << cmd << "\n";
        }
    };

    virtual void SetUp() override {
        execv_helper_.reset(new FakeExecVHelper());
        system_properties_.clear();
        initializeDefaultExpectedFlags();
    }

    // Initializes the default flags expected to a run.  It currently matches to the expected flags
    // with RunDex2OatArgs::MakeDefaultTestArgs.
    //
    // default_expected_flags_ defines a mapping of <flag_name, expected_value>, where flag_name is
    // something like "--flag-name", and expected_value can be "=value" or ":value" (depending on
    // its delimiter), "" (if no value is needed), or a special value of FLAG_UNUSED to indicates
    // that it should not be used.
    void initializeDefaultExpectedFlags() {
        default_expected_flags_.clear();

        // Files
        default_expected_flags_["--zip-fd"] = "=" + std::to_string(ZIP_FD);
        default_expected_flags_["--zip-location"] = "=basename.apk";
        default_expected_flags_["--oat-fd"] = "=" + std::to_string(OAT_FD);
        default_expected_flags_["--oat-location"] = "=" + std::string(OUTPUT_PATH);
        default_expected_flags_["--input-vdex-fd"] = "=" + std::to_string(INPUT_VDEX_FD);
        default_expected_flags_["--output-vdex-fd"] = "=" + std::to_string(OUTPUT_VDEX_FD);
        default_expected_flags_["--classpath-dir"] = "=/dir/input";
        default_expected_flags_["--app-image-fd"] = FLAG_UNUSED;
        default_expected_flags_["--profile-file-fd"] = FLAG_UNUSED;
        default_expected_flags_["--swap-fd"] = FLAG_UNUSED;
        default_expected_flags_["--class-loader-context"] = FLAG_UNUSED;
        default_expected_flags_["--class-loader-context-fds"] = FLAG_UNUSED;

        // Arch
        default_expected_flags_["--instruction-set"] = "=arm64";
        default_expected_flags_["--instruction-set-features"] = FLAG_UNUSED;
        default_expected_flags_["--instruction-set-variant"] = FLAG_UNUSED;
        default_expected_flags_["--cpu-set"] = FLAG_UNUSED;

        // Misc
        default_expected_flags_["--compiler-filter"] = "=extract";
        default_expected_flags_["--compilation-reason"] = "=rundex2oattest";
        default_expected_flags_["--compact-dex-level"] = FLAG_UNUSED;
        default_expected_flags_["-j"] = FLAG_UNUSED;
        default_expected_flags_["--max-image-block-size"] = FLAG_UNUSED;
        default_expected_flags_["--very-large-app-threshold"] = FLAG_UNUSED;
        default_expected_flags_["--resolve-startup-const-strings"] = FLAG_UNUSED;

        // Debug
        default_expected_flags_["--debuggable"] = FLAG_UNUSED;
        default_expected_flags_["--generate-debug-info"] = FLAG_UNUSED;
        default_expected_flags_["--generate-mini-debug-info"] = FLAG_UNUSED;

        // Runtime
        // TODO(victorhsieh): Check if the previous flag is actually --runtime-arg.
        default_expected_flags_["-Xms"] = FLAG_UNUSED;
        default_expected_flags_["-Xmx"] = FLAG_UNUSED;
        default_expected_flags_["-Xbootclasspath"] = FLAG_UNUSED;
        default_expected_flags_["-Xtarget-sdk-version"] = FLAG_UNUSED;
        default_expected_flags_["-Xhidden-api-policy"] = FLAG_UNUSED;
        default_expected_flags_["-Xnorelocate"] = FLAG_UNUSED;

        // Test only
        default_expected_flags_["--foo"] = FLAG_UNUSED;
        default_expected_flags_["--bar"] = FLAG_UNUSED;
        default_expected_flags_["--baz"] = FLAG_UNUSED;
    }

    void SetExpectedFlagUsed(const std::string& flag, const std::string& value) {
        auto iter = default_expected_flags_.find(flag);
        ASSERT_NE(iter, default_expected_flags_.end()) << "Must define the default value";
        iter->second = value;
    }

    void VerifyExpectedFlags() {
        for (auto const& [flag, value] : default_expected_flags_) {
            if (value == FLAG_UNUSED) {
                EXPECT_TRUE(execv_helper_->FlagNotUsed(flag))
                    << "Flag " << flag << " should be unused, but got the value " << value;
            } else if (value == "") {
                EXPECT_TRUE(execv_helper_->HasArg(flag))
                    << "Flag " << flag << " should be specified without value, but got " << value;
            } else {
                EXPECT_TRUE(execv_helper_->HasArg(flag + value))
                    << "Flag " << flag << value << " is not specificed";
            }
        }
    }

    void setSystemProperty(const std::string& key, const std::string& value) {
        system_properties_[key] = value;
    }

    void CallRunDex2Oat(std::unique_ptr<RunDex2OatArgs> args) {
        FakeRunDex2Oat runner(execv_helper_.get(), &system_properties_);
        runner.Initialize(args->output_oat,
                          args->output_vdex,
                          args->output_image,
                          args->input_dex,
                          args->input_vdex,
                          args->dex_metadata,
                          args->profile,
                          args->class_loader_context,
                          args->class_loader_context_fds,
                          args->swap_fd,
                          args->instruction_set,
                          args->compiler_filter,
                          args->debuggable,
                          args->post_bootcomplete,
                          args->for_restore,
                          args->target_sdk_version,
                          args->enable_hidden_api_checks,
                          args->generate_compact_dex,
                          args->use_jitzygote_image,
                          args->compilation_reason);
        runner.Exec(/*exit_code=*/ 0);
    }

  private:
    std::unique_ptr<FakeExecVHelper> execv_helper_;
    std::map<std::string, std::string> default_expected_flags_;
    FakeSystemProperties system_properties_;
};

TEST_F(RunDex2OatTest, BasicInputOutput) {
    auto execv_helper = std::make_unique<FakeExecVHelper>();
    CallRunDex2Oat(RunDex2OatArgs::MakeDefaultTestArgs());

    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, WithAllOtherInputFds) {
    auto args = RunDex2OatArgs::MakeDefaultTestArgs();
    args->output_image.reset(IMAGE_FD, "UNUSED_PATH");
    args->profile.reset(PROFILE_FD, "UNUSED_PATH");
    args->swap_fd = SWAP_FD;
    CallRunDex2Oat(std::move(args));

    SetExpectedFlagUsed("--app-image-fd", "=" + std::to_string(IMAGE_FD));
    SetExpectedFlagUsed("--profile-file-fd", "=" + std::to_string(PROFILE_FD));
    SetExpectedFlagUsed("--swap-fd", "=" + std::to_string(SWAP_FD));
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, WithClassLoaderContext) {
    auto args = RunDex2OatArgs::MakeDefaultTestArgs();
    args->class_loader_context = "CLASS_LOADER_CONTEXT";
    CallRunDex2Oat(std::move(args));

    SetExpectedFlagUsed("--class-loader-context", "=CLASS_LOADER_CONTEXT");
    SetExpectedFlagUsed("--class-loader-context-fds", FLAG_UNUSED);
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, WithClassLoaderContextAndFds) {
    auto args = RunDex2OatArgs::MakeDefaultTestArgs();
    args->class_loader_context = "CLASS_LOADER_CONTEXT";
    args->class_loader_context_fds = "CLASS_LOADER_CONTEXT_FDS";
    CallRunDex2Oat(std::move(args));

    SetExpectedFlagUsed("--class-loader-context", "=CLASS_LOADER_CONTEXT");
    SetExpectedFlagUsed("--class-loader-context-fds", "=CLASS_LOADER_CONTEXT_FDS");
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, WithOnlyClassLoaderContextFds) {
    auto args = RunDex2OatArgs::MakeDefaultTestArgs();
    args->class_loader_context_fds = "CLASS_LOADER_CONTEXT_FDS";
    CallRunDex2Oat(std::move(args));

    SetExpectedFlagUsed("--class-loader-context", FLAG_UNUSED);
    SetExpectedFlagUsed("--class-loader-context-fds", FLAG_UNUSED);
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, DoNotGenerateCompactDex) {
    auto args = RunDex2OatArgs::MakeDefaultTestArgs();
    args->generate_compact_dex = false;
    CallRunDex2Oat(std::move(args));

    SetExpectedFlagUsed("--compact-dex-level", "=none");
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, DoNotGenerateCompactDexWithVdexInPlaceUpdate) {
    auto args = RunDex2OatArgs::MakeDefaultTestArgs();
    args->generate_compact_dex = true;
    args->input_vdex.reset(INPUT_VDEX_FD, "UNUSED_PATH");
    args->output_vdex.reset(INPUT_VDEX_FD, "UNUSED_PATH");
    CallRunDex2Oat(std::move(args));

    SetExpectedFlagUsed("--compact-dex-level", "=none");
    SetExpectedFlagUsed("--output-vdex-fd", "=" + std::to_string(INPUT_VDEX_FD));
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, ISA) {
    setSystemProperty("dalvik.vm.isa.x86.features", "a-x86-feature");
    setSystemProperty("dalvik.vm.isa.x86.variant", "a-x86-variant");
    auto args = RunDex2OatArgs::MakeDefaultTestArgs();
    args->instruction_set = "x86";
    CallRunDex2Oat(std::move(args));

    SetExpectedFlagUsed("--instruction-set", "=x86");
    SetExpectedFlagUsed("--instruction-set-features", "=a-x86-feature");
    SetExpectedFlagUsed("--instruction-set-variant", "=a-x86-variant");
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, CpuSetPreBootComplete) {
    setSystemProperty("dalvik.vm.boot-dex2oat-cpu-set", "1,2");
    auto args = RunDex2OatArgs::MakeDefaultTestArgs();
    args->post_bootcomplete = false;
    CallRunDex2Oat(std::move(args));

    SetExpectedFlagUsed("--cpu-set", "=1,2");
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, CpuSetPostBootCompleteNotForRestore) {
    setSystemProperty("dalvik.vm.dex2oat-cpu-set", "1,2");
    auto args = RunDex2OatArgs::MakeDefaultTestArgs();
    args->post_bootcomplete = true;
    args->for_restore = false;
    CallRunDex2Oat(std::move(args));

    SetExpectedFlagUsed("--cpu-set", "=1,2");
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, CpuSetPostBootCompleteForRestore) {
    setSystemProperty("dalvik.vm.restore-dex2oat-cpu-set", "1,2");
    setSystemProperty("dalvik.vm.dex2oat-cpu-set", "2,3");
    auto args = RunDex2OatArgs::MakeDefaultTestArgs();
    args->post_bootcomplete = true;
    args->for_restore = true;
    CallRunDex2Oat(std::move(args));

    SetExpectedFlagUsed("--cpu-set", "=1,2");
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, CpuSetPostBootCompleteForRestore_Backup) {
    setSystemProperty("dalvik.vm.restore-dex2oat-cpu-set", "");
    setSystemProperty("dalvik.vm.dex2oat-cpu-set", "1,2");
    auto args = RunDex2OatArgs::MakeDefaultTestArgs();
    args->post_bootcomplete = true;
    args->for_restore = true;
    CallRunDex2Oat(std::move(args));

    SetExpectedFlagUsed("--cpu-set", "=1,2");
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, Runtime) {
    setSystemProperty("dalvik.vm.dex2oat-Xms", "1234m");
    setSystemProperty("dalvik.vm.dex2oat-Xmx", "5678m");
    auto args = RunDex2OatArgs::MakeDefaultTestArgs();
    args->target_sdk_version = 30;
    args->enable_hidden_api_checks = true;
    CallRunDex2Oat(std::move(args));

    SetExpectedFlagUsed("-Xms", "1234m");
    SetExpectedFlagUsed("-Xmx", "5678m");
    SetExpectedFlagUsed("-Xtarget-sdk-version", ":30");
    SetExpectedFlagUsed("-Xhidden-api-policy", ":enabled");
    SetExpectedFlagUsed("-Xnorelocate", FLAG_UNUSED);
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, SkipRelocationInMinFramework) {
    setSystemProperty("vold.decrypt", "trigger_restart_min_framework");
    CallRunDex2Oat(RunDex2OatArgs::MakeDefaultTestArgs());

    SetExpectedFlagUsed("--compiler-filter", "=extract");
    SetExpectedFlagUsed("-Xnorelocate", "");
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, SkipRelocationIfDecryptedWithFullDiskEncryption) {
    setSystemProperty("vold.decrypt", "1");
    CallRunDex2Oat(RunDex2OatArgs::MakeDefaultTestArgs());

    SetExpectedFlagUsed("--compiler-filter", "=extract");
    SetExpectedFlagUsed("-Xnorelocate", "");
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, DalvikVmDex2oatFilter) {
    setSystemProperty("dalvik.vm.dex2oat-filter", "speed");
    auto args = RunDex2OatArgs::MakeDefaultTestArgs();
    args->compiler_filter = nullptr;
    CallRunDex2Oat(std::move(args));

    SetExpectedFlagUsed("--compiler-filter", "=speed");
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, ResolveStartupStartings) {
    setSystemProperty("dalvik.vm.dex2oat-resolve-startup-strings", "false");
    CallRunDex2Oat(RunDex2OatArgs::MakeDefaultTestArgs());

    SetExpectedFlagUsed("--resolve-startup-const-strings", "=false");
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, ResolveStartupStartingsOverride) {
    setSystemProperty("dalvik.vm.dex2oat-resolve-startup-strings", "false");
    setSystemProperty("persist.device_config.runtime.dex2oat_resolve_startup_strings", "true");
    CallRunDex2Oat(RunDex2OatArgs::MakeDefaultTestArgs());

    SetExpectedFlagUsed("--resolve-startup-const-strings", "=true");
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, ThreadsPreBootComplete) {
    setSystemProperty("dalvik.vm.boot-dex2oat-threads", "2");
    auto args = RunDex2OatArgs::MakeDefaultTestArgs();
    args->post_bootcomplete = false;
    CallRunDex2Oat(std::move(args));

    SetExpectedFlagUsed("-j", "2");
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, ThreadsPostBootCompleteNotForRestore) {
    setSystemProperty("dalvik.vm.dex2oat-threads", "3");
    auto args = RunDex2OatArgs::MakeDefaultTestArgs();
    args->post_bootcomplete = true;
    args->for_restore = false;
    CallRunDex2Oat(std::move(args));

    SetExpectedFlagUsed("-j", "3");
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, ThreadsPostBootCompleteForRestore) {
    setSystemProperty("dalvik.vm.restore-dex2oat-threads", "4");
    setSystemProperty("dalvik.vm.dex2oat-threads", "5");
    auto args = RunDex2OatArgs::MakeDefaultTestArgs();
    args->post_bootcomplete = true;
    args->for_restore = true;
    CallRunDex2Oat(std::move(args));

    SetExpectedFlagUsed("-j", "4");
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, ThreadsPostBootCompleteForRestore_Backup) {
    setSystemProperty("dalvik.vm.restore-dex2oat-threads", "");
    setSystemProperty("dalvik.vm.dex2oat-threads", "5");
    auto args = RunDex2OatArgs::MakeDefaultTestArgs();
    args->post_bootcomplete = true;
    args->for_restore = true;
    CallRunDex2Oat(std::move(args));

    SetExpectedFlagUsed("-j", "5");
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, Debuggable) {
    auto args = RunDex2OatArgs::MakeDefaultTestArgs();
    args->debuggable = true;
    CallRunDex2Oat(std::move(args));

    SetExpectedFlagUsed("--debuggable", "");
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, AlwaysDebuggable) {
    setSystemProperty("dalvik.vm.always_debuggable", "1");
    CallRunDex2Oat(RunDex2OatArgs::MakeDefaultTestArgs());

    SetExpectedFlagUsed("--debuggable", "");
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, GenerateDebugInfo) {
    setSystemProperty("debug.generate-debug-info", "true");
    CallRunDex2Oat(RunDex2OatArgs::MakeDefaultTestArgs());

    SetExpectedFlagUsed("--generate-debug-info", "");
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, HiddenApiCheck) {
    auto args = RunDex2OatArgs::MakeDefaultTestArgs();
    args->enable_hidden_api_checks = true;
    CallRunDex2Oat(std::move(args));

    SetExpectedFlagUsed("-Xhidden-api-policy", ":enabled");
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, Misc) {
    setSystemProperty("dalvik.vm.dex2oat-max-image-block-size", "524288");
    setSystemProperty("dalvik.vm.dex2oat-very-large", "100000");
    CallRunDex2Oat(RunDex2OatArgs::MakeDefaultTestArgs());

    SetExpectedFlagUsed("--max-image-block-size", "=524288");
    SetExpectedFlagUsed("--very-large-app-threshold", "=100000");
    VerifyExpectedFlags();
}

TEST_F(RunDex2OatTest, ExtraFlags) {
    setSystemProperty("dalvik.vm.dex2oat-flags", "--foo=123 --bar:456 --baz");
    CallRunDex2Oat(RunDex2OatArgs::MakeDefaultTestArgs());

    SetExpectedFlagUsed("--foo", "=123");
    SetExpectedFlagUsed("--bar", ":456");
    SetExpectedFlagUsed("--baz", "");
    VerifyExpectedFlags();
}

}  // namespace installd
}  // namespace android