From efba76ffeef3c0ee1722edf3dcb52fa7f8117488 Mon Sep 17 00:00:00 2001 From: Timothy Werquin Date: Sun, 15 Oct 2023 23:30:53 +0200 Subject: [PATCH] Increase unistdpp test coverage, upload to codecov --- .github/workflows/build.yml | 11 ++++++++-- libs/unistdpp/socket.cpp | 6 +++++- test/CMakeLists.txt | 7 ++++++ test/unit/TestUnistdpp.cpp | 43 +++++++++++++++++++++++++++++-------- 4 files changed, 55 insertions(+), 12 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d123ed4..04336f8 100755 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -12,7 +12,7 @@ jobs: lfs: 'true' - name: Install Deps - run: sudo apt-get install -y ninja-build libsdl2-dev xxd + run: sudo apt-get install -y ninja-build libsdl2-dev xxd llvm - name: Configure CMake run: cmake --preset dev-host @@ -21,7 +21,14 @@ jobs: run: cmake --build build/host - name: Test - run: ctest --test-dir build/host --output-on-failure + run: cmake --build build/host --target make-coverage + + - name: Upload Coverage + uses: codecov/codecov-action@v3 + with: + files: ./build/host/report.lcov + fail_ci_if_error: true # optional (default = false) + verbose: true # optional (default = false) build: runs-on: ubuntu-latest diff --git a/libs/unistdpp/socket.cpp b/libs/unistdpp/socket.cpp index 4143c94..e89fb97 100644 --- a/libs/unistdpp/socket.cpp +++ b/libs/unistdpp/socket.cpp @@ -42,7 +42,11 @@ Address::fromHostPort(std::string_view host, int port) { addr->sin_port = htons(port); - if (inet_pton(AF_INET, host.data(), &addr->sin_addr) <= 0) { + int err = inet_pton(AF_INET, host.data(), &addr->sin_addr); + if (err == 0) { + return tl::unexpected(std::errc::invalid_argument); + } + if (err < 0) { return tl::unexpected(getErrno()); } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8a04810..8dda4ae 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -14,6 +14,13 @@ if (COVERAGE) --instr-profile merged.profdata -Xdemangler c++filt -Xdemangler -n "${CMAKE_BINARY_DIR}/test/unit/unit-tests" + COMMAND llvm-cov export + --format lcov + -ignore-filename-regex "'build/|stb_.*|test/unit/'" + --instr-profile merged.profdata + -Xdemangler c++filt -Xdemangler -n + "${CMAKE_BINARY_DIR}/test/unit/unit-tests" > "${CMAKE_BINARY_DIR}/report.lcov" + DEPENDS unit-tests ) endif() diff --git a/test/unit/TestUnistdpp.cpp b/test/unit/TestUnistdpp.cpp index fdf8d2c..b580a38 100644 --- a/test/unit/TestUnistdpp.cpp +++ b/test/unit/TestUnistdpp.cpp @@ -15,19 +15,21 @@ namespace Catch { template struct StringMaker> { static std::string convert(const Result& value) { - return value.has_value() ? std::to_string(*value) - : std::make_error_code(value.error()).message(); + return value.has_value() ? std::to_string(*value) : toString(value.error()); } }; template<> struct StringMaker> { static std::string convert(const Result& value) { - return value.has_value() ? "OK" - : std::make_error_code(value.error()).message(); + return value.has_value() ? "OK" : toString(value.error()); } }; } // namespace Catch +TEST_CASE("error", "[unistdpp]") { + REQUIRE(toString(std::errc::invalid_argument) == "Invalid argument"); +} + TEST_CASE("open", "[unistdpp]") { FD fd; SECTION("FD move") { @@ -36,7 +38,9 @@ TEST_CASE("open", "[unistdpp]") { fd = std::move(*res); REQUIRE(fd.isValid()); + REQUIRE(fd); REQUIRE_FALSE(res->isValid()); + REQUIRE_FALSE(*res); } FD fd2 = std::move(fd); @@ -80,13 +84,19 @@ TEST_CASE("pipe", "[unistdpp]") { auto [readFd, writeFd] = std::move(*pipe); - const char test[] = "testing"; + REQUIRE_FALSE(readFd.writeAll(12)); - REQUIRE(writeFd.writeAll(test, sizeof(test)).has_value()); + struct Test { + int a; + float b; + }; - char buf[512]; - REQUIRE(readFd.readAll(buf, sizeof(test)).has_value()); - REQUIRE(memcmp(buf, test, sizeof(test)) == 0); + REQUIRE(writeFd.writeAll(Test{ 55, 1.2f }).has_value()); + + auto res = readFd.readAll(); + REQUIRE(res.has_value()); + REQUIRE(res->a == 55); + REQUIRE(res->b == 1.2f); } TEST_CASE("poll", "[unistdpp]") { @@ -108,6 +118,13 @@ TEST_CASE("poll", "[unistdpp]") { REQUIRE(res.has_value()); REQUIRE(*res == 0); + pollfds.emplace_back(waitFor(write1, Wait::WRITE)); + res = unistdpp::poll(pollfds, nowait); + REQUIRE(res); + REQUIRE(*res == 1); + REQUIRE(canWrite(pollfds.back())); + pollfds.pop_back(); + const char test[] = "test"; REQUIRE(write1.writeAll(test, sizeof(test)).has_value()); @@ -152,10 +169,16 @@ TEST_CASE("TCP Socket", "[unistdpp]") { auto clientSock = unistdpp::socket(AF_INET, SOCK_STREAM, 0); REQUIRE(clientSock.has_value()); + REQUIRE_FALSE(clientSock->readAll().has_value()); + int yes = 1; REQUIRE(unistdpp::setsockopt( *serverSock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes))); + auto failAddr = Address::fromHostPort("foo.blah", 4444); + REQUIRE_FALSE(failAddr.has_value()); + REQUIRE(failAddr.error() == std::errc::invalid_argument); + constexpr auto port = 43224; auto serverAddr = Address::fromHostPort(INADDR_ANY, port); REQUIRE(unistdpp::bind(*serverSock, serverAddr)); @@ -217,6 +240,8 @@ TEST_CASE("Unix Socket", "[unistdpp]") { REQUIRE(*size == sizeof(testMsg)); REQUIRE(memcmp(buf, testMsg, sizeof(testMsg)) == 0); + REQUIRE(recvAddr.type() == AF_UNIX); + REQUIRE( unistdpp::sendto(*serverSock, testMsg, sizeof(testMsg), 0, &recvAddr));