2023-08-04 18:44:56 +03:00
|
|
|
|
|
|
|
#include "config.hh"
|
|
|
|
#include "args.hh"
|
|
|
|
#include "nix_api_util.h"
|
|
|
|
#include "nix_api_util_internal.h"
|
2023-12-14 21:14:58 +02:00
|
|
|
#include "tests/nix_api_util.hh"
|
2023-08-04 18:44:56 +03:00
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
namespace nixC {
|
|
|
|
|
2024-02-25 22:21:05 +02:00
|
|
|
TEST_F(nix_api_util_context, nix_context_error)
|
|
|
|
{
|
2023-08-04 18:44:56 +03:00
|
|
|
std::string err_msg_ref;
|
|
|
|
try {
|
|
|
|
throw nix::Error("testing error");
|
2024-02-25 22:21:05 +02:00
|
|
|
} catch (nix::Error & e) {
|
2023-08-04 18:44:56 +03:00
|
|
|
err_msg_ref = e.what();
|
|
|
|
nix_context_error(ctx);
|
|
|
|
}
|
|
|
|
ASSERT_EQ(ctx->last_err_code, NIX_ERR_NIX_ERROR);
|
|
|
|
ASSERT_EQ(ctx->name, "nix::Error");
|
|
|
|
ASSERT_EQ(*ctx->last_err, err_msg_ref);
|
|
|
|
ASSERT_EQ(ctx->info->msg.str(), "testing error");
|
|
|
|
|
|
|
|
try {
|
|
|
|
throw std::runtime_error("testing exception");
|
2024-02-25 22:21:05 +02:00
|
|
|
} catch (std::exception & e) {
|
2023-08-04 18:44:56 +03:00
|
|
|
err_msg_ref = e.what();
|
|
|
|
nix_context_error(ctx);
|
|
|
|
}
|
|
|
|
ASSERT_EQ(ctx->last_err_code, NIX_ERR_UNKNOWN);
|
|
|
|
ASSERT_EQ(*ctx->last_err, err_msg_ref);
|
|
|
|
}
|
|
|
|
|
2024-02-25 22:21:05 +02:00
|
|
|
TEST_F(nix_api_util_context, nix_set_err_msg)
|
|
|
|
{
|
2023-08-04 18:44:56 +03:00
|
|
|
ASSERT_EQ(ctx->last_err_code, NIX_OK);
|
|
|
|
nix_set_err_msg(ctx, NIX_ERR_UNKNOWN, "unknown test error");
|
|
|
|
ASSERT_EQ(ctx->last_err_code, NIX_ERR_UNKNOWN);
|
|
|
|
ASSERT_EQ(*ctx->last_err, "unknown test error");
|
|
|
|
}
|
|
|
|
|
2024-02-25 22:21:05 +02:00
|
|
|
TEST(nix_api_util, nix_version_get)
|
|
|
|
{
|
2023-08-04 18:44:56 +03:00
|
|
|
ASSERT_EQ(std::string(nix_version_get()), PACKAGE_VERSION);
|
|
|
|
}
|
|
|
|
|
2023-12-14 21:14:58 +02:00
|
|
|
struct MySettings : nix::Config
|
|
|
|
{
|
|
|
|
nix::Setting<std::string> 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);
|
2023-08-04 18:44:56 +03:00
|
|
|
}
|
|
|
|
|
2023-12-14 21:14:58 +02:00
|
|
|
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);
|
2023-08-04 18:44:56 +03:00
|
|
|
}
|
|
|
|
|
2024-02-25 22:21:05 +02:00
|
|
|
TEST_F(nix_api_util_context, nix_err_msg)
|
|
|
|
{
|
2023-08-04 18:44:56 +03:00
|
|
|
// no error
|
2024-02-25 22:21:05 +02:00
|
|
|
EXPECT_THROW(nix_err_msg(nullptr, ctx, NULL), nix::Error);
|
2023-08-04 18:44:56 +03:00
|
|
|
|
|
|
|
// set error
|
|
|
|
nix_set_err_msg(ctx, NIX_ERR_UNKNOWN, "unknown test error");
|
|
|
|
|
|
|
|
// basic usage
|
|
|
|
std::string err_msg = nix_err_msg(NULL, ctx, NULL);
|
|
|
|
ASSERT_EQ(err_msg, "unknown test error");
|
|
|
|
|
|
|
|
// advanced usage
|
|
|
|
unsigned int sz;
|
2024-02-25 22:21:05 +02:00
|
|
|
err_msg = nix_err_msg(nix_c_context_create(), ctx, &sz);
|
2023-08-04 18:44:56 +03:00
|
|
|
ASSERT_EQ(sz, err_msg.size());
|
|
|
|
}
|
|
|
|
|
2024-02-25 22:21:05 +02:00
|
|
|
TEST_F(nix_api_util_context, nix_err_info_msg)
|
|
|
|
{
|
2023-08-04 18:44:56 +03:00
|
|
|
// no error
|
|
|
|
EXPECT_THROW(nix_err_info_msg(NULL, ctx, NULL, 256), nix::Error);
|
|
|
|
|
|
|
|
try {
|
|
|
|
throw nix::Error("testing error");
|
2024-02-25 22:21:05 +02:00
|
|
|
} catch (...) {
|
2023-08-04 18:44:56 +03:00
|
|
|
nix_context_error(ctx);
|
|
|
|
}
|
|
|
|
char buf[256];
|
2024-02-25 22:21:05 +02:00
|
|
|
nix_err_info_msg(nix_c_context_create(), ctx, buf, 256);
|
2023-08-04 18:44:56 +03:00
|
|
|
ASSERT_EQ(std::string(buf), "testing error");
|
|
|
|
|
|
|
|
// should overflow
|
|
|
|
EXPECT_THROW(nix_err_info_msg(NULL, ctx, buf, 1), nix::Error);
|
|
|
|
}
|
|
|
|
|
2024-02-25 22:21:05 +02:00
|
|
|
TEST_F(nix_api_util_context, nix_err_name)
|
|
|
|
{
|
2023-08-04 18:44:56 +03:00
|
|
|
// no error
|
|
|
|
EXPECT_THROW(nix_err_name(NULL, ctx, NULL, 256), nix::Error);
|
|
|
|
|
|
|
|
std::string err_msg_ref;
|
|
|
|
try {
|
|
|
|
throw nix::Error("testing error");
|
2024-02-25 22:21:05 +02:00
|
|
|
} catch (...) {
|
2023-08-04 18:44:56 +03:00
|
|
|
nix_context_error(ctx);
|
|
|
|
}
|
|
|
|
char err_name[32];
|
2024-02-25 22:21:05 +02:00
|
|
|
nix_err_name(nix_c_context_create(), ctx, err_name, 32);
|
2023-08-04 18:44:56 +03:00
|
|
|
ASSERT_EQ(std::string(err_name), "nix::Error");
|
|
|
|
|
|
|
|
// overflow
|
|
|
|
EXPECT_THROW(nix_err_name(NULL, ctx, err_name, 1), nix::Error);
|
|
|
|
}
|
|
|
|
|
2024-02-25 22:21:05 +02:00
|
|
|
TEST_F(nix_api_util_context, nix_err_code)
|
|
|
|
{
|
2023-08-04 18:44:56 +03:00
|
|
|
ASSERT_EQ(nix_err_code(ctx), NIX_OK);
|
|
|
|
nix_set_err_msg(ctx, NIX_ERR_UNKNOWN, "unknown test error");
|
|
|
|
ASSERT_EQ(nix_err_code(ctx), NIX_ERR_UNKNOWN);
|
|
|
|
}
|
|
|
|
}
|