mirror of
https://github.com/pikami/cosmium.git
synced 2025-12-19 17:00:37 +00:00
Added some tests for sharedlibrary
This commit is contained in:
@@ -22,6 +22,7 @@ const (
|
||||
ResponseServerInstanceAlreadyExists = 2
|
||||
ResponseFailedToParseConfiguration = 3
|
||||
ResponseServerInstanceNotFound = 4
|
||||
ResponseFailedToLoadState = 5
|
||||
)
|
||||
|
||||
//export CreateServerInstance
|
||||
@@ -44,6 +45,7 @@ func CreateServerInstance(serverName *C.char, configurationJSON *C.char) int {
|
||||
}
|
||||
|
||||
configuration.PopulateCalculatedFields()
|
||||
configuration.ApplyDefaultsToEmptyFields()
|
||||
|
||||
repository := repositories.NewDataRepository(repositories.RepositoryOptions{
|
||||
InitialDataFilePath: configuration.InitialDataFilePath,
|
||||
@@ -85,4 +87,20 @@ func GetServerInstanceState(serverName *C.char) *C.char {
|
||||
return nil
|
||||
}
|
||||
|
||||
//export LoadServerInstanceState
|
||||
func LoadServerInstanceState(serverName *C.char, stateJSON *C.char) int {
|
||||
serverNameStr := C.GoString(serverName)
|
||||
stateJSONStr := C.GoString(stateJSON)
|
||||
|
||||
if serverInstance, ok := serverInstances[serverNameStr]; ok {
|
||||
err := serverInstance.repository.LoadStateJSON(stateJSONStr)
|
||||
if err != nil {
|
||||
return ResponseFailedToLoadState
|
||||
}
|
||||
return ResponseSuccess
|
||||
}
|
||||
|
||||
return ResponseServerInstanceNotFound
|
||||
}
|
||||
|
||||
func main() {}
|
||||
|
||||
30
sharedlibrary/tests/main.c
Normal file
30
sharedlibrary/tests/main.c
Normal file
@@ -0,0 +1,30 @@
|
||||
#include "shared.h"
|
||||
|
||||
void test_CreateServerInstance();
|
||||
void test_StopServerInstance();
|
||||
void test_ServerInstanceStateMethods();
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc < 2)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s <path_to_shared_library>\n", argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
const char *libPath = argv[1];
|
||||
handle = dlopen(libPath, RTLD_LAZY);
|
||||
if (!handle)
|
||||
{
|
||||
fprintf(stderr, "Failed to load shared library: %s\n", dlerror());
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
printf("Running tests for library: %s\n", libPath);
|
||||
test_CreateServerInstance();
|
||||
test_ServerInstanceStateMethods();
|
||||
test_StopServerInstance();
|
||||
|
||||
dlclose(handle);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
36
sharedlibrary/tests/shared.c
Normal file
36
sharedlibrary/tests/shared.c
Normal file
@@ -0,0 +1,36 @@
|
||||
#include "shared.h"
|
||||
|
||||
void *handle = NULL;
|
||||
|
||||
void *load_function(const char *func_name)
|
||||
{
|
||||
void *func = dlsym(handle, func_name);
|
||||
if (!func)
|
||||
{
|
||||
fprintf(stderr, "Failed to load function %s: %s\n", func_name, dlerror());
|
||||
}
|
||||
return func;
|
||||
}
|
||||
|
||||
char *compact_json(const char *json)
|
||||
{
|
||||
size_t len = strlen(json);
|
||||
char *compact = (char *)malloc(len + 1);
|
||||
if (!compact)
|
||||
{
|
||||
fprintf(stderr, "Failed to allocate memory for compacted JSON\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *dest = compact;
|
||||
for (const char *src = json; *src != '\0'; ++src)
|
||||
{
|
||||
if (!isspace((unsigned char)*src)) // Skip spaces, newlines, tabs, etc.
|
||||
{
|
||||
*dest++ = *src;
|
||||
}
|
||||
}
|
||||
*dest = '\0'; // Null-terminate the string
|
||||
|
||||
return compact;
|
||||
}
|
||||
15
sharedlibrary/tests/shared.h
Normal file
15
sharedlibrary/tests/shared.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef SHARED_H
|
||||
#define SHARED_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <dlfcn.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
extern void *handle;
|
||||
|
||||
void *load_function(const char *func_name);
|
||||
char *compact_json(const char *json);
|
||||
|
||||
#endif
|
||||
26
sharedlibrary/tests/test_create.c
Normal file
26
sharedlibrary/tests/test_create.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#include "shared.h"
|
||||
|
||||
void test_CreateServerInstance()
|
||||
{
|
||||
typedef int (*CreateServerInstanceFn)(char *, char *);
|
||||
CreateServerInstanceFn CreateServerInstance = (CreateServerInstanceFn)load_function("CreateServerInstance");
|
||||
|
||||
if (!CreateServerInstance)
|
||||
{
|
||||
fprintf(stderr, "Failed to find CreateServerInstance function\n");
|
||||
return;
|
||||
}
|
||||
|
||||
char *serverName = "TestServer";
|
||||
char *configJSON = "{\"host\":\"localhost\",\"port\":8080}";
|
||||
|
||||
int result = CreateServerInstance(serverName, configJSON);
|
||||
if (result == 0)
|
||||
{
|
||||
printf("CreateServerInstance: SUCCESS\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("CreateServerInstance: FAILED (result = %d)\n", result);
|
||||
}
|
||||
}
|
||||
64
sharedlibrary/tests/test_instance_state.c
Normal file
64
sharedlibrary/tests/test_instance_state.c
Normal file
@@ -0,0 +1,64 @@
|
||||
#include "shared.h"
|
||||
|
||||
void test_ServerInstanceStateMethods()
|
||||
{
|
||||
typedef int (*LoadServerInstanceStateFn)(char *, char *);
|
||||
LoadServerInstanceStateFn LoadServerInstanceState = (LoadServerInstanceStateFn)load_function("LoadServerInstanceState");
|
||||
if (!LoadServerInstanceState)
|
||||
{
|
||||
fprintf(stderr, "Failed to find LoadServerInstanceState function\n");
|
||||
return;
|
||||
}
|
||||
|
||||
char *serverName = "TestServer";
|
||||
char *stateJSON = "{\"databases\":{\"test-db\":{\"id\":\"test-db\"}}}";
|
||||
int result = LoadServerInstanceState(serverName, stateJSON);
|
||||
if (result == 0)
|
||||
{
|
||||
printf("LoadServerInstanceState: SUCCESS\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("LoadServerInstanceState: FAILED (result = %d)\n", result);
|
||||
}
|
||||
|
||||
typedef char *(*GetServerInstanceStateFn)(char *);
|
||||
GetServerInstanceStateFn GetServerInstanceState = (GetServerInstanceStateFn)load_function("GetServerInstanceState");
|
||||
if (!GetServerInstanceState)
|
||||
{
|
||||
fprintf(stderr, "Failed to find GetServerInstanceState function\n");
|
||||
return;
|
||||
}
|
||||
|
||||
char *state = GetServerInstanceState(serverName);
|
||||
if (state)
|
||||
{
|
||||
printf("GetServerInstanceState: SUCCESS (state = %s)\n", state);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("GetServerInstanceState: FAILED\n");
|
||||
}
|
||||
|
||||
const char *expected_state = "{\"databases\":{\"test-db\":{\"id\":\"test-db\",\"_ts\":0,\"_rid\":\"\",\"_etag\":\"\",\"_self\":\"\"}},\"collections\":{\"test-db\":{}},\"documents\":{\"test-db\":{}}}";
|
||||
char *compact_state = compact_json(state);
|
||||
if (!compact_state)
|
||||
{
|
||||
free(state);
|
||||
return;
|
||||
}
|
||||
|
||||
if (strcmp(compact_state, expected_state) == 0)
|
||||
{
|
||||
printf("GetServerInstanceState: State matches expected value.\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("GetServerInstanceState: State does not match expected value.\n");
|
||||
printf("Expected: %s\n", expected_state);
|
||||
printf("Actual: %s\n", compact_state);
|
||||
}
|
||||
|
||||
free(state);
|
||||
free(compact_state);
|
||||
}
|
||||
24
sharedlibrary/tests/test_stop.c
Normal file
24
sharedlibrary/tests/test_stop.c
Normal file
@@ -0,0 +1,24 @@
|
||||
#include "shared.h"
|
||||
|
||||
void test_StopServerInstance()
|
||||
{
|
||||
typedef int (*StopServerInstanceFn)(char *);
|
||||
StopServerInstanceFn StopServerInstance = (StopServerInstanceFn)load_function("StopServerInstance");
|
||||
|
||||
if (!StopServerInstance)
|
||||
{
|
||||
fprintf(stderr, "Failed to find StopServerInstance function\n");
|
||||
return;
|
||||
}
|
||||
|
||||
char *serverName = "TestServer";
|
||||
int result = StopServerInstance(serverName);
|
||||
if (result == 0)
|
||||
{
|
||||
printf("StopServerInstance: SUCCESS\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("StopServerInstance: FAILED (result = %d)\n", result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user