1763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo//
2763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo// Copyright (C) 2015 The Android Open Source Project
3763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo//
4763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo// Licensed under the Apache License, Version 2.0 (the "License");
5763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo// you may not use this file except in compliance with the License.
6763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo// You may obtain a copy of the License at
7763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo//
8763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo//      http://www.apache.org/licenses/LICENSE-2.0
9763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo//
10763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo// Unless required by applicable law or agreed to in writing, software
11763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo// distributed under the License is distributed on an "AS IS" BASIS,
12763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo// See the License for the specific language governing permissions and
14763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo// limitations under the License.
15763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo//
16763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo
17e7ba31b9e8e5c34cdc8d6c84b0549d192023633bAlex Deymo#include "update_engine/boot_control_chromeos.h"
18763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo
19763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo#include <gtest/gtest.h>
20763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo
21763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymonamespace chromeos_update_engine {
22763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo
23763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymoclass BootControlChromeOSTest : public ::testing::Test {
24763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo protected:
25763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  void SetUp() override {
26763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo    // We don't run Init() for bootctl_, we set its internal values instead.
27763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo    bootctl_.num_slots_ = 2;
28763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo    bootctl_.current_slot_ = 0;
29763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo    bootctl_.boot_disk_name_ = "/dev/null";
30763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  }
31763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo
32763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  BootControlChromeOS bootctl_;  // BootControlChromeOS under test.
33763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo};
34763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo
35763e7dbaac735da0ae802933a1015b6b7874bce2Alex DeymoTEST_F(BootControlChromeOSTest, SysfsBlockDeviceTest) {
36763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  EXPECT_EQ("/sys/block/sda", bootctl_.SysfsBlockDevice("/dev/sda"));
37763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  EXPECT_EQ("", bootctl_.SysfsBlockDevice("/foo/sda"));
38763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  EXPECT_EQ("", bootctl_.SysfsBlockDevice("/dev/foo/bar"));
39763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  EXPECT_EQ("", bootctl_.SysfsBlockDevice("/"));
40763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  EXPECT_EQ("", bootctl_.SysfsBlockDevice("./"));
41763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  EXPECT_EQ("", bootctl_.SysfsBlockDevice(""));
42763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo}
43763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo
44763e7dbaac735da0ae802933a1015b6b7874bce2Alex DeymoTEST_F(BootControlChromeOSTest, GetPartitionNumberTest) {
45763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  // The partition name should not be case-sensitive.
46763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  EXPECT_EQ(2, bootctl_.GetPartitionNumber("kernel", 0));
47763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  EXPECT_EQ(2, bootctl_.GetPartitionNumber("boot", 0));
48763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  EXPECT_EQ(2, bootctl_.GetPartitionNumber("KERNEL", 0));
49763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  EXPECT_EQ(2, bootctl_.GetPartitionNumber("BOOT", 0));
50763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo
51763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  EXPECT_EQ(3, bootctl_.GetPartitionNumber("ROOT", 0));
52763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  EXPECT_EQ(3, bootctl_.GetPartitionNumber("system", 0));
53763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo
54763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  EXPECT_EQ(3, bootctl_.GetPartitionNumber("ROOT", 0));
55763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  EXPECT_EQ(3, bootctl_.GetPartitionNumber("system", 0));
56763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo
57763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  // Slot B.
58763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  EXPECT_EQ(4, bootctl_.GetPartitionNumber("KERNEL", 1));
59763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  EXPECT_EQ(5, bootctl_.GetPartitionNumber("ROOT", 1));
60763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo
61763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  // Slot C doesn't exists.
62763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  EXPECT_EQ(-1, bootctl_.GetPartitionNumber("KERNEL", 2));
63763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  EXPECT_EQ(-1, bootctl_.GetPartitionNumber("ROOT", 2));
64763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo
65763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  // Non A/B partitions are ignored.
66763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  EXPECT_EQ(-1, bootctl_.GetPartitionNumber("OEM", 0));
67763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo  EXPECT_EQ(-1, bootctl_.GetPartitionNumber("A little panda", 0));
68763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo}
69763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo
70763e7dbaac735da0ae802933a1015b6b7874bce2Alex Deymo}  // namespace chromeos_update_engine
71