Skip to content

Commit

Permalink
Increase unistdpp test coverage, upload to codecov
Browse files Browse the repository at this point in the history
  • Loading branch information
timower committed Oct 15, 2023
1 parent 6abeb4c commit efba76f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion libs/unistdpp/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
7 changes: 7 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
43 changes: 34 additions & 9 deletions test/unit/TestUnistdpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ namespace Catch {
template<typename T>
struct StringMaker<Result<T>> {
static std::string convert(const Result<T>& 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<Result<void>> {
static std::string convert(const Result<void>& 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") {
Expand All @@ -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);
Expand Down Expand Up @@ -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<Test>();
REQUIRE(res.has_value());
REQUIRE(res->a == 55);
REQUIRE(res->b == 1.2f);
}

TEST_CASE("poll", "[unistdpp]") {
Expand All @@ -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());

Expand Down Expand Up @@ -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<char>().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));
Expand Down Expand Up @@ -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));

Expand Down

0 comments on commit efba76f

Please sign in to comment.