Files
git/tests/git_test.zi
waoziandClaude Opus 5.5 6a1f6a06a4 Git object formats, packfiles and remote URL sanitizing
Object ids, loose objects, trees, commits and refs; packfile index lookup,
entries and deltas; gix-url location validation and credential removal.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-15 21:32:50 -03:00

152 lines
6.6 KiB
Ziran

#import "git"
#import "git_url"
#import "git_pack"
Matches :: (id: []u8, hex: string) -> bool {
text: [40]u8
GitWriteHex(id, text[:], 0)
i: s64 = 0
while i < 40 {
if text[i] != hex[i] { return false }
i += 1
}
return true
}
Bytes :: (text: string, into: []u8) -> []u8 {
i: s64 = 0
while i < text.count {
into[i] = text[i]
i += 1
}
return into[:text.count]
}
// GitSanitizeUrl(text) == expected; an empty expectation means rejected.
Sanitized :: (text: string, expected: string) -> bool {
input: [256]u8
out: [272]u8
length := GitSanitizeUrl(Bytes(text, input[:]), out[:])
if expected.count == 0 { return length == GitUrlInvalid }
if length != expected.count { return false }
i: s64 = 0
while i < length {
if out[i] != expected[i] { return false }
i += 1
}
return true
}
#program_export
main :: () -> s32 {
id: [20]u8
storage: [64]u8
empty := storage[:0]
if !GitHashObject(GitBlob, empty, id[:]) || !Matches(id[:], "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391") { return 1 }
hello_text: [8]u8
hello := Bytes("hello\n", hello_text[:])
if !GitHashObject(GitBlob, hello, id[:]) || !Matches(id[:], "ce013625030ba8dba906f756967f9e9ca394464a") { return 2 }
if !GitHashObject(GitTree, empty, id[:]) || !Matches(id[:], "4b825dc642cb6eb9a060e54bf8d69288fbee4904") { return 3 }
// A loose object survives encoding and decoding.
packed: [128]u8
length := GitLooseEncode(GitBlob, hello, packed[:])
if length != GitLooseEncodedSize(GitBlob, 6) { return 4 }
plain: [64]u8
inflated := GitLooseDecode(packed[:length], plain[:])
kind: s64 = 0
size: s64 = 0
header := GitParseHeader(plain[:inflated], *kind, *size)
if inflated != 13 || header != 7 || kind != GitBlob || size != 6 || plain[7] != cast(u8)104 { return 5 }
packed[length - 1] ^= cast(u8)1
if GitLooseDecode(packed[:length], plain[:]) != GitError { return 6 }
// Tree order puts "a.b" before the tree "a" (which sorts as "a/").
name_a: [4]u8
name_b: [4]u8
items: [2]GitTreeItem
scratch: [2]GitTreeItem
items[0].name = Bytes("a", name_a[:])
items[0].mode = GitModeTree
items[0].id = id[:]
items[1].name = Bytes("a.b", name_b[:])
items[1].mode = GitModeBlob
items[1].id = id[:]
if !GitTreeSort(items[:], scratch[:]) || items[0].name.count != 3 { return 7 }
tree: [128]u8
tree_length := GitTreeEncode(items[:], tree[:])
if tree_length != GitTreeEncodedSize(items[:]) || tree_length != 11 + 20 + 8 + 20 { return 8 }
entry: GitTreeEntry
next := GitTreeNext(tree[:tree_length], 0, *entry)
next = GitTreeNext(tree[:tree_length], next, *entry)
if next != tree_length || entry.mode != GitModeTree || entry.name_end - entry.name_start != 1 { return 9 }
// A commit round trip.
author: GitSignature
author_name: [8]u8
author_email: [8]u8
author.name = Bytes("Codex", author_name[:])
author.email = Bytes("a@b", author_email[:])
author.seconds = 1700000000
author.offset_minutes = -90
message_text: [16]u8
message := Bytes("snapshot\n", message_text[:])
commit: [256]u8
commit_length := GitCommitEncode(id[:], id[:], *author, *author, message, commit[:])
if commit_length != GitCommitEncodedSize(1, *author, *author, message) { return 10 }
tree_id: [20]u8
parents: [40]u8
if !GitCommitTree(commit[:commit_length], tree_id[:]) || tree_id[0] != id[0] ||
GitCommitParents(commit[:commit_length], parents[:]) != 1 { return 11 }
// "Codex <a@b> 1700000000 -0130"
if commit[commit_length - 16] != cast(u8)45 || commit[commit_length - 15] != cast(u8)48 ||
commit[commit_length - 13] != cast(u8)51 { return 12 }
head: [32]u8
ref_id: [20]u8
target_start: s64 = 0
target_end: s64 = 0
if GitParseRef(Bytes("ref: refs/heads/main\n", head[:]), *target_start, *target_end, ref_id[:]) != 1 ||
target_start != 5 || target_end != 20 { return 13 }
if !Sanitized("https://alice:[email protected]/org/repo.git", "https://github.com/org/repo.git") { return 20 }
if !Sanitized("http://user:[email protected][email protected]/a/b", "http://[email protected]/a/b") { return 21 }
if !Sanitized("ssh://user:synthetic#[email protected]/org/repo", "ssh://example.com/org/repo") { return 22 }
if !Sanitized("https://alice%40example.com:secret%[email protected]/org/repo.git", "https://github.com/org/repo.git") { return 23 }
if !Sanitized("file://git:secret@server/share/repo.git", "file://server/share/repo.git") { return 24 }
if !Sanitized("remote-hg::[email protected]:org/repo.git", "remote-hg::[email protected]:org/repo.git") { return 25 }
if !Sanitized("hg::https://alice:[email protected]/org/repo.git", "hg::https://example.invalid/org/repo.git") { return 26 }
if !Sanitized("ext::ssh alice:[email protected] git-upload-pack /repo", "") { return 27 }
if !Sanitized("[email protected]:org/repo.git", "github.com:org/repo.git") { return 28 }
if !Sanitized("ssh://git:[email protected]/org/repo.git", "ssh://[email protected]/org/repo.git") { return 29 }
if !Sanitized("alice@[::1]:org/repo.git", "[::1]:org/repo.git") { return 30 }
if !Sanitized("https://exa mple.com/repo", "") { return 31 }
if !Sanitized("/home/user/repo", "/home/user/repo") { return 32 }
if !Sanitized("http://[fe80::1%25eth0]:8080/repo", "http://[fe80::1%25eth0]:8080/repo") { return 33 }
if !Sanitized("http://example.com:0/repo", "") { return 34 }
if !Sanitized("[email protected]:org/repo.git", "[email protected]:org/repo.git") { return 35 }
if !Sanitized("https://[email protected]/org/repo.git", "https://github.com/org/repo.git") { return 36 }
if !Sanitized("git@[2001:db8::1]:org/repo.git", "git@[2001:db8::1]:org/repo.git") { return 37 }
if !Sanitized("alice@[::1]:dir name/repo", "[::1]:dir name/repo") { return 38 }
if !Sanitized("alice@[::1]:repo%FF.git", "[::1]:repo%FF.git") { return 39 }
if !Sanitized("https://alice:secret-token@[invalid", "") { return 40 }
// Delta: copy "hello" from the base, then insert " there!".
base_text: [16]u8
base := Bytes("hello world", base_text[:])
delta: [16]u8
delta[0] = cast(u8)11
delta[1] = cast(u8)12
delta[2] = cast(u8)0x90
delta[3] = cast(u8)5
delta[4] = cast(u8)7
tail := " there!"
i: s64 = 0
while i < 7 {
delta[5 + i] = tail[i]
i += 1
}
result: [16]u8
if GitDeltaApply(base, delta[:12], result[:]) != 12 || result[5] != cast(u8)32 || result[11] != cast(u8)33 { return 41 }
if GitDeltaApply(base, delta[:11], result[:]) != GitPackError { return 42 }
return 0
}