aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJan200101 <sentrycraft123@gmail.com>2021-06-06 17:10:10 +0200
committerJan200101 <sentrycraft123@gmail.com>2021-06-06 17:10:10 +0200
commit537994756d80178bdaf25e96968f34d2e144797f (patch)
treea093dfc43a8487c5866808e74e7250a6818cf6e0 /src
parentdf1df2929646abd77955b80e3ded3574ac005179 (diff)
downloadpolecat-537994756d80178bdaf25e96968f34d2e144797f.tar.gz
polecat-537994756d80178bdaf25e96968f34d2e144797f.zip
fix win env, add proper mocking code to test implementation
Diffstat (limited to 'src')
-rw-r--r--src/config.c2
-rw-r--r--src/defines.h2
-rw-r--r--src/dxvk.c6
-rw-r--r--src/lutris.c12
-rw-r--r--src/main.c2
-rw-r--r--src/mock/CMakeLists.txt40
-rw-r--r--src/mock/libcurl.c134
-rw-r--r--src/mock/runners/wine.json1
-rw-r--r--src/mock/wine/CMakeLists.txt35
-rw-r--r--src/mock/wine/main.c8
-rw-r--r--src/net.c2
11 files changed, 185 insertions, 59 deletions
diff --git a/src/config.c b/src/config.c
index 02889b0..0f514f3 100644
--- a/src/config.c
+++ b/src/config.c
@@ -18,7 +18,6 @@ static void getXDGDir(const char* envvar, const char* homeext, char* config, con
else
{
char* home = getenv("HOME");
- if (!home) home = "";
if (!home)
{
#ifdef _WIN32
@@ -27,6 +26,7 @@ static void getXDGDir(const char* envvar, const char* homeext, char* config, con
#endif
home = "";
}
+ if (!home) home = "";
strncpy(config, home, size);
strncat(config, homeext, size - strlen(config));
}
diff --git a/src/defines.h b/src/defines.h
index 82d5f05..115ddf0 100644
--- a/src/defines.h
+++ b/src/defines.h
@@ -50,4 +50,4 @@
#define mkdir(path, perm) mkdir(path)
#endif
-#endif \ No newline at end of file
+#endif
diff --git a/src/dxvk.c b/src/dxvk.c
index 356d81d..4ffbddd 100644
--- a/src/dxvk.c
+++ b/src/dxvk.c
@@ -215,8 +215,8 @@ COMMAND(dxvk, installed)
char dxvkdir[PATH_MAX];
getDXVKDir(dxvkdir, sizeof(dxvkdir));
- size_t dxvklen = strlen(dxvkdir)+1;
- dxvkdir[dxvklen-1] = '/';
+ size_t dxvklen = strlen(dxvkdir);
+ dxvkdir[dxvklen] = '/';
DIR *dir;
struct dirent *ent;
@@ -231,7 +231,7 @@ COMMAND(dxvk, installed)
if (ent->d_name[0] == '.') continue;
strncat(dxvkdir, ent->d_name, sizeof(dxvkdir) - dxvklen - 1);
int isdirec = isDir(dxvkdir);
- dxvkdir[dxvklen] = '\0';
+ dxvkdir[dxvklen+1] = '\0';
if (!isdirec) continue;
diff --git a/src/lutris.c b/src/lutris.c
index d885ae8..e2903ee 100644
--- a/src/lutris.c
+++ b/src/lutris.c
@@ -218,9 +218,7 @@ COMMAND(lutris, install)
case TASK:
parseVar(&installer.directives[i]->arguments[0], installer.variables, installer.variablecount);
- #ifdef _WIN32
- #warning TODO
- #else
+ #ifndef _WIN32 // TODO
setenv("WINEPREFIX", installer.directives[i]->arguments[0], 1);
#endif
switch(installer.directives[i]->task)
@@ -235,15 +233,11 @@ COMMAND(lutris, install)
case CREATE_PREFIX:
printf("CREATE_PREFIX\n");
- #ifdef _WIN32
- #warning TODO
- #else
+ #ifndef _WIN32 // TODO
setenv("WINEDEBUG", "-all", 1);
#endif
system("wineboot");
- #ifdef _WIN32
- #warning TODO
- #else
+ #ifndef _WIN32 // TODO
unsetenv("WINEDEBUG");
#endif
break;
diff --git a/src/main.c b/src/main.c
index 689d2b4..d865ecf 100644
--- a/src/main.c
+++ b/src/main.c
@@ -10,10 +10,12 @@
#include "common.h"
#include "config.h"
+#ifndef _WIN32
// if something fails
// we need to free the new argv
char** nargv;
static void free_nargv() { free(nargv); }
+#endif
static const struct Command main_commands[] = {
#ifndef _WIN32
diff --git a/src/mock/CMakeLists.txt b/src/mock/CMakeLists.txt
index 8b48c04..a45d88e 100644
--- a/src/mock/CMakeLists.txt
+++ b/src/mock/CMakeLists.txt
@@ -1,20 +1,48 @@
-SET(CURL_SOURCES
+add_subdirectory(wine)
+
+
+set(WINE_JSON_INFILE "${CMAKE_CURRENT_SOURCE_DIR}/runners/wine.json")
+set(WINE_JSON_OUTFILE "${CMAKE_CURRENT_BINARY_DIR}/wine_json")
+
+
+add_custom_command(
+ OUTPUT ${WINE_JSON_OUTFILE}.c ${WINE_JSON_OUTFILE}.h
+ COMMAND ${CMAKE_COMMAND}
+ "-Dbin_in=${WINE_JSON_INFILE}"
+ -P ${CMAKE_SOURCE_DIR}/cmake/FileEmbed.cmake
+)
+
+add_custom_target(wine_json ALL DEPENDS ${WINE_JSON_OUTFILE}.c ${WINE_JSON_OUTFILE}.h)
+
+set(WINE_MOCK
+ ${WINE_TAR_OUT}.c
+ ${WINE_TAR_OUT}.h
+)
+
+file(TOUCH ${WINE_MOCK})
+
+SET(MOCK_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/libcurl.c
+ ${WINE_MOCK}
+ ${WINE_JSON_OUTFILE}.c
+ ${WINE_JSON_OUTFILE}.h
)
-add_library(mockcurl STATIC ${CURL_SOURCES})
-target_compile_options(mockcurl PUBLIC ${CFLAGS})
-target_include_directories(mockcurl PUBLIC ${LIBCURL_INCLUDE_DIRS})
+add_library(mock STATIC ${MOCK_SOURCES})
+target_compile_options(mock PUBLIC ${CFLAGS})
+target_include_directories(mock PUBLIC ${LIBCURL_INCLUDE_DIRS})
+add_dependencies(mock wine_tar wine_json)
+target_include_directories(mock PRIVATE ${WINE_TAR_DIR} ${CMAKE_CURRENT_BINARY_DIR})
set(NAME ${CMAKE_PROJECT_NAME}_mock)
add_executable(${NAME} ${SOURCES})
target_link_libraries(${NAME} LINK_PUBLIC ${JSONC_LIBRARIES})
target_link_libraries(${NAME} LINK_PUBLIC ${LIBARCHIVE_LIBRARIES})
-target_link_libraries(${NAME} LINK_PUBLIC mockcurl)
+target_link_libraries(${NAME} LINK_PUBLIC mock)
target_include_directories(${NAME} PUBLIC ${JSONC_INCLUDE_DIRS})
target_include_directories(${NAME} PUBLIC ${LIBARCHIVE_INCLUDE_DIRS})
target_include_directories(${NAME} PUBLIC ${LIBCURL_INCLUDE_DIRS})
-target_compile_options(${NAME} PUBLIC ${CFLAGS}) \ No newline at end of file
+target_compile_options(${NAME} PUBLIC ${CFLAGS})
diff --git a/src/mock/libcurl.c b/src/mock/libcurl.c
index 95901be..1e58b15 100644
--- a/src/mock/libcurl.c
+++ b/src/mock/libcurl.c
@@ -1,11 +1,28 @@
#include <stdio.h>
#include <stdarg.h>
+#include <string.h>
#include <curl/curl.h>
#include "../defines.h"
+#include "wine_mock_tar_xz.h"
+#include "wine_json.h"
+
+typedef size_t (*callback_t)(const void*, size_t, size_t, void*);
+typedef int (*xfercallback_t)(void*, curl_off_t, curl_off_t, curl_off_t, curl_off_t);
+
+#ifndef NDEBUG
+#define debug_printf(...) printf(__VA_ARGS__)
+#else
+#define debug_printf(...)
+#endif
+
+callback_t callbackfunc = NULL;
+xfercallback_t xfercallbackfunc = NULL;
char* url = NULL;
void* data = NULL;
+long noprogress = 0;
+
#ifdef curl_easy_setopt
#undef curl_easy_setopt
@@ -13,48 +30,89 @@ void* data = NULL;
CURLcode curl_global_init(UNUSED long flags)
{
- puts("[MOCK] curl_global_init(...)");
- return CURLE_OK;
+ debug_printf("[MOCK] %s(...)\n", __func__);
+ return CURLE_OK;
}
CURL* curl_easy_init()
{
- puts("[MOCK] curl_easy_init(...)");
- return NULL;
+ debug_printf("[MOCK] %s(...)\n", __func__);
+ return NULL;
}
CURLcode curl_easy_setopt(UNUSED CURL *handle, CURLoption option, ...)
{
- puts("[MOCK] curl_easy_setopt(...)");
+ debug_printf("[MOCK] %s(...)\n", __func__);
+
+ va_list arg;
+ va_start(arg, option);
+
+ switch (option)
+ {
+ case CURLOPT_URL:
+ url = va_arg(arg, char*);
+ debug_printf("CURLOPT_URL\t%s\n", url);
+ break;
+
+ case CURLOPT_WRITEDATA:
+ data = va_arg(arg, void*);
+ debug_printf("CURLOPT_WRITEDATA\t%p\n", data);
+ break;
- va_list arg;
- va_start(arg, option);
+ case CURLOPT_WRITEFUNCTION:
+ callbackfunc = va_arg(arg, callback_t);
+ debug_printf("CURLOPT_WRITEFUNCTION\n");
+ break;
- switch (option)
- {
- case CURLOPT_URL:
- url = va_arg(arg, char*);
- printf("CURLOPT_URL\t%s\n", url);
- break;
+ case CURLOPT_XFERINFOFUNCTION:
+ xfercallbackfunc = va_arg(arg, xfercallback_t);
+ debug_printf("CURLOPT_XFERINFOFUNCTION\n");
+ break;
- case CURLOPT_WRITEDATA:
- data = va_arg(arg, void*);
- printf("CURLOPT_WRITEDATA\t%p\n", data);
- break;
+ case CURLOPT_NOPROGRESS:
+ noprogress = va_arg(arg, long);
+ debug_printf("CURLOPT_NOPROGRESS\t%li\n", noprogress);
+ break;
- default:
- break;
- }
+ default:
+ break;
+ }
- va_end(arg);
+ va_end(arg);
- return CURLE_OK;
+ return CURLE_OK;
}
CURLcode curl_easy_perform(UNUSED CURL *easy_handle)
{
- puts("[MOCK] curl_easy_perform(...)");
- return CURLE_OK;
+ const void* output = NULL;
+ size_t output_size = 0;
+
+ debug_printf("[MOCK] %s(...)\n", __func__);
+ if (!strcmp(url, WINE_API))
+ {
+ output = wine_json;
+ output_size = wine_json_size;
+ }
+ else if (!strcmp(url, "mockurl"))
+ {
+ output = wine_mock_tar_xz;
+ output_size = wine_mock_tar_xz_size;
+ }
+
+ if (output && data && callbackfunc)
+ {
+ callbackfunc(output, 1, output_size, data);
+
+ if (xfercallbackfunc && !noprogress)
+ {
+ xfercallbackfunc(data, (curl_off_t)output_size, (curl_off_t)output_size, 0, 0);
+ }
+
+ return CURLE_OK;
+ }
+
+ return CURLE_UNSUPPORTED_PROTOCOL;
}
#ifdef curl_easy_getinfo
@@ -63,33 +121,33 @@ CURLcode curl_easy_perform(UNUSED CURL *easy_handle)
CURLcode curl_easy_getinfo(UNUSED CURL *curl, CURLINFO info, ...)
{
- puts("[MOCK] curl_easy_getinfo(...)");
+ debug_printf("[MOCK] %s(...)\n", __func__);
- if (info == CURLINFO_RESPONSE_CODE)
- {
- va_list arg;
- va_start(arg, info);
- long* http_code = va_arg(arg, long*);
- *http_code = 200;
- va_end(arg);
- }
+ if (info == CURLINFO_RESPONSE_CODE)
+ {
+ va_list arg;
+ va_start(arg, info);
+ long* http_code = va_arg(arg, long*);
+ *http_code = 200;
+ va_end(arg);
+ }
- return CURLE_OK;
+ return CURLE_OK;
}
const char* curl_easy_strerror(UNUSED CURLcode error)
{
- puts("[MOCK] curl_easy_strerror(...)");
- return "error";
+ debug_printf("[MOCK] %s(...)\n", __func__);
+ return __func__;;
}
void curl_easy_cleanup(CURL *handle)
{
- puts("[MOCK] curl_easy_cleanup(...)");
+ debug_printf("[MOCK] %s(...)\n", __func__);
}
void curl_global_cleanup()
{
- puts("[MOCK] curl_global_cleanup(...)");
+ debug_printf("[MOCK] %s(...)\n", __func__);
} \ No newline at end of file
diff --git a/src/mock/runners/wine.json b/src/mock/runners/wine.json
new file mode 100644
index 0000000..b3bd282
--- /dev/null
+++ b/src/mock/runners/wine.json
@@ -0,0 +1 @@
+{"versions":[{"version":"mock","url":"mockurl"}]} \ No newline at end of file
diff --git a/src/mock/wine/CMakeLists.txt b/src/mock/wine/CMakeLists.txt
new file mode 100644
index 0000000..8275b27
--- /dev/null
+++ b/src/mock/wine/CMakeLists.txt
@@ -0,0 +1,35 @@
+SET(WINE_SOURCES
+ ${CMAKE_CURRENT_SOURCE_DIR}/main.c
+)
+
+set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/mock/bin")
+file(MAKE_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
+
+set(WINE_TAR_DIR "${CMAKE_CURRENT_BINARY_DIR}")
+set(WINE_TAR_DIR "${WINE_TAR_DIR}" PARENT_SCOPE)
+
+add_executable(wine ${WINE_SOURCES})
+target_compile_options(wine PUBLIC ${CFLAGS})
+
+
+set(WINE_TAR_FILE_NAME "wine-mock.tar.xz")
+set(WINE_TAR_FILE "${WINE_TAR_DIR}/${WINE_TAR_FILE_NAME}")
+string(REGEX REPLACE "\\.| |-" "_" WINE_TAR_FILE_NAME ${WINE_TAR_FILE_NAME})
+set(WINE_TAR_OUT "${WINE_TAR_DIR}/${WINE_TAR_FILE_NAME}")
+set(WINE_TAR_OUT "${WINE_TAR_OUT}" PARENT_SCOPE)
+add_custom_command(
+ OUTPUT ${WINE_TAR_FILE}
+ COMMAND
+ ${CMAKE_COMMAND} -E tar "cfv" "${WINE_TAR_FILE}" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}"
+ DEPENDS wine
+)
+
+add_custom_command(
+ OUTPUT ${WINE_TAR_OUT}.c ${WINE_TAR_OUT}.h
+ COMMAND ${CMAKE_COMMAND}
+ "-Dbin_in=${WINE_TAR_FILE}"
+ -P ${CMAKE_SOURCE_DIR}/cmake/FileEmbed.cmake
+ DEPENDS ${WINE_TAR_FILE}
+)
+
+add_custom_target(wine_tar ALL DEPENDS ${WINE_TAR_OUT}.c ${WINE_TAR_OUT}.h) \ No newline at end of file
diff --git a/src/mock/wine/main.c b/src/mock/wine/main.c
new file mode 100644
index 0000000..5b9c25f
--- /dev/null
+++ b/src/mock/wine/main.c
@@ -0,0 +1,8 @@
+#include <stdio.h>
+
+int main(int argc, char** argv)
+{
+ for (int i = 0; i < argc; ++i)
+ printf("%s ", argv[i]);
+ puts("");
+} \ No newline at end of file
diff --git a/src/net.c b/src/net.c
index f58e8fe..d5c3034 100644
--- a/src/net.c
+++ b/src/net.c
@@ -9,7 +9,7 @@
#include "net.h"
#include "common.h"
-static inline size_t memoryCallback(void* contents, size_t size, size_t nmemb, void* userp)
+static inline size_t memoryCallback(const void* contents, size_t size, size_t nmemb, void* userp)
{
size_t realsize = size * nmemb;
struct MemoryStruct* mem = (struct MemoryStruct*)userp;