1 /*
2 * Copyright (C) 2024 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 use std::io::Read;
18 use std::path::{Path, PathBuf};
19 use tempfile::{tempdir, TempDir};
20
21 /// Container mockup
22 pub(crate) struct ContainerMock {
23 pub tmp_dir: TempDir,
24 pub name: String,
25 pub package_map: PathBuf,
26 pub flag_map: PathBuf,
27 pub flag_val: PathBuf,
28 pub flag_info: PathBuf,
29 }
30
31 /// Implementation for container mockup
32 impl ContainerMock {
new() -> Self33 pub(crate) fn new() -> Self {
34 let tmp_dir = tempdir().unwrap();
35 let package_map = tmp_dir.path().join("package.map");
36 let flag_map = tmp_dir.path().join("flag.map");
37 let flag_val = tmp_dir.path().join("flag.val");
38 let flag_info = tmp_dir.path().join("flag.info");
39 std::fs::copy("./tests/data/package.map", &package_map).unwrap();
40 std::fs::copy("./tests/data/flag.map", &flag_map).unwrap();
41 std::fs::copy("./tests/data/flag.val", &flag_val).unwrap();
42 std::fs::copy("./tests/data/flag.info", &flag_info).unwrap();
43 Self { tmp_dir, name: String::from("mockup"), package_map, flag_map, flag_val, flag_info }
44 }
45 }
46
47 /// Implement drop trait for ContainerMock
48 impl Drop for ContainerMock {
drop(&mut self)49 fn drop(&mut self) {
50 std::fs::remove_dir_all(&self.tmp_dir).unwrap();
51 }
52 }
53
54 /// Storage root dir mockup
55 pub(crate) struct StorageRootDirMock {
56 pub tmp_dir: TempDir,
57 pub flags_dir: PathBuf,
58 pub maps_dir: PathBuf,
59 pub boot_dir: PathBuf,
60 }
61
62 /// Implementation for storage root dir mockup
63 impl StorageRootDirMock {
new() -> Self64 pub(crate) fn new() -> Self {
65 let tmp_dir = tempdir().unwrap();
66 let flags_dir = tmp_dir.path().join("flags");
67 let maps_dir = tmp_dir.path().join("maps");
68 let boot_dir = tmp_dir.path().join("boot");
69 std::fs::create_dir(&flags_dir).unwrap();
70 std::fs::create_dir(&maps_dir).unwrap();
71 std::fs::create_dir(&boot_dir).unwrap();
72 Self { tmp_dir, flags_dir, maps_dir, boot_dir }
73 }
74 }
75
76 /// Implement drop trait for StorageRootDirMock
77 impl Drop for StorageRootDirMock {
drop(&mut self)78 fn drop(&mut self) {
79 std::fs::remove_dir_all(&self.tmp_dir).unwrap();
80 }
81 }
82
83 /// Check if has the same content
has_same_content(file_one: &Path, file_two: &Path) -> bool84 pub(crate) fn has_same_content(file_one: &Path, file_two: &Path) -> bool {
85 assert!(file_one.exists());
86 assert!(file_two.exists());
87
88 let mut f1 = std::fs::File::open(file_one).unwrap();
89 let mut b1 = Vec::new();
90 f1.read_to_end(&mut b1).unwrap();
91
92 let mut f2 = std::fs::File::open(file_two).unwrap();
93 let mut b2 = Vec::new();
94 f2.read_to_end(&mut b2).unwrap();
95
96 b1 == b2
97 }
98