Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependency on strings.h #481

Merged
merged 3 commits into from
Mar 2, 2017
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
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ if(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
add_flag("-Wno-cast-align")
add_flag("-Wno-conversion")
add_flag("-Wno-covered-switch-default")
# Due to clang's tolower() macro being recursive https://github.com/TokTok/c-toxcore/pull/481
add_flag("-Wno-disabled-macro-expansion")
add_flag("-Wno-documentation-deprecated-sync")
add_flag("-Wno-format-nonliteral")
add_flag("-Wno-missing-field-initializers")
Expand Down Expand Up @@ -421,6 +423,7 @@ auto_test(tox)
auto_test(tox_many)
auto_test(tox_many_tcp)
auto_test(tox_one)
auto_test(tox_strncasecmp)
auto_test(version)

if(BUILD_TOXAV)
Expand Down
11 changes: 9 additions & 2 deletions auto_tests/Makefile.inc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if BUILD_TESTS

TESTS = encryptsave_test messenger_autotest crypto_test network_test onion_test TCP_test tox_test dht_autotest
check_PROGRAMS = encryptsave_test messenger_autotest crypto_test network_test onion_test TCP_test tox_test dht_autotest
TESTS = encryptsave_test messenger_autotest crypto_test network_test onion_test TCP_test tox_test dht_autotest tox_strncasecmp_test
check_PROGRAMS = encryptsave_test messenger_autotest crypto_test network_test onion_test TCP_test tox_test dht_autotest tox_strncasecmp_test

AUTOTEST_CFLAGS = \
$(LIBSODIUM_CFLAGS) \
Expand Down Expand Up @@ -100,4 +100,11 @@ encryptsave_test_CFLAGS = $(AUTOTEST_CFLAGS)
encryptsave_test_LDADD = $(AUTOTEST_LDADD)


tox_strncasecmp_test_SOURCES = ../auto_tests/tox_strncasecmp_test.c

tox_strncasecmp_test_CFLAGS = $(AUTOTEST_CFLAGS)

tox_strncasecmp_test_LDADD = $(AUTOTEST_LDADD)


EXTRA_DIST += $(top_srcdir)/auto_tests/helpers.h
193 changes: 193 additions & 0 deletions auto_tests/tox_strncasecmp_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <check.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>

#include "helpers.h"

#include "../testing/misc_tools.c"

typedef enum {
NEGATIVE,
ZERO,
POSITIVE
} Comparison;

static const char *Comparison_Str[] = { "NEGATIVE", "ZERO", "POSITIVE" };

void verify(const char *s1, const char *s2, size_t n, Comparison expected)
{
int r = tox_strncasecmp(s1, s2, n);
Comparison actual = r < 0 ? NEGATIVE : r == 0 ? ZERO : POSITIVE;

ck_assert_msg(actual == expected,
"tox_strncasecmp(\"%s\", \"%s\", %u) == %s, but expected %s.",
s1, s2, n, Comparison_Str[actual], Comparison_Str[expected]);
}

START_TEST(test_general)
{
// empty strings are equal
verify("", "", 100, ZERO);
verify("", "", -1, ZERO);

// ====== Same Case Test Cases ======

// equal strings with n=0 are equal
verify("", "", 0, ZERO);
verify("AAA", "AAA", 0, ZERO);

// unequal strings with n=0 are equal
verify("A", "B", 0, ZERO);
verify("AAA", "BBB", 0, ZERO);
verify("AAA", "BBBBBB", 0 , ZERO);
verify("AAAAAA", "BBB", 0, ZERO);

// equal strings are equal
verify("AAA", "AAA", 0, ZERO);
verify("AAA", "AAA", 1, ZERO);
verify("AAA", "AAA", 2, ZERO);
verify("AAA", "AAA", 3, ZERO);
verify("AAA", "AAA", 4, ZERO);
verify("AAA", "AAA", 5, ZERO);
verify("AAA", "AAA", -1, ZERO);

verify("AAA", "AAAAAA", 0, ZERO);
verify("AAA", "AAAAAA", 1, ZERO);
verify("AAA", "AAAAAA", 2, ZERO);
verify("AAA", "AAAAAA", 3, ZERO);
verify("AAA", "AAAAAA", 4, NEGATIVE);
verify("AAA", "AAAAAA", 5, NEGATIVE);
verify("AAA", "AAAAAA", -1, NEGATIVE);

verify("AAAAAA", "AAA", 0, ZERO);
verify("AAAAAA", "AAA", 1, ZERO);
verify("AAAAAA", "AAA", 2, ZERO);
verify("AAAAAA", "AAA", 3, ZERO);
verify("AAAAAA", "AAA", 4, POSITIVE);
verify("AAAAAA", "AAA", 5, POSITIVE);
verify("AAAAAA", "AAA", -1, POSITIVE);

verify("I'm eating wafers and drinking tea.", "I'm eating wafers and drinking tea.", -1, ZERO);

// unequal strings are equal only up to n
verify("AAAB", "AAAA", 0, ZERO);
verify("AAAB", "AAAA", 1, ZERO);
verify("AAAB", "AAAA", 2, ZERO);
verify("AAAB", "AAAA", 3, ZERO);
verify("AAAB", "AAAA", 4, POSITIVE);
verify("AAAB", "AAAA", 5, POSITIVE);
verify("AAAB", "AAAA", -1, POSITIVE);

verify("AAAA", "AAAB", 0, ZERO);
verify("AAAA", "AAAB", 1, ZERO);
verify("AAAA", "AAAB", 2, ZERO);
verify("AAAA", "AAAB", 3, ZERO);
verify("AAAA", "AAAB", 4, NEGATIVE);
verify("AAAA", "AAAB", 5, NEGATIVE);
verify("AAAA", "AAAB", -1, NEGATIVE);

verify("The wafers are salty.", "The wafers are sweet.", 16, ZERO);
verify("The wafers are salty.", "The wafers are sweet.", 17, NEGATIVE);
verify("The wafers are salty.", "The wafers are sweet.", -1, NEGATIVE);

// the comparison should stop at first mismatch
verify("AAABA", "AAAAB", -1, POSITIVE);
verify("AAAAB", "AAABA", -1, NEGATIVE);

// ====== Different Case Test Cases ======

// equal strings with n=0 are equal
verify("", "", 0, ZERO);
verify("aaa", "AAA", 0, ZERO);

// unequal strings with n=0 are equal
verify("a", "B", 0, ZERO);
verify("aaa", "BBB", 0, ZERO);
verify("aaa", "BBBBBB", 0 , ZERO);
verify("aaaaaa", "BBB", 0, ZERO);

// equal strings are equal
verify("aaa", "AAA", 0, ZERO);
verify("AAA", "aaa", 1, ZERO);
verify("aaa", "AAA", 2, ZERO);
verify("aaa", "AAA", 3, ZERO);
verify("AAA", "aaa", 4, ZERO);
verify("AAA", "aaa", 5, ZERO);
verify("AAA", "aaa", -1, ZERO);

verify("aaa", "AAAAAA", 0, ZERO);
verify("AAA", "AAAaaa", 1, ZERO);
verify("aaA", "aaaAAA", 2, ZERO);
verify("AaA", "aAAAAA", 3, ZERO);
verify("AAA", "AAAAAA", 4, NEGATIVE);
verify("Aaa", "AAaaAA", 5, NEGATIVE);
verify("AAA", "AAAAAa", -1, NEGATIVE);

verify("AAAAAA", "aaa", 0, ZERO);
verify("AAAaaa", "AAA", 1, ZERO);
verify("aaaAAA", "aaA", 2, ZERO);
verify("aAAAAA", "AaA", 3, ZERO);
verify("AAAAAA", "AAA", 4, POSITIVE);
verify("AAaaAA", "Aaa", 5, POSITIVE);
verify("AAAAAa", "AAA", -1, POSITIVE);

verify("I'm Eating Wafers And Drinking Tea.", "I'm eating wafers and drinking tea.", -1, ZERO);

// unequal strings are equal only up to n
verify("aaaB", "AAAA", 0, ZERO);
verify("AaAB", "aAAA", 1, ZERO);
verify("aAAB", "AaAA", 2, ZERO);
verify("AAAB", "AAaA", 3, ZERO);
verify("AAAB", "AAAA", 4, POSITIVE);
verify("AAAb", "AAAA", 5, POSITIVE);
verify("AAAB", "AAAa", -1, POSITIVE);

verify("AAAA", "aaaB", 0, ZERO);
verify("aAAA", "AaAB", 1, ZERO);
verify("AaAA", "aAAB", 2, ZERO);
verify("AAaA", "AAAB", 3, ZERO);
verify("AAAA", "AAAB", 4, NEGATIVE);
verify("AAAA", "AAAb", 5, NEGATIVE);
verify("AAAa", "AAAB", -1, NEGATIVE);

verify("The Wafers Are Salty.", "The wafers are sweet.", 16, ZERO);
verify("The Wafers Are Salty.", "The wafers are sweet.", 17, NEGATIVE);
verify("The Wafers Are Salty.", "The wafers are sweet.", -1, NEGATIVE);

// the comparison should stop at first mismatch
verify("aAaBA", "AAAAb", -1, POSITIVE);
verify("AAAAb", "aAaBA", -1, NEGATIVE);
}
END_TEST

static Suite *tox_strncasecmp_suite(void)
{
Suite *s = suite_create("tox_strncasecmp");

DEFTESTCASE(general);

return s;
}

int main(int argc, char *argv[])
{
srand((unsigned int) time(NULL));

Suite *s = tox_strncasecmp_suite();
SRunner *test_runner = srunner_create(s);

int number_failed = 0;
srunner_run_all(test_runner, CK_NORMAL);
number_failed = srunner_ntests_failed(test_runner);

srunner_free(test_runner);

return number_failed;
}
2 changes: 1 addition & 1 deletion other/DHT_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ static void manage_keys(DHT *dht)

int main(int argc, char *argv[])
{
if (argc == 2 && !strncasecmp(argv[1], "-h", 3)) {
if (argc == 2 && !tox_strncasecmp(argv[1], "-h", 3)) {
printf("Usage (connected) : %s [--ipv4|--ipv6] IP PORT KEY\n", argv[0]);
printf("Usage (unconnected): %s [--ipv4|--ipv6]\n", argv[0]);
exit(0);
Expand Down
21 changes: 19 additions & 2 deletions testing/misc_tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
#include "config.h"
#endif

#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

// You are responsible for freeing the return value!
uint8_t *hex_string_to_bin(const char *hex_string)
Expand All @@ -50,12 +50,29 @@ uint8_t *hex_string_to_bin(const char *hex_string)
return ret;
}

/* Reimplementation of strncasecmp() function from strings.h, as strings.h is
* POSIX and not portable. Specifically it doesn't exist on MSVC.
*/
int tox_strncasecmp(const char *s1, const char *s2, size_t n)
{
while (n--) {
int c1 = tolower(*(s1++));
int c2 = tolower(*(s2++));

if (c1 == '\0' || c2 == '\0' || c1 != c2) {
return c1 - c2;
}
}

return 0;
}

int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled)
{
int argvoffset = 0, argi;

for (argi = 1; argi < argc; argi++) {
if (!strncasecmp(argv[argi], "--ipv", 5)) {
if (!tox_strncasecmp(argv[argi], "--ipv", 5)) {
if (argv[argi][5] && !argv[argi][6]) {
char c = argv[argi][5];

Expand Down