1 //
2 // Copyright (C) 2015 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 #include "update_engine/cros/boot_control_chromeos.h"
18
19 #include <gtest/gtest.h>
20
21 using std::string;
22
23 namespace chromeos_update_engine {
24
25 class BootControlChromeOSTest : public ::testing::Test {
26 protected:
SetUp()27 void SetUp() override {
28 // We don't run Init() for bootctl_, we set its internal values instead.
29 bootctl_.num_slots_ = 2;
30 bootctl_.current_slot_ = 0;
31 bootctl_.boot_disk_name_ = "/dev/null";
32 }
33
34 BootControlChromeOS bootctl_; // BootControlChromeOS under test.
35 };
36
TEST_F(BootControlChromeOSTest,SysfsBlockDeviceTest)37 TEST_F(BootControlChromeOSTest, SysfsBlockDeviceTest) {
38 EXPECT_EQ("/sys/block/sda", bootctl_.SysfsBlockDevice("/dev/sda"));
39 EXPECT_EQ("", bootctl_.SysfsBlockDevice("/foo/sda"));
40 EXPECT_EQ("", bootctl_.SysfsBlockDevice("/dev/foo/bar"));
41 EXPECT_EQ("", bootctl_.SysfsBlockDevice("/"));
42 EXPECT_EQ("", bootctl_.SysfsBlockDevice("./"));
43 EXPECT_EQ("", bootctl_.SysfsBlockDevice(""));
44 }
45
TEST_F(BootControlChromeOSTest,GetPartitionNumberTest)46 TEST_F(BootControlChromeOSTest, GetPartitionNumberTest) {
47 // The partition name should not be case-sensitive.
48 EXPECT_EQ(2, bootctl_.GetPartitionNumber("kernel", 0));
49 EXPECT_EQ(2, bootctl_.GetPartitionNumber("boot", 0));
50 EXPECT_EQ(2, bootctl_.GetPartitionNumber("KERNEL", 0));
51 EXPECT_EQ(2, bootctl_.GetPartitionNumber("BOOT", 0));
52
53 EXPECT_EQ(3, bootctl_.GetPartitionNumber("ROOT", 0));
54 EXPECT_EQ(3, bootctl_.GetPartitionNumber("system", 0));
55
56 EXPECT_EQ(3, bootctl_.GetPartitionNumber("ROOT", 0));
57 EXPECT_EQ(3, bootctl_.GetPartitionNumber("system", 0));
58
59 // Slot B.
60 EXPECT_EQ(4, bootctl_.GetPartitionNumber("KERNEL", 1));
61 EXPECT_EQ(5, bootctl_.GetPartitionNumber("ROOT", 1));
62
63 // Slot C doesn't exists.
64 EXPECT_EQ(-1, bootctl_.GetPartitionNumber("KERNEL", 2));
65 EXPECT_EQ(-1, bootctl_.GetPartitionNumber("ROOT", 2));
66
67 // Non A/B partitions are ignored.
68 EXPECT_EQ(-1, bootctl_.GetPartitionNumber("OEM", 0));
69 EXPECT_EQ(-1, bootctl_.GetPartitionNumber("A little panda", 0));
70 }
71
TEST_F(BootControlChromeOSTest,ParseDlcPartitionNameTest)72 TEST_F(BootControlChromeOSTest, ParseDlcPartitionNameTest) {
73 string id, package;
74
75 EXPECT_TRUE(bootctl_.ParseDlcPartitionName("dlc/id/package", &id, &package));
76 EXPECT_EQ(id, "id");
77 EXPECT_EQ(package, "package");
78
79 EXPECT_FALSE(
80 bootctl_.ParseDlcPartitionName("dlc-foo/id/package", &id, &package));
81 EXPECT_FALSE(
82 bootctl_.ParseDlcPartitionName("dlc-foo/id/package/", &id, &package));
83 EXPECT_FALSE(bootctl_.ParseDlcPartitionName("dlc/id", &id, &package));
84 EXPECT_FALSE(bootctl_.ParseDlcPartitionName("dlc/id/", &id, &package));
85 EXPECT_FALSE(bootctl_.ParseDlcPartitionName("dlc//package", &id, &package));
86 EXPECT_FALSE(bootctl_.ParseDlcPartitionName("dlc", &id, &package));
87 EXPECT_FALSE(bootctl_.ParseDlcPartitionName("foo", &id, &package));
88 }
89
90 } // namespace chromeos_update_engine
91