Path: blob/main/lib/libc/tests/string/memcmp_test.c
39485 views
/*-1* Copyright (c) 2016 Jilles Tjoelker <[email protected]>2* Copyright (c) 2023 The FreeBSD Foundation3* All rights reserved.4*5* Portions of this software were developed by Robert Clausecker6* <[email protected]> under sponsorship from the FreeBSD Foundation.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE21* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27* SUCH DAMAGE.28*/2930#include <assert.h>31#include <dlfcn.h>32#include <stdio.h>33#include <stdlib.h>34#include <string.h>3536#include <atf-c.h>3738#ifndef MEMCMP39#define MEMCMP memcmp40#endif4142/*43* On FreeBSD we previously demanded that memcmp returns the difference44* between the characters at the first site of mismatch. However,45* ISO/IEC 9899:1990 only specifies that a number greater than, equal46* to, or less than zero shall be returned. If a unit test for the47* more strict behaviour is desired, define RES(x) to be (x).48*/49#ifndef RES50#define RES(x) (((x) > 0) - ((x) < 0))51#endif5253static int (*memcmp_fn)(const void *, const void *, size_t);5455static void56check_memcmp(const char *a, const char *b, size_t len, int expected)57{58int got;5960got = memcmp_fn(a, b, len);61ATF_CHECK_EQ_MSG(RES(expected), RES(got),62"%s(%p, %p, %zu) gave %d, but wanted %d",63__XSTRING(MEMCMP), a, b, len, got, expected);64}6566ATF_TC_WITHOUT_HEAD(zero);67ATF_TC_BODY(zero, tc)68{6970check_memcmp("a", "b", 0, 0);71check_memcmp("", "", 0, 0);72}7374ATF_TC_WITHOUT_HEAD(eq);75ATF_TC_BODY(eq, tc)76{77unsigned char data1[256], data2[256];78int i;7980for (i = 0; i < 256; i++)81data1[i] = data2[i] = i ^ 0x55;82for (i = 1; i < 256; i++)83check_memcmp(data1, data2, i, 0);84for (i = 1; i < 256; i++)85check_memcmp(data1 + i, data2 + i, 256 - i, 0);86}8788ATF_TC_WITHOUT_HEAD(neq);89ATF_TC_BODY(neq, tc)90{91unsigned char data1[256], data2[256];92int i;9394for (i = 0; i < 256; i++) {95data1[i] = i;96data2[i] = i ^ 0x55;97}98for (i = 1; i < 256; i++)99check_memcmp(data1, data2, i, -0x55);100for (i = 1; i < 256; i++)101check_memcmp(data1 + i, data2 + i, 256 - i, i - (i ^ 0x55));102}103104ATF_TC_WITHOUT_HEAD(diff);105ATF_TC_BODY(diff, tc)106{107unsigned char data1[256], data2[256];108int i;109110memset(data1, 'a', sizeof(data1));111memset(data2, 'a', sizeof(data2));112data1[128] = 255;113data2[128] = 0;114for (i = 1; i < 66; i++) {115check_memcmp(data1 + 128, data2 + 128, i, 255);116check_memcmp(data2 + 128, data1 + 128, i, -255);117check_memcmp(data1 + 129 - i, data2 + 129 - i, i, 255);118check_memcmp(data2 + 129 - i, data1 + 129 - i, i, -255);119check_memcmp(data1 + 129 - i, data2 + 129 - i, i * 2, 255);120check_memcmp(data2 + 129 - i, data1 + 129 - i, i * 2, -255);121}122data1[128] = 'c';123data2[128] = 'e';124for (i = 1; i < 66; i++) {125check_memcmp(data1 + 128, data2 + 128, i, -2);126check_memcmp(data2 + 128, data1 + 128, i, 2);127check_memcmp(data1 + 129 - i, data2 + 129 - i, i, -2);128check_memcmp(data2 + 129 - i, data1 + 129 - i, i, 2);129check_memcmp(data1 + 129 - i, data2 + 129 - i, i * 2, -2);130check_memcmp(data2 + 129 - i, data1 + 129 - i, i * 2, 2);131}132memset(data1 + 129, 'A', sizeof(data1) - 129);133memset(data2 + 129, 'Z', sizeof(data2) - 129);134for (i = 1; i < 66; i++) {135check_memcmp(data1 + 128, data2 + 128, i, -2);136check_memcmp(data2 + 128, data1 + 128, i, 2);137check_memcmp(data1 + 129 - i, data2 + 129 - i, i, -2);138check_memcmp(data2 + 129 - i, data1 + 129 - i, i, 2);139check_memcmp(data1 + 129 - i, data2 + 129 - i, i * 2, -2);140check_memcmp(data2 + 129 - i, data1 + 129 - i, i * 2, 2);141}142}143144ATF_TP_ADD_TCS(tp)145{146void *dl_handle;147148dl_handle = dlopen(NULL, RTLD_LAZY);149memcmp_fn = dlsym(dl_handle, "test_" __XSTRING(MEMCMP));150if (memcmp_fn == NULL)151memcmp_fn = MEMCMP;152153ATF_TP_ADD_TC(tp, zero);154ATF_TP_ADD_TC(tp, eq);155ATF_TP_ADD_TC(tp, neq);156ATF_TP_ADD_TC(tp, diff);157158return (atf_no_error());159}160161162