blob: 3366c0d5369e1913909d4c4bcced10ae2f62658a [file] [log] [blame]
Gwendal Grignouee8efd12023-06-21 00:05:091// Copyright 2023 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Allen Shih87ce1a12025-04-17 05:22:345#include "libsegmentation/feature_management_util.h"
Gwendal Grignouee8efd12023-06-21 00:05:096
Gwendal Grignou98c9e712023-06-21 22:48:007#include <base/files/scoped_temp_dir.h>
Allen Shih87ce1a12025-04-17 05:22:348#include <brillo/file_utils.h>
Gwendal Grignouee8efd12023-06-21 00:05:099#include <gmock/gmock.h>
10#include <gtest/gtest.h>
11
Gwendal Grignouee8efd12023-06-21 00:05:0912namespace segmentation {
13
14using ::testing::Return;
15
16// Test fixture for testing feature management.
17class FeatureManagementUtilTest : public ::testing::Test {
18 public:
19 FeatureManagementUtilTest() = default;
20 ~FeatureManagementUtilTest() override = default;
21};
22
Gwendal Grignou98c9e712023-06-21 22:48:0023class FeatureManagementUtilRootDevTest : public ::testing::Test {
24 public:
25 FeatureManagementUtilRootDevTest() = default;
26 ~FeatureManagementUtilRootDevTest() override = default;
27
28 void SetUp() override {
29 EXPECT_TRUE(_root.CreateUniqueTempDir());
30 base::FilePath vars =
31 _root.GetPath().Append("usr/sbin/partition_vars.json");
32
33 std::string vars_content = R"""(
34{ "load_base_vars": {
35 "DEFAULT_ROOTDEV": "
36 /sys/devices/pci0000:00/0000:00:17.0/ata*/host*/target*/*/block/sd*
37 /sys/devices/pci0000:00/0000:00:1c.*/0000:*:00.0/nvme/nvme*/nvme*n1
38 /sys/devices/pci0000:00/0000:00:1a.0/mmc_host/mmc*/mmc*:000*/block/mmcblk*
39 /sys/devices/pci0000:00/0000:00:1d.*/0000:*:00.0/nvme/nvme*/nvme*n1
40 /sys/devices/pci0000:00/0000:00:06.*/0000:*:00.0/nvme/nvme*/nvme*n1
41 /sys/devices/pci0000:00/0000:00:12.7/host*/target*/*/block/sd*"
42 }
43})""";
44 ASSERT_TRUE(brillo::WriteStringToFile(vars, vars_content));
45 }
46
47 protected:
48 base::ScopedTempDir _root;
49};
50
51TEST_F(FeatureManagementUtilRootDevTest, NoDefaultRoot) {
52 // No path defined.
53 EXPECT_FALSE(FeatureManagementUtil::GetDefaultRoot(_root.GetPath()));
54}
55
56TEST_F(FeatureManagementUtilRootDevTest, OneDefaultRoot) {
57 // One good path defined.
58 EXPECT_TRUE(brillo::TouchFile(
59 _root.GetPath().Append("sys/devices/pci0000:00/0000:00:06.1/0000:03:00.0/"
60 "nvme/nvme0/nvme0n1/size")));
61 EXPECT_TRUE(FeatureManagementUtil::GetDefaultRoot(_root.GetPath()));
62}
63
64TEST_F(FeatureManagementUtilRootDevTest, OneWrongRoot) {
65 // One path defined, but does not match any globs.
66 EXPECT_TRUE(brillo::TouchFile(
67 _root.GetPath().Append("sys/devices/pci0000:00/0000:00:06.1/0000:03:00.0/"
68 "nvme/nvme0/nvme0n2/size")));
69 EXPECT_FALSE(FeatureManagementUtil::GetDefaultRoot(_root.GetPath()));
70}
71
Gwendal Grignouee8efd12023-06-21 00:05:0972} // namespace segmentation