1
0
forked from cory/tildefriends
Files
apps
core
deps
codemirror
crypt_blowfish
libbacktrace
libbacktrace_config
libsodium
.github
build-aux
builds
contrib
dist-build
m4
msvc-scripts
packaging
regen-msvc
src
test
default
Makefile.am
Makefile.in
aead_aes256gcm.c
aead_aes256gcm.exp
aead_aes256gcm2.c
aead_aes256gcm2.exp
aead_chacha20poly1305.c
aead_chacha20poly1305.exp
aead_chacha20poly13052.c
aead_chacha20poly13052.exp
aead_xchacha20poly1305.c
aead_xchacha20poly1305.exp
auth.c
auth.exp
auth2.c
auth2.exp
auth3.c
auth3.exp
auth5.c
auth5.exp
auth6.c
auth6.exp
auth7.c
auth7.exp
box.c
box.exp
box2.c
box2.exp
box7.c
box7.exp
box8.c
box8.exp
box_easy.c
box_easy.exp
box_easy2.c
box_easy2.exp
box_seal.c
box_seal.exp
box_seed.c
box_seed.exp
chacha20.c
chacha20.exp
cmptest.h
codecs.c
codecs.exp
core1.c
core1.exp
core2.c
core2.exp
core3.c
core3.exp
core4.c
core4.exp
core5.c
core5.exp
core6.c
core6.exp
core_ed25519.c
core_ed25519.exp
core_ristretto255.c
core_ristretto255.exp
ed25519_convert.c
ed25519_convert.exp
generichash.c
generichash.exp
generichash2.c
generichash2.exp
generichash3.c
generichash3.exp
hash.c
hash.exp
hash3.c
hash3.exp
index.html.tpl
kdf.c
kdf.exp
keygen.c
keygen.exp
kx.c
kx.exp
metamorphic.c
metamorphic.exp
misuse.c
misuse.exp
onetimeauth.c
onetimeauth.exp
onetimeauth2.c
onetimeauth2.exp
onetimeauth7.c
onetimeauth7.exp
pre.js.inc
pwhash_argon2i.c
pwhash_argon2i.exp
pwhash_argon2id.c
pwhash_argon2id.exp
pwhash_scrypt.c
pwhash_scrypt.exp
pwhash_scrypt_ll.c
pwhash_scrypt_ll.exp
randombytes.c
randombytes.exp
run.sh
scalarmult.c
scalarmult.exp
scalarmult2.c
scalarmult2.exp
scalarmult5.c
scalarmult5.exp
scalarmult6.c
scalarmult6.exp
scalarmult7.c
scalarmult7.exp
scalarmult8.c
scalarmult8.exp
scalarmult_ed25519.c
scalarmult_ed25519.exp
scalarmult_ristretto255.c
scalarmult_ristretto255.exp
secretbox.c
secretbox.exp
secretbox2.c
secretbox2.exp
secretbox7.c
secretbox7.exp
secretbox8.c
secretbox8.exp
secretbox_easy.c
secretbox_easy.exp
secretbox_easy2.c
secretbox_easy2.exp
secretstream.c
secretstream.exp
shorthash.c
shorthash.exp
sign.c
sign.exp
siphashx24.c
siphashx24.exp
sodium_core.c
sodium_core.exp
sodium_utils.c
sodium_utils.exp
sodium_utils2.c
sodium_utils2.exp
sodium_utils3.c
sodium_utils3.exp
sodium_version.c
sodium_version.exp
stream.c
stream.exp
stream2.c
stream2.exp
stream3.c
stream3.exp
stream4.c
stream4.exp
verify1.c
verify1.exp
wasi-test-wrapper.sh
wintest.bat
xchacha20.c
xchacha20.exp
quirks
Makefile.am
Makefile.in
constcheck.sh
.gitignore
AUTHORS
ChangeLog
LICENSE
Makefile.am
Makefile.in
README.markdown
THANKS
aclocal.m4
appveyor.yml
autogen.sh
azure-pipelines.yml
build.zig
configure
configure.ac
lgtm.yml
libsodium-uninstalled.pc.in
libsodium.pc.in
libsodium.sln
libsodium.vcxproj
libsodium.vcxproj.filters
logo.png
libuv
lit
openssl
picohttpparser
quickjs
speedscope
sqlite
xopt
zlib
docs
src
tools
.dockerignore
Dockerfile
LICENSE
Makefile
README.md
tildefriends/deps/libsodium/test/default/cmptest.h
2023-02-19 23:23:53 +00:00

239 lines
4.8 KiB
C

#ifndef __CMPTEST_H__
#define __CMPTEST_H__
#ifdef NDEBUG
#/**/undef/**/ NDEBUG
#endif
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "sodium.h"
#include "quirks.h"
#ifdef __EMSCRIPTEN__
# undef TEST_SRCDIR
# define TEST_SRCDIR "/test-data"
#endif
#ifndef TEST_SRCDIR
# define TEST_SRCDIR "."
#endif
#define TEST_NAME_RES TEST_NAME ".res"
#define TEST_NAME_OUT TEST_SRCDIR "/" TEST_NAME ".exp"
#ifdef HAVE_ARC4RANDOM
# undef rand
# define rand(X) arc4random(X)
#endif
int xmain(void);
static unsigned char *guard_page;
#ifdef BENCHMARKS
# include <sys/time.h>
# ifndef ITERATIONS
# define ITERATIONS 128
# endif
struct {
void *pnt;
size_t size;
} mempool[1024];
static size_t mempool_idx;
static __attribute__((malloc)) void *mempool_alloc(size_t size)
{
size_t i;
if (size >= (size_t) 0x80000000 - (size_t) 0x00000fff) {
return NULL;
}
size = (size + (size_t) 0x00000fff) & ~ (size_t) 0x00000fff;
for (i = 0U; i < mempool_idx; i++) {
if (mempool[i].size >= (size | (size_t) 0x80000000)) {
mempool[i].size &= ~ (size_t) 0x80000000;
return mempool[i].pnt;
}
}
if (mempool_idx >= sizeof mempool / sizeof mempool[0]) {
return NULL;
}
mempool[mempool_idx].size = size;
return (mempool[mempool_idx++].pnt = (void *) malloc(size));
}
static void mempool_free(void *pnt)
{
size_t i;
for (i = 0U; i < mempool_idx; i++) {
if (mempool[i].pnt == pnt) {
if ((mempool[i].size & (size_t) 0x80000000) != (size_t) 0x0) {
break;
}
mempool[i].size |= (size_t) 0x80000000;
return;
}
}
abort();
}
static __attribute__((malloc)) void *mempool_allocarray(size_t count, size_t size)
{
if (count > (size_t) 0U && size >= (size_t) SIZE_MAX / count) {
return NULL;
}
return mempool_alloc(count * size);
}
static int mempool_free_all(void)
{
size_t i;
int ret = 0;
for (i = 0U; i < mempool_idx; i++) {
if ((mempool[i].size & (size_t) 0x80000000) == (size_t) 0x0) {
ret = -1;
}
free(mempool[i].pnt);
mempool[i].pnt = NULL;
}
mempool_idx = (size_t) 0U;
return ret;
}
#define sodium_malloc(X) mempool_alloc(X)
#define sodium_free(X) mempool_free(X)
#define sodium_allocarray(X, Y) mempool_allocarray((X), (Y))
static unsigned long long now(void)
{
#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
struct timespec tp;
if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0) {
abort();
}
return (unsigned long long) tp.tv_sec * 1000000ULL +
(unsigned long long) tp.tv_nsec / 1000ULL;
#else
struct timeval tp;
if (gettimeofday(&tp, NULL) != 0) {
abort();
}
return (unsigned long long) tp.tv_sec * 1000000ULL +
(unsigned long long) tp.tv_usec;
#endif
}
int main(void)
{
unsigned long long ts_start;
unsigned long long ts_end;
unsigned int i;
if (sodium_init() != 0) {
return 99;
}
#ifndef __EMSCRIPTEN__
randombytes_set_implementation(&randombytes_salsa20_implementation);
#endif
ts_start = now();
for (i = 0; i < ITERATIONS; i++) {
if (xmain() != 0) {
abort();
}
}
ts_end = now();
printf("%llu\n", 1000000ULL * (ts_end - ts_start) / ITERATIONS);
if (mempool_free_all() != 0) {
fprintf(stderr, "** memory leaks detected **\n");
return 99;
}
return 0;
}
#undef printf
#define printf(...) do { } while(0)
#elif !defined(BROWSER_TESTS)
static FILE *fp_res;
int main(void)
{
FILE *fp_out;
unsigned char *_guard_page;
int c;
if ((fp_res = fopen(TEST_NAME_RES, "w+")) == NULL) {
perror("fopen(" TEST_NAME_RES ")");
return 99;
}
if (sodium_init() != 0) {
return 99;
}
# if defined(__EMSCRIPTEN__) || defined(__SANITIZE_ADDRESS__)
guard_page = _guard_page = NULL;
#else
if ((_guard_page = (unsigned char *) sodium_malloc(0)) == NULL) {
perror("sodium_malloc()");
return 99;
}
guard_page = _guard_page + 1;
#endif
if (xmain() != 0) {
return 99;
}
fflush(fp_res);
rewind(fp_res);
if ((fp_out = fopen(TEST_NAME_OUT, "r")) == NULL) {
perror("fopen(" TEST_NAME_OUT ")");
return 99;
}
do {
if ((c = fgetc(fp_res)) != fgetc(fp_out)) {
return 99;
}
} while (c != EOF);
sodium_free(_guard_page);
return 0;
}
#undef printf
#define printf(...) fprintf(fp_res, __VA_ARGS__)
#else
int main(void)
{
if (sodium_init() != 0) {
return 99;
}
if (xmain() != 0) {
return 99;
}
printf("--- SUCCESS ---\n");
return 0;
}
#endif
#define main xmain
#endif