regulators: Implement i2c_compare_byte action

Implement the i2c_compare_byte action in the JSON config file.  See
i2c_compare_byte.md for more information about this action.

Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I258027f969684022f6fd5d85ab4dc32b4746a69c
diff --git a/phosphor-regulators/src/actions/i2c_action.hpp b/phosphor-regulators/src/actions/i2c_action.hpp
new file mode 100644
index 0000000..42b99e0
--- /dev/null
+++ b/phosphor-regulators/src/actions/i2c_action.hpp
@@ -0,0 +1,73 @@
+/**
+ * Copyright © 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include "action.hpp"
+#include "action_environment.hpp"
+#include "device.hpp"
+#include "i2c_interface.hpp"
+
+namespace phosphor::power::regulators
+{
+
+/**
+ * @class I2CAction
+ *
+ * Abstract base class for actions that communicate with a device using an I2C
+ * interface.
+ */
+class I2CAction : public Action
+{
+  public:
+    // Specify which compiler-generated methods we want
+    I2CAction() = default;
+    I2CAction(const I2CAction&) = delete;
+    I2CAction(I2CAction&&) = delete;
+    I2CAction& operator=(const I2CAction&) = delete;
+    I2CAction& operator=(I2CAction&&) = delete;
+    virtual ~I2CAction() = default;
+
+  protected:
+    /**
+     * Returns the I2C interface to the current device within the specified
+     * action environment.
+     *
+     * Opens the interface if it was not already open.
+     *
+     * Throws an exception if an error occurs.
+     *
+     * @param environment action execution environment
+     * @return I2C interface to current device
+     */
+    i2c::I2CInterface& getI2CInterface(ActionEnvironment& environment)
+    {
+        // Get current device from action environment
+        Device& device = environment.getDevice();
+
+        // Get I2C interface from device
+        i2c::I2CInterface& interface = device.getI2CInterface();
+
+        // Open interface if necessary
+        if (!interface.isOpen())
+        {
+            interface.open();
+        }
+
+        return interface;
+    }
+};
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/src/actions/i2c_compare_byte_action.cpp b/phosphor-regulators/src/actions/i2c_compare_byte_action.cpp
new file mode 100644
index 0000000..8436e56
--- /dev/null
+++ b/phosphor-regulators/src/actions/i2c_compare_byte_action.cpp
@@ -0,0 +1,64 @@
+/**
+ * Copyright © 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "i2c_compare_byte_action.hpp"
+
+#include "action_error.hpp"
+#include "i2c_interface.hpp"
+
+#include <exception>
+#include <ios>
+#include <sstream>
+
+namespace phosphor::power::regulators
+{
+
+bool I2CCompareByteAction::execute(ActionEnvironment& environment)
+{
+    bool isEqual{false};
+    try
+    {
+        // Read actual value of device register
+        uint8_t actualValue{0x00};
+        i2c::I2CInterface& interface = getI2CInterface(environment);
+        interface.read(reg, actualValue);
+
+        // Modify actual value to only include bits specified in the mask
+        actualValue &= mask;
+
+        // Check if actual value equals expected value
+        isEqual = (actualValue == value);
+    }
+    catch (const i2c::I2CException& e)
+    {
+        // Nest I2CException within an ActionError so caller will have both the
+        // low level I2C error information and the action information
+        std::throw_with_nested(ActionError(*this));
+    }
+    return isEqual;
+}
+
+std::string I2CCompareByteAction::toString() const
+{
+    std::ostringstream ss;
+    ss << "i2c_compare_byte: { register: 0x" << std::hex << std::uppercase
+       << static_cast<uint16_t>(reg) << ", value: 0x"
+       << static_cast<uint16_t>(value) << ", mask: 0x"
+       << static_cast<uint16_t>(mask) << " }";
+    return ss.str();
+}
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/src/actions/i2c_compare_byte_action.hpp b/phosphor-regulators/src/actions/i2c_compare_byte_action.hpp
new file mode 100644
index 0000000..a32bbdf
--- /dev/null
+++ b/phosphor-regulators/src/actions/i2c_compare_byte_action.hpp
@@ -0,0 +1,141 @@
+/**
+ * Copyright © 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+#include "action_environment.hpp"
+#include "i2c_action.hpp"
+
+#include <cstdint>
+#include <string>
+
+namespace phosphor::power::regulators
+{
+
+/**
+ * @class I2CCompareByteAction
+ *
+ * Compares a device register to a byte value.  Communicates with the device
+ * directly using the I2C interface.
+ *
+ * Implements the i2c_compare_byte action in the JSON config file.
+ */
+class I2CCompareByteAction : public I2CAction
+{
+  public:
+    // Specify which compiler-generated methods we want
+    I2CCompareByteAction() = delete;
+    I2CCompareByteAction(const I2CCompareByteAction&) = delete;
+    I2CCompareByteAction(I2CCompareByteAction&&) = delete;
+    I2CCompareByteAction& operator=(const I2CCompareByteAction&) = delete;
+    I2CCompareByteAction& operator=(I2CCompareByteAction&&) = delete;
+    virtual ~I2CCompareByteAction() = default;
+
+    /**
+     * Constructor.
+     *
+     * @param reg Device register address.  Note: named 'reg' because 'register'
+     *            is a reserved keyword.
+     * @param value Expected byte value.
+     * @param mask Bit mask.  Specifies which bits should be compared within the
+     *             byte value.  Only the bits with a value of 1 in the mask will
+     *             be compared.  If not specified, defaults to 0xFF which means
+     *             that all bits will be compared.
+     */
+    explicit I2CCompareByteAction(uint8_t reg, uint8_t value,
+                                  uint8_t mask = 0xFF) :
+        reg{reg},
+        value{value}, mask{mask}
+    {
+    }
+
+    /**
+     * Executes this action.
+     *
+     * Compares a device register to a byte value using the I2C interface.
+     *
+     * The device register, byte value, and mask (if any) were specified in the
+     * constructor.
+     *
+     * The device is obtained from the specified action environment.
+     *
+     * Throws an exception if an error occurs.
+     *
+     * @param environment action execution environment
+     * @return true if the register contained the expected value, otherwise
+     *         returns false.
+     */
+    virtual bool execute(ActionEnvironment& environment) override;
+
+    /**
+     * Returns the device register address.
+     *
+     * @return register address
+     */
+    uint8_t getRegister() const
+    {
+        return reg;
+    }
+
+    /**
+     * Returns the expected byte value.
+     *
+     * @return expected value
+     */
+    uint8_t getValue() const
+    {
+        return value;
+    }
+
+    /**
+     * Returns the bit mask.
+     *
+     * Specifies which bits should be compared within the byte value.  Only the
+     * bits with a value of 1 in the mask will be compared.
+     *
+     * @return bit mask
+     */
+    uint8_t getMask() const
+    {
+        return mask;
+    }
+
+    /**
+     * Returns a string description of this action.
+     *
+     * @return description of action
+     */
+    virtual std::string toString() const override;
+
+  private:
+    /**
+     * Device register address.  Note: named 'reg' because 'register' is a
+     * reserved keyword.
+     */
+    const uint8_t reg{0x00};
+
+    /**
+     * Expected byte value.
+     */
+    const uint8_t value{0x00};
+
+    /**
+     * Bit mask.  Specifies which bits should be compared within the byte value.
+     * Only the bits with a value of 1 in the mask will be compared.
+     */
+    const uint8_t mask{0xFF};
+};
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/src/meson.build b/phosphor-regulators/src/meson.build
index 121bf45..a70a86a 100644
--- a/phosphor-regulators/src/meson.build
+++ b/phosphor-regulators/src/meson.build
@@ -6,7 +6,8 @@
 phosphor_regulators_library_source_files = [
     'id_map.cpp',
 
-    'actions/if_action.cpp'
+    'actions/if_action.cpp',
+    'actions/i2c_compare_byte_action.cpp'
 ]
 
 phosphor_regulators_library = static_library(
diff --git a/phosphor-regulators/test/actions/i2c_action_tests.cpp b/phosphor-regulators/test/actions/i2c_action_tests.cpp
new file mode 100644
index 0000000..70e55d7
--- /dev/null
+++ b/phosphor-regulators/test/actions/i2c_action_tests.cpp
@@ -0,0 +1,163 @@
+/**
+ * Copyright © 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "action_environment.hpp"
+#include "device.hpp"
+#include "i2c_action.hpp"
+#include "i2c_interface.hpp"
+#include "id_map.hpp"
+#include "mocked_i2c_interface.hpp"
+
+#include <memory>
+#include <stdexcept>
+#include <string>
+#include <utility>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+using namespace phosphor::power::regulators;
+
+using ::testing::Return;
+using ::testing::Throw;
+
+/**
+ * Define a test implementation of the I2CAction abstract base class.
+ */
+class I2CActionImpl : public I2CAction
+{
+  public:
+    virtual bool execute(ActionEnvironment& /* environment */) override
+    {
+        return true;
+    }
+
+    virtual std::string toString() const override
+    {
+        return "i2c_action_impl: {}";
+    }
+
+    // Make test a friend so it can access protected getI2CInterface() method
+    FRIEND_TEST(I2CActionTests, GetI2CInterface);
+};
+
+TEST(I2CActionTests, GetI2CInterface)
+{
+    // Test where works: device was not open
+    try
+    {
+        // Create mock I2CInterface
+        std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
+            std::make_unique<i2c::MockedI2CInterface>();
+        EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(false));
+        EXPECT_CALL(*i2cInterface, open).Times(1);
+
+        // Create Device, IDMap, ActionEnvironment, and I2CAction
+        Device device{"reg1", true, "/system/chassis/motherboard/reg1",
+                      std::move(i2cInterface)};
+        IDMap idMap{};
+        idMap.addDevice(device);
+        ActionEnvironment env{idMap, "reg1"};
+        I2CActionImpl action{};
+
+        // Get I2CInterface.  Should succeed without an exception.
+        action.getI2CInterface(env);
+    }
+    catch (...)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where works: device was already open
+    try
+    {
+        // Create mock I2CInterface
+        std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
+            std::make_unique<i2c::MockedI2CInterface>();
+        EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
+        EXPECT_CALL(*i2cInterface, open).Times(0);
+
+        // Create Device, IDMap, ActionEnvironment, and I2CAction
+        Device device{"reg1", true, "/system/chassis/motherboard/reg1",
+                      std::move(i2cInterface)};
+        IDMap idMap{};
+        idMap.addDevice(device);
+        ActionEnvironment env{idMap, "reg1"};
+        I2CActionImpl action{};
+
+        // Get I2CInterface.  Should succeed without an exception.
+        action.getI2CInterface(env);
+    }
+    catch (...)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where fails: getting current device fails
+    try
+    {
+        // Create IDMap, ActionEnvironment, and I2CAction
+        IDMap idMap{};
+        ActionEnvironment env{idMap, "reg1"};
+        I2CActionImpl action{};
+
+        // Get I2CInterface.  Should throw an exception since "reg1" is not a
+        // valid device in the IDMap.
+        action.getI2CInterface(env);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& e)
+    {
+        EXPECT_STREQ(e.what(), "Unable to find device with ID \"reg1\"");
+    }
+    catch (...)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where fails: opening interface fails
+    try
+    {
+        // Create mock I2CInterface
+        std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
+            std::make_unique<i2c::MockedI2CInterface>();
+        EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(false));
+        EXPECT_CALL(*i2cInterface, open)
+            .Times(1)
+            .WillOnce(
+                Throw(i2c::I2CException{"Failed to open", "/dev/i2c-1", 0x70}));
+
+        // Create Device, IDMap, ActionEnvironment, and I2CAction
+        Device device{"reg1", true, "/system/chassis/motherboard/reg1",
+                      std::move(i2cInterface)};
+        IDMap idMap{};
+        idMap.addDevice(device);
+        ActionEnvironment env{idMap, "reg1"};
+        I2CActionImpl action{};
+
+        // Get I2CInterface.  Should throw an exception from the open() call.
+        action.getI2CInterface(env);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const i2c::I2CException& e)
+    {
+        EXPECT_STREQ(e.what(),
+                     "I2CException: Failed to open: bus /dev/i2c-1, addr 0x70");
+    }
+    catch (...)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+}
diff --git a/phosphor-regulators/test/actions/i2c_compare_byte_action_tests.cpp b/phosphor-regulators/test/actions/i2c_compare_byte_action_tests.cpp
new file mode 100644
index 0000000..b721898
--- /dev/null
+++ b/phosphor-regulators/test/actions/i2c_compare_byte_action_tests.cpp
@@ -0,0 +1,273 @@
+/**
+ * Copyright © 2020 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "action_environment.hpp"
+#include "action_error.hpp"
+#include "device.hpp"
+#include "i2c_compare_byte_action.hpp"
+#include "i2c_interface.hpp"
+#include "id_map.hpp"
+#include "mocked_i2c_interface.hpp"
+
+#include <cstdint>
+#include <memory>
+#include <stdexcept>
+#include <string>
+#include <utility>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+using namespace phosphor::power::regulators;
+
+using ::testing::A;
+using ::testing::Return;
+using ::testing::SetArgReferee;
+using ::testing::Throw;
+
+TEST(I2CCompareByteActionTests, Constructor)
+{
+    // Test where mask is not specified
+    {
+        I2CCompareByteAction action{0x7C, 0xDE};
+        EXPECT_EQ(action.getRegister(), 0x7C);
+        EXPECT_EQ(action.getValue(), 0xDE);
+        EXPECT_EQ(action.getMask(), 0xFF);
+    }
+
+    // Test where mask is specified
+    {
+        I2CCompareByteAction action{0xA0, 0x03, 0x47};
+        EXPECT_EQ(action.getRegister(), 0xA0);
+        EXPECT_EQ(action.getValue(), 0x03);
+        EXPECT_EQ(action.getMask(), 0x47);
+    }
+}
+
+TEST(I2CCompareByteActionTests, Execute)
+{
+    // Test where works: Equal: Mask specified
+    try
+    {
+        // Create mock I2CInterface: read() returns value 0xD7
+        std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
+            std::make_unique<i2c::MockedI2CInterface>();
+        EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
+        EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>()))
+            .Times(1)
+            .WillOnce(SetArgReferee<1>(0xD7));
+
+        // Create Device, IDMap, and ActionEnvironment
+        Device device{"reg1", true, "/system/chassis/motherboard/reg1",
+                      std::move(i2cInterface)};
+        IDMap idMap{};
+        idMap.addDevice(device);
+        ActionEnvironment env{idMap, "reg1"};
+
+        // Actual value: 0xD7 = 1101 0111
+        // Mask        : 0x7E = 0111 1110
+        // Result      : 0x56 = 0101 0110
+        I2CCompareByteAction action{0xA0, 0x56, 0x7E};
+        EXPECT_EQ(action.execute(env), true);
+    }
+    catch (...)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where works: Equal: Mask not specified
+    try
+    {
+        // Create mock I2CInterface: read() returns value 0xD7
+        std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
+            std::make_unique<i2c::MockedI2CInterface>();
+        EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
+        EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>()))
+            .Times(1)
+            .WillOnce(SetArgReferee<1>(0xD7));
+
+        // Create Device, IDMap, and ActionEnvironment
+        Device device{"reg1", true, "/system/chassis/motherboard/reg1",
+                      std::move(i2cInterface)};
+        IDMap idMap{};
+        idMap.addDevice(device);
+        ActionEnvironment env{idMap, "reg1"};
+
+        I2CCompareByteAction action{0xA0, 0xD7};
+        EXPECT_EQ(action.execute(env), true);
+    }
+    catch (...)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where works: Not equal: Mask specified
+    try
+    {
+        // Create mock I2CInterface: read() returns value 0xD7
+        std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
+            std::make_unique<i2c::MockedI2CInterface>();
+        EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
+        EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>()))
+            .Times(1)
+            .WillOnce(SetArgReferee<1>(0xD7));
+
+        // Create Device, IDMap, and ActionEnvironment
+        Device device{"reg1", true, "/system/chassis/motherboard/reg1",
+                      std::move(i2cInterface)};
+        IDMap idMap{};
+        idMap.addDevice(device);
+        ActionEnvironment env{idMap, "reg1"};
+
+        // Actual value: 0xD7 = 1101 0111
+        // Mask        : 0x7E = 0111 1110
+        // Result      : 0x56 = 0101 0110
+        I2CCompareByteAction action{0xA0, 0x57, 0x7E};
+        EXPECT_EQ(action.execute(env), false);
+    }
+    catch (...)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where works: Not equal: Mask not specified
+    try
+    {
+        // Create mock I2CInterface: read() returns value 0xD7
+        std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
+            std::make_unique<i2c::MockedI2CInterface>();
+        EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
+        EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>()))
+            .Times(1)
+            .WillOnce(SetArgReferee<1>(0xD7));
+
+        // Create Device, IDMap, and ActionEnvironment
+        Device device{"reg1", true, "/system/chassis/motherboard/reg1",
+                      std::move(i2cInterface)};
+        IDMap idMap{};
+        idMap.addDevice(device);
+        ActionEnvironment env{idMap, "reg1"};
+
+        I2CCompareByteAction action{0xA0, 0xD6};
+        EXPECT_EQ(action.execute(env), false);
+    }
+    catch (...)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where fails: Getting I2CInterface fails
+    try
+    {
+        // Create IDMap and ActionEnvironment
+        IDMap idMap{};
+        ActionEnvironment env{idMap, "reg1"};
+
+        I2CCompareByteAction action{0xA0, 0xD6};
+        action.execute(env);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& e)
+    {
+        EXPECT_STREQ(e.what(), "Unable to find device with ID \"reg1\"");
+    }
+    catch (...)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where fails: Reading byte fails
+    try
+    {
+        // Create mock I2CInterface: read() throws an I2CException
+        std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
+            std::make_unique<i2c::MockedI2CInterface>();
+        EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
+        EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>()))
+            .Times(1)
+            .WillOnce(Throw(
+                i2c::I2CException{"Failed to read byte", "/dev/i2c-1", 0x70}));
+
+        // Create Device, IDMap, and ActionEnvironment
+        Device device{"reg1", true, "/system/chassis/motherboard/reg1",
+                      std::move(i2cInterface)};
+        IDMap idMap{};
+        idMap.addDevice(device);
+        ActionEnvironment env{idMap, "reg1"};
+
+        I2CCompareByteAction action{0xA0, 0xD6};
+        action.execute(env);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const ActionError& e)
+    {
+        EXPECT_STREQ(e.what(), "ActionError: i2c_compare_byte: { register: "
+                               "0xA0, value: 0xD6, mask: 0xFF }");
+        try
+        {
+            // Re-throw inner I2CException
+            std::rethrow_if_nested(e);
+            ADD_FAILURE() << "Should not have reached this line.";
+        }
+        catch (const i2c::I2CException& ie)
+        {
+            EXPECT_STREQ(
+                ie.what(),
+                "I2CException: Failed to read byte: bus /dev/i2c-1, addr 0x70");
+        }
+        catch (...)
+        {
+            ADD_FAILURE() << "Should not have caught exception.";
+        }
+    }
+    catch (...)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+}
+
+TEST(I2CCompareByteActionTests, GetRegister)
+{
+    I2CCompareByteAction action{0x7C, 0xDE};
+    EXPECT_EQ(action.getRegister(), 0x7C);
+}
+
+TEST(I2CCompareByteActionTests, GetValue)
+{
+    I2CCompareByteAction action{0xA0, 0x03, 0x47};
+    EXPECT_EQ(action.getValue(), 0x03);
+}
+
+TEST(I2CCompareByteActionTests, GetMask)
+{
+    // Test where mask is not specified
+    {
+        I2CCompareByteAction action{0x7C, 0xDE};
+        EXPECT_EQ(action.getMask(), 0xFF);
+    }
+
+    // Test where mask is specified
+    {
+        I2CCompareByteAction action{0xA0, 0x03, 0x47};
+        EXPECT_EQ(action.getMask(), 0x47);
+    }
+}
+
+TEST(I2CCompareByteActionTests, ToString)
+{
+    I2CCompareByteAction action{0x7C, 0xDE, 0xFE};
+    EXPECT_EQ(action.toString(),
+              "i2c_compare_byte: { register: 0x7C, value: 0xDE, mask: 0xFE }");
+}
diff --git a/phosphor-regulators/test/meson.build b/phosphor-regulators/test/meson.build
index 2a41cd1..d5e3d5d 100644
--- a/phosphor-regulators/test/meson.build
+++ b/phosphor-regulators/test/meson.build
@@ -13,6 +13,8 @@
     'actions/action_error_tests.cpp',
     'actions/action_utils_tests.cpp',
     'actions/and_action_tests.cpp',
+    'actions/i2c_action_tests.cpp',
+    'actions/i2c_compare_byte_action_tests.cpp',
     'actions/if_action_tests.cpp',
     'actions/not_action_tests.cpp',
     'actions/or_action_tests.cpp',