nspkg / engine / checksum_test.v
23 lines · 21 sloc · 810 bytes · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1module engine
2
3import crypto.md5
4import crypto.sha256
5import os
6
7fn test_checksums() ! {
8 dir := os.join_path(os.vtmp_dir(), 'alpm-native-checksum-${os.getpid()}')
9 os.mkdir_all(dir)!
10 defer { os.rmdir_all(dir) or { eprintln(err) } }
11 path := os.join_path(dir, 'payload')
12 for data in [[]u8{}, 'abc'.bytes(), []u8{len: 32768, init: u8(index % 251)},
13 []u8{len: 100003, init: u8(index % 251)}] {
14 os.write_file_array(path, data)!
15 assert compute_md5sum(path)? == md5.sum(data).hex()
16 assert compute_sha256sum(path)? == sha256.sum256(data).hex()
17 }
18 assert compute_md5sum(path + '-missing') == none
19 assert compute_sha256sum(path + '-missing') == none
20 // opening a directory succeeds on linux; reading it must fail, not hash EOF.
21 assert compute_md5sum(dir) == none
22 assert compute_sha256sum(dir) == none
23}
24