Gwendal Grignou | ee8efd1 | 2023-06-21 00:05:09 | [diff] [blame] | 1 | // 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 Shih | 87ce1a1 | 2025-04-17 05:22:34 | [diff] [blame] | 5 | #include "libsegmentation/feature_management_util.h" |
Gwendal Grignou | ee8efd1 | 2023-06-21 00:05:09 | [diff] [blame] | 6 | |
Gwendal Grignou | 98c9e71 | 2023-06-21 22:48:00 | [diff] [blame] | 7 | #include <base/files/scoped_temp_dir.h> |
Allen Shih | 87ce1a1 | 2025-04-17 05:22:34 | [diff] [blame] | 8 | #include <brillo/file_utils.h> |
Gwendal Grignou | ee8efd1 | 2023-06-21 00:05:09 | [diff] [blame] | 9 | #include <gmock/gmock.h> |
| 10 | #include <gtest/gtest.h> |
| 11 | |
Gwendal Grignou | ee8efd1 | 2023-06-21 00:05:09 | [diff] [blame] | 12 | namespace segmentation { |
| 13 | |
| 14 | using ::testing::Return; |
| 15 | |
| 16 | // Test fixture for testing feature management. |
| 17 | class FeatureManagementUtilTest : public ::testing::Test { |
| 18 | public: |
| 19 | FeatureManagementUtilTest() = default; |
| 20 | ~FeatureManagementUtilTest() override = default; |
| 21 | }; |
| 22 | |
Gwendal Grignou | 98c9e71 | 2023-06-21 22:48:00 | [diff] [blame] | 23 | class 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 | |
| 51 | TEST_F(FeatureManagementUtilRootDevTest, NoDefaultRoot) { |
| 52 | // No path defined. |
| 53 | EXPECT_FALSE(FeatureManagementUtil::GetDefaultRoot(_root.GetPath())); |
| 54 | } |
| 55 | |
| 56 | TEST_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 | |
| 64 | TEST_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 Grignou | ee8efd1 | 2023-06-21 00:05:09 | [diff] [blame] | 72 | } // namespace segmentation |