From afb7a48046142fe666eda64338f89677e79db705 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Thu, 21 Jun 2018 10:40:21 +0200 Subject: Fix unit test for get_library_version for release commits `git describe` may return a tag name or --g, where is the number of commits since the last tag and is the hash of the current commit. The current test case only considers the latter case. This patch adds a regular expression to handle both cases. --- unittest/test_offline.cc | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/unittest/test_offline.cc b/unittest/test_offline.cc index aad875f..6f52a61 100644 --- a/unittest/test_offline.cc +++ b/unittest/test_offline.cc @@ -22,6 +22,8 @@ #include "catch.hpp" #include #include +#include +#include #include "../NK_C_API.h" using namespace nitrokey::proto; @@ -167,7 +169,14 @@ TEST_CASE("Test version getter", "[fast]") { REQUIRE(nitrokey::get_minor_library_version() >= 3u); const char *library_version = nitrokey::get_library_version(); REQUIRE(library_version != nullptr); + + // The library version has to match the pattern returned by git describe: + // v. or v.--g, where is the number + // of commits since the last tag, and is the hash of the current + // commit. (This assumes that all tags have the name v..) std::string s = library_version; - REQUIRE(s.length() >= 8); - REQUIRE(s.find("g") != std::string::npos); + std::string version("v[0-9]+\\.[0-9]+"); + std::string git_suffix("-[0-9]+-g[0-9a-z]+"); + std::regex pattern(version + "(" + git_suffix + "|)"); + REQUIRE(std::regex_match(s, pattern)); } -- cgit v1.2.1