summaryrefslogtreecommitdiff
path: root/test/behaviour/plumber.zig
blob: 4726714b059ff55d025f8f3fc484cc70aa163eed (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//! docs/typ/reference.typ, The plumber <plumber>: plumb/rules and
//! plumb/send, Plan 9's plumber(4) files.
const std = @import("std");
const Session = @import("session").Session;

fn has(hay: []const u8, needle: []const u8) bool {
    return std.mem.indexOf(u8, hay, needle) != null;
}

test "plumber: plumb/rules reads the rules in force, a write adds rule sets, > replaces them, a bad one is refused in plumb(6)'s words, one writer at a time" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    // The defaults, `include basic`'s rules in their place.
    try std.testing.expect(has(try s.read("/plumb/rules"), "type\tis\ttext\ndata\tmatches\t'https?://.*'\nplumb\tto\tweb\n\n"));
    // Added, the last rule set at the close.
    try s.write("/plumb/rules", "type is text\ndata matches 'gopher://.*'\nplumb to web\n");
    try std.testing.expect(std.mem.endsWith(u8, try s.read("/plumb/rules"), "attr\tadd\taction=showdiff\nplumb\tto\tedit\n\ntype\tis\ttext\ndata\tmatches\t'gopher://.*'\nplumb\tto\tweb\n\n"));
    // A rule set refused at the write it is whole in, said with its line;
    // the rules stay as they were.
    const before = try s.read("/plumb/rules");
    try std.testing.expectEqual(@as(u16, 22), try s.refused("/plumb/rules", "type is text\nplumb to web\n\ndata isnt x\nplumb to web\n\n"));
    try std.testing.expectEqualStrings("plumb/rules:4: unknown verb isnt", s.said);
    try std.testing.expectEqualStrings(before, try s.read("/plumb/rules"));
    try std.testing.expectEqual(@as(u16, 22), try s.refused("/plumb/rules", "data matches '([a-z]+)\\.c'\narg isfile $1\nplumb to edit\n\n"));
    try std.testing.expect(has(s.said, "$1: no submatches"));
    // `>`: replaced.
    const f = try s.hold("/plumb/rules", 1);
    try s.truncate(f);
    try std.testing.expectEqual(@as(u16, 0), try s.writeFid(f, "type is text\nplumb to edit\n\n"));
    // One writer at a time, as plumber(4)'s.
    try std.testing.expectError(error.Refused, s.hold("/plumb/rules", 1));
    s.clunk(f);
    try std.testing.expectEqualStrings("type\tis\ttext\nplumb\tto\tedit\n\n", try s.read("/plumb/rules"));
}

test "plumber: plumb/send takes a message in its six lines and data, written in pieces or whole, and sends it where the rules say, or says why not in Plan 9's words" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    // A URL: the web port, the host's link opener.
    try s.write("/plumb/send", "test\n\n/tmp\ntext\n\n19\nhttps://ziglang.org");
    try std.testing.expectEqualStrings("https://ziglang.org", s.link);
    // In pieces: its data's count says when it is whole.
    const f = try s.hold("/plumb/send", 1);
    try std.testing.expectEqual(@as(u16, 0), try s.writeFid(f, "test\n\n/tmp\ntext\n\n19\nhttps://"));
    try std.testing.expectEqualStrings("https://ziglang.org", s.link);
    try std.testing.expectEqual(@as(u16, 0), try s.writeFid(f, "example.org"));
    try std.testing.expectEqualStrings("https://example.org", s.link);
    s.clunk(f);
    // No rule takes it, and it names no port.
    try std.testing.expect(try s.refused("/plumb/send", "test\n\n/\ntext\n\n9\nno-such-x") != 0);
    try std.testing.expectEqualStrings("couldn't find destination for message", s.said);
    // Not a message at all.
    try std.testing.expect(try s.refused("/plumb/send", "test\ntext\n") != 0);
    try std.testing.expectEqualStrings("bad plumb message format", s.said);
    try std.testing.expect(try s.refused("/plumb/send", "a\nb\nc\nd\ne\n1\nxy") != 0);
    try std.testing.expectEqualStrings("bad plumb message format", s.said);
    // A file to the edit port: opened as Look opens one, at its place.
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "plumbed.txt", .data = "one\ntwo\nthree\n" });
    var buf: [4096]u8 = undefined;
    const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
    try s.write("/plumb/send", s.at("test\n\n{s}\ntext\n\n{d}\nplumbed.txt:2", .{ dir, "plumbed.txt:2".len }));
    try std.testing.expect(has(try s.read("/index"), "/plumbed.txt"));
}

test "plumber: a port's file is read by programs: each open made before a send gets its own copy, one made after none, and a client rule's message waits for the port's first reader" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "seen.txt", .data = "one\ntwo\n" });
    var buf: [4096]u8 = undefined;
    const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
    // A file for each port the rules name, read-only, as plumber(4)'s.
    try std.testing.expectEqual(@as(u16, 0o444), (try s.stat("/plumb/web")).mode & 0o777);
    try std.testing.expectEqual(@as(u16, 0o444), (try s.stat("/plumb/edit")).mode & 0o777);
    // Two readers of edit, then a look: each reads the look's message,
    // and the editor opens the file all the same.
    const a = try s.hold("/plumb/edit", 0);
    const b = try s.hold("/plumb/edit", 0);
    try s.write("/look", s.at("{s}/seen.txt\n", .{dir}));
    const want = s.at("pardes\nedit\n{s}\ntext\n\n{d}\n{s}/seen.txt", .{ "/", dir.len + "/seen.txt".len, dir });
    try std.testing.expectEqualStrings(want, try s.readFid(a));
    try std.testing.expectEqualStrings(want, try s.readFid(b));
    try std.testing.expect(has(try s.read("/index"), "/seen.txt"));
    // An open made after the send has nothing: its read waits.
    const late = try s.hold("/plumb/edit", 0);
    try std.testing.expectError(error.Refused, s.readFid(late));
    s.clunk(late);
    s.clunk(a);
    s.clunk(b);
    // A port only a program reads, its rule a client: the message waits
    // for the port's first reader, the program started read nothing.
    try s.write("/plumb/rules", "type is text\ndata matches 'letter-[0-9]+'\nplumb to seemail\nplumb client true\n\n");
    try s.write("/plumb/send", "test\n\n/\ntext\n\n9\nletter-42");
    const first = try s.hold("/plumb/seemail", 0);
    try std.testing.expectEqualStrings("test\nseemail\n/\ntext\n\n9\nletter-42", try s.readFid(first));
    try std.testing.expectError(error.Refused, s.readFid(first));
    // Once read, a port's message goes to its readers, no program started.
    try s.write("/plumb/send", "test\n\n/\ntext\n\n9\nletter-43");
    try std.testing.expectEqualStrings("test\nseemail\n/\ntext\n\n9\nletter-43", try s.readFid(first));
    s.clunk(first);
    // Not to be written.
    try std.testing.expectError(error.Refused, s.hold("/plumb/seemail", 1));
}