first tests

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

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();
}