summaryrefslogtreecommitdiff
path: root/test/consumer/main.zig
blob: 3dc555d891e08ca6b79222dbb2ebc187ae583c8e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//! cloud9 from a package that depends on it: a client and a server, each
//! a cloud9 session over its own end of a socket pair, and 9proc.
const std = @import("std");
const c9 = @import("cloud9");
const testing = std.testing;
const File = std.Io.File;

test "a client and a server talk 9P over a socket pair" {
    if (@import("builtin").os.tag != .linux) return error.SkipZigTest;
    const linux = std.os.linux;
    var fds: [2]linux.fd_t = undefined;
    try testing.expectEqual(0, linux.socketpair(linux.AF.UNIX, linux.SOCK.STREAM, 0, &fds));
    const near: File = .{ .handle = fds[0], .flags = .{ .nonblocking = false } };
    const far: File = .{ .handle = fds[1], .flags = .{ .nonblocking = false } };
    defer near.close(testing.io);
    defer far.close(testing.io);

    var bufs: [4][8192]u8 = undefined;
    var client: c9.Client = .init(.{ .in = &bufs[0], .out = &bufs[1] });
    var server: c9.Server = .init(.{ .in = &bufs[2], .out = &bufs[3] });
    const root: c9.Qid = .{ .type = c9.qtdir, .version = 0, .path = 1 };
    const hello = "hello from the server";

    _ = try client.submit(.{ .version = .{} });
    try send(near, &client);
    const version = (try request(far, &server)).msg.tversion;
    try server.negotiate(version.msize, version.version);
    server.release();
    try send(far, &server);
    try testing.expectEqualStrings("9P2000", (try result(near, &client)).result.version.version);

    _ = try client.submit(.{ .attach = .{ .fid = 0, .uname = "glenda" } });
    try send(near, &client);
    const attach = try request(far, &server);
    try testing.expectEqualStrings("glenda", attach.msg.tattach.uname);
    try server.reply(attach.tag, .{ .rattach = .{ .qid = root } });
    server.release();
    try send(far, &server);
    try testing.expectEqual(root.path, (try result(near, &client)).result.attach.path);

    _ = try client.submit(.{ .read = .{ .fid = 0, .offset = 0, .count = 64 } });
    try send(near, &client);
    const read = try request(far, &server);
    try server.reply(read.tag, .{ .rread = .{ .data = hello } });
    server.release();
    try send(far, &server);
    try testing.expectEqualStrings(hello, (try result(near, &client)).result.read);
}

test "9proc comes from the same dependency, on the same cloud9" {
    try @import("9proc").core.validName("pane");
    try testing.expectError(error.BadName, @import("9proc").core.validName("a/b"));
}

/// Writes what `end` (a client or a server) has to say on `file`.
fn send(file: File, end: anytype) !void {
    const bytes = end.output();
    try file.writeStreamingAll(testing.io, bytes);
    end.wrote(bytes.len);
}

/// Reads from `file` into `server` until it holds a whole request.
fn request(file: File, server: *c9.Server) !c9.Decoded {
    while (true) {
        if (try server.receive()) |got| return got;
        try recv(file, server);
    }
}

/// Reads from `file` into `client` until it holds a whole answer.
fn result(file: File, client: *c9.Client) !c9.Client.Done {
    while (true) {
        if (client.take()) |done| return done;
        try recv(file, client);
    }
}

fn recv(file: File, end: anytype) !void {
    var buf: [8192]u8 = undefined;
    const n = try file.readStreaming(testing.io, &.{&buf});
    try testing.expectEqual(n, end.push(buf[0..n]));
}