From 41f1669deab98db5c954d9bfff174dfc8f20bea3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Luis=20Lafuente?= Date: Thu, 14 Dec 2023 20:14:58 +0100 Subject: [PATCH] C API: add tests for libutil and libstore --- src/libstore/c/nix_api_store.cc | 5 -- src/libstore/c/nix_api_store_internal.h | 6 ++ tests/unit/libstore/local.mk | 2 +- tests/unit/libstore/nix_api_store.cc | 70 +++++++++++++++++++ .../libutil-support/tests/nix_api_util.hh | 25 +++++++ tests/unit/libutil/nix_api_util.cc | 50 ++++++++----- 6 files changed, 133 insertions(+), 25 deletions(-) create mode 100644 tests/unit/libstore/nix_api_store.cc create mode 100644 tests/unit/libutil-support/tests/nix_api_util.hh diff --git a/src/libstore/c/nix_api_store.cc b/src/libstore/c/nix_api_store.cc index 496b20534..4aff815ff 100644 --- a/src/libstore/c/nix_api_store.cc +++ b/src/libstore/c/nix_api_store.cc @@ -8,11 +8,6 @@ #include "globals.hh" -struct StorePath -{ - nix::StorePath path; -}; - nix_err nix_libstore_init(nix_c_context * context) { if (context) diff --git a/src/libstore/c/nix_api_store_internal.h b/src/libstore/c/nix_api_store_internal.h index ef5edc788..13db0c07c 100644 --- a/src/libstore/c/nix_api_store_internal.h +++ b/src/libstore/c/nix_api_store_internal.h @@ -6,4 +6,10 @@ struct Store { nix::ref ptr; }; + +struct StorePath +{ + nix::StorePath path; +}; + #endif diff --git a/tests/unit/libstore/local.mk b/tests/unit/libstore/local.mk index 960dece89..fe1254487 100644 --- a/tests/unit/libstore/local.mk +++ b/tests/unit/libstore/local.mk @@ -26,6 +26,6 @@ libstore-tests_CXXFLAGS += $(libstore-tests_EXTRA_INCLUDES) libstore-tests_LIBS = \ libstore-test-support libutil-test-support \ - libstore libutil + libstore libstorec libutil libutilc libstore-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) diff --git a/tests/unit/libstore/nix_api_store.cc b/tests/unit/libstore/nix_api_store.cc new file mode 100644 index 000000000..3fe55ae93 --- /dev/null +++ b/tests/unit/libstore/nix_api_store.cc @@ -0,0 +1,70 @@ +#include "nix_api_util.h" +#include "nix_api_util_internal.h" +#include "nix_api_store.h" +#include "nix_api_store_internal.h" +#include "tests/nix_api_util.hh" + +#define STORE_DIR "/nix/store/" +#define HASH_PART "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q" +const char * validPath = STORE_DIR HASH_PART "-x"; + +namespace nixC { + +class nix_api_store_test : public nix_api_util_context +{ +public: + void SetUp() override + { + nix_api_util_context::SetUp(); + nix_libstore_init(ctx); + store = nix_store_open(ctx, "dummy://", NULL); + }; + void TearDown() override + { + nix_store_unref(store); + nix_api_util_context::TearDown(); + } + + Store * store; +}; + +TEST_F(nix_api_util_context, nix_libstore_init) +{ + auto ret = nix_libstore_init(ctx); + ASSERT_EQ(NIX_OK, ret); +} + +TEST_F(nix_api_store_test, nix_store_get_uri) +{ + char value[256]; + auto ret = nix_store_get_uri(ctx, store, value, 256); + ASSERT_EQ(NIX_OK, ret); + ASSERT_STREQ("dummy", value); +} + +TEST_F(nix_api_store_test, InvalidPathFails) +{ + nix_store_parse_path(ctx, store, "invalid-path"); + ASSERT_EQ(ctx->last_err_code, NIX_ERR_NIX_ERROR); +} + +TEST_F(nix_api_store_test, ReturnsValidStorePath) +{ + StorePath * result = nix_store_parse_path(ctx, store, validPath); + ASSERT_NE(result, nullptr); + ASSERT_STREQ("x", result->path.name().data()); + ASSERT_STREQ(HASH_PART "-x", result->path.to_string().data()); +} + +TEST_F(nix_api_store_test, SetsLastErrCodeToNixOk) +{ + nix_store_parse_path(ctx, store, validPath); + ASSERT_EQ(ctx->last_err_code, NIX_OK); +} + +TEST_F(nix_api_store_test, DoesNotCrashWhenContextIsNull) +{ + ASSERT_NO_THROW(nix_store_parse_path(nullptr, store, validPath)); +} + +} diff --git a/tests/unit/libutil-support/tests/nix_api_util.hh b/tests/unit/libutil-support/tests/nix_api_util.hh new file mode 100644 index 000000000..f2ee58da2 --- /dev/null +++ b/tests/unit/libutil-support/tests/nix_api_util.hh @@ -0,0 +1,25 @@ +#pragma once +///@file +#include "nix_api_util.h" + +#include + + +class nix_api_util_context : public ::testing::Test +{ +protected: + static void SetUpTestSuite() + { + nix_libutil_init(NULL); + } + void SetUp() override + { + ctx = nix_c_context_create(); + }; + void TearDown() override + { + nix_c_context_free(ctx); + ctx = nullptr; + } + nix_c_context * ctx; +}; diff --git a/tests/unit/libutil/nix_api_util.cc b/tests/unit/libutil/nix_api_util.cc index 26353fe84..8cc2b6616 100644 --- a/tests/unit/libutil/nix_api_util.cc +++ b/tests/unit/libutil/nix_api_util.cc @@ -3,26 +3,12 @@ #include "args.hh" #include "nix_api_util.h" #include "nix_api_util_internal.h" +#include "tests/nix_api_util.hh" #include namespace nixC { -class nix_api_util_context : public ::testing::Test { -protected: - static void SetUpTestSuite() { - nix_libutil_init(NULL); - } - void SetUp() override { - ctx = nix_c_context_create(); - }; - void TearDown() override { - nix_c_context_free(ctx); - ctx = nullptr; - } - nix_c_context* ctx; -}; - TEST_F(nix_api_util_context, nix_context_error) { std::string err_msg_ref; try { @@ -57,12 +43,38 @@ TEST(nix_api_util, nix_version_get) { ASSERT_EQ(std::string(nix_version_get()), PACKAGE_VERSION); } -TEST_F(nix_api_util_context, nix_setting_get) { - // todo +struct MySettings : nix::Config +{ + nix::Setting settingSet{this, "empty", "setting-name", "Description"}; +}; + +MySettings mySettings; +static nix::GlobalConfig::Register rs(&mySettings); + +TEST_F(nix_api_util_context, nix_setting_get) +{ + ASSERT_EQ(ctx->last_err_code, NIX_OK); + char value[256]; + nix_err result = nix_setting_get(ctx, "invalid-key", value, 256); + ASSERT_EQ(result, NIX_ERR_KEY); + + result = nix_setting_get(ctx, "setting-name", value, 256); + ASSERT_EQ(result, NIX_OK); + ASSERT_STREQ("empty", value); } -TEST_F(nix_api_util_context, nix_setting_set) { - // todo +TEST_F(nix_api_util_context, nix_setting_set) +{ + nix_err result = nix_setting_set(ctx, "invalid-key", "new-value"); + ASSERT_EQ(result, NIX_ERR_KEY); + + result = nix_setting_set(ctx, "setting-name", "new-value"); + ASSERT_EQ(result, NIX_OK); + + char value[256]; + result = nix_setting_get(ctx, "setting-name", value, 256); + ASSERT_EQ(result, NIX_OK); + ASSERT_STREQ("new-value", value); } TEST_F(nix_api_util_context, nix_err_msg) {