unit netmount_client;

// M22.3 proof: the client side of the network filesystem (spec 0032).
//
//   run netmount_client <keyword>     spawned by `netmount`
//
// Every step goes through the mounted prefix `net/<keyword>/`, which the kernel
// routes to `apps/utilities/net_mount` and then over TCP to
// `scripts/netfs_host.py`. The host tree this expects is the one the QMP
// harness creates:
//
//   notes.txt       4096 bytes, byte i is 'A' + (i mod 26)
//   bytes.bin       1024 bytes, byte i is (i mod 256)
//   remove_me.txt   10 bytes ("delete me\n")
//   sub/inner.txt   6 bytes ("inner\n")
//
// The unit lists, reads, creates, appends through the IO_MAX boundary, truncates
// shorter and longer, creates and removes a directory, renames and removes. It
// leaves `renamed.txt` (300 bytes of '0'+i mod 10) for the parent and the host
// harness to compare byte for byte, and removes `big.txt`, `trunc.txt` and
// `remove_me.txt`.
//
// The unit never touches the screen; it also runs under `langhost` (where the
// prefix is `Invalid`/`NotFound` and every check fails, which is why the parent
// never runs it there).

uses core, stdlib, lib.out;

const
  NotesBytes = 4096;
  BinaryBytes = 1024;
  WrittenBytes = 300;
  TruncBytes = 100;
  IoMax = 131072;
  FillByte = 90;
  FirstByte = 65;
  LastByte = 66;

var
  ok: bool;
  step: int;
  st: Status;
  root: string[256];
  path: string[256];
  sub: string[256];
  renamed: string[256];
  name: string[64];
  kind: uint;
  size: uint;
  index: uint;
  offset: int;
  got: int;
  i: int;
  count: int;
  chunk: bytes[256];
  small: bytes[WrittenBytes];
  trunc: bytes[TruncBytes];
  big: bytes[IoMax];
  over: bytes[IoMax + 1];
  good: bool;
  shown: int;
  names: array[string[64], 16];

procedure check(label: const string[0]; passed: bool);
begin
  step := step + 1;
  if passed then
  begin
    out.say("netmount_client: ");
    out.say(label);
    out.say(" ok\n");
  end
  else
  begin
    ok := false;
    out.say("netmount_client: FAIL ");
    out.say(label);
    out.say("\n");
  end;
end;

// `base/name`, or just `name` at the root, into the 256-byte `out`.
function join(base: const string[0]; entry: const string[0];
              out: var string[256]): Status;
var
  base_len: int;
  entry_len: int;
  total: int;
  split: int;
  at: int;
  copied: int;
  stage: bytes[256];
begin
  base_len := int(core.len(base));
  entry_len := int(core.len(entry));
  if base_len = 0 then
  begin
    total := entry_len;
    split := 0;
  end
  else
  begin
    total := base_len + 1 + entry_len;
    split := base_len + 1;
  end;
  if total > 256 then
    return CapacityExceeded;
  at := 0;
  while at < base_len do
  begin
    stage[at] := base[at];
    at := at + 1;
  end;
  if base_len > 0 then
    stage[base_len] := byte(47);
  at := 0;
  while at < entry_len do
  begin
    stage[split + at] := entry[at];
    at := at + 1;
  end;
  copied := core.substr_into(out, stage, 0, total);
  if copied = total then
    return Ok;
  if copied = -6 then
    return CapacityExceeded;
  return Invalid;
end;

// Lists `dir` into `names`/`count`, up to the 16-entry array.
procedure load_names(dir: const string[0]);
begin
  count := 0;
  index := uint(0);
  st := core.fs_list(dir, index, name, kind, size);
  while st = Ok and count < 16 do
  begin
    names[count] := name;
    count := count + 1;
    index := index + uint(1);
    st := core.fs_list(dir, index, name, kind, size);
  end;
end;

procedure check_root_listing();
begin
  load_names(root);
  check("listing count", count = 4);
  check("listing sorted by bytes",
    count = 4
      and names[0] = "bytes.bin"
      and names[1] = "notes.txt"
      and names[2] = "remove_me.txt"
      and names[3] = "sub");
  check("listing ends", st = NotFound);
  st := core.fs_list(root, uint(0), name, kind, size);
  check("bytes.bin is a file", st = Ok and name = "bytes.bin"
    and kind = uint(0) and size = uint(BinaryBytes));
  st := core.fs_list(root, uint(3), name, kind, size);
  check("sub is a directory", st = Ok and name = "sub"
    and kind = uint(1) and size = uint(0));

  join(root, "sub", sub);
  load_names(sub);
  check("subdirectory listing", count = 1 and names[0] = "inner.txt" and st = NotFound);
end;

// 4096 bytes, byte i = 'A' + (i mod 26), read 256 bytes at a time.
procedure check_notes();
begin
  join(root, "notes.txt", path);
  size := uint(0);
  st := core.fs_size(path, size);
  check("notes size", st = Ok and size = uint(NotesBytes));
  offset := 0;
  while offset < NotesBytes and ok do
  begin
    st := core.fs_read(path, uint(offset), chunk, got);
    good := st = Ok and got = 256;
    i := 0;
    while good and i < got do
    begin
      if chunk[i] <> byte(65 + ((offset + i) mod 26)) then
        good := false;
      i := i + 1;
    end;
    check("notes chunk", good);
    offset := offset + 256;
  end;
  st := core.fs_read(path, uint(NotesBytes), chunk, got);
  check("notes end of file", st = Ok and got = 0);
end;

// 1024 bytes, byte i = (i mod 256): every raw byte crosses unchanged.
procedure check_binary();
begin
  join(root, "bytes.bin", path);
  size := uint(0);
  st := core.fs_size(path, size);
  check("binary size", st = Ok and size = uint(BinaryBytes));
  offset := 0;
  while offset < BinaryBytes and ok do
  begin
    st := core.fs_read(path, uint(offset), chunk, got);
    good := st = Ok and got = 256;
    i := 0;
    while good and i < got do
    begin
      if chunk[i] <> byte((offset + i) mod 256) then
        good := false;
      i := i + 1;
    end;
    check("binary chunk", good);
    offset := offset + 256;
  end;
end;

// 300 bytes of '0'+ (i mod 10): created, read back, and left for the parent.
procedure check_write();
begin
  join(root, "written.txt", path);
  i := 0;
  while i < WrittenBytes do
  begin
    small[i] := byte(48 + (i mod 10));
    i := i + 1;
  end;
  got := -1;
  st := core.fs_write(path, uint(0), small, got);
  check("create written", st = Ok and got = WrittenBytes);
  size := uint(0);
  st := core.fs_size(path, size);
  check("written size", st = Ok and size = uint(WrittenBytes));
  st := core.fs_read(path, uint(0), chunk, got);
  good := st = Ok and got = 256;
  i := 0;
  while good and i < got do
  begin
    if chunk[i] <> byte(48 + (i mod 10)) then
      good := false;
    i := i + 1;
  end;
  check("written read back", good);
  st := core.fs_read(path, uint(256), chunk, got);
  good := st = Ok and got = WrittenBytes - 256;
  i := 0;
  while good and i < got do
  begin
    if chunk[i] <> byte(48 + ((256 + i) mod 10)) then
      good := false;
    i := i + 1;
  end;
  check("written tail", good);
end;

// The 128 KiB all-or-nothing bound over the wire, and one byte over refused
// before anything reaches the mount (spec 0032, D1).
procedure check_io_max();
begin
  join(root, "big.txt", path);
  st := core.fill_bytes(big, uint(0), byte(FillByte), uint(IoMax));
  check("fill big", st = Ok);
  big[0] := byte(FirstByte);
  big[IoMax - 1] := byte(LastByte);
  got := -1;
  st := core.fs_write(path, uint(0), big, got);
  check("write IO_MAX", st = Ok and got = IoMax);
  size := uint(0);
  st := core.fs_size(path, size);
  check("IO_MAX size", st = Ok and size = uint(IoMax));
  st := core.fs_read(path, uint(0), chunk, got);
  check("IO_MAX first chunk", st = Ok and got = 256
    and chunk[0] = byte(FirstByte) and chunk[255] = byte(FillByte));
  st := core.fs_read(path, uint(65536), chunk, got);
  check("IO_MAX middle chunk", st = Ok and got = 256 and chunk[0] = byte(FillByte));
  st := core.fs_read(path, uint(IoMax - 1), chunk, got);
  check("IO_MAX last byte", st = Ok and got = 1 and chunk[0] = byte(LastByte));

  st := core.fill_bytes(over, uint(0), byte(FillByte), uint(IoMax + 1));
  check("fill over", st = Ok);
  got := -1;
  st := core.fs_write(path, uint(0), over, got);
  check("over IO_MAX refused", st = CapacityExceeded and got = -1);
  size := uint(0);
  st := core.fs_size(path, size);
  check("over IO_MAX leaves the size", st = Ok and size = uint(IoMax));
  st := core.fs_read(path, uint(0), chunk, got);
  check("over IO_MAX leaves the bytes", st = Ok and chunk[0] = byte(FirstByte));
end;

// Truncate shorter and longer; the grown tail is zero-filled.
procedure check_truncate();
begin
  join(root, "trunc.txt", path);
  i := 0;
  while i < TruncBytes do
  begin
    trunc[i] := byte(116);   // 't'
    i := i + 1;
  end;
  got := -1;
  st := core.fs_write(path, uint(0), trunc, got);
  check("create trunc", st = Ok and got = TruncBytes);
  st := core.fs_truncate(path, uint(10));
  check("truncate shorter", st = Ok);
  size := uint(0);
  st := core.fs_size(path, size);
  check("truncated size", st = Ok and size = uint(10));
  st := core.fs_read(path, uint(0), chunk, got);
  check("truncated bytes", st = Ok and got = 10 and chunk[0] = byte(116));
  st := core.fs_truncate(path, uint(20));
  check("truncate longer", st = Ok);
  st := core.fs_read(path, uint(10), chunk, got);
  check("truncate zero fills", st = Ok and got = 10
    and chunk[0] = byte(0) and chunk[9] = byte(0));
  st := core.fs_remove(path);
  check("remove trunc", st = Ok);
  st := core.fs_size(path, size);
  check("trunc gone", st = NotFound);
end;

// Create and remove an empty directory.
procedure check_directory();
begin
  join(root, "scratch", path);
  st := core.fs_mkdir(path);
  check("mkdir", st = Ok);
  st := core.fs_list(path, uint(0), name, kind, size);
  check("new directory is empty", st = NotFound);
  st := core.fs_remove(path);
  check("remove empty directory", st = Ok);
  st := core.fs_list(path, uint(0), name, kind, size);
  check("directory gone", st = NotFound);
end;

// Rename the created file and leave it for the host comparison.
procedure check_rename();
begin
  join(root, "written.txt", path);
  join(root, "renamed.txt", renamed);
  st := core.fs_rename(path, renamed);
  check("rename", st = Ok);
  size := uint(0);
  st := core.fs_size(path, size);
  check("renamed away", st = NotFound);
  size := uint(0);
  st := core.fs_size(renamed, size);
  check("renamed size", st = Ok and size = uint(WrittenBytes));
  st := core.fs_read(renamed, uint(0), chunk, got);
  good := st = Ok and got = 256;
  i := 0;
  while good and i < got do
  begin
    if chunk[i] <> byte(48 + (i mod 10)) then
      good := false;
    i := i + 1;
  end;
  check("renamed bytes", good);
end;

procedure check_remove();
begin
  join(root, "remove_me.txt", path);
  st := core.fs_remove(path);
  check("remove fixture", st = Ok);
  size := uint(0);
  st := core.fs_size(path, size);
  check("removed fixture gone", st = NotFound);

  join(root, "big.txt", path);
  st := core.fs_remove(path);
  check("remove big", st = Ok);
  st := core.fs_size(path, size);
  check("big gone", st = NotFound);
end;

procedure main(args: const string[1024]);
begin
  ok := true;
  step := 0;
  if int(core.len(args)) = 0 then
  begin
    out.say("netmount_client: FAIL missing keyword\n");
    return;
  end;
  root := "net/" + args;
  check_root_listing();
  check_notes();
  check_binary();
  check_write();
  check_io_max();
  check_truncate();
  check_directory();
  check_rename();
  check_remove();
  if ok then
    out.say("netmount_client: ok\n")
  else
    out.say("netmount_client: FAILED\n");
end;
