nats.zig/tests/nats.zig
torque 67cb801f54
tests.connection: add basic encrypted connection tests
Since I went to the effort of getting this to build with LibreSSL, it
makes sense to check that it works. And it seems to. There's nothing
special about the certificates added, except that they don't expire
for 100 years, which is hopefully long enough that nothing will matter
any more. They get baked into the test executable, since this was the
best way I could figure out to be able to consistently load them
during runtime, since they're stored relative to the source, not
necessarily relative to the working directory in use when the test
executable is run.

This required some augmentation to the test server launcher which will
make it easier to add additional flags to it in the future, if that
becomes necessary.
2023-09-02 17:12:00 -07:00

100 lines
2.4 KiB
Zig

// This file is licensed under the CC0 1.0 license.
// See: https://creativecommons.org/publicdomain/zero/1.0/legalcode
const std = @import("std");
const nats = @import("nats");
test "version" {
const version = nats.getVersion();
const vernum = nats.getVersionNumber();
try std.testing.expectEqualStrings("3.6.1", version);
try std.testing.expectEqual(@as(u32, 0x03_06_01), vernum);
try std.testing.expect(nats.checkCompatibility());
}
test "time" {
const now = nats.now();
const nownano = nats.nowInNanoseconds();
nats.sleep(1);
const later = nats.now();
const laternano = nats.nowInNanoseconds();
try std.testing.expect(later >= now);
try std.testing.expect(laternano >= nownano);
}
test "init" {
{
try nats.init(nats.default_spin_count);
defer nats.deinit();
}
{
// a completely random number
try nats.init(900_142_069);
nats.deinit();
}
{
try nats.init(0);
try nats.deinitWait(1000);
}
}
test "misc" {
{
try nats.init(nats.default_spin_count);
defer nats.deinit();
try nats.setMessageDeliveryPoolSize(500);
}
{
try nats.init(nats.default_spin_count);
defer nats.deinit();
// just test that the function is wrapped properly
nats.releaseThreadMemory();
}
blk: {
try nats.init(nats.default_spin_count);
defer nats.deinit();
// this is a mess of a test that is designed to fail because actually we're
// testing out the error reporting functions instead of signing. Nice bait
// and switch.
const signed = nats.sign("12345678", "12345678") catch {
const err = nats.getLastError();
std.debug.print("as expected, signing failed: {s}\n", .{err.desc});
var stackmem = [_]u8{0} ** 512;
var stackbuf: []u8 = &stackmem;
nats.getLastErrorStack(&stackbuf) catch {
std.debug.print("Actually, the error stack was too big\n", .{});
break :blk;
};
std.debug.print("stack: {s}\n", .{stackbuf});
break :blk;
};
std.heap.raw_c_allocator.free(signed);
}
}
test "inbox" {
try nats.init(nats.default_spin_count);
defer nats.deinit();
const inbox = try nats.createInbox();
defer nats.destroyInbox(inbox);
std.debug.print("inbox: {s}\n", .{inbox});
}