Merge pull request #5 from m1ngsama/feat/dev-tools

[Feature] Development and testing infrastructure
This commit is contained in:
m1ngsama 2025-12-02 12:39:44 +08:00 committed by GitHub
commit cbdc91b04d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 114 additions and 1 deletions

2
.gitignore vendored
View file

@ -6,3 +6,5 @@ host_key
host_key.pub
*.swp
.DS_Store
test.log
*.dSYM/

View file

@ -23,7 +23,7 @@ SOURCES = $(wildcard $(SRC_DIR)/*.c)
OBJECTS = $(SOURCES:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
TARGET = tnt
.PHONY: all clean install uninstall
.PHONY: all clean install uninstall debug release asan valgrind check info
all: $(TARGET)
@ -56,6 +56,19 @@ release: CFLAGS += -O3 -DNDEBUG
release: clean $(TARGET)
strip $(TARGET)
asan: CFLAGS += -g -fsanitize=address -fno-omit-frame-pointer
asan: LDFLAGS += -fsanitize=address
asan: clean $(TARGET)
@echo "AddressSanitizer build complete. Run with: ASAN_OPTIONS=detect_leaks=1 ./tnt"
valgrind: debug
@echo "Run: valgrind --leak-check=full --track-origins=yes ./tnt"
# Static analysis
check:
@command -v cppcheck >/dev/null 2>&1 && cppcheck --enable=warning,performance --quiet src/ || echo "cppcheck not installed"
@command -v clang-tidy >/dev/null 2>&1 && clang-tidy src/*.c -- -Iinclude $(INCLUDES) || echo "clang-tidy not installed"
# Show build info
info:
@echo "Compiler: $(CC)"

60
test_basic.sh Executable file
View file

@ -0,0 +1,60 @@
#!/bin/sh
# Basic functional tests
# Usage: ./test_basic.sh
PORT=${PORT:-2222}
PASS=0
FAIL=0
cleanup() {
kill $SERVER_PID 2>/dev/null
rm -f test.log
}
trap cleanup EXIT
echo "=== TNT Basic Tests ==="
# Start server
./tnt -p $PORT >test.log 2>&1 &
SERVER_PID=$!
sleep 2
# Test 1: Server started
if kill -0 $SERVER_PID 2>/dev/null; then
echo "✓ Server started"
PASS=$((PASS + 1))
else
echo "✗ Server failed to start"
FAIL=$((FAIL + 1))
exit 1
fi
# Test 2: SSH connection
if timeout 5 ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
-o BatchMode=yes -p $PORT localhost exit 2>/dev/null; then
echo "✓ SSH connection works"
PASS=$((PASS + 1))
else
echo "✗ SSH connection failed"
FAIL=$((FAIL + 1))
fi
# Test 3: Message logging
echo "test message" | timeout 5 ssh -o StrictHostKeyChecking=no \
-o UserKnownHostsFile=/dev/null -p $PORT localhost >/dev/null 2>&1 &
sleep 3
if [ -f messages.log ]; then
echo "✓ Message logging works"
PASS=$((PASS + 1))
else
echo "✗ Message logging failed"
FAIL=$((FAIL + 1))
fi
# Summary
echo ""
echo "PASSED: $PASS"
echo "FAILED: $FAIL"
[ $FAIL -eq 0 ] && echo "All tests passed" || echo "Some tests failed"
exit $FAIL

38
test_stress.sh Executable file
View file

@ -0,0 +1,38 @@
#!/bin/sh
# Stress test for TNT server
# Usage: ./test_stress.sh [num_clients]
PORT=${PORT:-2222}
CLIENTS=${1:-10}
DURATION=${2:-30}
echo "Starting TNT server on port $PORT..."
./tnt -p $PORT &
SERVER_PID=$!
sleep 2
if ! kill -0 $SERVER_PID 2>/dev/null; then
echo "Server failed to start"
exit 1
fi
echo "Spawning $CLIENTS clients for ${DURATION}s..."
for i in $(seq 1 $CLIENTS); do
(
sleep $((i % 5))
echo "test user $i" | timeout $DURATION ssh -o StrictHostKeyChecking=no \
-o UserKnownHostsFile=/dev/null -p $PORT localhost \
>/dev/null 2>&1
) &
done
echo "Running stress test..."
sleep $DURATION
echo "Cleaning up..."
kill $SERVER_PID 2>/dev/null
wait
echo "Stress test complete"
ps aux | grep tnt | grep -v grep && echo "WARNING: tnt process still running"