1 #include "general_systemd.hpp" 2 #include "skip_action.hpp" 3 #include "version_handlers_builder.hpp" 4 5 #include <nlohmann/json.hpp> 6 7 #include <gmock/gmock.h> 8 #include <gtest/gtest.h> 9 10 namespace ipmi_flash 11 { 12 namespace 13 { 14 using ::testing::IsEmpty; 15 16 using json = nlohmann::json; 17 18 TEST(VersionJsonTest, ValidConfigurationNoVersionHandler) 19 { 20 auto j2 = R"( 21 [{ 22 "blob" : "/flash/sink_seq", 23 "version":{ 24 "handler": { 25 "type" : "file", 26 "path" : "/tmp/version_info" 27 }, 28 "actions":{ 29 "open" :{ 30 "type" : "systemd", 31 "unit" : "absolute" 32 } 33 } 34 } 35 }] 36 )"_json; 37 auto h = VersionHandlersBuilder().buildHandlerFromJson(j2); 38 ASSERT_THAT(h, ::testing::SizeIs(1)); 39 EXPECT_THAT(h[0].blobId, "/version/sink_seq"); 40 EXPECT_FALSE(h[0].actions == nullptr); 41 EXPECT_FALSE(h[0].handler == nullptr); 42 } 43 44 TEST(VersionJsonTest, ValidConfigurationVersionBlobName) 45 { 46 auto j2 = R"( 47 [{ 48 "blob" : "/version/sink_seq", 49 "version":{ 50 "handler": { 51 "type" : "file", 52 "path" : "/tmp/version_info" 53 }, 54 "actions": { 55 "open" : { 56 "type" : "systemd", 57 "unit" : "phosphor-ipmi-flash-version-sink-sequencer.target" 58 } 59 } 60 } 61 }] 62 )"_json; 63 auto h = VersionHandlersBuilder().buildHandlerFromJson(j2); 64 ASSERT_THAT(h, ::testing::SizeIs(1)); 65 EXPECT_THAT(h[0].blobId, "/version/sink_seq"); 66 EXPECT_FALSE(h[0].actions == nullptr); 67 EXPECT_FALSE(h[0].handler == nullptr); 68 } 69 70 TEST(VersionJsonTest, MissingHandlerType) 71 { 72 auto j2 = R"( 73 [{ 74 "blob" : "/flash/image", 75 "version":{ 76 "handler": { 77 "path" : "/tmp/version_info" 78 }, 79 "actions": { 80 "open" : { 81 "type" : "systemd", 82 "unit" : "absolute"} 83 } 84 } 85 }] 86 )"_json; 87 EXPECT_THAT(VersionHandlersBuilder().buildHandlerFromJson(j2), IsEmpty()); 88 } 89 90 TEST(VersionJsonTest, BadBlobName) 91 { 92 auto j2 = R"( 93 [{ 94 "blob" : "/bad/image", 95 "version":{ 96 "handler": { 97 "type" : "file", 98 "path" : "/tmp/version_info" 99 }, 100 "actions": { 101 "open" : { 102 "type" : "systemd", 103 "unit" : "absolute"} 104 } 105 } 106 }] 107 )"_json; 108 EXPECT_THAT(VersionHandlersBuilder().buildHandlerFromJson(j2), IsEmpty()); 109 } 110 111 TEST(VersionJsonTest, MissingActions) 112 { 113 auto j2 = R"( 114 [{ 115 "blob" : "/flash/image", 116 "version":{ 117 "handler": { 118 "type" : "file", 119 "path" : "/tmp/version_info" 120 } 121 } 122 }] 123 )"_json; 124 EXPECT_THAT(VersionHandlersBuilder().buildHandlerFromJson(j2), IsEmpty()); 125 } 126 127 TEST(VersionJsonTest, MissingOpenAction) 128 { 129 auto j2 = R"( 130 [{ 131 "blob" : "/flash/image", 132 "version":{ 133 "handler": { 134 "type" : "file", 135 "path" : "/tmp/version_info" 136 }, 137 "actions": {} 138 } 139 }] 140 )"_json; 141 EXPECT_THAT(VersionHandlersBuilder().buildHandlerFromJson(j2), IsEmpty()); 142 } 143 144 TEST(VersionJsonTest, OneInvalidTwoValidSucceeds) 145 { 146 auto j2 = R"( 147 [{ 148 "blob" : "/flash/sink_seq0", 149 "version":{ 150 "handler": { 151 "type" : "file", 152 "path" : "/tmp/version_info" 153 }, 154 "actions":{ 155 "open" :{ 156 "type" : "systemd", 157 "unit" : "absolute" 158 } 159 } 160 } 161 }, 162 { 163 "blob" : "/version/sink_seq1", 164 "version":{ 165 "handler": { 166 "type" : "file", 167 "path" : "/tmp/version_info" 168 }, 169 "actions":{ 170 "open" :{ 171 "type" : "systemd", 172 "unit" : "absolute" 173 } 174 } 175 } 176 }, 177 { 178 "blob" : "/bad/sink_seq", 179 "version":{ 180 "handler": { 181 "type" : "file", 182 "path" : "/tmp/version_info" 183 }, 184 "actions":{ 185 "open" :{ 186 "type" : "systemd", 187 "unit" : "absolute" 188 } 189 } 190 } 191 } 192 ] 193 )"_json; 194 auto h = VersionHandlersBuilder().buildHandlerFromJson(j2); 195 ASSERT_THAT(h, ::testing::SizeIs(2)); 196 EXPECT_THAT(h[0].blobId, "/version/sink_seq0"); 197 EXPECT_THAT(h[1].blobId, "/version/sink_seq1"); 198 } 199 200 TEST(VersionJsonTest, BlobNameIsTooShort) 201 { 202 auto j2 = R"( 203 [{ 204 "blob" : "/flash/", 205 "version":{ 206 "handler": { 207 "type" : "file", 208 "path" : "/tmp/version_info" 209 }, 210 "actions":{ 211 "open" :{ 212 "type" : "systemd", 213 "unit" : "absolute" 214 } 215 } 216 } 217 }] 218 )"_json; 219 EXPECT_THAT(VersionHandlersBuilder().buildHandlerFromJson(j2), IsEmpty()); 220 } 221 222 TEST(VersionJsonTest, OpenSkipAction) 223 { 224 auto j2 = R"( 225 [{ 226 "blob" : "/flash/sink_seqs", 227 "version":{ 228 "handler": { 229 "type" : "file", 230 "path" : "/tmp/version_info" 231 }, 232 "actions":{ 233 "open" :{ 234 "type" : "skip" 235 } 236 } 237 } 238 }] 239 )"_json; 240 auto h = VersionHandlersBuilder().buildHandlerFromJson(j2); 241 EXPECT_THAT(h, ::testing::SizeIs(1)); 242 EXPECT_TRUE(h[0].blobId == "/version/sink_seqs"); 243 ASSERT_FALSE(h[0].actions == nullptr); 244 EXPECT_FALSE(h[0].actions->onOpen == nullptr); 245 } 246 247 TEST(VersionJsonTest, OpenActionsWithDifferentModes) 248 { 249 auto j2 = R"( 250 [{ 251 "blob" : "/flash/blob1", 252 "version":{ 253 "handler": { 254 "type" : "file", 255 "path" : "/tmp/version_info" 256 }, 257 "actions":{ 258 "open" :{ 259 "type" : "systemd", 260 "unit" : "absolute", 261 "mode" : "replace-nope" 262 } 263 } 264 } 265 }, 266 { 267 "blob" : "/flash/blob2", 268 "version":{ 269 "handler": { 270 "type" : "file", 271 "path" : "/tmp/version_info" 272 }, 273 "actions":{ 274 "open" :{ 275 "type" : "systemd", 276 "unit" : "absolute", 277 "mode" : "replace-fake" 278 } 279 } 280 } 281 } 282 ] 283 )"_json; 284 auto h = VersionHandlersBuilder().buildHandlerFromJson(j2); 285 ASSERT_THAT(h, ::testing::SizeIs(2)); 286 287 EXPECT_FALSE(h[0].handler == nullptr); 288 EXPECT_FALSE(h[0].actions == nullptr); 289 EXPECT_THAT(h[0].blobId, "/version/blob1"); 290 auto onOpen0 = reinterpret_cast<SystemdNoFile*>(h[0].actions->onOpen.get()); 291 EXPECT_THAT(onOpen0->getMode(), "replace-nope"); 292 293 EXPECT_FALSE(h[1].handler == nullptr); 294 EXPECT_FALSE(h[1].actions == nullptr); 295 EXPECT_THAT(h[1].blobId, "/version/blob2"); 296 auto onOpen1 = reinterpret_cast<SystemdNoFile*>(h[1].actions->onOpen.get()); 297 EXPECT_THAT(onOpen1->getMode(), "replace-fake"); 298 } 299 } // namespace 300 } // namespace ipmi_flash 301