1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/browser/chromeos/arc/test/test_arc_session_manager.h"
6 
7 #include <utility>
8 
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "chrome/browser/chromeos/arc/session/arc_session_manager.h"
12 
13 namespace arc {
14 namespace {
15 
CreateFilesAndDirectories(const base::FilePath & temp_dir,base::FilePath * source_dir,base::FilePath * dest_dir)16 bool CreateFilesAndDirectories(const base::FilePath& temp_dir,
17                                base::FilePath* source_dir,
18                                base::FilePath* dest_dir) {
19   if (!base::CreateTemporaryDirInDir(temp_dir, "source", source_dir))
20     return false;
21 
22   // Create empty prop files so ArcSessionManager's property expansion code
23   // works like production.
24   for (const char* filename :
25        {"default.prop", "build.prop", "vendor_build.prop",
26         "system_ext_build.prop", "product_build.prop", "odm_build.prop"}) {
27     if (base::WriteFile(source_dir->Append(filename), "", 1) != 1)
28       return false;
29   }
30 
31   return base::CreateTemporaryDirInDir(temp_dir, "dest", dest_dir);
32 }
33 
34 }  // namespace
35 
CreateTestArcSessionManager(std::unique_ptr<ArcSessionRunner> arc_session_runner)36 std::unique_ptr<ArcSessionManager> CreateTestArcSessionManager(
37     std::unique_ptr<ArcSessionRunner> arc_session_runner) {
38   auto manager = std::make_unique<ArcSessionManager>(
39       std::move(arc_session_runner),
40       std::make_unique<AdbSideloadingAvailabilityDelegateImpl>());
41   // Our unit tests the ArcSessionManager::ExpandPropertyFiles() function won't
42   // be automatically called. Because of that, we can call
43   // OnExpandPropertyFilesForTesting() instead with |true| for easier unit
44   // testing (without calling base::RunLoop().RunUntilIdle() here and there.)
45   manager->OnExpandPropertyFilesAndReadSaltForTesting(true);
46   return manager;
47 }
48 
ExpandPropertyFilesForTesting(ArcSessionManager * arc_session_manager,const base::FilePath & temp_dir)49 bool ExpandPropertyFilesForTesting(ArcSessionManager* arc_session_manager,
50                                    const base::FilePath& temp_dir) {
51   // For browser_tests, do the actual prop file expansion to make it more
52   // similar to production. Calling ExpandPropertyFiles() here is fine as long
53   // as the caller doesn't explicitly call ArcServiceLauncher::Initialize()
54   // after recreating ASM with ArcServiceLauncher::ResetForTesting().
55   base::FilePath source_dir, dest_dir;
56   if (!CreateFilesAndDirectories(temp_dir, &source_dir, &dest_dir))
57     return false;
58   arc_session_manager->set_property_files_source_dir_for_testing(source_dir);
59   arc_session_manager->set_property_files_dest_dir_for_testing(dest_dir);
60   arc_session_manager->ExpandPropertyFilesAndReadSalt();
61   return true;
62 }
63 
64 }  // namespace arc
65