ab ovo
This commit is contained in:
318
tests/integration/test_rate_limiting_integration.zig
Normal file
318
tests/integration/test_rate_limiting_integration.zig
Normal file
@@ -0,0 +1,318 @@
|
||||
//! Integration tests for rate limiting functionality
|
||||
//!
|
||||
//! Tests the full integration of rate limiting in the P2P network stack:
|
||||
//! - Message rate limiting per peer
|
||||
//! - Connection rate limiting per IP
|
||||
//! - Per-peer isolation
|
||||
//! - Burst allowance
|
||||
//! - Rate limiter cleanup
|
||||
|
||||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
const core = @import("core");
|
||||
const network = @import("network");
|
||||
const p2p_lib = @import("p2p");
|
||||
|
||||
const RateLimiter = core.rate_limiter.RateLimiter;
|
||||
const PeerRateLimiter = core.rate_limiter.PeerRateLimiter;
|
||||
const ConnectionRateLimiter = core.rate_limiter.ConnectionRateLimiter;
|
||||
const PeerId = network.peer.PeerId;
|
||||
const NetworkManager = network.network_manager.NetworkManager;
|
||||
const P2P = p2p_lib.P2P;
|
||||
const P2PConfig = p2p_lib.P2PConfig;
|
||||
|
||||
// Test that message flooding is rate limited
|
||||
test "message flood is rate limited" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
// Create peer rate limiter with low limits for testing
|
||||
var peer_limiter = PeerRateLimiter.init(allocator, .{
|
||||
.tokens_per_second = 10,
|
||||
.burst_size = 5,
|
||||
});
|
||||
defer peer_limiter.deinit();
|
||||
|
||||
const peer_id = [_]u8{1} ** 32;
|
||||
|
||||
// Should be able to consume burst
|
||||
var i: usize = 0;
|
||||
while (i < 5) : (i += 1) {
|
||||
try testing.expect(peer_limiter.tryConsume(peer_id));
|
||||
}
|
||||
|
||||
// Should be rate limited now
|
||||
try testing.expect(!peer_limiter.tryConsume(peer_id));
|
||||
try testing.expect(peer_limiter.isRateLimited(peer_id));
|
||||
}
|
||||
|
||||
// Test that connection flooding is rate limited
|
||||
test "connection flood is rate limited" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
// Create connection rate limiter with low limits
|
||||
var conn_limiter = ConnectionRateLimiter.init(
|
||||
allocator,
|
||||
.{ .tokens_per_second = 5, .burst_size = 3 }, // Global limit
|
||||
.{ .tokens_per_second = 2, .burst_size = 2 }, // Per-IP limit
|
||||
);
|
||||
defer conn_limiter.deinit();
|
||||
|
||||
const test_ip = "192.168.1.100";
|
||||
|
||||
// Should be able to consume burst
|
||||
var i: usize = 0;
|
||||
while (i < 2) : (i += 1) {
|
||||
try testing.expect(conn_limiter.tryAcceptConnection(test_ip));
|
||||
}
|
||||
|
||||
// Should be rate limited now (per-IP limit)
|
||||
try testing.expect(!conn_limiter.tryAcceptConnection(test_ip));
|
||||
|
||||
// Different IP should still work (up to global limit)
|
||||
const test_ip2 = "192.168.1.101";
|
||||
try testing.expect(conn_limiter.tryAcceptConnection(test_ip2));
|
||||
}
|
||||
|
||||
// Test per-peer isolation (one peer's flood doesn't affect others)
|
||||
test "per-peer isolation" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var peer_limiter = PeerRateLimiter.init(allocator, .{
|
||||
.tokens_per_second = 10,
|
||||
.burst_size = 2,
|
||||
});
|
||||
defer peer_limiter.deinit();
|
||||
|
||||
const peer1 = [_]u8{1} ** 32;
|
||||
const peer2 = [_]u8{2} ** 32;
|
||||
|
||||
// Exhaust peer1's tokens
|
||||
try testing.expect(peer_limiter.tryConsume(peer1));
|
||||
try testing.expect(peer_limiter.tryConsume(peer1));
|
||||
try testing.expect(!peer_limiter.tryConsume(peer1));
|
||||
|
||||
// Peer2 should still have tokens
|
||||
try testing.expect(peer_limiter.tryConsume(peer2));
|
||||
try testing.expect(peer_limiter.tryConsume(peer2));
|
||||
try testing.expect(!peer_limiter.tryConsume(peer2));
|
||||
}
|
||||
|
||||
// Test burst allowance works
|
||||
test "burst allowance works" {
|
||||
var limiter = RateLimiter.init(.{
|
||||
.tokens_per_second = 1,
|
||||
.burst_size = 10,
|
||||
});
|
||||
|
||||
// Should be able to consume entire burst immediately
|
||||
var i: usize = 0;
|
||||
while (i < 10) : (i += 1) {
|
||||
try testing.expect(limiter.tryConsume());
|
||||
}
|
||||
|
||||
// Should be rate limited after burst
|
||||
try testing.expect(!limiter.tryConsume());
|
||||
try testing.expect(limiter.isRateLimited());
|
||||
}
|
||||
|
||||
// Test rate limiter cleanup works
|
||||
test "rate limiter cleanup works" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var peer_limiter = PeerRateLimiter.init(allocator, .{
|
||||
.tokens_per_second = 100,
|
||||
.burst_size = 10,
|
||||
});
|
||||
defer peer_limiter.deinit();
|
||||
|
||||
const peer1 = [_]u8{1} ** 32;
|
||||
const peer2 = [_]u8{2} ** 32;
|
||||
|
||||
// Add some peers
|
||||
try testing.expect(peer_limiter.tryConsume(peer1));
|
||||
try testing.expect(peer_limiter.tryConsume(peer2));
|
||||
try testing.expectEqual(@as(usize, 2), peer_limiter.getPeerCount());
|
||||
|
||||
// Remove a peer
|
||||
peer_limiter.removePeer(peer1);
|
||||
try testing.expectEqual(@as(usize, 1), peer_limiter.getPeerCount());
|
||||
|
||||
// Removed peer should get fresh tokens when they reconnect
|
||||
try testing.expect(peer_limiter.tryConsume(peer1));
|
||||
}
|
||||
|
||||
// Test NetworkManager with rate limiting
|
||||
test "NetworkManager rate limiting integration" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
// Create mock connection map
|
||||
var connections = std.AutoHashMap(PeerId, *core.Socket).init(allocator);
|
||||
defer connections.deinit();
|
||||
|
||||
// Create network manager
|
||||
var net_manager = NetworkManager.init(allocator, &connections);
|
||||
defer net_manager.deinit();
|
||||
|
||||
// Create and attach rate limiter
|
||||
var peer_limiter = PeerRateLimiter.init(allocator, .{
|
||||
.tokens_per_second = 10,
|
||||
.burst_size = 5,
|
||||
});
|
||||
defer peer_limiter.deinit();
|
||||
|
||||
net_manager.setPeerRateLimiter(&peer_limiter);
|
||||
|
||||
// Verify stats are initialized
|
||||
const stats = net_manager.getStats();
|
||||
try testing.expectEqual(@as(u64, 0), stats.messages_rate_limited.load(.acquire));
|
||||
}
|
||||
|
||||
// Test P2P configuration with rate limiting
|
||||
test "P2P configuration with rate limiting" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
// Create config with custom rate limits
|
||||
const config = P2PConfig{
|
||||
.library = .{},
|
||||
.network = .{
|
||||
.enable_rate_limiting = true,
|
||||
.message_rate_limit = 500,
|
||||
.message_burst_size = 50,
|
||||
.connection_rate_limit = 20,
|
||||
.connection_burst_size = 10,
|
||||
},
|
||||
.protocol = .{},
|
||||
.file_sharing = .{},
|
||||
.events = .{},
|
||||
};
|
||||
|
||||
var p2p_inst = try P2P.init(allocator, config);
|
||||
defer p2p_inst.deinit();
|
||||
|
||||
try testing.expectEqual(true, config.network.enable_rate_limiting);
|
||||
try testing.expectEqual(@as(u32, 500), config.network.message_rate_limit);
|
||||
}
|
||||
|
||||
// Test rate limiting can be disabled
|
||||
test "rate limiting can be disabled" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
const config = P2PConfig{
|
||||
.library = .{},
|
||||
.network = .{
|
||||
.enable_rate_limiting = false,
|
||||
},
|
||||
.protocol = .{},
|
||||
.file_sharing = .{},
|
||||
.events = .{},
|
||||
};
|
||||
|
||||
var p2p_inst = try P2P.init(allocator, config);
|
||||
defer p2p_inst.deinit();
|
||||
|
||||
try p2p_inst.start();
|
||||
defer p2p_inst.stop() catch {};
|
||||
|
||||
// Rate limiters should not be initialized
|
||||
try testing.expect(p2p_inst.peer_rate_limiter == null);
|
||||
try testing.expect(p2p_inst.connection_rate_limiter == null);
|
||||
}
|
||||
|
||||
// Test rate limiting enabled by default
|
||||
test "rate limiting enabled by default" {
|
||||
const config = P2PConfig.default();
|
||||
try testing.expect(config.network.enable_rate_limiting == true);
|
||||
}
|
||||
|
||||
// Test multiple peers with different consumption rates
|
||||
test "multiple peers with different consumption rates" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var peer_limiter = PeerRateLimiter.init(allocator, .{
|
||||
.tokens_per_second = 10,
|
||||
.burst_size = 5,
|
||||
});
|
||||
defer peer_limiter.deinit();
|
||||
|
||||
const fast_peer = [_]u8{1} ** 32;
|
||||
const slow_peer = [_]u8{2} ** 32;
|
||||
|
||||
// Fast peer exhausts tokens quickly
|
||||
var i: usize = 0;
|
||||
while (i < 5) : (i += 1) {
|
||||
try testing.expect(peer_limiter.tryConsume(fast_peer));
|
||||
}
|
||||
try testing.expect(!peer_limiter.tryConsume(fast_peer));
|
||||
|
||||
// Slow peer consumes slowly
|
||||
try testing.expect(peer_limiter.tryConsume(slow_peer));
|
||||
try testing.expect(peer_limiter.tryConsume(slow_peer));
|
||||
|
||||
// Fast peer is rate limited, slow peer is not
|
||||
try testing.expect(peer_limiter.isRateLimited(fast_peer));
|
||||
try testing.expect(!peer_limiter.isRateLimited(slow_peer));
|
||||
}
|
||||
|
||||
// Test high-performance configuration
|
||||
test "high-performance config has higher rate limits" {
|
||||
const default_config = P2PConfig.default();
|
||||
const hp_config = P2PConfig.high_performance();
|
||||
|
||||
try testing.expect(hp_config.network.message_rate_limit >= default_config.network.message_rate_limit);
|
||||
try testing.expect(hp_config.network.connection_rate_limit >= default_config.network.connection_rate_limit);
|
||||
}
|
||||
|
||||
// Test low-resource configuration
|
||||
test "low-resource config has lower rate limits" {
|
||||
const default_config = P2PConfig.default();
|
||||
const lr_config = P2PConfig.low_resource();
|
||||
|
||||
try testing.expect(lr_config.network.message_rate_limit <= default_config.network.message_rate_limit);
|
||||
try testing.expect(lr_config.network.connection_rate_limit <= default_config.network.connection_rate_limit);
|
||||
}
|
||||
|
||||
// Test rate limiter thread safety (basic)
|
||||
test "rate limiter is thread safe" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
var peer_limiter = PeerRateLimiter.init(allocator, .{
|
||||
.tokens_per_second = 1000,
|
||||
.burst_size = 100,
|
||||
});
|
||||
defer peer_limiter.deinit();
|
||||
|
||||
const peer_id = [_]u8{1} ** 32;
|
||||
|
||||
// Multiple threads trying to consume tokens
|
||||
const ThreadContext = struct {
|
||||
limiter: *PeerRateLimiter,
|
||||
peer: [32]u8,
|
||||
success_count: std.atomic.Value(u32),
|
||||
|
||||
fn worker(ctx: *@This()) void {
|
||||
var i: usize = 0;
|
||||
while (i < 50) : (i += 1) {
|
||||
if (ctx.limiter.tryConsume(ctx.peer)) {
|
||||
_ = ctx.success_count.fetchAdd(1, .monotonic);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var ctx = ThreadContext{
|
||||
.limiter = &peer_limiter,
|
||||
.peer = peer_id,
|
||||
.success_count = std.atomic.Value(u32).init(0),
|
||||
};
|
||||
|
||||
const thread1 = try std.Thread.spawn(.{}, ThreadContext.worker, .{&ctx});
|
||||
const thread2 = try std.Thread.spawn(.{}, ThreadContext.worker, .{&ctx});
|
||||
|
||||
thread1.join();
|
||||
thread2.join();
|
||||
|
||||
// Total successful consumes should be <= burst_size (100)
|
||||
const total = ctx.success_count.load(.acquire);
|
||||
try testing.expect(total <= 100);
|
||||
try testing.expect(total > 0); // Should have consumed some tokens
|
||||
}
|
||||
Reference in New Issue
Block a user