73 lines
2.2 KiB
MoonBit
73 lines
2.2 KiB
MoonBit
///|
|
|
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)
|
|
}
|