Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ jobs:
windows-mingw:
name: Windows MinGW CMake
runs-on: windows-latest
# Known failure: contrib/win32/stdlib.c declares `char *str[32]` and passes it
# to strcpy (nfc-tools/libnfc#765). Remove this line once that is fixed.
continue-on-error: true
defaults:
run:
shell: msys2 {0}
Expand All @@ -111,7 +108,9 @@ jobs:
cp /mingw64/lib/libusb0.dll.a build/LibUSB-Win32/lib/gcc/libusb.dll.a
- name: Configure and build
run: |
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug -DBUILD_SOFTWARE_TESTS=ON
# Examples are off: pn53x-diagnose, pn53x-sam and pn53x-tamashell do not link
# on Windows (maksimtech/libnfc#3)
cmake -S . -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug -DBUILD_EXAMPLES=OFF -DBUILD_SOFTWARE_TESTS=ON
cmake --build build
- name: Run tests
run: ctest --test-dir build --no-tests=error --output-on-failure
30 changes: 17 additions & 13 deletions contrib/win32/stdlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,28 @@
// Handle platform specific includes
#include "contrib/windows.h"

//There is no setenv()and unsetenv() in windows,but we can use putenv() instead.
#include <stdlib.h>
#include <string.h>

// There is no setenv() and unsetenv() in Windows, but _putenv_s() is there: it
// copies its arguments, so there is no fixed-size buffer to overflow.
int setenv(const char *name, const char *value, int overwrite)
{
char *env = getenv(name);
if ((env && overwrite) || (!env)) {
char *str[32];
strcpy(str, name);
strcat(str, "=");
strcat(str, value);
return putenv(str);
if (!name || !*name || strchr(name, '=') || !value) {
return -1;
}
// POSIX: an existing variable is left alone, and that is not an error
if (!overwrite && getenv(name)) {
return 0;
}
return -1;
return _putenv_s(name, value) == 0 ? 0 : -1;
}

void unsetenv(const char *name)
{
char *str[32];
strcpy(str, name);
strcat(str, "=");
putenv(str);
if (!name || !*name || strchr(name, '=')) {
return;
}
// an empty value removes the variable from the environment
_putenv_s(name, "");
}
6 changes: 6 additions & 0 deletions test/software/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ ADD_TEST(NAME no_hardware COMMAND test_no_hardware)
# on the readers plugged into the machine running the tests.
SET_TESTS_PROPERTIES(no_hardware PROPERTIES
ENVIRONMENT "LIBNFC_AUTO_SCAN=false;LIBNFC_INTRUSIVE_SCAN=false;LIBNFC_LOG_LEVEL=0")

IF(WIN32)
# setenv()/unsetenv() replacements that Windows lacks
ADD_EXECUTABLE(test_win32_stdlib test_win32_stdlib.c ${CMAKE_SOURCE_DIR}/contrib/win32/stdlib.c)
ADD_TEST(NAME win32_stdlib COMMAND test_win32_stdlib)
ENDIF(WIN32)
91 changes: 91 additions & 0 deletions test/software/test_win32_stdlib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*-
* Free/Libre Near Field Communication (NFC) library
*
* Windows only: tests of the setenv()/unsetenv() replacements in
* contrib/win32/stdlib.c, which Windows lacks.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "contrib/windows.h"

static int failures = 0;

#define CHECK(cond) \
do { \
if (!(cond)) { \
fprintf(stderr, "%s:%d: check failed: %s\n", __FILE__, __LINE__, #cond); \
failures++; \
} \
} while (0)

static int
value_is(const char *name, const char *expected)
{
const char *value = getenv(name);
return value != NULL && strcmp(value, expected) == 0;
}

static void
test_set_and_unset(void)
{
CHECK(setenv("LIBNFC_TEST_VAR", "first", 1) == 0);
CHECK(value_is("LIBNFC_TEST_VAR", "first"));

unsetenv("LIBNFC_TEST_VAR");
CHECK(getenv("LIBNFC_TEST_VAR") == NULL);
}

/* POSIX: with overwrite == 0 an existing variable is left alone and the call succeeds. */
static void
test_overwrite_flag(void)
{
CHECK(setenv("LIBNFC_TEST_VAR", "first", 1) == 0);

CHECK(setenv("LIBNFC_TEST_VAR", "second", 0) == 0);
CHECK(value_is("LIBNFC_TEST_VAR", "first"));

CHECK(setenv("LIBNFC_TEST_VAR", "second", 1) == 0);
CHECK(value_is("LIBNFC_TEST_VAR", "second"));

unsetenv("LIBNFC_TEST_VAR");
}

/* The names and values libnfc itself sets are short, but the function must not
* assume that: a 40-character name and a 200-character value. */
static void
test_long_name_and_value(void)
{
const char *name = "LIBNFC_TEST_A_VARIABLE_WITH_A_LONG_NAME";
char value[201];
memset(value, 'x', sizeof(value) - 1);
value[sizeof(value) - 1] = '\0';

CHECK(setenv(name, value, 1) == 0);
CHECK(value_is(name, value));

unsetenv(name);
CHECK(getenv(name) == NULL);
}

int
main(void)
{
test_set_and_unset();
test_overwrite_flag();
test_long_name_and_value();

if (failures) {
fprintf(stderr, "%d check(s) failed\n", failures);
return 1;
}
printf("all checks passed\n");
return 0;
}
Loading