Fix memory leaks in integration tests

This commit is contained in:
jaina heartles 2022-12-18 06:21:04 -08:00
parent 64bbb7dae9
commit 613215efec
3 changed files with 21 additions and 6 deletions

View File

@ -322,6 +322,7 @@ fn ApiConn(comptime DbConn: type) type {
pub fn close(self: *Self) void { pub fn close(self: *Self) void {
util.deepFree(self.allocator, self.community); util.deepFree(self.allocator, self.community);
if (self.token_info) |info| util.deepFree(self.allocator, info);
self.db.releaseConnection(); self.db.releaseConnection();
} }
@ -353,11 +354,14 @@ fn ApiConn(comptime DbConn: type) type {
const user = try services.actors.get(self.db, info.user_id, self.allocator); const user = try services.actors.get(self.db, info.user_id, self.allocator);
defer util.deepFree(self.allocator, user); defer util.deepFree(self.allocator, user);
const username = try util.deepClone(self.allocator, user.username);
errdefer util.deepFree(self.allocator, username);
return AuthorizationInfo{ return AuthorizationInfo{
.id = user.id, .id = user.id,
.username = try util.deepClone(self.allocator, user.username), .username = username,
.community_id = self.community.id, .community_id = self.community.id,
.host = self.community.host, .host = try util.deepClone(self.allocator, self.community.host),
.issued_at = info.issued_at, .issued_at = info.issued_at,
}; };

View File

@ -104,7 +104,7 @@ pub fn login(
error.NoRows => error.InvalidLogin, error.NoRows => error.InvalidLogin,
else => error.DatabaseFailure, else => error.DatabaseFailure,
}; };
errdefer util.deepFree(alloc, info); defer alloc.free(info.hash);
try verifyPassword(info.hash, password, alloc); try verifyPassword(info.hash, password, alloc);
@ -162,6 +162,7 @@ pub fn verifyToken(
alloc: std.mem.Allocator, alloc: std.mem.Allocator,
) VerifyTokenError!TokenInfo { ) VerifyTokenError!TokenInfo {
const hash = try hashToken(token, alloc); const hash = try hashToken(token, alloc);
defer alloc.free(hash);
return db.queryRow( return db.queryRow(
TokenInfo, TokenInfo,

View File

@ -39,7 +39,9 @@ fn connectAndLogin(
var conn = try api_source.connectUnauthorized(host, alloc); var conn = try api_source.connectUnauthorized(host, alloc);
defer conn.close(); defer conn.close();
return try util.deepClone(alloc, try conn.login(username, password)); const result = try conn.login(username, password);
defer util.deepFree(conn.allocator, result);
return try util.deepClone(alloc, result);
} }
test "login as root" { test "login as root" {
@ -54,6 +56,7 @@ test "login as root" {
var conn = try src.connectToken(admin_host, login.token, alloc); var conn = try src.connectToken(admin_host, login.token, alloc);
defer conn.close(); defer conn.close();
const auth = try conn.verifyAuthorization(); const auth = try conn.verifyAuthorization();
defer util.deepFree(conn.allocator, auth);
try std.testing.expectEqual(login.user_id, auth.id); try std.testing.expectEqual(login.user_id, auth.id);
try std.testing.expectEqualStrings(root_user, auth.username); try std.testing.expectEqualStrings(root_user, auth.username);
@ -74,6 +77,7 @@ test "create community" {
const host = "fedi.example.com"; const host = "fedi.example.com";
const community = try conn.createCommunity("https://" ++ host, null); const community = try conn.createCommunity("https://" ++ host, null);
defer util.deepFree(alloc, community);
try std.testing.expectEqual(api.Community.Scheme.https, community.scheme); try std.testing.expectEqual(api.Community.Scheme.https, community.scheme);
try std.testing.expectEqual(api.Community.Kind.local, community.kind); try std.testing.expectEqual(api.Community.Kind.local, community.kind);
@ -83,7 +87,10 @@ test "create community" {
} }
test "create community and transfer to new owner" { test "create community and transfer to new owner" {
const alloc = std.testing.allocator; //const alloc = std.testing.allocator;
var gpa = std.heap.GeneralPurposeAllocator(.{ .stack_trace_frames = 16 }){};
defer _ = gpa.deinit();
const alloc = gpa.allocator();
var db = try makeDb(alloc); var db = try makeDb(alloc);
defer db.deinit(); defer db.deinit();
var src = try ApiSource.init(&db); var src = try ApiSource.init(&db);
@ -97,7 +104,9 @@ test "create community and transfer to new owner" {
defer conn.close(); defer conn.close();
const community = try conn.createCommunity("https://" ++ host, null); const community = try conn.createCommunity("https://" ++ host, null);
defer util.deepFree(conn.allocator, community);
const invite = try conn.createInvite(.{ .to_community = community.id, .kind = .community_owner }); const invite = try conn.createInvite(.{ .to_community = community.id, .kind = .community_owner });
defer util.deepFree(conn.allocator, invite);
break :blk try util.deepClone(alloc, invite); break :blk try util.deepClone(alloc, invite);
}; };
defer util.deepFree(alloc, invite); defer util.deepFree(alloc, invite);
@ -108,7 +117,7 @@ test "create community and transfer to new owner" {
var conn = try src.connectUnauthorized(host, alloc); var conn = try src.connectUnauthorized(host, alloc);
defer conn.close(); defer conn.close();
_ = try conn.register(username, password, .{ .invite_code = invite.code }); util.deepFree(conn.allocator, try conn.register(username, password, .{ .invite_code = invite.code }));
} }
const login = try connectAndLogin(&src, host, username, password, alloc); const login = try connectAndLogin(&src, host, username, password, alloc);
@ -118,6 +127,7 @@ test "create community and transfer to new owner" {
defer conn.close(); defer conn.close();
const auth = try conn.verifyAuthorization(); const auth = try conn.verifyAuthorization();
defer util.deepFree(conn.allocator, auth);
try std.testing.expectEqual(login.user_id, auth.id); try std.testing.expectEqual(login.user_id, auth.id);
try std.testing.expectEqualStrings(username, auth.username); try std.testing.expectEqualStrings(username, auth.username);