mnix: Nix binary-cache client PoC in MoonBit (install hello from cache.nixos.org) (#1)

This commit was merged in pull request #1.
This commit is contained in:
nandi committed 2026-11-24 22:12:02 +00:00
1 parent 4e7ce11973
commit fa70164336
48 files changed
+3112 -2

No files matched your search

+72
View File
@@ -0,0 +1,72 @@
///|
fn sample() -> Node {
Directory(entries=[
(
"bin",
Directory(entries=[
("hello", File(executable=true, contents=b"#!/bin/sh\necho hi\n")),
("link", Symlink(target="hello")),
]),
),
("empty", File(executable=false, contents=b"")),
("readme", File(executable=false, contents=b"0123456789")),
])
}
///|
test "roundtrip" {
let n = sample()
let bytes = serialize(n)
assert_eq(bytes.length() % 8, 0)
assert_eq(parse(bytes), n)
assert_eq(serialize(parse(bytes)), bytes)
}
///|
test "single file roundtrip" {
let n = File(executable=false, contents=b"abc")
assert_eq(parse(serialize(n)), n)
}
///|
test "known encoding of a regular file" {
// Matches `nix-store --dump` for a 3-byte file "abc" (hand-checked layout).
let b = serialize(File(executable=false, contents=b"abc"))
// magic(8+16) "(" (16) "type"(16) "regular"(16) "contents"(16) data(16) ")"(16)
assert_eq(b.length(), 24 + 16 + 16 + 16 + 16 + 16 + 16)
assert_eq(b[0], b'\x0d')
assert_eq(b[8], b'n')
}
///|
test "rejects malformations" {
let good = serialize(sample())
// truncated
assert_true(@store.raises(() => parse(good[0:good.length() - 8].to_owned())))
// trailing garbage
let trailing = @buffer.Buffer()
trailing.write_bytes(good)
trailing.write_bytes(b"\x00\x00\x00\x00\x00\x00\x00\x00")
assert_true(@store.raises(() => parse(trailing.to_bytes())))
// bad magic
assert_true(@store.raises(() => parse(b"hello")))
// unsorted entries
let bad = Directory(entries=[
("b", File(executable=false, contents=b"")),
("a", File(executable=false, contents=b"")),
])
assert_true(@store.raises(() => parse(serialize(bad))))
// path traversal name
let evil = Directory(entries=[("..", File(executable=false, contents=b""))])
assert_true(@store.raises(() => parse(serialize(evil))))
let evil2 = Directory(entries=[("a/b", File(executable=false, contents=b""))])
assert_true(@store.raises(() => parse(serialize(evil2))))
}
///|
test "bytewise ordering is lexicographic, not length-first" {
assert_true(bytes_compare(b"hello", b"link") < 0)
assert_true(bytes_compare(b"a", b"ab") < 0)
assert_true(bytes_compare(b"b", b"a") > 0)
assert_eq(bytes_compare(b"x", b"x"), 0)
}