Files
shadowsocks-libev/tests/test_cache.c
Max LvandClaude Opus 4.6 1c404eccf4 Modernize build: switch to CMake, add unit tests, fix man pages
Remove autotools (configure.ac, Makefile.am, m4/, autogen.sh) and make
CMake the sole build system. Enhance CMake with proper Find modules for
MbedTLS, libsodium, PCRE, and c-ares with version/feature detection.
Add support for connmarktos, nftables, stack protector, and MinGW.

Add 10 unit test suites (CTest) covering base64, buffer, cache, crypto,
jconf, json, netutils, ppbloom, rule, and utils modules. Add a Python
stress test measuring bandwidth per cipher with memory leak detection.

Fix man page typos ("Resovle" -> "Resolve"), document missing TCP buffer
options, fix ss-manager duplicate --executable entry, add example section
to ss-tunnel, and document --workdir and --nftables-sets options.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-07 16:40:27 +08:00

130 lines
2.4 KiB
C

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
int verbose = 0;
#include "cache.h"
static void
test_create_delete(void)
{
struct cache *c = NULL;
int ret = cache_create(&c, 100, NULL);
assert(ret == 0);
assert(c != NULL);
ret = cache_delete(c, 0);
assert(ret == 0);
(void)ret;
}
static void
test_create_null(void)
{
int ret = cache_create(NULL, 100, NULL);
assert(ret == EINVAL);
(void)ret;
}
static void
test_insert_lookup(void)
{
struct cache *c = NULL;
cache_create(&c, 100, NULL);
char *data = strdup("test_data");
cache_insert(c, "key1", 4, data);
char *result = NULL;
cache_lookup(c, "key1", 4, &result);
assert(result != NULL);
assert(strcmp(result, "test_data") == 0);
cache_delete(c, 0);
}
static void
test_key_exist(void)
{
struct cache *c = NULL;
cache_create(&c, 100, NULL);
char *data = strdup("value");
cache_insert(c, "mykey", 5, data);
assert(cache_key_exist(c, "mykey", 5) == 1);
assert(cache_key_exist(c, "nokey", 5) == 0);
cache_delete(c, 0);
}
static void
test_remove(void)
{
struct cache *c = NULL;
cache_create(&c, 100, NULL);
char *data = strdup("to_remove");
cache_insert(c, "rmkey", 5, data);
assert(cache_key_exist(c, "rmkey", 5) == 1);
cache_remove(c, "rmkey", 5);
assert(cache_key_exist(c, "rmkey", 5) == 0);
cache_delete(c, 0);
}
static void
test_lookup_missing(void)
{
struct cache *c = NULL;
cache_create(&c, 100, NULL);
char *result = (char *)0xdeadbeef;
cache_lookup(c, "missing", 7, &result);
assert(result == NULL);
cache_delete(c, 0);
}
static void
test_eviction(void)
{
struct cache *c = NULL;
cache_create(&c, 3, NULL);
/* Insert 3 entries to fill cache */
cache_insert(c, "k1", 2, strdup("v1"));
cache_insert(c, "k2", 2, strdup("v2"));
cache_insert(c, "k3", 2, strdup("v3"));
/* This should trigger eviction of the oldest entry */
cache_insert(c, "k4", 2, strdup("v4"));
/* k1 should have been evicted */
assert(cache_key_exist(c, "k1", 2) == 0);
/* k4 should exist */
assert(cache_key_exist(c, "k4", 2) == 1);
cache_delete(c, 0);
}
int
main(void)
{
test_create_delete();
test_create_null();
test_insert_lookup();
test_key_exist();
test_remove();
test_lookup_missing();
test_eviction();
return 0;
}