M5: moderation, permissions, channel CRUD, in-app account management
- Server-side moderation & permissions (kick/ban/move/server-mute, channel CRUD). - Database schema v2: channels, bans; BLAKE2b channel passwords, Argon2id accounts. - C ABI additions and client-side handling (vc_kick_user, vc_ban_user, vc_set_permission, vc_set_server_mute, vc_move_user, vc_create/edit/delete_channel, vc_create/reset/delete/list_account). - vccli flags for all M5 operations plus --username/--password auth. - Four new tests covering permissions, kick/ban/move/mute, admin accounts, channel CRUD. - Docs: protocol.md envelope updates, security.md channel-password hashing, PROGRESS.md.
This commit is contained in:
@@ -113,4 +113,36 @@ if(VOICECAT_USE_VCPKG_DEPS)
|
||||
target_include_directories(test_tofu_flow PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
|
||||
add_test(NAME tofu_flow COMMAND test_tofu_flow)
|
||||
set_tests_properties(tofu_flow PROPERTIES TIMEOUT 90)
|
||||
|
||||
# M5 Phase 1: permission enforcement + SetPermissionRequest + channel create.
|
||||
add_executable(test_m5_permissions test_m5_permissions.cpp)
|
||||
target_link_libraries(test_m5_permissions PRIVATE voicecat::server)
|
||||
target_compile_features(test_m5_permissions PRIVATE cxx_std_20)
|
||||
target_include_directories(test_m5_permissions PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
|
||||
add_test(NAME m5_permissions COMMAND test_m5_permissions)
|
||||
set_tests_properties(m5_permissions PROPERTIES TIMEOUT 60)
|
||||
|
||||
# M5 Phase 2: kick/ban/move/server-mute.
|
||||
add_executable(test_m5_kick_ban_move_mute test_m5_kick_ban_move_mute.cpp)
|
||||
target_link_libraries(test_m5_kick_ban_move_mute PRIVATE voicecat::server)
|
||||
target_compile_features(test_m5_kick_ban_move_mute PRIVATE cxx_std_20)
|
||||
target_include_directories(test_m5_kick_ban_move_mute PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
|
||||
add_test(NAME m5_kick_ban_move_mute COMMAND test_m5_kick_ban_move_mute)
|
||||
set_tests_properties(m5_kick_ban_move_mute PROPERTIES TIMEOUT 90)
|
||||
|
||||
# M5 Phase 3: in-app admin account management.
|
||||
add_executable(test_m5_admin_accounts test_m5_admin_accounts.cpp)
|
||||
target_link_libraries(test_m5_admin_accounts PRIVATE voicecat::server)
|
||||
target_compile_features(test_m5_admin_accounts PRIVATE cxx_std_20)
|
||||
target_include_directories(test_m5_admin_accounts PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
|
||||
add_test(NAME m5_admin_accounts COMMAND test_m5_admin_accounts)
|
||||
set_tests_properties(m5_admin_accounts PROPERTIES TIMEOUT 90)
|
||||
|
||||
# M5 Phase 4: channel CRUD & password enforcement.
|
||||
add_executable(test_m5_channel_crud test_m5_channel_crud.cpp)
|
||||
target_link_libraries(test_m5_channel_crud PRIVATE voicecat::server)
|
||||
target_compile_features(test_m5_channel_crud PRIVATE cxx_std_20)
|
||||
target_include_directories(test_m5_channel_crud PRIVATE ${VC_TEST_INTERNAL_INCLUDES})
|
||||
add_test(NAME m5_channel_crud COMMAND test_m5_channel_crud)
|
||||
set_tests_properties(m5_channel_crud PROPERTIES TIMEOUT 90)
|
||||
endif()
|
||||
|
||||
276
tests/test_m5_admin_accounts.cpp
Normal file
276
tests/test_m5_admin_accounts.cpp
Normal file
@@ -0,0 +1,276 @@
|
||||
/*
|
||||
* test_m5_admin_accounts — Phase 3 of M5: in-app account management over the wire.
|
||||
*
|
||||
* Verifies:
|
||||
* - admin can create an account via vc_create_account.
|
||||
* - the new account can authenticate.
|
||||
* - admin can reset the password.
|
||||
* - the new account can authenticate with the new password.
|
||||
* - admin can list accounts and sees the new entry.
|
||||
* - admin can delete the account.
|
||||
* - the deleted account can no longer authenticate.
|
||||
*/
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
#ifdef VOICECAT_HAS_NET
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <filesystem>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "voicecat.h"
|
||||
#include "server.h"
|
||||
#include "db.h"
|
||||
|
||||
struct EventStore {
|
||||
std::mutex mu;
|
||||
std::condition_variable cv;
|
||||
|
||||
bool auth_ok{false};
|
||||
bool auth_done{false};
|
||||
uint32_t self_user_id{0};
|
||||
bool channel_list_received{false};
|
||||
bool disconnected{false};
|
||||
|
||||
struct ResultAck {
|
||||
bool ok{false};
|
||||
uint32_t code{0};
|
||||
std::string message;
|
||||
};
|
||||
std::vector<ResultAck> generic_results;
|
||||
|
||||
bool account_list_received{false};
|
||||
|
||||
vc_client* client{nullptr};
|
||||
const char* label{nullptr};
|
||||
};
|
||||
|
||||
static void on_event(void* user, const vc_event* ev) {
|
||||
auto* s = static_cast<EventStore*>(user);
|
||||
std::lock_guard lk(s->mu);
|
||||
switch (ev->type) {
|
||||
case VC_EVENT_SERVER_IDENTITY:
|
||||
vc_confirm_server_identity(s->client, 1);
|
||||
break;
|
||||
case VC_EVENT_AUTH_RESULT:
|
||||
s->auth_ok = (ev->result == VC_OK);
|
||||
s->auth_done = true;
|
||||
s->self_user_id = ev->user_id;
|
||||
break;
|
||||
case VC_EVENT_CHANNEL_LIST:
|
||||
s->channel_list_received = true;
|
||||
break;
|
||||
case VC_EVENT_GENERIC_RESULT: {
|
||||
EventStore::ResultAck gr;
|
||||
gr.ok = (ev->result == VC_OK);
|
||||
gr.code = ev->u32a;
|
||||
gr.message = ev->text ? ev->text : "";
|
||||
s->generic_results.push_back(std::move(gr));
|
||||
break;
|
||||
}
|
||||
case VC_EVENT_ACCOUNT_LIST:
|
||||
s->account_list_received = true;
|
||||
break;
|
||||
case VC_EVENT_DISCONNECTED:
|
||||
s->disconnected = true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
s->cv.notify_all();
|
||||
}
|
||||
|
||||
template<typename Pred>
|
||||
static bool wait_for(EventStore& s, Pred pred, int timeout_ms) {
|
||||
auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms);
|
||||
std::unique_lock lk(s.mu);
|
||||
return s.cv.wait_until(lk, deadline, [&] { return pred(s); });
|
||||
}
|
||||
|
||||
static int g_failures = 0;
|
||||
#define CHECK(cond) \
|
||||
do { \
|
||||
if (!(cond)) { \
|
||||
std::printf("FAIL: %s (%s:%d)\n", #cond, __FILE__, __LINE__); \
|
||||
++g_failures; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
static bool wait_generic(EventStore& s, int timeout_ms) {
|
||||
return wait_for(s, [](EventStore& st) { return !st.generic_results.empty(); }, timeout_ms);
|
||||
}
|
||||
|
||||
static bool last_generic_ok(EventStore& s) {
|
||||
std::lock_guard lk(s.mu);
|
||||
return !s.generic_results.empty() && s.generic_results.back().ok;
|
||||
}
|
||||
|
||||
static void reset_generic(EventStore& s) {
|
||||
std::lock_guard lk(s.mu);
|
||||
s.generic_results.clear();
|
||||
}
|
||||
|
||||
int main() {
|
||||
auto tmp = std::filesystem::temp_directory_path() /
|
||||
("vctest_m5_acct_" + std::to_string(
|
||||
std::chrono::steady_clock::now().time_since_epoch().count()));
|
||||
std::filesystem::create_directories(tmp);
|
||||
std::string data_dir = tmp.string();
|
||||
|
||||
{
|
||||
voicecat::server::Database db(data_dir + "/voicecat.db");
|
||||
std::string err;
|
||||
if (!db.open(err)) { std::printf("FAIL: db.open: %s\n", err.c_str()); return 1; }
|
||||
if (!db.create_account("admin", "admin-pass", true, err)) {
|
||||
std::printf("FAIL: create admin: %s\n", err.c_str());
|
||||
std::filesystem::remove_all(tmp);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
std::atomic<uint16_t> bound_port{0};
|
||||
std::mutex ready_mu;
|
||||
std::condition_variable ready_cv;
|
||||
bool ready{false};
|
||||
|
||||
voicecat::server::Config cfg;
|
||||
cfg.data_dir = data_dir;
|
||||
cfg.bind_port = 0;
|
||||
cfg.server_name = "VoiceCat-M5-Acct";
|
||||
cfg.allow_guests = false;
|
||||
cfg.on_ready = [&](uint16_t p) {
|
||||
bound_port.store(p);
|
||||
{ std::lock_guard lk(ready_mu); ready = true; }
|
||||
ready_cv.notify_all();
|
||||
};
|
||||
|
||||
voicecat::server::Server server(cfg);
|
||||
std::thread server_thread([&] { server.run(); });
|
||||
|
||||
{
|
||||
std::unique_lock lk(ready_mu);
|
||||
if (!ready_cv.wait_for(lk, std::chrono::seconds(10), [&] { return ready; })) {
|
||||
std::printf("FAIL: server did not become ready\n");
|
||||
server.stop();
|
||||
server_thread.join();
|
||||
std::filesystem::remove_all(tmp);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
uint16_t port = bound_port.load();
|
||||
|
||||
EventStore evAdmin;
|
||||
evAdmin.label = "admin";
|
||||
vc_callbacks cbA{on_event, nullptr, &evAdmin};
|
||||
vc_config cfgA{"test-admin", "0.1", VC_LOG_OFF};
|
||||
vc_client* admin = vc_client_create(&cfgA, cbA);
|
||||
evAdmin.client = admin;
|
||||
CHECK(admin != nullptr);
|
||||
CHECK(vc_connect(admin, "127.0.0.1", port) == VC_OK);
|
||||
CHECK(vc_authenticate_user(admin, "admin", "admin-pass") == VC_OK);
|
||||
CHECK(wait_for(evAdmin, [](EventStore& s){ return s.auth_ok; }, 20000));
|
||||
CHECK(wait_for(evAdmin, [](EventStore& s){ return s.channel_list_received; }, 3000));
|
||||
|
||||
// Create charlie.
|
||||
reset_generic(evAdmin);
|
||||
CHECK(vc_create_account(admin, "charlie", "charlie-pass") == VC_OK);
|
||||
CHECK(wait_generic(evAdmin, 5000));
|
||||
CHECK(last_generic_ok(evAdmin));
|
||||
|
||||
// Charlie logs in.
|
||||
{
|
||||
EventStore evCharlie;
|
||||
evCharlie.label = "charlie";
|
||||
vc_callbacks cb{on_event, nullptr, &evCharlie};
|
||||
vc_config cfgC{"test-charlie", "0.1", VC_LOG_OFF};
|
||||
vc_client* charlie = vc_client_create(&cfgC, cb);
|
||||
evCharlie.client = charlie;
|
||||
CHECK(charlie != nullptr);
|
||||
CHECK(vc_connect(charlie, "127.0.0.1", port) == VC_OK);
|
||||
CHECK(vc_authenticate_user(charlie, "charlie", "charlie-pass") == VC_OK);
|
||||
CHECK(wait_for(evCharlie, [](EventStore& s){ return s.auth_ok; }, 20000));
|
||||
CHECK(evCharlie.auth_ok);
|
||||
vc_disconnect(charlie);
|
||||
vc_client_destroy(charlie);
|
||||
}
|
||||
|
||||
// Reset charlie's password.
|
||||
reset_generic(evAdmin);
|
||||
CHECK(vc_reset_password(admin, "charlie", "new-pass") == VC_OK);
|
||||
CHECK(wait_generic(evAdmin, 5000));
|
||||
CHECK(last_generic_ok(evAdmin));
|
||||
|
||||
// Charlie logs in with new password.
|
||||
{
|
||||
EventStore evCharlie;
|
||||
evCharlie.label = "charlie2";
|
||||
vc_callbacks cb{on_event, nullptr, &evCharlie};
|
||||
vc_config cfgC{"test-charlie2", "0.1", VC_LOG_OFF};
|
||||
vc_client* charlie = vc_client_create(&cfgC, cb);
|
||||
evCharlie.client = charlie;
|
||||
CHECK(charlie != nullptr);
|
||||
CHECK(vc_connect(charlie, "127.0.0.1", port) == VC_OK);
|
||||
CHECK(vc_authenticate_user(charlie, "charlie", "new-pass") == VC_OK);
|
||||
CHECK(wait_for(evCharlie, [](EventStore& s){ return s.auth_ok; }, 20000));
|
||||
CHECK(evCharlie.auth_ok);
|
||||
vc_disconnect(charlie);
|
||||
vc_client_destroy(charlie);
|
||||
}
|
||||
|
||||
// List accounts.
|
||||
reset_generic(evAdmin);
|
||||
evAdmin.account_list_received = false;
|
||||
CHECK(vc_list_accounts(admin) == VC_OK);
|
||||
CHECK(wait_for(evAdmin, [](EventStore& s){ return s.account_list_received; }, 3000));
|
||||
|
||||
// Delete charlie.
|
||||
reset_generic(evAdmin);
|
||||
CHECK(vc_delete_account(admin, "charlie") == VC_OK);
|
||||
CHECK(wait_generic(evAdmin, 3000));
|
||||
CHECK(last_generic_ok(evAdmin));
|
||||
|
||||
// Charlie cannot log in anymore.
|
||||
{
|
||||
EventStore evCharlie;
|
||||
evCharlie.label = "charlie3";
|
||||
vc_callbacks cb{on_event, nullptr, &evCharlie};
|
||||
vc_config cfgC{"test-charlie3", "0.1", VC_LOG_OFF};
|
||||
vc_client* charlie = vc_client_create(&cfgC, cb);
|
||||
evCharlie.client = charlie;
|
||||
CHECK(charlie != nullptr);
|
||||
CHECK(vc_connect(charlie, "127.0.0.1", port) == VC_OK);
|
||||
CHECK(vc_authenticate_user(charlie, "charlie", "new-pass") == VC_OK);
|
||||
CHECK(wait_for(evCharlie, [](EventStore& s){ return s.auth_done || s.disconnected; }, 20000));
|
||||
CHECK(!evCharlie.auth_ok);
|
||||
vc_disconnect(charlie);
|
||||
vc_client_destroy(charlie);
|
||||
}
|
||||
|
||||
vc_disconnect(admin);
|
||||
vc_client_destroy(admin);
|
||||
server.stop();
|
||||
server_thread.join();
|
||||
std::filesystem::remove_all(tmp);
|
||||
|
||||
if (g_failures == 0) {
|
||||
std::printf("m5_admin_accounts: all checks passed\n");
|
||||
return 0;
|
||||
}
|
||||
std::printf("m5_admin_accounts: %d failure(s)\n", g_failures);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#else // !VOICECAT_HAS_NET
|
||||
|
||||
int main() {
|
||||
std::printf("m5_admin_accounts: SKIP (VOICECAT_HAS_NET not defined)\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif // VOICECAT_HAS_NET
|
||||
319
tests/test_m5_channel_crud.cpp
Normal file
319
tests/test_m5_channel_crud.cpp
Normal file
@@ -0,0 +1,319 @@
|
||||
/*
|
||||
* test_m5_channel_crud — Phase 4 of M5: channel CRUD & password enforcement.
|
||||
*
|
||||
* Verifies:
|
||||
* - admin can create a password-protected channel.
|
||||
* - normal user cannot join without the password.
|
||||
* - normal user can join with the password.
|
||||
* - admin can edit the channel name.
|
||||
* - both clients receive VC_EVENT_CHANNEL_LIST after the update.
|
||||
* - admin can delete the channel; remaining users are moved to Lobby.
|
||||
*/
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
#ifdef VOICECAT_HAS_NET
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <filesystem>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "voicecat.h"
|
||||
#include "server.h"
|
||||
#include "db.h"
|
||||
|
||||
struct EventStore {
|
||||
std::mutex mu;
|
||||
std::condition_variable cv;
|
||||
|
||||
bool auth_ok{false};
|
||||
uint32_t self_user_id{0};
|
||||
int channel_list_count{0};
|
||||
bool disconnected{false};
|
||||
|
||||
struct ResultAck {
|
||||
bool ok{false};
|
||||
uint32_t code{0};
|
||||
std::string message;
|
||||
};
|
||||
std::vector<ResultAck> generic_results;
|
||||
|
||||
vc_client* client{nullptr};
|
||||
const char* label{nullptr};
|
||||
};
|
||||
|
||||
static void on_event(void* user, const vc_event* ev) {
|
||||
auto* s = static_cast<EventStore*>(user);
|
||||
std::lock_guard lk(s->mu);
|
||||
switch (ev->type) {
|
||||
case VC_EVENT_SERVER_IDENTITY:
|
||||
vc_confirm_server_identity(s->client, 1);
|
||||
break;
|
||||
case VC_EVENT_AUTH_RESULT:
|
||||
s->auth_ok = (ev->result == VC_OK);
|
||||
s->self_user_id = ev->user_id;
|
||||
break;
|
||||
case VC_EVENT_CHANNEL_LIST:
|
||||
++s->channel_list_count;
|
||||
break;
|
||||
case VC_EVENT_JOIN_RESULT: {
|
||||
EventStore::ResultAck gr;
|
||||
gr.ok = (ev->result == VC_OK);
|
||||
gr.code = ev->channel_id;
|
||||
gr.message = ev->text ? ev->text : "";
|
||||
s->generic_results.push_back(std::move(gr));
|
||||
break;
|
||||
}
|
||||
case VC_EVENT_GENERIC_RESULT: {
|
||||
EventStore::ResultAck gr;
|
||||
gr.ok = (ev->result == VC_OK);
|
||||
gr.code = ev->u32a;
|
||||
gr.message = ev->text ? ev->text : "";
|
||||
s->generic_results.push_back(std::move(gr));
|
||||
break;
|
||||
}
|
||||
case VC_EVENT_DISCONNECTED:
|
||||
s->disconnected = true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
s->cv.notify_all();
|
||||
}
|
||||
|
||||
template<typename Pred>
|
||||
static bool wait_for(EventStore& s, Pred pred, int timeout_ms) {
|
||||
auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms);
|
||||
std::unique_lock lk(s.mu);
|
||||
return s.cv.wait_until(lk, deadline, [&] { return pred(s); });
|
||||
}
|
||||
|
||||
static int g_failures = 0;
|
||||
#define CHECK(cond) \
|
||||
do { \
|
||||
if (!(cond)) { \
|
||||
std::printf("FAIL: %s (%s:%d)\n", #cond, __FILE__, __LINE__); \
|
||||
++g_failures; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
static bool wait_generic(EventStore& s, int timeout_ms) {
|
||||
return wait_for(s, [](EventStore& st) { return !st.generic_results.empty(); }, timeout_ms);
|
||||
}
|
||||
|
||||
static bool last_generic_ok(EventStore& s) {
|
||||
std::lock_guard lk(s.mu);
|
||||
return !s.generic_results.empty() && s.generic_results.back().ok;
|
||||
}
|
||||
|
||||
static void reset_generic(EventStore& s) {
|
||||
std::lock_guard lk(s.mu);
|
||||
s.generic_results.clear();
|
||||
}
|
||||
|
||||
static int channel_count(vc_client* c) {
|
||||
vc_channel_list cl{};
|
||||
if (vc_list_channels(c, &cl) != VC_OK) return -1;
|
||||
int n = static_cast<int>(cl.count);
|
||||
vc_free_channel_list(&cl);
|
||||
return n;
|
||||
}
|
||||
|
||||
static uint32_t find_channel_by_name(vc_client* c, const char* name) {
|
||||
vc_channel_list cl{};
|
||||
if (vc_list_channels(c, &cl) != VC_OK) return 0;
|
||||
uint32_t id = 0;
|
||||
for (size_t i = 0; i < cl.count; ++i) {
|
||||
if (std::strcmp(cl.items[i].name, name) == 0) {
|
||||
id = cl.items[i].id;
|
||||
break;
|
||||
}
|
||||
}
|
||||
vc_free_channel_list(&cl);
|
||||
return id;
|
||||
}
|
||||
|
||||
int main() {
|
||||
auto tmp = std::filesystem::temp_directory_path() /
|
||||
("vctest_m5_ch_" + std::to_string(
|
||||
std::chrono::steady_clock::now().time_since_epoch().count()));
|
||||
std::filesystem::create_directories(tmp);
|
||||
std::string data_dir = tmp.string();
|
||||
|
||||
{
|
||||
voicecat::server::Database db(data_dir + "/voicecat.db");
|
||||
std::string err;
|
||||
if (!db.open(err)) { std::printf("FAIL: db.open: %s\n", err.c_str()); return 1; }
|
||||
if (!db.create_account("admin", "admin-pass", true, err)) {
|
||||
std::printf("FAIL: create admin: %s\n", err.c_str());
|
||||
std::filesystem::remove_all(tmp);
|
||||
return 1;
|
||||
}
|
||||
if (!db.create_account("bob", "bob-pass", false, err)) {
|
||||
std::printf("FAIL: create bob: %s\n", err.c_str());
|
||||
std::filesystem::remove_all(tmp);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
std::atomic<uint16_t> bound_port{0};
|
||||
std::mutex ready_mu;
|
||||
std::condition_variable ready_cv;
|
||||
bool ready{false};
|
||||
|
||||
voicecat::server::Config cfg;
|
||||
cfg.data_dir = data_dir;
|
||||
cfg.bind_port = 0;
|
||||
cfg.server_name = "VoiceCat-M5-Ch";
|
||||
cfg.allow_guests = false;
|
||||
cfg.on_ready = [&](uint16_t p) {
|
||||
bound_port.store(p);
|
||||
{ std::lock_guard lk(ready_mu); ready = true; }
|
||||
ready_cv.notify_all();
|
||||
};
|
||||
|
||||
voicecat::server::Server server(cfg);
|
||||
std::thread server_thread([&] { server.run(); });
|
||||
|
||||
{
|
||||
std::unique_lock lk(ready_mu);
|
||||
if (!ready_cv.wait_for(lk, std::chrono::seconds(10), [&] { return ready; })) {
|
||||
std::printf("FAIL: server did not become ready\n");
|
||||
server.stop();
|
||||
server_thread.join();
|
||||
std::filesystem::remove_all(tmp);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
uint16_t port = bound_port.load();
|
||||
|
||||
EventStore evAdmin;
|
||||
evAdmin.label = "admin";
|
||||
vc_callbacks cbA{on_event, nullptr, &evAdmin};
|
||||
vc_config cfgA{"test-admin", "0.1", VC_LOG_OFF};
|
||||
vc_client* admin = vc_client_create(&cfgA, cbA);
|
||||
evAdmin.client = admin;
|
||||
CHECK(admin != nullptr);
|
||||
CHECK(vc_connect(admin, "127.0.0.1", port) == VC_OK);
|
||||
CHECK(vc_authenticate_user(admin, "admin", "admin-pass") == VC_OK);
|
||||
CHECK(wait_for(evAdmin, [](EventStore& s){ return s.auth_ok; }, 20000));
|
||||
CHECK(wait_for(evAdmin, [](EventStore& s){ return s.channel_list_count > 0; }, 3000));
|
||||
|
||||
EventStore evBob;
|
||||
evBob.label = "bob";
|
||||
vc_callbacks cbB{on_event, nullptr, &evBob};
|
||||
vc_config cfgB{"test-bob", "0.1", VC_LOG_OFF};
|
||||
vc_client* bob = vc_client_create(&cfgB, cbB);
|
||||
evBob.client = bob;
|
||||
CHECK(bob != nullptr);
|
||||
CHECK(vc_connect(bob, "127.0.0.1", port) == VC_OK);
|
||||
CHECK(vc_authenticate_user(bob, "bob", "bob-pass") == VC_OK);
|
||||
CHECK(wait_for(evBob, [](EventStore& s){ return s.auth_ok; }, 20000));
|
||||
CHECK(wait_for(evBob, [](EventStore& s){ return s.channel_list_count > 0; }, 3000));
|
||||
|
||||
// Admin creates a password-protected channel.
|
||||
reset_generic(evAdmin);
|
||||
int base_count = channel_count(admin);
|
||||
CHECK(base_count >= 2); // Lobby + Music Room
|
||||
|
||||
vc_channel_info ch{};
|
||||
ch.name = "Private Room";
|
||||
ch.topic = "secret";
|
||||
ch.password_protected = 1;
|
||||
ch.password = "swordfish";
|
||||
ch.max_users = 10;
|
||||
ch.sort_order = 5;
|
||||
CHECK(vc_create_channel(admin, &ch) == VC_OK);
|
||||
CHECK(wait_generic(evAdmin, 3000));
|
||||
CHECK(last_generic_ok(evAdmin));
|
||||
|
||||
// Wait for the new channel to appear in bob's list.
|
||||
CHECK(wait_for(evBob, [](EventStore& s){ return s.channel_list_count > 1; }, 3000));
|
||||
int new_count = channel_count(bob);
|
||||
CHECK(new_count == base_count + 1);
|
||||
|
||||
uint32_t private_id = find_channel_by_name(admin, "Private Room");
|
||||
CHECK(private_id != 0);
|
||||
|
||||
// Bob tries to join without password — should fail.
|
||||
reset_generic(evBob);
|
||||
CHECK(vc_join_channel(bob, private_id, nullptr) == VC_OK);
|
||||
CHECK(wait_generic(evBob, 3000));
|
||||
CHECK(!last_generic_ok(evBob));
|
||||
|
||||
// Bob joins with password — should succeed.
|
||||
reset_generic(evBob);
|
||||
CHECK(vc_join_channel(bob, private_id, "swordfish") == VC_OK);
|
||||
CHECK(wait_generic(evBob, 3000));
|
||||
CHECK(last_generic_ok(evBob));
|
||||
|
||||
// Admin edits the channel name.
|
||||
reset_generic(evAdmin);
|
||||
int admin_list_count = evAdmin.channel_list_count;
|
||||
vc_channel_info edit{};
|
||||
edit.id = private_id;
|
||||
edit.name = "Renamed Room";
|
||||
edit.topic = "still secret";
|
||||
edit.password_protected = 1;
|
||||
edit.password = "swordfish"; // keep same password
|
||||
edit.sort_order = 5;
|
||||
CHECK(vc_edit_channel(admin, &edit) == VC_OK);
|
||||
CHECK(wait_generic(evAdmin, 3000));
|
||||
CHECK(last_generic_ok(evAdmin));
|
||||
|
||||
// Both clients should receive a channel-list update.
|
||||
CHECK(wait_for(evAdmin, [admin_list_count](EventStore& s){ return s.channel_list_count > admin_list_count; }, 3000));
|
||||
CHECK(wait_for(evBob, [private_id](EventStore& s){ return find_channel_by_name(s.client, "Renamed Room") == private_id; }, 3000));
|
||||
|
||||
// Admin deletes the channel.
|
||||
reset_generic(evAdmin);
|
||||
CHECK(vc_delete_channel(admin, private_id) == VC_OK);
|
||||
CHECK(wait_generic(evAdmin, 3000));
|
||||
CHECK(last_generic_ok(evAdmin));
|
||||
|
||||
// Bob should see the channel disappear and be back in Lobby (id=1).
|
||||
CHECK(wait_for(evBob, [private_id](EventStore& s){ return find_channel_by_name(s.client, "Renamed Room") == 0; }, 3000));
|
||||
{
|
||||
vc_user_list ul{};
|
||||
CHECK(vc_list_users(bob, &ul) == VC_OK);
|
||||
bool found_self = false;
|
||||
for (size_t i = 0; i < ul.count; ++i) {
|
||||
if (ul.items[i].id == evBob.self_user_id) {
|
||||
found_self = true;
|
||||
CHECK(ul.items[i].channel_id == 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
CHECK(found_self);
|
||||
vc_free_user_list(&ul);
|
||||
}
|
||||
|
||||
vc_disconnect(admin);
|
||||
vc_disconnect(bob);
|
||||
vc_client_destroy(admin);
|
||||
vc_client_destroy(bob);
|
||||
server.stop();
|
||||
server_thread.join();
|
||||
std::filesystem::remove_all(tmp);
|
||||
|
||||
if (g_failures == 0) {
|
||||
std::printf("m5_channel_crud: all checks passed\n");
|
||||
return 0;
|
||||
}
|
||||
std::printf("m5_channel_crud: %d failure(s)\n", g_failures);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#else // !VOICECAT_HAS_NET
|
||||
|
||||
int main() {
|
||||
std::printf("m5_channel_crud: SKIP (VOICECAT_HAS_NET not defined)\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif // VOICECAT_HAS_NET
|
||||
331
tests/test_m5_kick_ban_move_mute.cpp
Normal file
331
tests/test_m5_kick_ban_move_mute.cpp
Normal file
@@ -0,0 +1,331 @@
|
||||
/*
|
||||
* test_m5_kick_ban_move_mute — Phase 2 of M5: moderation.
|
||||
*
|
||||
* Verifies:
|
||||
* - admin can kick a user (target receives disconnect).
|
||||
* - admin can ban a password user; re-auth fails.
|
||||
* - admin can move a user to another channel.
|
||||
* - admin can server-mute/deafen a user; the target reflects it locally.
|
||||
*/
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
#ifdef VOICECAT_HAS_NET
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <filesystem>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "voicecat.h"
|
||||
#include "server.h"
|
||||
#include "db.h"
|
||||
|
||||
struct EventStore {
|
||||
std::mutex mu;
|
||||
std::condition_variable cv;
|
||||
|
||||
bool auth_ok{false};
|
||||
bool auth_done{false};
|
||||
uint32_t self_user_id{0};
|
||||
bool channel_list_received{false};
|
||||
bool disconnected{false};
|
||||
vc_result disconnect_reason{VC_OK};
|
||||
std::string disconnect_text;
|
||||
|
||||
struct ResultAck {
|
||||
bool ok{false};
|
||||
uint32_t code{0};
|
||||
std::string message;
|
||||
};
|
||||
std::vector<ResultAck> generic_results;
|
||||
|
||||
uint32_t last_updated_user{0};
|
||||
uint32_t last_updated_channel{0};
|
||||
|
||||
vc_client* client{nullptr};
|
||||
const char* label{nullptr};
|
||||
};
|
||||
|
||||
static void on_event(void* user, const vc_event* ev) {
|
||||
auto* s = static_cast<EventStore*>(user);
|
||||
std::lock_guard lk(s->mu);
|
||||
switch (ev->type) {
|
||||
case VC_EVENT_SERVER_IDENTITY:
|
||||
vc_confirm_server_identity(s->client, 1);
|
||||
break;
|
||||
case VC_EVENT_AUTH_RESULT:
|
||||
s->auth_ok = (ev->result == VC_OK);
|
||||
s->auth_done = true;
|
||||
s->self_user_id = ev->user_id;
|
||||
break;
|
||||
case VC_EVENT_CHANNEL_LIST:
|
||||
s->channel_list_received = true;
|
||||
break;
|
||||
case VC_EVENT_GENERIC_RESULT: {
|
||||
EventStore::ResultAck gr;
|
||||
gr.ok = (ev->result == VC_OK);
|
||||
gr.code = ev->u32a;
|
||||
gr.message = ev->text ? ev->text : "";
|
||||
s->generic_results.push_back(std::move(gr));
|
||||
break;
|
||||
}
|
||||
case VC_EVENT_USER_UPDATED:
|
||||
s->last_updated_user = ev->user_id;
|
||||
s->last_updated_channel = ev->channel_id;
|
||||
break;
|
||||
case VC_EVENT_DISCONNECTED:
|
||||
s->disconnected = true;
|
||||
s->disconnect_reason = static_cast<vc_result>(ev->result);
|
||||
s->disconnect_text = ev->text ? ev->text : "";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
s->cv.notify_all();
|
||||
}
|
||||
|
||||
template<typename Pred>
|
||||
static bool wait_for(EventStore& s, Pred pred, int timeout_ms) {
|
||||
auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms);
|
||||
std::unique_lock lk(s.mu);
|
||||
return s.cv.wait_until(lk, deadline, [&] { return pred(s); });
|
||||
}
|
||||
|
||||
static int g_failures = 0;
|
||||
#define CHECK(cond) \
|
||||
do { \
|
||||
if (!(cond)) { \
|
||||
std::printf("FAIL: %s (%s:%d)\n", #cond, __FILE__, __LINE__); \
|
||||
++g_failures; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
static bool wait_generic(EventStore& s, int timeout_ms) {
|
||||
return wait_for(s, [](EventStore& st) { return !st.generic_results.empty(); }, timeout_ms);
|
||||
}
|
||||
|
||||
static bool last_generic_ok(EventStore& s) {
|
||||
std::lock_guard lk(s.mu);
|
||||
return !s.generic_results.empty() && s.generic_results.back().ok;
|
||||
}
|
||||
|
||||
static void reset_generic(EventStore& s) {
|
||||
std::lock_guard lk(s.mu);
|
||||
s.generic_results.clear();
|
||||
}
|
||||
|
||||
static void reset_updated(EventStore& s) {
|
||||
std::lock_guard lk(s.mu);
|
||||
s.last_updated_user = 0;
|
||||
s.last_updated_channel = 0;
|
||||
}
|
||||
|
||||
static bool wait_updated(EventStore& s, uint32_t user_id, int timeout_ms) {
|
||||
return wait_for(s, [user_id](EventStore& st) {
|
||||
return st.last_updated_user == user_id;
|
||||
}, timeout_ms);
|
||||
}
|
||||
|
||||
int main() {
|
||||
auto tmp = std::filesystem::temp_directory_path() /
|
||||
("vctest_m5_mod_" + std::to_string(
|
||||
std::chrono::steady_clock::now().time_since_epoch().count()));
|
||||
std::filesystem::create_directories(tmp);
|
||||
std::string data_dir = tmp.string();
|
||||
|
||||
{
|
||||
voicecat::server::Database db(data_dir + "/voicecat.db");
|
||||
std::string err;
|
||||
if (!db.open(err)) { std::printf("FAIL: db.open: %s\n", err.c_str()); return 1; }
|
||||
if (!db.create_account("admin", "admin-pass", true, err)) {
|
||||
std::printf("FAIL: create admin: %s\n", err.c_str());
|
||||
std::filesystem::remove_all(tmp);
|
||||
return 1;
|
||||
}
|
||||
if (!db.create_account("alice", "alice-pass", false, err)) {
|
||||
std::printf("FAIL: create alice: %s\n", err.c_str());
|
||||
std::filesystem::remove_all(tmp);
|
||||
return 1;
|
||||
}
|
||||
if (!db.create_account("bob", "bob-pass", false, err)) {
|
||||
std::printf("FAIL: create bob: %s\n", err.c_str());
|
||||
std::filesystem::remove_all(tmp);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
std::atomic<uint16_t> bound_port{0};
|
||||
std::mutex ready_mu;
|
||||
std::condition_variable ready_cv;
|
||||
bool ready{false};
|
||||
|
||||
voicecat::server::Config cfg;
|
||||
cfg.data_dir = data_dir;
|
||||
cfg.bind_port = 0;
|
||||
cfg.server_name = "VoiceCat-M5-Mod";
|
||||
cfg.allow_guests = false;
|
||||
cfg.on_ready = [&](uint16_t p) {
|
||||
bound_port.store(p);
|
||||
{ std::lock_guard lk(ready_mu); ready = true; }
|
||||
ready_cv.notify_all();
|
||||
};
|
||||
|
||||
voicecat::server::Server server(cfg);
|
||||
std::thread server_thread([&] { server.run(); });
|
||||
|
||||
{
|
||||
std::unique_lock lk(ready_mu);
|
||||
if (!ready_cv.wait_for(lk, std::chrono::seconds(10), [&] { return ready; })) {
|
||||
std::printf("FAIL: server did not become ready\n");
|
||||
server.stop();
|
||||
server_thread.join();
|
||||
std::filesystem::remove_all(tmp);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
uint16_t port = bound_port.load();
|
||||
|
||||
auto make_client = [&](const char* label, const char* user, const char* pass) -> EventStore* {
|
||||
auto* ev = new EventStore();
|
||||
ev->label = label;
|
||||
vc_callbacks cb{on_event, nullptr, ev};
|
||||
vc_config cfgx{label, "0.1", VC_LOG_OFF};
|
||||
ev->client = vc_client_create(&cfgx, cb);
|
||||
if (!ev->client) return nullptr;
|
||||
if (vc_connect(ev->client, "127.0.0.1", port) != VC_OK) return nullptr;
|
||||
if (vc_authenticate_user(ev->client, user, pass) != VC_OK) return nullptr;
|
||||
return ev;
|
||||
};
|
||||
|
||||
EventStore* evAdmin = make_client("admin", "admin", "admin-pass");
|
||||
CHECK(evAdmin != nullptr);
|
||||
CHECK(wait_for(*evAdmin, [](EventStore& s){ return s.auth_ok; }, 20000));
|
||||
CHECK(wait_for(*evAdmin, [](EventStore& s){ return s.channel_list_received; }, 3000));
|
||||
uint32_t admin_id = evAdmin->self_user_id;
|
||||
CHECK(admin_id != 0);
|
||||
|
||||
EventStore* evAlice = make_client("alice", "alice", "alice-pass");
|
||||
CHECK(evAlice != nullptr);
|
||||
CHECK(wait_for(*evAlice, [](EventStore& s){ return s.auth_ok; }, 20000));
|
||||
CHECK(wait_for(*evAlice, [](EventStore& s){ return s.channel_list_received; }, 3000));
|
||||
uint32_t alice_id = evAlice->self_user_id;
|
||||
CHECK(alice_id != 0);
|
||||
|
||||
EventStore* evBob = make_client("bob", "bob", "bob-pass");
|
||||
CHECK(evBob != nullptr);
|
||||
CHECK(wait_for(*evBob, [](EventStore& s){ return s.auth_ok; }, 20000));
|
||||
CHECK(wait_for(*evBob, [](EventStore& s){ return s.channel_list_received; }, 3000));
|
||||
uint32_t bob_id = evBob->self_user_id;
|
||||
CHECK(bob_id != 0);
|
||||
|
||||
// Kick bob.
|
||||
reset_generic(*evAdmin);
|
||||
CHECK(vc_kick_user(evAdmin->client, bob_id, "bye bob") == VC_OK);
|
||||
CHECK(wait_generic(*evAdmin, 3000));
|
||||
CHECK(last_generic_ok(*evAdmin));
|
||||
CHECK(wait_for(*evBob, [](EventStore& s){ return s.disconnected; }, 3000));
|
||||
CHECK(evBob->disconnect_reason == VC_ERR_IO);
|
||||
vc_client_destroy(evBob->client);
|
||||
delete evBob;
|
||||
|
||||
// Reconnect bob.
|
||||
evBob = make_client("bob", "bob", "bob-pass");
|
||||
CHECK(evBob != nullptr);
|
||||
CHECK(wait_for(*evBob, [](EventStore& s){ return s.auth_ok; }, 20000));
|
||||
CHECK(wait_for(*evBob, [](EventStore& s){ return s.channel_list_received; }, 3000));
|
||||
bob_id = evBob->self_user_id;
|
||||
CHECK(bob_id != 0);
|
||||
|
||||
// Ban bob permanently.
|
||||
reset_generic(*evAdmin);
|
||||
CHECK(vc_ban_user(evAdmin->client, bob_id, "spam", 0) == VC_OK);
|
||||
CHECK(wait_generic(*evAdmin, 3000));
|
||||
CHECK(last_generic_ok(*evAdmin));
|
||||
CHECK(wait_for(*evBob, [](EventStore& s){ return s.disconnected; }, 3000));
|
||||
vc_client_destroy(evBob->client);
|
||||
delete evBob;
|
||||
|
||||
// Bob tries to reconnect — should fail auth due to username ban.
|
||||
{
|
||||
EventStore evBanned;
|
||||
evBanned.label = "banned-bob";
|
||||
vc_callbacks cb{on_event, nullptr, &evBanned};
|
||||
vc_config cfgb{"banned-bob", "0.1", VC_LOG_OFF};
|
||||
vc_client* banned_bob = vc_client_create(&cfgb, cb);
|
||||
evBanned.client = banned_bob;
|
||||
CHECK(banned_bob != nullptr);
|
||||
CHECK(vc_connect(banned_bob, "127.0.0.1", port) == VC_OK);
|
||||
CHECK(vc_authenticate_user(banned_bob, "bob", "bob-pass") == VC_OK);
|
||||
CHECK(wait_for(evBanned, [](EventStore& s){ return s.auth_done || s.disconnected; }, 20000));
|
||||
CHECK(!evBanned.auth_ok); // banned
|
||||
vc_disconnect(banned_bob);
|
||||
vc_client_destroy(banned_bob);
|
||||
}
|
||||
|
||||
// Move alice to Music Room (channel id 2).
|
||||
reset_generic(*evAdmin);
|
||||
reset_updated(*evAlice);
|
||||
CHECK(vc_move_user(evAdmin->client, alice_id, 2) == VC_OK);
|
||||
CHECK(wait_generic(*evAdmin, 3000));
|
||||
CHECK(last_generic_ok(*evAdmin));
|
||||
CHECK(wait_updated(*evAlice, alice_id, 3000));
|
||||
{
|
||||
vc_channel_list cl{};
|
||||
CHECK(vc_list_channels(evAlice->client, &cl) == VC_OK);
|
||||
CHECK(cl.count >= 2);
|
||||
vc_free_channel_list(&cl);
|
||||
vc_user_list ul{};
|
||||
CHECK(vc_list_users(evAlice->client, &ul) == VC_OK);
|
||||
bool found_alice = false;
|
||||
for (size_t i = 0; i < ul.count; ++i) {
|
||||
if (ul.items[i].id == alice_id) {
|
||||
found_alice = true;
|
||||
CHECK(ul.items[i].channel_id == 2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
CHECK(found_alice);
|
||||
vc_free_user_list(&ul);
|
||||
}
|
||||
|
||||
// Server-mute alice.
|
||||
reset_generic(*evAdmin);
|
||||
reset_updated(*evAlice);
|
||||
CHECK(vc_set_server_mute(evAdmin->client, alice_id, true, false) == VC_OK);
|
||||
CHECK(wait_generic(*evAdmin, 3000));
|
||||
CHECK(last_generic_ok(*evAdmin));
|
||||
CHECK(wait_updated(*evAlice, alice_id, 3000));
|
||||
|
||||
// Cleanup.
|
||||
vc_disconnect(evAdmin->client);
|
||||
vc_disconnect(evAlice->client);
|
||||
vc_client_destroy(evAdmin->client);
|
||||
vc_client_destroy(evAlice->client);
|
||||
delete evAdmin;
|
||||
delete evAlice;
|
||||
|
||||
server.stop();
|
||||
server_thread.join();
|
||||
std::filesystem::remove_all(tmp);
|
||||
|
||||
if (g_failures == 0) {
|
||||
std::printf("m5_kick_ban_move_mute: all checks passed\n");
|
||||
return 0;
|
||||
}
|
||||
std::printf("m5_kick_ban_move_mute: %d failure(s)\n", g_failures);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#else // !VOICECAT_HAS_NET
|
||||
|
||||
int main() {
|
||||
std::printf("m5_kick_ban_move_mute: SKIP (VOICECAT_HAS_NET not defined)\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif // VOICECAT_HAS_NET
|
||||
266
tests/test_m5_permissions.cpp
Normal file
266
tests/test_m5_permissions.cpp
Normal file
@@ -0,0 +1,266 @@
|
||||
/*
|
||||
* test_m5_permissions — Phase 1 of M5: server-side permission enforcement.
|
||||
*
|
||||
* Verifies:
|
||||
* - admin gets is_admin/can_create_temp_channel/etc.
|
||||
* - normal password user gets no permissions.
|
||||
* - admin can create a channel.
|
||||
* - normal user cannot create a channel.
|
||||
* - admin can grant can_create_temp_channel via vc_set_permission.
|
||||
* - granted normal user can now create a channel.
|
||||
*/
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
#ifdef VOICECAT_HAS_NET
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <filesystem>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "voicecat.h"
|
||||
#include "server.h"
|
||||
#include "db.h"
|
||||
|
||||
struct EventStore {
|
||||
std::mutex mu;
|
||||
std::condition_variable cv;
|
||||
|
||||
bool auth_ok{false};
|
||||
uint32_t self_user_id{0};
|
||||
bool channel_list_received{false};
|
||||
bool disconnected{false};
|
||||
|
||||
// VC_EVENT_GENERIC_RESULT tracking
|
||||
struct ResultAck {
|
||||
bool ok{false};
|
||||
uint32_t code{0};
|
||||
std::string message;
|
||||
};
|
||||
std::vector<ResultAck> generic_results;
|
||||
|
||||
vc_client* client{nullptr};
|
||||
const char* label{nullptr};
|
||||
};
|
||||
|
||||
static void on_event(void* user, const vc_event* ev) {
|
||||
auto* s = static_cast<EventStore*>(user);
|
||||
std::lock_guard lk(s->mu);
|
||||
switch (ev->type) {
|
||||
case VC_EVENT_SERVER_IDENTITY:
|
||||
vc_confirm_server_identity(s->client, 1);
|
||||
break;
|
||||
case VC_EVENT_AUTH_RESULT:
|
||||
s->auth_ok = (ev->result == VC_OK);
|
||||
s->self_user_id = ev->user_id;
|
||||
break;
|
||||
case VC_EVENT_CHANNEL_LIST:
|
||||
s->channel_list_received = true;
|
||||
break;
|
||||
case VC_EVENT_GENERIC_RESULT: {
|
||||
EventStore::ResultAck gr;
|
||||
gr.ok = (ev->result == VC_OK);
|
||||
gr.code = ev->u32a;
|
||||
gr.message = ev->text ? ev->text : "";
|
||||
s->generic_results.push_back(std::move(gr));
|
||||
break;
|
||||
}
|
||||
case VC_EVENT_DISCONNECTED:
|
||||
s->disconnected = true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
s->cv.notify_all();
|
||||
}
|
||||
|
||||
template<typename Pred>
|
||||
static bool wait_for(EventStore& s, Pred pred, int timeout_ms) {
|
||||
auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeout_ms);
|
||||
std::unique_lock lk(s.mu);
|
||||
return s.cv.wait_until(lk, deadline, [&] { return pred(s); });
|
||||
}
|
||||
|
||||
static int g_failures = 0;
|
||||
#define CHECK(cond) \
|
||||
do { \
|
||||
if (!(cond)) { \
|
||||
std::printf("FAIL: %s (%s:%d)\n", #cond, __FILE__, __LINE__); \
|
||||
++g_failures; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
static bool wait_generic(EventStore& s, int timeout_ms) {
|
||||
return wait_for(s, [](EventStore& st) { return !st.generic_results.empty(); }, timeout_ms);
|
||||
}
|
||||
|
||||
static bool last_generic_ok(EventStore& s) {
|
||||
std::lock_guard lk(s.mu);
|
||||
return !s.generic_results.empty() && s.generic_results.back().ok;
|
||||
}
|
||||
|
||||
static void reset_generic(EventStore& s) {
|
||||
std::lock_guard lk(s.mu);
|
||||
s.generic_results.clear();
|
||||
}
|
||||
|
||||
int main() {
|
||||
auto tmp = std::filesystem::temp_directory_path() /
|
||||
("vctest_m5_perm_" + std::to_string(
|
||||
std::chrono::steady_clock::now().time_since_epoch().count()));
|
||||
std::filesystem::create_directories(tmp);
|
||||
std::string data_dir = tmp.string();
|
||||
|
||||
// Pre-provision admin and normal user.
|
||||
{
|
||||
voicecat::server::Database db(data_dir + "/voicecat.db");
|
||||
std::string err;
|
||||
if (!db.open(err)) { std::printf("FAIL: db.open: %s\n", err.c_str()); return 1; }
|
||||
if (!db.create_account("admin", "admin-pass", true, err)) {
|
||||
std::printf("FAIL: create admin: %s\n", err.c_str());
|
||||
std::filesystem::remove_all(tmp);
|
||||
return 1;
|
||||
}
|
||||
if (!db.create_account("bob", "bob-pass", false, err)) {
|
||||
std::printf("FAIL: create bob: %s\n", err.c_str());
|
||||
std::filesystem::remove_all(tmp);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
std::atomic<uint16_t> bound_port{0};
|
||||
std::mutex ready_mu;
|
||||
std::condition_variable ready_cv;
|
||||
bool ready{false};
|
||||
|
||||
voicecat::server::Config cfg;
|
||||
cfg.data_dir = data_dir;
|
||||
cfg.bind_port = 0;
|
||||
cfg.server_name = "VoiceCat-M5-Perm";
|
||||
cfg.allow_guests = false;
|
||||
cfg.on_ready = [&](uint16_t p) {
|
||||
bound_port.store(p);
|
||||
{ std::lock_guard lk(ready_mu); ready = true; }
|
||||
ready_cv.notify_all();
|
||||
};
|
||||
|
||||
voicecat::server::Server server(cfg);
|
||||
std::thread server_thread([&] { server.run(); });
|
||||
|
||||
{
|
||||
std::unique_lock lk(ready_mu);
|
||||
if (!ready_cv.wait_for(lk, std::chrono::seconds(10), [&] { return ready; })) {
|
||||
std::printf("FAIL: server did not become ready\n");
|
||||
server.stop();
|
||||
server_thread.join();
|
||||
std::filesystem::remove_all(tmp);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
uint16_t port = bound_port.load();
|
||||
|
||||
// Admin client.
|
||||
EventStore evA;
|
||||
evA.label = "admin";
|
||||
vc_callbacks cbA{on_event, nullptr, &evA};
|
||||
vc_config cfgA{"test-admin", "0.1", VC_LOG_OFF};
|
||||
vc_client* admin = vc_client_create(&cfgA, cbA);
|
||||
evA.client = admin;
|
||||
CHECK(admin != nullptr);
|
||||
CHECK(vc_connect(admin, "127.0.0.1", port) == VC_OK);
|
||||
CHECK(vc_authenticate_user(admin, "admin", "admin-pass") == VC_OK);
|
||||
CHECK(wait_for(evA, [](EventStore& s){ return s.auth_ok; }, 20000));
|
||||
CHECK(wait_for(evA, [](EventStore& s){ return s.channel_list_received; }, 3000));
|
||||
uint32_t admin_id = evA.self_user_id;
|
||||
CHECK(admin_id != 0);
|
||||
|
||||
// Normal client.
|
||||
EventStore evB;
|
||||
evB.label = "bob";
|
||||
vc_callbacks cbB{on_event, nullptr, &evB};
|
||||
vc_config cfgB{"test-bob", "0.1", VC_LOG_OFF};
|
||||
vc_client* bob = vc_client_create(&cfgB, cbB);
|
||||
evB.client = bob;
|
||||
CHECK(bob != nullptr);
|
||||
CHECK(vc_connect(bob, "127.0.0.1", port) == VC_OK);
|
||||
CHECK(vc_authenticate_user(bob, "bob", "bob-pass") == VC_OK);
|
||||
CHECK(wait_for(evB, [](EventStore& s){ return s.auth_ok; }, 20000));
|
||||
CHECK(wait_for(evB, [](EventStore& s){ return s.channel_list_received; }, 3000));
|
||||
uint32_t bob_id = evB.self_user_id;
|
||||
CHECK(bob_id != 0);
|
||||
|
||||
// Verify permissions reported by the core.
|
||||
vc_permissions admin_perms{};
|
||||
CHECK(vc_get_permissions(admin, &admin_perms) == VC_OK);
|
||||
CHECK(admin_perms.is_admin != 0);
|
||||
CHECK(admin_perms.can_create_temp_channel != 0);
|
||||
|
||||
vc_permissions bob_perms{};
|
||||
CHECK(vc_get_permissions(bob, &bob_perms) == VC_OK);
|
||||
CHECK(bob_perms.is_admin == 0);
|
||||
CHECK(bob_perms.can_create_temp_channel == 0);
|
||||
|
||||
// Admin creates a channel.
|
||||
vc_channel_info ch{};
|
||||
ch.name = "Admin Channel";
|
||||
ch.topic = "created by admin";
|
||||
ch.max_users = 0;
|
||||
ch.sort_order = 10;
|
||||
CHECK(vc_create_channel(admin, &ch) == VC_OK);
|
||||
CHECK(wait_generic(evA, 3000));
|
||||
CHECK(last_generic_ok(evA));
|
||||
|
||||
// Normal user tries to create a channel — should be denied.
|
||||
reset_generic(evB);
|
||||
vc_channel_info ch2{};
|
||||
ch2.name = "Bob Channel";
|
||||
ch2.topic = "created by bob";
|
||||
ch2.sort_order = 11;
|
||||
CHECK(vc_create_channel(bob, &ch2) == VC_OK);
|
||||
CHECK(wait_generic(evB, 3000));
|
||||
CHECK(!last_generic_ok(evB));
|
||||
|
||||
// Admin grants can_create_temp_channel to bob.
|
||||
reset_generic(evA);
|
||||
vc_permissions grant{};
|
||||
grant.can_create_temp_channel = 1;
|
||||
CHECK(vc_set_permission(admin, bob_id, &grant) == VC_OK);
|
||||
CHECK(wait_generic(evA, 3000));
|
||||
CHECK(last_generic_ok(evA));
|
||||
|
||||
// Bob creates a channel — should succeed now.
|
||||
reset_generic(evB);
|
||||
CHECK(vc_create_channel(bob, &ch2) == VC_OK);
|
||||
CHECK(wait_generic(evB, 3000));
|
||||
CHECK(last_generic_ok(evB));
|
||||
|
||||
// Cleanup.
|
||||
vc_disconnect(admin);
|
||||
vc_disconnect(bob);
|
||||
vc_client_destroy(admin);
|
||||
vc_client_destroy(bob);
|
||||
server.stop();
|
||||
server_thread.join();
|
||||
std::filesystem::remove_all(tmp);
|
||||
|
||||
if (g_failures == 0) {
|
||||
std::printf("m5_permissions: all checks passed\n");
|
||||
return 0;
|
||||
}
|
||||
std::printf("m5_permissions: %d failure(s)\n", g_failures);
|
||||
return 1;
|
||||
}
|
||||
|
||||
#else // !VOICECAT_HAS_NET
|
||||
|
||||
int main() {
|
||||
std::printf("m5_permissions: SKIP (VOICECAT_HAS_NET not defined)\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif // VOICECAT_HAS_NET
|
||||
Reference in New Issue
Block a user