first tests

This commit is contained in:
Motiejus Jakštys 2024-10-10 21:15:07 -04:00
parent c8e65b47a2
commit dc7f35ab7b
6 changed files with 73 additions and 22 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.zig-cache
zig-out

View File

@ -64,18 +64,8 @@ pub fn build(b: *std.Build) void {
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const lib_unit_tests = b.addTest(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
const exe_unit_tests = b.addTest(.{
.root_source_file = b.path("src/main.zig"),
.root_source_file = b.path("src/test_all.zig"),
.target = target,
.optimize = optimize,
});
@ -86,6 +76,5 @@ pub fn build(b: *std.Build) void {
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
test_step.dependOn(&run_exe_unit_tests.step);
}

View File

@ -16,6 +16,10 @@ pub fn main() !void {
try bw.flush(); // don't forget to flush!
}
const Tunnel = struct {
name: []u8,
};
test "simple test" {
var list = std.ArrayList(i32).init(std.testing.allocator);
defer list.deinit(); // try commenting this out and see if zig detects the memory leak!

View File

@ -1,10 +0,0 @@
const std = @import("std");
const testing = std.testing;
export fn add(a: i32, b: i32) i32 {
return a + b;
}
test "basic add functionality" {
try testing.expect(add(3, 7) == 10);
}

4
src/test_all.zig Normal file
View File

@ -0,0 +1,4 @@
test "nyotun test suite" {
_ = @import("main.zig");
_ = @import("tunnel.zig");
}

62
src/tunnel.zig Normal file
View File

@ -0,0 +1,62 @@
const std = @import("std");
const os = std.os;
const mem = std.mem;
const posix = std.posix;
const log = std.log.scoped(.nyotun);
const IFF_TUN = 0x0001; // <linux/if_tun.h>
const IFF_NO_PI = 0x1000; // <linux/if_tun.h>
const TUNSETIFF = 0x800454ca; // same value on all archs, see go/src/syscall/*.go
dev: [posix.IFNAMESIZE - 1:0]u8,
tunFile: posix.fd_t,
pub fn init(devname: ?[]const u8) !Tunnel {
const tunFile = try posix.open(
"/dev/net/tun",
posix.O{
.ACCMODE = .RDWR,
.CLOEXEC = true,
},
0,
);
errdefer posix.close(tunFile);
var ifr: posix.ifreq = undefined;
@memset(@as([*]u8, @ptrCast(&ifr))[0..@sizeOf(posix.ifreq)], 0);
ifr.ifru.flags = IFF_TUN | IFF_NO_PI;
if (devname) |name| {
if (name.len >= posix.IFNAMESIZE - 1) {
return error.BadInterfaceName;
}
@memcpy(ifr.ifrn.name[0..name.len], name);
}
const err = posix.system.ioctl(tunFile, TUNSETIFF, @intFromPtr(&ifr));
if (posix.errno(err) != .SUCCESS) {
log.err("unable to set TUNSETIFF: {s}", .{@tagName(posix.errno(err))});
return error.OpenDevTun;
}
var tunnel = Tunnel{
.dev = undefined,
.tunFile = tunFile,
};
@memcpy(tunnel.dev[0..posix.IFNAMESIZE], mem.sliceTo(&ifr.ifrn.name, 0));
return tunnel;
}
pub fn close(self: *Tunnel) void {
posix.close(self.tunFile);
}
const Tunnel = @This();
test "init tunnel" {
var tun = try init("nyotun");
tun.close();
}