summaryrefslogtreecommitdiffstats
path: root/winpr/libwinpr/environment/test
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 01:24:41 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 01:24:41 +0000
commita9bcc81f821d7c66f623779fa5147e728eb3c388 (patch)
tree98676963bcdd537ae5908a067a8eb110b93486a6 /winpr/libwinpr/environment/test
parentInitial commit. (diff)
downloadfreerdp3-a9bcc81f821d7c66f623779fa5147e728eb3c388.tar.xz
freerdp3-a9bcc81f821d7c66f623779fa5147e728eb3c388.zip
Adding upstream version 3.3.0+dfsg1.upstream/3.3.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'winpr/libwinpr/environment/test')
-rw-r--r--winpr/libwinpr/environment/test/CMakeLists.txt28
-rw-r--r--winpr/libwinpr/environment/test/TestEnvironmentGetEnvironmentStrings.c43
-rw-r--r--winpr/libwinpr/environment/test/TestEnvironmentGetSetEB.c138
-rw-r--r--winpr/libwinpr/environment/test/TestEnvironmentMergeEnvironmentStrings.c34
-rw-r--r--winpr/libwinpr/environment/test/TestEnvironmentSetEnvironmentVariable.c72
5 files changed, 315 insertions, 0 deletions
diff --git a/winpr/libwinpr/environment/test/CMakeLists.txt b/winpr/libwinpr/environment/test/CMakeLists.txt
new file mode 100644
index 0000000..459d425
--- /dev/null
+++ b/winpr/libwinpr/environment/test/CMakeLists.txt
@@ -0,0 +1,28 @@
+
+set(MODULE_NAME "TestEnvironment")
+set(MODULE_PREFIX "TEST_ENVIRONMENT")
+
+set(${MODULE_PREFIX}_DRIVER ${MODULE_NAME}.c)
+
+set(${MODULE_PREFIX}_TESTS
+ TestEnvironmentGetEnvironmentStrings.c
+ TestEnvironmentSetEnvironmentVariable.c
+ TestEnvironmentMergeEnvironmentStrings.c
+ TestEnvironmentGetSetEB.c)
+
+create_test_sourcelist(${MODULE_PREFIX}_SRCS
+ ${${MODULE_PREFIX}_DRIVER}
+ ${${MODULE_PREFIX}_TESTS})
+
+add_executable(${MODULE_NAME} ${${MODULE_PREFIX}_SRCS})
+
+target_link_libraries(${MODULE_NAME} winpr)
+
+set_target_properties(${MODULE_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${TESTING_OUTPUT_DIRECTORY}")
+
+foreach(test ${${MODULE_PREFIX}_TESTS})
+ get_filename_component(TestName ${test} NAME_WE)
+ add_test(${TestName} ${TESTING_OUTPUT_DIRECTORY}/${MODULE_NAME} ${TestName})
+endforeach()
+
+set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER "WinPR/Test")
diff --git a/winpr/libwinpr/environment/test/TestEnvironmentGetEnvironmentStrings.c b/winpr/libwinpr/environment/test/TestEnvironmentGetEnvironmentStrings.c
new file mode 100644
index 0000000..c596399
--- /dev/null
+++ b/winpr/libwinpr/environment/test/TestEnvironmentGetEnvironmentStrings.c
@@ -0,0 +1,43 @@
+
+#include <stdio.h>
+#include <winpr/crt.h>
+#include <winpr/tchar.h>
+#include <winpr/environment.h>
+
+int TestEnvironmentGetEnvironmentStrings(int argc, char* argv[])
+{
+ int r = -1;
+ TCHAR* p = NULL;
+ size_t length = 0;
+ LPTCH lpszEnvironmentBlock = NULL;
+
+ WINPR_UNUSED(argc);
+ WINPR_UNUSED(argv);
+
+ lpszEnvironmentBlock = GetEnvironmentStrings();
+
+ p = lpszEnvironmentBlock;
+
+ while (p[0] && p[1])
+ {
+ const int rc = _sntprintf(NULL, 0, _T("%s\n"), p);
+ if (rc < 1)
+ {
+ _tprintf(_T("test failed: return %d\n"), rc);
+ goto fail;
+ }
+ length = _tcslen(p);
+ if (length != (size_t)(rc - 1))
+ {
+ _tprintf(_T("test failed: length %") _T(PRIuz) _T(" != %d [%s]\n"), length, rc - 1, p);
+ goto fail;
+ }
+ p += (length + 1);
+ }
+
+ r = 0;
+fail:
+ FreeEnvironmentStrings(lpszEnvironmentBlock);
+
+ return r;
+}
diff --git a/winpr/libwinpr/environment/test/TestEnvironmentGetSetEB.c b/winpr/libwinpr/environment/test/TestEnvironmentGetSetEB.c
new file mode 100644
index 0000000..603b4c2
--- /dev/null
+++ b/winpr/libwinpr/environment/test/TestEnvironmentGetSetEB.c
@@ -0,0 +1,138 @@
+
+#include <stdio.h>
+#include <winpr/crt.h>
+#include <winpr/tchar.h>
+#include <winpr/environment.h>
+
+int TestEnvironmentGetSetEB(int argc, char* argv[])
+{
+ int rc = 0;
+#ifndef _WIN32
+ char test[1024];
+ TCHAR* p = NULL;
+ DWORD length = 0;
+ LPTCH lpszEnvironmentBlock = "SHELL=123\0test=1\0test1=2\0DISPLAY=WINPR_TEST_VALUE\0\0";
+ LPTCH lpszEnvironmentBlockNew = NULL;
+
+ WINPR_UNUSED(argc);
+ WINPR_UNUSED(argv);
+
+ rc = -1;
+ /* Get length of an variable */
+ length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "DISPLAY", NULL, 0);
+
+ if (0 == length)
+ return -1;
+
+ /* Get the variable itself */
+ p = (LPSTR)malloc(length);
+
+ if (!p)
+ goto fail;
+
+ if (GetEnvironmentVariableEBA(lpszEnvironmentBlock, "DISPLAY", p, length) != length - 1)
+ goto fail;
+
+ printf("GetEnvironmentVariableA(WINPR_TEST_VARIABLE) = %s\n", p);
+
+ if (strcmp(p, "WINPR_TEST_VALUE") != 0)
+ goto fail;
+
+ /* Get length of an non-existing variable */
+ length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "BLA", NULL, 0);
+
+ if (0 != length)
+ {
+ printf("Unset variable returned\n");
+ goto fail;
+ }
+
+ /* Get length of an similar called variables */
+ length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "XDISPLAY", NULL, 0);
+
+ if (0 != length)
+ {
+ printf("Similar named variable returned (XDISPLAY, length %d)\n", length);
+ goto fail;
+ }
+
+ length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "DISPLAYX", NULL, 0);
+
+ if (0 != length)
+ {
+ printf("Similar named variable returned (DISPLAYX, length %d)\n", length);
+ goto fail;
+ }
+
+ length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "DISPLA", NULL, 0);
+
+ if (0 != length)
+ {
+ printf("Similar named variable returned (DISPLA, length %d)\n", length);
+ goto fail;
+ }
+
+ length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "ISPLAY", NULL, 0);
+
+ if (0 != length)
+ {
+ printf("Similar named variable returned (ISPLAY, length %d)\n", length);
+ goto fail;
+ }
+
+ /* Set variable in empty environment block */
+ if (SetEnvironmentVariableEBA(&lpszEnvironmentBlockNew, "test", "5"))
+ {
+ if (GetEnvironmentVariableEBA(lpszEnvironmentBlockNew, "test", test, 1023))
+ {
+ if (strcmp(test, "5") != 0)
+ goto fail;
+ }
+ else
+ goto fail;
+ }
+
+ /* Clear variable */
+ if (SetEnvironmentVariableEBA(&lpszEnvironmentBlockNew, "test", NULL))
+ {
+ if (GetEnvironmentVariableEBA(lpszEnvironmentBlockNew, "test", test, 1023))
+ goto fail;
+ else
+ {
+ // not found .. this is expected
+ }
+ }
+
+ free(lpszEnvironmentBlockNew);
+ lpszEnvironmentBlockNew = (LPTCH)calloc(1024, sizeof(TCHAR));
+
+ if (!lpszEnvironmentBlockNew)
+ goto fail;
+
+ memcpy(lpszEnvironmentBlockNew, lpszEnvironmentBlock, length);
+
+ /* Set variable in empty environment block */
+ if (SetEnvironmentVariableEBA(&lpszEnvironmentBlockNew, "test", "5"))
+ {
+ if (0 != GetEnvironmentVariableEBA(lpszEnvironmentBlockNew, "testr", test, 1023))
+ {
+ printf("GetEnvironmentVariableEBA returned unset variable\n");
+ goto fail;
+ }
+
+ if (GetEnvironmentVariableEBA(lpszEnvironmentBlockNew, "test", test, 1023))
+ {
+ if (strcmp(test, "5") != 0)
+ goto fail;
+ }
+ else
+ goto fail;
+ }
+
+ rc = 0;
+fail:
+ free(p);
+ free(lpszEnvironmentBlockNew);
+#endif
+ return rc;
+}
diff --git a/winpr/libwinpr/environment/test/TestEnvironmentMergeEnvironmentStrings.c b/winpr/libwinpr/environment/test/TestEnvironmentMergeEnvironmentStrings.c
new file mode 100644
index 0000000..428418d
--- /dev/null
+++ b/winpr/libwinpr/environment/test/TestEnvironmentMergeEnvironmentStrings.c
@@ -0,0 +1,34 @@
+
+#include <stdio.h>
+#include <winpr/crt.h>
+#include <winpr/tchar.h>
+#include <winpr/environment.h>
+
+int TestEnvironmentMergeEnvironmentStrings(int argc, char* argv[])
+{
+#ifndef _WIN32
+ TCHAR* p = NULL;
+ size_t length = 0;
+ LPTCH lpszEnvironmentBlock = NULL;
+ LPTCH lpsz2Merge = "SHELL=123\0test=1\0test1=2\0DISPLAY=:77\0\0";
+ LPTCH lpszMergedEnvironmentBlock = NULL;
+
+ WINPR_UNUSED(argc);
+ WINPR_UNUSED(argv);
+
+ lpszEnvironmentBlock = GetEnvironmentStrings();
+ lpszMergedEnvironmentBlock = MergeEnvironmentStrings(lpszEnvironmentBlock, lpsz2Merge);
+ p = (TCHAR*)lpszMergedEnvironmentBlock;
+
+ while (p[0] && p[1])
+ {
+ printf("%s\n", p);
+ length = strlen(p);
+ p += (length + 1);
+ }
+
+ FreeEnvironmentStrings(lpszMergedEnvironmentBlock);
+ FreeEnvironmentStrings(lpszEnvironmentBlock);
+#endif
+ return 0;
+}
diff --git a/winpr/libwinpr/environment/test/TestEnvironmentSetEnvironmentVariable.c b/winpr/libwinpr/environment/test/TestEnvironmentSetEnvironmentVariable.c
new file mode 100644
index 0000000..04b7064
--- /dev/null
+++ b/winpr/libwinpr/environment/test/TestEnvironmentSetEnvironmentVariable.c
@@ -0,0 +1,72 @@
+
+#include <stdio.h>
+#include <winpr/crt.h>
+#include <winpr/tchar.h>
+#include <winpr/environment.h>
+#include <winpr/error.h>
+
+#define TEST_NAME "WINPR_TEST_VARIABLE"
+#define TEST_VALUE "WINPR_TEST_VALUE"
+int TestEnvironmentSetEnvironmentVariable(int argc, char* argv[])
+{
+ int rc = -1;
+ DWORD nSize = 0;
+ LPSTR lpBuffer = NULL;
+ DWORD error = 0;
+
+ WINPR_UNUSED(argc);
+ WINPR_UNUSED(argv);
+
+ SetEnvironmentVariableA(TEST_NAME, TEST_VALUE);
+ nSize = GetEnvironmentVariableA(TEST_NAME, NULL, 0);
+
+ /* check if value returned is len + 1 ) */
+ if (nSize != strnlen(TEST_VALUE, sizeof(TEST_VALUE)) + 1)
+ {
+ printf("GetEnvironmentVariableA not found error\n");
+ return -1;
+ }
+
+ lpBuffer = (LPSTR)malloc(nSize);
+
+ if (!lpBuffer)
+ return -1;
+
+ nSize = GetEnvironmentVariableA(TEST_NAME, lpBuffer, nSize);
+
+ if (nSize != strnlen(TEST_VALUE, sizeof(TEST_VALUE)))
+ {
+ printf("GetEnvironmentVariableA wrong size returned\n");
+ goto fail;
+ }
+
+ if (strcmp(lpBuffer, TEST_VALUE) != 0)
+ {
+ printf("GetEnvironmentVariableA returned value doesn't match\n");
+ goto fail;
+ }
+
+ nSize = GetEnvironmentVariableA("__xx__notset_", lpBuffer, nSize);
+ error = GetLastError();
+
+ if (0 != nSize || ERROR_ENVVAR_NOT_FOUND != error)
+ {
+ printf("GetEnvironmentVariableA not found error\n");
+ goto fail;
+ }
+
+ /* clear variable */
+ SetEnvironmentVariableA(TEST_NAME, NULL);
+ nSize = GetEnvironmentVariableA(TEST_VALUE, NULL, 0);
+
+ if (0 != nSize)
+ {
+ printf("SetEnvironmentVariableA failed to clear variable\n");
+ goto fail;
+ }
+
+ rc = 0;
+fail:
+ free(lpBuffer);
+ return rc;
+}