| Matt Spinler | 09d6400 | 2019-09-11 14:29:46 -0500 | [diff] [blame] | 1 | #include "extensions/openpower-pels/data_interface.hpp" | 
| Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame] | 2 | #include "extensions/openpower-pels/host_interface.hpp" | 
 | 3 |  | 
 | 4 | #include <fcntl.h> | 
 | 5 |  | 
 | 6 | #include <filesystem> | 
 | 7 | #include <sdeventplus/source/io.hpp> | 
| Matt Spinler | 09d6400 | 2019-09-11 14:29:46 -0500 | [diff] [blame] | 8 |  | 
 | 9 | #include <gmock/gmock.h> | 
 | 10 |  | 
 | 11 | namespace openpower | 
 | 12 | { | 
 | 13 | namespace pels | 
 | 14 | { | 
 | 15 |  | 
 | 16 | class MockDataInterface : public DataInterfaceBase | 
 | 17 | { | 
 | 18 |   public: | 
 | 19 |     MockDataInterface() | 
 | 20 |     { | 
 | 21 |     } | 
| Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame] | 22 |     MOCK_METHOD(std::string, getMachineTypeModel, (), (const override)); | 
 | 23 |     MOCK_METHOD(std::string, getMachineSerialNumber, (), (const override)); | 
 | 24 |     MOCK_METHOD(std::string, getServerFWVersion, (), (const override)); | 
 | 25 |     MOCK_METHOD(std::string, getBMCFWVersion, (), (const override)); | 
 | 26 |  | 
 | 27 |     void changeHostState(bool newState) | 
 | 28 |     { | 
 | 29 |         setHostState(newState); | 
 | 30 |     } | 
 | 31 |  | 
 | 32 |     void setHMCManaged(bool managed) | 
 | 33 |     { | 
 | 34 |         _hmcManaged = managed; | 
 | 35 |     } | 
 | 36 | }; | 
 | 37 |  | 
 | 38 | /** | 
 | 39 |  * @brief The mock HostInterface class | 
| Matt Spinler | 5342d9a | 2019-12-12 11:03:39 -0600 | [diff] [blame] | 40 |  * | 
 | 41 |  * This replaces the PLDM calls with a FIFO for the asynchronous | 
 | 42 |  * responses. | 
| Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame] | 43 |  */ | 
 | 44 | class MockHostInterface : public HostInterface | 
 | 45 | { | 
 | 46 |   public: | 
| Matt Spinler | 5342d9a | 2019-12-12 11:03:39 -0600 | [diff] [blame] | 47 |     /** | 
 | 48 |      * @brief Constructor | 
 | 49 |      * | 
 | 50 |      * @param[in] event - The sd_event object | 
 | 51 |      * @param[in] dataIface - The DataInterface class | 
 | 52 |      */ | 
| Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame] | 53 |     MockHostInterface(sd_event* event, DataInterfaceBase& dataIface) : | 
 | 54 |         HostInterface(event, dataIface) | 
 | 55 |     { | 
| Matt Spinler | 5342d9a | 2019-12-12 11:03:39 -0600 | [diff] [blame] | 56 |         char templ[] = "/tmp/cmdfifoXXXXXX"; | 
 | 57 |         std::filesystem::path dir = mkdtemp(templ); | 
 | 58 |         _fifo = dir / "fifo"; | 
| Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame] | 59 |     } | 
 | 60 |  | 
| Matt Spinler | 5342d9a | 2019-12-12 11:03:39 -0600 | [diff] [blame] | 61 |     /** | 
 | 62 |      * @brief Destructor | 
 | 63 |      */ | 
| Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame] | 64 |     virtual ~MockHostInterface() | 
 | 65 |     { | 
| Matt Spinler | 5342d9a | 2019-12-12 11:03:39 -0600 | [diff] [blame] | 66 |         std::filesystem::remove_all(_fifo.parent_path()); | 
| Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame] | 67 |     } | 
 | 68 |  | 
 | 69 |     MOCK_METHOD(CmdStatus, sendNewLogCmd, (uint32_t, uint32_t), (override)); | 
 | 70 |  | 
| Matt Spinler | 5342d9a | 2019-12-12 11:03:39 -0600 | [diff] [blame] | 71 |     /** | 
 | 72 |      * @brief Cancels waiting for a command response | 
 | 73 |      */ | 
 | 74 |     virtual void cancelCmd() override | 
 | 75 |     { | 
 | 76 |         _inProgress = false; | 
 | 77 |         _source = nullptr; | 
 | 78 |     } | 
 | 79 |  | 
 | 80 |     /** | 
 | 81 |      * @brief Returns the amount of time to wait before retrying after | 
 | 82 |      *        a failed send command. | 
 | 83 |      * | 
 | 84 |      * @return milliseconds - The amount of time to wait | 
 | 85 |      */ | 
 | 86 |     virtual std::chrono::milliseconds getSendRetryDelay() const override | 
 | 87 |     { | 
 | 88 |         return std::chrono::milliseconds(2); | 
 | 89 |     } | 
 | 90 |  | 
 | 91 |     /** | 
 | 92 |      * @brief Returns the amount of time to wait before retrying after | 
 | 93 |      *        a command receive. | 
 | 94 |      * | 
 | 95 |      * @return milliseconds - The amount of time to wait | 
 | 96 |      */ | 
 | 97 |     virtual std::chrono::milliseconds getReceiveRetryDelay() const override | 
 | 98 |     { | 
 | 99 |         return std::chrono::milliseconds(2); | 
 | 100 |     } | 
 | 101 |  | 
 | 102 |     /** | 
 | 103 |      * @brief Returns the number of commands processed | 
 | 104 |      */ | 
 | 105 |     size_t numCmdsProcessed() const | 
 | 106 |     { | 
 | 107 |         return _cmdsProcessed; | 
 | 108 |     } | 
 | 109 |  | 
 | 110 |     /** | 
 | 111 |      * @brief Writes the data passed in to the FIFO | 
 | 112 |      * | 
 | 113 |      * @param[in] hostResponse - use a 0 to indicate success | 
 | 114 |      * | 
 | 115 |      * @return CmdStatus - success or failure | 
 | 116 |      */ | 
 | 117 |     CmdStatus send(uint8_t hostResponse) | 
 | 118 |     { | 
 | 119 |         // Create a FIFO once. | 
 | 120 |         if (!std::filesystem::exists(_fifo)) | 
 | 121 |         { | 
 | 122 |             if (mkfifo(_fifo.c_str(), 0622)) | 
 | 123 |             { | 
 | 124 |                 ADD_FAILURE() << "Failed mkfifo " << _fifo << strerror(errno); | 
 | 125 |                 exit(-1); | 
 | 126 |             } | 
 | 127 |         } | 
 | 128 |  | 
 | 129 |         // Open it and register the reponse callback to | 
 | 130 |         // be used on FD activity. | 
 | 131 |         int fd = open(_fifo.c_str(), O_NONBLOCK | O_RDWR); | 
 | 132 |         EXPECT_TRUE(fd >= 0) << "Unable to open FIFO"; | 
 | 133 |  | 
 | 134 |         auto callback = [this](sdeventplus::source::IO& source, int fd, | 
 | 135 |                                uint32_t events) { | 
 | 136 |             this->receive(source, fd, events); | 
 | 137 |         }; | 
 | 138 |  | 
 | 139 |         try | 
 | 140 |         { | 
 | 141 |             _source = std::make_unique<sdeventplus::source::IO>( | 
 | 142 |                 _event, fd, EPOLLIN, | 
 | 143 |                 std::bind(callback, std::placeholders::_1, | 
 | 144 |                           std::placeholders::_2, std::placeholders::_3)); | 
 | 145 |         } | 
 | 146 |         catch (std::exception& e) | 
 | 147 |         { | 
 | 148 |             ADD_FAILURE() << "Event exception: " << e.what(); | 
 | 149 |             close(fd); | 
 | 150 |             return CmdStatus::failure; | 
 | 151 |         } | 
 | 152 |  | 
 | 153 |         // Write the fake host reponse to the FIFO | 
 | 154 |         auto bytesWritten = write(fd, &hostResponse, sizeof(hostResponse)); | 
 | 155 |         EXPECT_EQ(bytesWritten, sizeof(hostResponse)); | 
 | 156 |  | 
 | 157 |         _inProgress = true; | 
 | 158 |  | 
 | 159 |         return CmdStatus::success; | 
 | 160 |     } | 
 | 161 |  | 
| Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame] | 162 |   protected: | 
| Matt Spinler | 5342d9a | 2019-12-12 11:03:39 -0600 | [diff] [blame] | 163 |     /** | 
 | 164 |      * @brief Reads the data written to the fifo and then calls | 
 | 165 |      *        the subscriber's callback. | 
 | 166 |      * | 
 | 167 |      * Nonzero data indicates a command failure (for testing bad path). | 
 | 168 |      * | 
 | 169 |      * @param[in] source - The event source object | 
 | 170 |      * @param[in] fd - The file descriptor used | 
 | 171 |      * @param[in] events - The event bits | 
 | 172 |      */ | 
| Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame] | 173 |     void receive(sdeventplus::source::IO& source, int fd, | 
 | 174 |                  uint32_t events) override | 
 | 175 |     { | 
| Matt Spinler | 5342d9a | 2019-12-12 11:03:39 -0600 | [diff] [blame] | 176 |         if (!(events & EPOLLIN)) | 
 | 177 |         { | 
 | 178 |             return; | 
 | 179 |         } | 
 | 180 |  | 
 | 181 |         _inProgress = false; | 
 | 182 |  | 
 | 183 |         int newFD = open(_fifo.c_str(), O_NONBLOCK | O_RDONLY); | 
 | 184 |         ASSERT_TRUE(newFD >= 0) << "Failed to open FIFO"; | 
 | 185 |  | 
 | 186 |         // Read the host success/failure response from the FIFO. | 
 | 187 |         uint8_t data; | 
 | 188 |         auto bytesRead = read(newFD, &data, sizeof(data)); | 
 | 189 |         EXPECT_EQ(bytesRead, sizeof(data)); | 
 | 190 |  | 
 | 191 |         close(newFD); | 
 | 192 |  | 
 | 193 |         ResponseStatus status = ResponseStatus::success; | 
 | 194 |         if (data != 0) | 
 | 195 |         { | 
 | 196 |             status = ResponseStatus::failure; | 
 | 197 |         } | 
 | 198 |  | 
 | 199 |         if (_responseFunc) | 
 | 200 |         { | 
 | 201 |             (*_responseFunc)(status); | 
 | 202 |         } | 
 | 203 |  | 
| Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame] | 204 |         // Keep account of the number of commands responses for testing. | 
 | 205 |         _cmdsProcessed++; | 
 | 206 |     } | 
 | 207 |  | 
 | 208 |   private: | 
| Matt Spinler | 5342d9a | 2019-12-12 11:03:39 -0600 | [diff] [blame] | 209 |     /** | 
 | 210 |      * @brief The event source for the fifo | 
 | 211 |      */ | 
 | 212 |     std::unique_ptr<sdeventplus::source::IO> _source; | 
 | 213 |  | 
 | 214 |     /** | 
 | 215 |      * @brief the path to the fifo | 
 | 216 |      */ | 
 | 217 |     std::filesystem::path _fifo; | 
 | 218 |  | 
 | 219 |     /** | 
 | 220 |      * @brief The number of commands processed | 
 | 221 |      */ | 
| Matt Spinler | f60ac27 | 2019-12-11 13:47:50 -0600 | [diff] [blame] | 222 |     size_t _cmdsProcessed = 0; | 
| Matt Spinler | 09d6400 | 2019-09-11 14:29:46 -0500 | [diff] [blame] | 223 | }; | 
 | 224 |  | 
 | 225 | } // namespace pels | 
 | 226 | } // namespace openpower |