Path: blob/main/lib/libc/tests/string/strncmp_test.c
39485 views
/*-1* Copyright (c) 2023 The FreeBSD Foundation2*3* This software was developed by Robert Clausecker <[email protected]>4* under sponsorship from the FreeBSD Foundation.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ''AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE26*/2728#include <sys/cdefs.h>2930#include <atf-c.h>31#include <dlfcn.h>32#include <string.h>3334int (*volatile strncmp_fn)(const char *, const char *, size_t);3536static void37alignment_testcase(char *a, char *b, int want, size_t len)38{39int res;4041res = strncmp_fn(a, b, len);42ATF_CHECK_MSG(want == (res > 0) - (res < 0),43"strcmp(%p \"%s\", %p \"%s\", %zu) = %d != %d",44(void *)a, a, (void *)b, b, len, res, want);45}4647static void48check_strncmp_alignments(char a[], char b[],49size_t a_off, size_t b_off, size_t len, size_t pos)50{51char *a_str, *b_str, a_orig, b_orig;5253a[a_off] = '\0';54b[b_off] = '\0';5556a_str = a + a_off + 1;57b_str = b + b_off + 1;5859a_str[len] = '\0';60b_str[len] = '\0';61a_str[len+1] = 'A';62b_str[len+1] = 'B';6364a_orig = a_str[pos];65b_orig = b_str[pos];6667alignment_testcase(a_str, b_str, 0, len + 16);68alignment_testcase(a_str, b_str, 0, len + 1);69alignment_testcase(a_str, b_str, 0, len);7071if (pos < len) {72a_str[pos] = '\0';73alignment_testcase(a_str, b_str, -1, len + 16);74alignment_testcase(a_str, b_str, -1, len + 1);75alignment_testcase(a_str, b_str, -1, len);76alignment_testcase(a_str, b_str, -1, pos + 1);77alignment_testcase(a_str, b_str, 0, pos);78a_str[pos] = a_orig;7980b_str[pos] = '\0';81alignment_testcase(a_str, b_str, 1, len + 16);82alignment_testcase(a_str, b_str, 1, len + 1);83alignment_testcase(a_str, b_str, 1, len);84alignment_testcase(a_str, b_str, 1, pos + 1);85alignment_testcase(a_str, b_str, 0, pos);86b_str[pos] = b_orig;87}8889a_str[pos] = 'X';90alignment_testcase(a_str, b_str, 1, len + 16);91alignment_testcase(a_str, b_str, 0, pos);92alignment_testcase(a_str, b_str, 1, pos + 1);93if (pos < len) {94alignment_testcase(a_str, b_str, 1, len);95alignment_testcase(a_str, b_str, 1, len + 1);96}97a_str[pos] = a_orig;9899b_str[pos] = 'X';100alignment_testcase(a_str, b_str, -1, len + 16);101alignment_testcase(a_str, b_str, 0, pos);102alignment_testcase(a_str, b_str, -1, pos + 1);103if (pos < len) {104alignment_testcase(a_str, b_str, -1, len);105alignment_testcase(a_str, b_str, -1, len + 1);106}107b_str[pos] = b_orig;108109a[a_off] = '-';110b[b_off] = '-';111a_str[len] = '-';112b_str[len] = '-';113a_str[len+1] = '-';114b_str[len+1] = '-';115}116117ATF_TC(strncmp_alignments);118ATF_TC_HEAD(strncmp_alignments, tc)119{120atf_tc_set_md_var(tc, "descr", "Test strncmp(3) with various alignments");121}122123ATF_TC_BODY(strncmp_alignments, tc)124{125size_t a_off, b_off, len, pos;126char a[64+16+16+3], b[64+16+16+3];127128memset(a, '-', sizeof(a));129memset(b, '-', sizeof(b));130a[sizeof(a) - 1] = '\0';131b[sizeof(b) - 1] = '\0';132133for (a_off = 0; a_off < 16; a_off++)134for (b_off = 0; b_off < 16; b_off++)135for (len = 1; len <= 64; len++)136for (pos = 0; pos <= len; pos++)137check_strncmp_alignments(a, b, a_off, b_off, len, pos);138}139140ATF_TC(strncmp_null);141ATF_TC_HEAD(strncmp_null, tc)142{143atf_tc_set_md_var(tc, "descr", "Test strncmp(3) with null pointers");144}145146ATF_TC_BODY(strncmp_null, tc)147{148alignment_testcase(NULL, NULL, 0, 0);149}150151ATF_TP_ADD_TCS(tp)152{153void *dl_handle;154155dl_handle = dlopen(NULL, RTLD_LAZY);156strncmp_fn = dlsym(dl_handle, "test_strncmp");157if (strncmp_fn == NULL)158strncmp_fn = strncmp;159160ATF_TP_ADD_TC(tp, strncmp_alignments);161ATF_TP_ADD_TC(tp, strncmp_null);162163return atf_no_error();164}165166167