regulators: Implement i2c_write_bytes action

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

Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: Ifd4ef481f0a6b74bd3e5689a7b9f3a989ed5e214
diff --git a/phosphor-regulators/src/actions/i2c_write_bytes_action.cpp b/phosphor-regulators/src/actions/i2c_write_bytes_action.cpp
new file mode 100644
index 0000000..21b31f1
--- /dev/null
+++ b/phosphor-regulators/src/actions/i2c_write_bytes_action.cpp
@@ -0,0 +1,90 @@
+/**
+ * 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_write_bytes_action.hpp"
+
+#include "action_error.hpp"
+#include "i2c_interface.hpp"
+
+#include <exception>
+#include <ios>
+#include <sstream>
+
+namespace phosphor::power::regulators
+{
+
+bool I2CWriteBytesAction::execute(ActionEnvironment& environment)
+{
+    try
+    {
+        i2c::I2CInterface& interface = getI2CInterface(environment);
+        uint8_t valuesToWrite[UINT8_MAX];
+        if (masks.size() == 0)
+        {
+            for (unsigned int i = 0; i < values.size(); ++i)
+            {
+                valuesToWrite[i] = values[i];
+            }
+        }
+        else
+        {
+            // Read current device register values.  Use I2C mode where the
+            // number of bytes to read is explicitly specified.
+            uint8_t size = values.size();
+            uint8_t currentValues[UINT8_MAX];
+            interface.read(reg, size, currentValues,
+                           i2c::I2CInterface::Mode::I2C);
+
+            // Combine values to write with current values
+            for (unsigned int i = 0; i < values.size(); ++i)
+            {
+                valuesToWrite[i] =
+                    (values[i] & masks[i]) | (currentValues[i] & (~masks[i]));
+            }
+        }
+
+        // Write values to device register
+        interface.write(reg, values.size(), valuesToWrite,
+                        i2c::I2CInterface::Mode::I2C);
+    }
+    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 true;
+}
+
+std::string I2CWriteBytesAction::toString() const
+{
+    std::ostringstream ss;
+    ss << "i2c_write_bytes: { register: 0x" << std::hex << std::uppercase
+       << static_cast<uint16_t>(reg) << ", values: [ ";
+    for (unsigned int i = 0; i < values.size(); ++i)
+    {
+        ss << ((i > 0) ? ", " : "") << "0x" << static_cast<uint16_t>(values[i]);
+    }
+    ss << " ], masks: [ ";
+    for (unsigned int i = 0; i < masks.size(); ++i)
+    {
+        ss << ((i > 0) ? ", " : "") << "0x" << static_cast<uint16_t>(masks[i]);
+    }
+    ss << " ] }";
+    return ss.str();
+}
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/src/actions/i2c_write_bytes_action.hpp b/phosphor-regulators/src/actions/i2c_write_bytes_action.hpp
new file mode 100644
index 0000000..edf2b2e
--- /dev/null
+++ b/phosphor-regulators/src/actions/i2c_write_bytes_action.hpp
@@ -0,0 +1,184 @@
+/**
+ * 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 <stdexcept>
+#include <string>
+#include <vector>
+
+namespace phosphor::power::regulators
+{
+
+/**
+ * @class I2CWriteBytesAction
+ *
+ * Writes bytes to a device register.  Communicates with the device directly
+ * using the I2C interface.
+ *
+ * Implements the i2c_write_bytes action in the JSON config file.
+ */
+class I2CWriteBytesAction : public I2CAction
+{
+  public:
+    // Specify which compiler-generated methods we want
+    I2CWriteBytesAction() = delete;
+    I2CWriteBytesAction(const I2CWriteBytesAction&) = delete;
+    I2CWriteBytesAction(I2CWriteBytesAction&&) = delete;
+    I2CWriteBytesAction& operator=(const I2CWriteBytesAction&) = delete;
+    I2CWriteBytesAction& operator=(I2CWriteBytesAction&&) = delete;
+    virtual ~I2CWriteBytesAction() = default;
+
+    /**
+     * Constructor.
+     *
+     * Throws an exception if any of the input parameters are invalid.
+     *
+     * @param reg Device register address.  Note: named 'reg' because 'register'
+     *            is a reserved keyword.
+     * @param values One or more byte values to write.  The bytes must be
+     *               specified in the order required by the device (e.g. in
+     *               little-endian order).
+     */
+    explicit I2CWriteBytesAction(uint8_t reg,
+                                 const std::vector<uint8_t>& values) :
+        reg{reg},
+        values{values}, masks{}
+    {
+        // Values vector must not be empty
+        if (values.size() < 1)
+        {
+            throw std::invalid_argument{"Values vector is empty"};
+        }
+    }
+
+    /**
+     * Constructor.
+     *
+     * Throws an exception if any of the input parameters are invalid.
+     *
+     * @param reg Device register address.  Note: named 'reg' because 'register'
+     *            is a reserved keyword.
+     * @param values One or more byte values to write.  The bytes must be
+     *               specified in the order required by the device (e.g. in
+     *               little-endian order).
+     * @param masks One or more bit masks.  The number of bit masks must match
+     *              the number of byte values to write.  Each mask specifies
+     *              which bits to write within the corresponding byte value.
+     *              Only the bits with a value of 1 in the mask will be written.
+     */
+    explicit I2CWriteBytesAction(uint8_t reg,
+                                 const std::vector<uint8_t>& values,
+                                 const std::vector<uint8_t>& masks) :
+        reg{reg},
+        values{values}, masks{masks}
+    {
+        // Values vector must not be empty
+        if (values.size() < 1)
+        {
+            throw std::invalid_argument{"Values vector is empty"};
+        }
+
+        // Masks vector must have same size as values vector
+        if (masks.size() != values.size())
+        {
+            throw std::invalid_argument{"Masks vector has invalid size"};
+        }
+    }
+
+    /**
+     * Executes this action.
+     *
+     * Writes bytes to a device register using the I2C interface.
+     *
+     * All of the bytes will be written in a single I2C operation.
+     *
+     * The device register, byte values, and bit masks (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
+     */
+    virtual bool execute(ActionEnvironment& environment) override;
+
+    /**
+     * Returns the device register address.
+     *
+     * @return register address
+     */
+    uint8_t getRegister() const
+    {
+        return reg;
+    }
+
+    /**
+     * Returns the byte values to write.
+     *
+     * @return values to write
+     */
+    const std::vector<uint8_t>& getValues() const
+    {
+        return values;
+    }
+
+    /**
+     * Returns the bit masks.
+     *
+     * Each mask specifies which bits to write within the corresponding byte
+     * value.  Only the bits with a value of 1 in the mask will be written.
+     *
+     * @return bit masks
+     */
+    const std::vector<uint8_t>& getMasks() const
+    {
+        return masks;
+    }
+
+    /**
+     * 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};
+
+    /**
+     * Byte values to write.
+     */
+    const std::vector<uint8_t> values{};
+
+    /**
+     * Bit masks.  Each mask specifies which bits to write within the
+     * corresponding byte value.  Only the bits with a value of 1 in the mask
+     * will be written.
+     */
+    const std::vector<uint8_t> masks{};
+};
+
+} // namespace phosphor::power::regulators
diff --git a/phosphor-regulators/src/meson.build b/phosphor-regulators/src/meson.build
index b5bb9f4..556c65b 100644
--- a/phosphor-regulators/src/meson.build
+++ b/phosphor-regulators/src/meson.build
@@ -10,7 +10,8 @@
     'actions/i2c_compare_bit_action.cpp',
     'actions/i2c_compare_byte_action.cpp',
     'actions/i2c_compare_bytes_action.cpp',
-    'actions/i2c_write_byte_action.cpp'
+    'actions/i2c_write_byte_action.cpp',
+    'actions/i2c_write_bytes_action.cpp'
 ]
 
 phosphor_regulators_library = static_library(
diff --git a/phosphor-regulators/test/actions/i2c_write_bytes_action_tests.cpp b/phosphor-regulators/test/actions/i2c_write_bytes_action_tests.cpp
new file mode 100644
index 0000000..ec698f4
--- /dev/null
+++ b/phosphor-regulators/test/actions/i2c_write_bytes_action_tests.cpp
@@ -0,0 +1,459 @@
+/**
+ * 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_interface.hpp"
+#include "i2c_write_bytes_action.hpp"
+#include "id_map.hpp"
+#include "mocked_i2c_interface.hpp"
+
+#include <cstdint>
+#include <memory>
+#include <stdexcept>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+using namespace phosphor::power::regulators;
+
+using ::testing::A;
+using ::testing::Args;
+using ::testing::ElementsAre;
+using ::testing::NotNull;
+using ::testing::Return;
+using ::testing::SetArrayArgument;
+using ::testing::Throw;
+using ::testing::TypedEq;
+
+// Test for I2CWriteBytesAction(uint8_t reg,
+//                              const std::vector<uint8_t>& values)
+TEST(I2CWriteBytesActionTests, Constructor1)
+{
+    // Test where works
+    try
+    {
+        std::vector<uint8_t> values{0x56, 0x14, 0xDA};
+        I2CWriteBytesAction action{0x7C, values};
+
+        EXPECT_EQ(action.getRegister(), 0x7C);
+
+        EXPECT_EQ(action.getValues().size(), 3);
+        EXPECT_EQ(action.getValues()[0], 0x56);
+        EXPECT_EQ(action.getValues()[1], 0x14);
+        EXPECT_EQ(action.getValues()[2], 0xDA);
+
+        EXPECT_EQ(action.getMasks().size(), 0);
+    }
+    catch (...)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where fails: Values vector is empty
+    try
+    {
+        std::vector<uint8_t> values{};
+        I2CWriteBytesAction action{0x7C, values};
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& e)
+    {
+        EXPECT_STREQ(e.what(), "Values vector is empty");
+    }
+    catch (...)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+}
+
+// Test for I2CWriteBytesAction(uint8_t reg,
+//                              const std::vector<uint8_t>& values,
+//                              const std::vector<uint8_t>& masks)
+TEST(I2CWriteBytesActionTests, Constructor2)
+{
+    // Test where works
+    try
+    {
+        std::vector<uint8_t> values{0x56, 0x14};
+        std::vector<uint8_t> masks{0x7E, 0x3C};
+        I2CWriteBytesAction action{0xA0, values, masks};
+
+        EXPECT_EQ(action.getRegister(), 0xA0);
+
+        EXPECT_EQ(action.getValues().size(), 2);
+        EXPECT_EQ(action.getValues()[0], 0x56);
+        EXPECT_EQ(action.getValues()[1], 0x14);
+
+        EXPECT_EQ(action.getMasks().size(), 2);
+        EXPECT_EQ(action.getMasks()[0], 0x7E);
+        EXPECT_EQ(action.getMasks()[1], 0x3C);
+    }
+    catch (...)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where fails: Values vector is empty
+    try
+    {
+        std::vector<uint8_t> values{};
+        std::vector<uint8_t> masks{};
+        I2CWriteBytesAction action{0xA0, values, masks};
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& e)
+    {
+        EXPECT_STREQ(e.what(), "Values vector is empty");
+    }
+    catch (...)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where fails: Masks vector different size than values vector
+    try
+    {
+        std::vector<uint8_t> values{0x56, 0x14, 0xFE};
+        std::vector<uint8_t> masks{0x7E, 0x3C};
+        I2CWriteBytesAction action{0x7C, values, masks};
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const std::invalid_argument& e)
+    {
+        EXPECT_STREQ(e.what(), "Masks vector has invalid size");
+    }
+    catch (...)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+}
+
+TEST(I2CWriteBytesActionTests, Execute)
+{
+    // Test where works: Masks not specified
+    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,
+                    read(A<uint8_t>(), A<uint8_t&>(), A<uint8_t*>(),
+                         A<i2c::I2CInterface::Mode>()))
+            .Times(0);
+        EXPECT_CALL(*i2cInterface,
+                    write(0x7C, 3, NotNull(), i2c::I2CInterface::Mode::I2C))
+            .With(Args<2, 1>(ElementsAre(0x56, 0x14, 0xDA)))
+            .Times(1);
+
+        // 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"};
+
+        std::vector<uint8_t> values{0x56, 0x14, 0xDA};
+        I2CWriteBytesAction action{0x7C, values};
+        EXPECT_EQ(action.execute(env), true);
+    }
+    catch (...)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where works: Masks specified
+    try
+    {
+        // Create mock I2CInterface: read() returns values 0x69, 0xA5
+        std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
+            std::make_unique<i2c::MockedI2CInterface>();
+        uint8_t actualValues[] = {0x69, 0xA5};
+        EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
+        EXPECT_CALL(*i2cInterface, read(0xA0, TypedEq<uint8_t&>(2), NotNull(),
+                                        i2c::I2CInterface::Mode::I2C))
+            .Times(1)
+            .WillOnce(SetArrayArgument<2>(actualValues, actualValues + 2));
+        EXPECT_CALL(*i2cInterface,
+                    write(0xA0, 2, NotNull(), i2c::I2CInterface::Mode::I2C))
+            .With(Args<2, 1>(ElementsAre(0xEA, 0xB3)))
+            .Times(1);
+
+        // 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"};
+
+        //                        Byte 1             Byte 2
+        // Value to write       : 0xD6 = 1101 0110 : 0xD2 = 1101 0010
+        // Mask                 : 0xC3 = 1100 0011 : 0x96 = 1001 0110
+        // Current value        : 0x69 = 0110 1001 : 0xA5 = 1010 0101
+        // Value to write & mask: 0xC2 = 1100 0010 : 0x92 = 1001 0010
+        // ~Mask                : 0x3C = 0011 1100 : 0x69 = 0110 1001
+        // Current value & ~mask: 0x28 = 0010 1000 : 0x21 = 0010 0001
+        // Final value to write : 0xEA = 1110 1010 : 0xB3 = 1011 0011
+        std::vector<uint8_t> values{0xD6, 0xD2};
+        std::vector<uint8_t> masks{0xC3, 0x96};
+        I2CWriteBytesAction action{0xA0, values, masks};
+        EXPECT_EQ(action.execute(env), true);
+    }
+    catch (...)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where works: Single byte
+    try
+    {
+        // Create mock I2CInterface: read() returns value 0x69
+        std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
+            std::make_unique<i2c::MockedI2CInterface>();
+        uint8_t actualValues[] = {0x69};
+        EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
+        EXPECT_CALL(*i2cInterface, read(0xA0, TypedEq<uint8_t&>(1), NotNull(),
+                                        i2c::I2CInterface::Mode::I2C))
+            .Times(1)
+            .WillOnce(SetArrayArgument<2>(actualValues, actualValues + 1));
+        EXPECT_CALL(*i2cInterface,
+                    write(0xA0, 1, NotNull(), i2c::I2CInterface::Mode::I2C))
+            .With(Args<2, 1>(ElementsAre(0xEA)))
+            .Times(1);
+
+        // 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"};
+
+        // Value to write       : 0xD6 = 1101 0110
+        // Mask                 : 0xC3 = 1100 0011
+        // Current value        : 0x69 = 0110 1001
+        // Value to write & mask: 0xC2 = 1100 0010
+        // ~Mask                : 0x3C = 0011 1100
+        // Current value & ~mask: 0x28 = 0010 1000
+        // Final value to write : 0xEA = 1110 1010
+        std::vector<uint8_t> values{0xD6};
+        std::vector<uint8_t> masks{0xC3};
+        I2CWriteBytesAction action{0xA0, values, masks};
+        EXPECT_EQ(action.execute(env), true);
+    }
+    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"};
+
+        std::vector<uint8_t> values{0x56, 0x14, 0xDB};
+        I2CWriteBytesAction action{0x7C, values};
+        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 bytes 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(0xA0, TypedEq<uint8_t&>(2), NotNull(),
+                                        i2c::I2CInterface::Mode::I2C))
+            .Times(1)
+            .WillOnce(Throw(i2c::I2CException{"Failed to read i2c block data",
+                                              "/dev/i2c-1", 0x70}));
+        EXPECT_CALL(*i2cInterface,
+                    write(A<uint8_t>(), A<uint8_t>(), A<const uint8_t*>(),
+                          A<i2c::I2CInterface::Mode>()))
+            .Times(0);
+
+        // 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"};
+
+        std::vector<uint8_t> values{0xD6, 0xD2};
+        std::vector<uint8_t> masks{0xC3, 0x96};
+        I2CWriteBytesAction action{0xA0, values, masks};
+        action.execute(env);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const ActionError& e)
+    {
+        EXPECT_STREQ(e.what(),
+                     "ActionError: i2c_write_bytes: { register: "
+                     "0xA0, values: [ 0xD6, 0xD2 ], masks: [ 0xC3, 0x96 ] }");
+        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 i2c block data: bus "
+                         "/dev/i2c-1, addr 0x70");
+        }
+        catch (...)
+        {
+            ADD_FAILURE() << "Should not have caught exception.";
+        }
+    }
+    catch (...)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+
+    // Test where fails: Writing bytes fails
+    try
+    {
+        // Create mock I2CInterface: write() 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&>(), A<uint8_t*>(),
+                         A<i2c::I2CInterface::Mode>()))
+            .Times(0);
+        EXPECT_CALL(*i2cInterface,
+                    write(0x7C, 3, NotNull(), i2c::I2CInterface::Mode::I2C))
+            .With(Args<2, 1>(ElementsAre(0x56, 0x14, 0xDA)))
+            .Times(1)
+            .WillOnce(Throw(i2c::I2CException{"Failed to write i2c block data",
+                                              "/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"};
+
+        std::vector<uint8_t> values{0x56, 0x14, 0xDA};
+        I2CWriteBytesAction action{0x7C, values};
+        EXPECT_EQ(action.execute(env), true);
+        action.execute(env);
+        ADD_FAILURE() << "Should not have reached this line.";
+    }
+    catch (const ActionError& e)
+    {
+        EXPECT_STREQ(e.what(),
+                     "ActionError: i2c_write_bytes: { register: "
+                     "0x7C, values: [ 0x56, 0x14, 0xDA ], masks: [  ] }");
+        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 write i2c block data: bus "
+                         "/dev/i2c-1, addr 0x70");
+        }
+        catch (...)
+        {
+            ADD_FAILURE() << "Should not have caught exception.";
+        }
+    }
+    catch (...)
+    {
+        ADD_FAILURE() << "Should not have caught exception.";
+    }
+}
+
+TEST(I2CWriteBytesActionTests, GetRegister)
+{
+    std::vector<uint8_t> values{0x56, 0x14};
+    I2CWriteBytesAction action{0xA0, values};
+    EXPECT_EQ(action.getRegister(), 0xA0);
+}
+
+TEST(I2CWriteBytesActionTests, GetValues)
+{
+    std::vector<uint8_t> values{0x56, 0x14};
+    std::vector<uint8_t> masks{0x7E, 0x3C};
+    I2CWriteBytesAction action{0xA0, values, masks};
+    EXPECT_EQ(action.getValues().size(), 2);
+    EXPECT_EQ(action.getValues()[0], 0x56);
+    EXPECT_EQ(action.getValues()[1], 0x14);
+}
+
+TEST(I2CWriteBytesActionTests, GetMasks)
+{
+    // Test where masks were not specified
+    {
+        std::vector<uint8_t> values{0x56, 0x14, 0xDA};
+        I2CWriteBytesAction action{0x7C, values};
+        EXPECT_EQ(action.getMasks().size(), 0);
+    }
+
+    // Test where masks were specified
+    {
+        std::vector<uint8_t> values{0x56, 0x14};
+        std::vector<uint8_t> masks{0x7E, 0x3C};
+        I2CWriteBytesAction action{0xA0, values, masks};
+        EXPECT_EQ(action.getMasks().size(), 2);
+        EXPECT_EQ(action.getMasks()[0], 0x7E);
+        EXPECT_EQ(action.getMasks()[1], 0x3C);
+    }
+}
+
+TEST(I2CWriteBytesActionTests, ToString)
+{
+    // Test where masks were not specified
+    {
+        std::vector<uint8_t> values{0x56, 0x14, 0xDA};
+        I2CWriteBytesAction action{0x7C, values};
+        EXPECT_EQ(action.toString(),
+                  "i2c_write_bytes: { register: 0x7C, values: "
+                  "[ 0x56, 0x14, 0xDA ], masks: [  ] }");
+    }
+
+    // Test where masks were specified
+    {
+        std::vector<uint8_t> values{0x56, 0x14};
+        std::vector<uint8_t> masks{0x7E, 0x3C};
+        I2CWriteBytesAction action{0xA0, values, masks};
+        EXPECT_EQ(action.toString(),
+                  "i2c_write_bytes: { register: 0xA0, values: "
+                  "[ 0x56, 0x14 ], masks: [ 0x7E, 0x3C ] }");
+    }
+}
diff --git a/phosphor-regulators/test/meson.build b/phosphor-regulators/test/meson.build
index f6a351c..0dc4c56 100644
--- a/phosphor-regulators/test/meson.build
+++ b/phosphor-regulators/test/meson.build
@@ -18,6 +18,7 @@
     'actions/i2c_compare_byte_action_tests.cpp',
     'actions/i2c_compare_bytes_action_tests.cpp',
     'actions/i2c_write_byte_action_tests.cpp',
+    'actions/i2c_write_bytes_action_tests.cpp',
     'actions/if_action_tests.cpp',
     'actions/not_action_tests.cpp',
     'actions/or_action_tests.cpp',