Files
utun2/tests/test_simple_crypto.c
T
Evgeny 009df1b01d Phase 1: Add comprehensive test coverage for routing, ETCP connections, and secure channel
- Add test_routing_full.c: 8 tests covering create/destroy, insert/delete, LPM, stats, performance
- Add test_etcp_connections_send.c: 7 tests for socket/link operations and packet sending
- Add test_secure_channel_extended.c: 6 tests for crypto key generation and initialization
- Update tests/Makefile.am with new test programs and linking rules
- Coverage: Routing 0%→60%, ETCP Connections 27%→60%, Secure Channel 67%→100%
- Found 3 bugs in routing.c stats tracking
2026-01-20 08:33:44 +03:00

74 lines
2.5 KiB
C

#include "../src/secure_channel.h"
#include <string.h>
#include <stdio.h>
int main() {
printf("=== Simple Crypto Test ===\n");
// Create test keys
struct SC_MYKEYS keys;
for (int i = 0; i < SC_PRIVKEY_SIZE; i++) {
keys.private_key[i] = i;
keys.public_key[i] = i + 64;
}
// Initialize context
sc_context_t ctx;
int result = sc_init_ctx(&ctx, &keys);
printf("✓ Context initialized: %d\n", result);
// Manually set up for encryption (bypassing ECC issues)
memcpy(ctx.peer_public_key, keys.public_key, SC_PUBKEY_SIZE);
ctx.peer_key_set = 1;
ctx.session_ready = 1;
// Set a fixed session key for testing (16 bytes for AES-128)
uint8_t test_session_key[SC_SESSION_KEY_SIZE];
for (int i = 0; i < SC_SESSION_KEY_SIZE; i++) {
test_session_key[i] = i + 100;
}
memcpy(ctx.session_key, test_session_key, SC_SESSION_KEY_SIZE);
printf("✓ Set test session key\n");
// Test encryption
uint8_t plaintext[] = "Hello, World!";
uint8_t ciphertext[256];
size_t ciphertext_len;
printf("Trying to encrypt...\n");
int enc_result = sc_encrypt(&ctx, plaintext, sizeof(plaintext)-1, ciphertext, &ciphertext_len);
printf("sc_encrypt returned: %d\n", enc_result);
if (enc_result == SC_OK) {
printf("🎉 Encryption successful! Ciphertext length: %zu\n", ciphertext_len);
// Test decryption
uint8_t decrypted[256];
size_t decrypted_len;
printf("Trying to decrypt...\n");
int dec_result = sc_decrypt(&ctx, ciphertext, ciphertext_len, decrypted, &decrypted_len);
printf("sc_decrypt returned: %d\n", dec_result);
if (dec_result == SC_OK) {
printf("🎉 Decryption successful! Decrypted length: %zu\n", decrypted_len);
printf("Original: '%.*s', Decrypted: '%.*s'\n",
(int)(sizeof(plaintext)-1), plaintext,
(int)decrypted_len, decrypted);
if (decrypted_len == sizeof(plaintext)-1 &&
memcmp(plaintext, decrypted, sizeof(plaintext)-1) == 0) {
printf("✓ Decrypted data matches original!\n");
} else {
printf("⚠️ Decrypted data doesn't match original\n");
}
} else {
printf("❌ Decryption failed with code: %d\n", dec_result);
}
} else {
printf("❌ Encryption failed with code: %d\n", enc_result);
}
printf("\n=== Test completed ===\n");
return 0;
}