bookmarkfs/tests/check_lib.c
2025-03-09 11:35:17 +08:00

167 lines
3.6 KiB
C

/**
* bookmarkfs/tests/check_lib.c
* ----
*
* Copyright (C) 2025 CismonX <admin@cismon.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "check_lib.h"
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "frontend_util.h"
#include "hash.h"
#include "prng.h"
// Forward declaration start
static int dispatch_subcmds (int, char *[]);
static size_t hash_check_cb (void *, void const **);
static int subcmd_hash (int, char *[]);
static int subcmd_prng (int, char *[]);
// Forward declaration end
static int
dispatch_subcmds (
int argc,
char *argv[]
) {
if (--argc < 1) {
return -1;
}
char const *cmd = *(++argv);
int status = -1;
if (0 == strcmp("hash", cmd)) {
status = subcmd_hash(argc, argv);
} else if (0 == strcmp("prng", cmd)) {
status = subcmd_prng(argc, argv);
} else if (0 == strcmp("watcher", cmd)) {
status = check_watcher(argc, argv);
} else if (0 == strcmp("sandbox", cmd)) {
status = check_sandbox(argc, argv);
} else if (0 == strcmp("hashmap", cmd)) {
status = check_hashmap(argc, argv);
} else {
log_printf("bad subcmd '%s'", cmd);
}
return status;
}
static size_t
hash_check_cb (
void *UNUSED_VAR(user_data),
void const **buf_ptr
) {
static char buf[4096];
*buf_ptr = buf;
return fread(buf, 1, sizeof(buf), stdin);
}
static int
subcmd_hash (
int argc,
char *argv[]
) {
unsigned long long seed = 0;
getopt_foreach(argc, argv, ":s:") {
case 's':
seed = strtoull(optarg, NULL, 16);
break;
default:
log_printf("bad option '-%c'", optopt);
return -1;
}
hash_seed(seed);
printf("%016" PRIx64 "\n", hash_digestcb(hash_check_cb, NULL));
return 0;
}
static int
subcmd_prng (
int argc,
char *argv[]
) {
uint64_t seed_buf[4], *seed = NULL;
int n = 0;
getopt_foreach(argc, argv, ":s:n:") {
case 's':
if (0 != prng_seed_from_hex(seed_buf, optarg)) {
return -1;
}
seed = seed_buf;
break;
case 'n':
n = atoi(optarg);
break;
default:
log_printf("bad option '-%c'", optopt);
return -1;
}
if (0 != prng_seed(seed)) {
return -1;
}
for (; n > 0; --n) {
printf("%016" PRIx64 "\n", prng_rand());
}
return 0;
}
int
prng_seed_from_hex (
uint64_t *buf,
char const *str
) {
int cnt = sscanf(str,
"%16" SCNx64 "%16" SCNx64 "%16" SCNx64 "%16" SCNx64,
&buf[0], &buf[1], &buf[2], &buf[3]);
if (cnt != 4) {
log_printf("bad seed '%s'", optarg);
return -1;
}
return 0;
}
int
main (
int argc,
char *argv[]
) {
int status = EXIT_FAILURE;
if (0 != dispatch_subcmds(argc, argv)) {
goto end;
}
status = EXIT_SUCCESS;
end:
return status;
}