test: common: random software id

Test that the random software id matches our regex.

Change-Id: I0eb9b5c58b993ef6172b059daec086872e7656fb
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/test/common/software/meson.build b/test/common/software/meson.build
new file mode 100644
index 0000000..7d0a077
--- /dev/null
+++ b/test/common/software/meson.build
@@ -0,0 +1,26 @@
+
+testcases = ['software_get_random_softwareid']
+
+foreach t : testcases
+    test(
+        t,
+        executable(
+            t,
+            f'@t@.cpp',
+            include_directories: [common_include],
+            dependencies: [
+                libpldm_dep,
+                sdbusplus_dep,
+                phosphor_logging_dep,
+                gtest,
+            ],
+            link_with: [
+                libpldmutil,
+                libpldmcreatepkg,
+                software_common_lib,
+                libexampledevice,
+            ],
+        ),
+    )
+endforeach
+
diff --git a/test/common/software/software_get_random_softwareid.cpp b/test/common/software/software_get_random_softwareid.cpp
new file mode 100644
index 0000000..47bda1d
--- /dev/null
+++ b/test/common/software/software_get_random_softwareid.cpp
@@ -0,0 +1,52 @@
+#include "../exampledevice/example_device.hpp"
+
+#include <phosphor-logging/lg2.hpp>
+#include <sdbusplus/asio/connection.hpp>
+#include <sdbusplus/asio/object_server.hpp>
+#include <sdbusplus/async.hpp>
+#include <sdbusplus/server.hpp>
+
+#include <memory>
+#include <regex>
+
+#include <gtest/gtest.h>
+
+PHOSPHOR_LOG2_USING;
+
+using namespace phosphor::software;
+using namespace phosphor::software::example_device;
+
+class TestSoftware : public Software
+{
+  public:
+    static std::string wrapGetRandomSoftwareId(Device& parent)
+    {
+        return Software::getRandomSoftwareId(parent);
+    };
+};
+
+constexpr const char* mb1ExampleComponent = "MB1ExampleComponent";
+
+TEST(SoftwareTest, testGetRandomSoftwareId)
+{
+    sdbusplus::async::context ctx;
+    ExampleCodeUpdater exampleUpdater(ctx);
+
+    std::string objPath =
+        "/xyz/openbmc_project/inventory/system/board/ExampleBoard/ExampleDevice";
+
+    SoftwareConfig config(objPath, 0x1234, "my.example.compatible", "Example",
+                          mb1ExampleComponent);
+
+    auto device = std::make_unique<ExampleDevice>(ctx, &exampleUpdater, config);
+
+    std::string swid = TestSoftware::wrapGetRandomSoftwareId(*device);
+    debug("{SWID}", "SWID", swid);
+
+    std::regex re("[a-zA-Z0-9]+_[0-9]+");
+    std::cmatch m;
+
+    EXPECT_TRUE(std::regex_match(swid.c_str(), m, re));
+
+    EXPECT_TRUE(swid.starts_with(std::string(mb1ExampleComponent) + "_"));
+}