diff options
author | Andrew Geissler <geissonator@yahoo.com> | 2019-02-01 10:33:54 -0600 |
---|---|---|
committer | Matt Spinler <spinler@us.ibm.com> | 2019-04-05 15:05:32 +0000 |
commit | 3b025e69dedd6d877f38bcc367296714d404e4eb (patch) | |
tree | c8b2e5bb88ff90d8ba558c2816102eccc6bf5354 /src | |
parent | 12025cdc6ae3115b4ab97ee40a597a752a5aa2f7 (diff) | |
download | phosphor-objmgr-3b025e69dedd6d877f38bcc367296714d404e4eb.tar.gz phosphor-objmgr-3b025e69dedd6d877f38bcc367296714d404e4eb.zip |
unit-test: Introduce unit tests to phosphor-objmgr
Move a function to make more testable and add a test case for it
Testing: Verified 100% test coverage in processing.cpp
Change-Id: I0a888009cfeb57bbc8ad295681bea00b79f2a23d
Signed-off-by: Andrew Geissler <geissonator@yahoo.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/main.cpp | 29 | ||||
-rw-r--r-- | src/processing.cpp | 23 | ||||
-rw-r--r-- | src/processing.hpp | 18 | ||||
-rw-r--r-- | src/test/Makefile.am.include | 4 | ||||
-rw-r--r-- | src/test/well_known.cpp | 37 |
5 files changed, 87 insertions, 24 deletions
diff --git a/src/main.cpp b/src/main.cpp index 7df7aa3..5870aea 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,4 @@ +#include "processing.hpp" #include "src/argument.hpp" #include <tinyxml2.h> @@ -85,26 +86,6 @@ struct NotFoundException final : public sdbusplus::exception_t }; }; -bool get_well_known( - boost::container::flat_map<std::string, std::string>& owners, - const std::string& request, std::string& well_known) -{ - // If it's already a well known name, just return - if (!boost::starts_with(request, ":")) - { - well_known = request; - return true; - } - - auto it = owners.find(request); - if (it == owners.end()) - { - return false; - } - well_known = it->second; - return true; -} - void update_owners(sdbusplus::asio::connection* conn, boost::container::flat_map<std::string, std::string>& owners, const std::string& new_object) @@ -882,7 +863,7 @@ int main(int argc, char** argv) interfaces_added; message.read(obj_path, interfaces_added); std::string well_known; - if (!get_well_known(name_owners, message.get_sender(), well_known)) + if (!getWellKnown(name_owners, message.get_sender(), well_known)) { return; // only introspect well-known } @@ -986,7 +967,7 @@ int main(int argc, char** argv) } std::string sender; - if (!get_well_known(name_owners, message.get_sender(), sender)) + if (!getWellKnown(name_owners, message.get_sender(), sender)) { return; } @@ -1043,8 +1024,8 @@ int main(int argc, char** argv) std::vector<Association>>(findAssociations->second); std::string well_known; - if (!get_well_known(name_owners, message.get_sender(), - well_known)) + if (!getWellKnown(name_owners, message.get_sender(), + well_known)) { return; } diff --git a/src/processing.cpp b/src/processing.cpp new file mode 100644 index 0000000..923e147 --- /dev/null +++ b/src/processing.cpp @@ -0,0 +1,23 @@ +#include "processing.hpp" + +#include <boost/algorithm/string/predicate.hpp> + +bool getWellKnown( + const boost::container::flat_map<std::string, std::string>& owners, + const std::string& request, std::string& wellKnown) +{ + // If it's already a well known name, just return + if (!boost::starts_with(request, ":")) + { + wellKnown = request; + return true; + } + + auto it = owners.find(request); + if (it == owners.end()) + { + return false; + } + wellKnown = it->second; + return true; +} diff --git a/src/processing.hpp b/src/processing.hpp new file mode 100644 index 0000000..27ef4c6 --- /dev/null +++ b/src/processing.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include <boost/container/flat_map.hpp> +#include <string> + +/** @brief Get well known name of input unique name + * + * If user passes in well known name then that will be returned. + * + * @param[in] owners - Current list of owners + * @param[in] request - The name to look up + * @param[out] wellKnown - The well known name if found + * + * @return True if well known name is found, false otherwise + */ +bool getWellKnown( + const boost::container::flat_map<std::string, std::string>& owners, + const std::string& request, std::string& wellKnown); diff --git a/src/test/Makefile.am.include b/src/test/Makefile.am.include new file mode 100644 index 0000000..a698753 --- /dev/null +++ b/src/test/Makefile.am.include @@ -0,0 +1,4 @@ +src_test_well_known_SOURCES = %reldir%/well_known.cpp src/processing.cpp + +check_PROGRAMS += \ + %reldir%/well_known diff --git a/src/test/well_known.cpp b/src/test/well_known.cpp new file mode 100644 index 0000000..c9f119c --- /dev/null +++ b/src/test/well_known.cpp @@ -0,0 +1,37 @@ +#include "src/processing.hpp" + +#include <gtest/gtest.h> + +// Verify if name does not start with a : that it is returned +TEST(WellKnownName, NameNotStartColon) +{ + boost::container::flat_map<std::string, std::string> owners; + const std::string request = "test"; + std::string well_known; + + EXPECT_TRUE(getWellKnown(owners, request, well_known)); + EXPECT_EQ(well_known, request); +} + +// Verify if name is not found, false is returned +TEST(WellKnownName, NameNotFound) +{ + boost::container::flat_map<std::string, std::string> owners; + const std::string request = ":test"; + std::string well_known; + + EXPECT_FALSE(getWellKnown(owners, request, well_known)); + EXPECT_TRUE(well_known.empty()); +} + +// Verify if name is found, true is returned and name is correct +TEST(WellKnownName, NameFound) +{ + boost::container::flat_map<std::string, std::string> owners; + const std::string request = ":1.25"; + std::string well_known; + + owners[request] = "test"; + EXPECT_TRUE(getWellKnown(owners, request, well_known)); + EXPECT_EQ(well_known, "test"); +} |