2023-09-21 21:32:44 +02:00
|
|
|
package castorev1_test
|
2022-11-20 00:21:13 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-09-21 21:32:44 +02:00
|
|
|
castorev1pb "code.tvl.fyi/tvix/castore/protos"
|
2022-11-20 00:21:13 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
dummyDigest = []byte{
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDirectorySize(t *testing.T) {
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
2023-09-21 21:32:44 +02:00
|
|
|
d := castorev1pb.Directory{
|
|
|
|
Directories: []*castorev1pb.DirectoryNode{},
|
|
|
|
Files: []*castorev1pb.FileNode{},
|
|
|
|
Symlinks: []*castorev1pb.SymlinkNode{},
|
2022-11-20 00:21:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, uint32(0), d.Size())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("containing single empty directory", func(t *testing.T) {
|
2023-09-21 21:32:44 +02:00
|
|
|
d := castorev1pb.Directory{
|
|
|
|
Directories: []*castorev1pb.DirectoryNode{{
|
2023-09-09 13:55:06 +02:00
|
|
|
Name: []byte([]byte("foo")),
|
2022-11-20 00:21:13 +01:00
|
|
|
Digest: dummyDigest,
|
|
|
|
Size: 0,
|
|
|
|
}},
|
2023-09-21 21:32:44 +02:00
|
|
|
Files: []*castorev1pb.FileNode{},
|
|
|
|
Symlinks: []*castorev1pb.SymlinkNode{},
|
2022-11-20 00:21:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, uint32(1), d.Size())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("containing single non-empty directory", func(t *testing.T) {
|
2023-09-21 21:32:44 +02:00
|
|
|
d := castorev1pb.Directory{
|
|
|
|
Directories: []*castorev1pb.DirectoryNode{{
|
2023-09-09 13:55:06 +02:00
|
|
|
Name: []byte("foo"),
|
2022-11-20 00:21:13 +01:00
|
|
|
Digest: dummyDigest,
|
|
|
|
Size: 4,
|
|
|
|
}},
|
2023-09-21 21:32:44 +02:00
|
|
|
Files: []*castorev1pb.FileNode{},
|
|
|
|
Symlinks: []*castorev1pb.SymlinkNode{},
|
2022-11-20 00:21:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, uint32(5), d.Size())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("containing single file", func(t *testing.T) {
|
2023-09-21 21:32:44 +02:00
|
|
|
d := castorev1pb.Directory{
|
|
|
|
Directories: []*castorev1pb.DirectoryNode{},
|
|
|
|
Files: []*castorev1pb.FileNode{{
|
2023-09-09 13:55:06 +02:00
|
|
|
Name: []byte("foo"),
|
2022-11-20 00:21:13 +01:00
|
|
|
Digest: dummyDigest,
|
|
|
|
Size: 42,
|
|
|
|
Executable: false,
|
|
|
|
}},
|
2023-09-21 21:32:44 +02:00
|
|
|
Symlinks: []*castorev1pb.SymlinkNode{},
|
2022-11-20 00:21:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, uint32(1), d.Size())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("containing single symlink", func(t *testing.T) {
|
2023-09-21 21:32:44 +02:00
|
|
|
d := castorev1pb.Directory{
|
|
|
|
Directories: []*castorev1pb.DirectoryNode{},
|
|
|
|
Files: []*castorev1pb.FileNode{},
|
|
|
|
Symlinks: []*castorev1pb.SymlinkNode{{
|
2023-09-09 13:55:06 +02:00
|
|
|
Name: []byte("foo"),
|
|
|
|
Target: []byte("bar"),
|
2022-11-20 00:21:13 +01:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, uint32(1), d.Size())
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
func TestDirectoryDigest(t *testing.T) {
|
2023-09-21 21:32:44 +02:00
|
|
|
d := castorev1pb.Directory{
|
|
|
|
Directories: []*castorev1pb.DirectoryNode{},
|
|
|
|
Files: []*castorev1pb.FileNode{},
|
|
|
|
Symlinks: []*castorev1pb.SymlinkNode{},
|
2022-11-20 00:21:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
dgst, err := d.Digest()
|
|
|
|
assert.NoError(t, err, "calling Digest() on a directory shouldn't error")
|
|
|
|
assert.Equal(t, []byte{
|
|
|
|
0xaf, 0x13, 0x49, 0xb9, 0xf5, 0xf9, 0xa1, 0xa6, 0xa0, 0x40, 0x4d, 0xea, 0x36, 0xdc,
|
|
|
|
0xc9, 0x49, 0x9b, 0xcb, 0x25, 0xc9, 0xad, 0xc1, 0x12, 0xb7, 0xcc, 0x9a, 0x93, 0xca,
|
|
|
|
0xe4, 0x1f, 0x32, 0x62,
|
|
|
|
}, dgst)
|
|
|
|
}
|
2022-12-27 16:11:42 +01:00
|
|
|
|
|
|
|
func TestDirectoryValidate(t *testing.T) {
|
|
|
|
t.Run("empty", func(t *testing.T) {
|
2023-09-21 21:32:44 +02:00
|
|
|
d := castorev1pb.Directory{
|
|
|
|
Directories: []*castorev1pb.DirectoryNode{},
|
|
|
|
Files: []*castorev1pb.FileNode{},
|
|
|
|
Symlinks: []*castorev1pb.SymlinkNode{},
|
2022-12-27 16:11:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(t, d.Validate())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid names", func(t *testing.T) {
|
|
|
|
{
|
2023-09-21 21:32:44 +02:00
|
|
|
d := castorev1pb.Directory{
|
|
|
|
Directories: []*castorev1pb.DirectoryNode{{
|
2023-09-09 13:55:06 +02:00
|
|
|
Name: []byte{},
|
2022-12-27 16:11:42 +01:00
|
|
|
Digest: dummyDigest,
|
|
|
|
Size: 42,
|
|
|
|
}},
|
2023-09-21 21:32:44 +02:00
|
|
|
Files: []*castorev1pb.FileNode{},
|
|
|
|
Symlinks: []*castorev1pb.SymlinkNode{},
|
2022-12-27 16:11:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.ErrorContains(t, d.Validate(), "invalid name")
|
|
|
|
}
|
|
|
|
{
|
2023-09-21 21:32:44 +02:00
|
|
|
d := castorev1pb.Directory{
|
|
|
|
Directories: []*castorev1pb.DirectoryNode{{
|
2023-09-09 13:55:06 +02:00
|
|
|
Name: []byte("."),
|
2022-12-27 16:11:42 +01:00
|
|
|
Digest: dummyDigest,
|
|
|
|
Size: 42,
|
|
|
|
}},
|
2023-09-21 21:32:44 +02:00
|
|
|
Files: []*castorev1pb.FileNode{},
|
|
|
|
Symlinks: []*castorev1pb.SymlinkNode{},
|
2022-12-27 16:11:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.ErrorContains(t, d.Validate(), "invalid name")
|
|
|
|
}
|
|
|
|
{
|
2023-09-21 21:32:44 +02:00
|
|
|
d := castorev1pb.Directory{
|
|
|
|
Directories: []*castorev1pb.DirectoryNode{},
|
|
|
|
Files: []*castorev1pb.FileNode{{
|
2023-09-09 13:55:06 +02:00
|
|
|
Name: []byte(".."),
|
2022-12-27 16:11:42 +01:00
|
|
|
Digest: dummyDigest,
|
|
|
|
Size: 42,
|
|
|
|
Executable: false,
|
|
|
|
}},
|
2023-09-21 21:32:44 +02:00
|
|
|
Symlinks: []*castorev1pb.SymlinkNode{},
|
2022-12-27 16:11:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.ErrorContains(t, d.Validate(), "invalid name")
|
|
|
|
}
|
|
|
|
{
|
2023-09-21 21:32:44 +02:00
|
|
|
d := castorev1pb.Directory{
|
|
|
|
Directories: []*castorev1pb.DirectoryNode{},
|
|
|
|
Files: []*castorev1pb.FileNode{},
|
|
|
|
Symlinks: []*castorev1pb.SymlinkNode{{
|
2023-09-09 13:55:06 +02:00
|
|
|
Name: []byte("\x00"),
|
|
|
|
Target: []byte("foo"),
|
2022-12-27 16:11:42 +01:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.ErrorContains(t, d.Validate(), "invalid name")
|
|
|
|
}
|
|
|
|
{
|
2023-09-21 21:32:44 +02:00
|
|
|
d := castorev1pb.Directory{
|
|
|
|
Directories: []*castorev1pb.DirectoryNode{},
|
|
|
|
Files: []*castorev1pb.FileNode{},
|
|
|
|
Symlinks: []*castorev1pb.SymlinkNode{{
|
2023-09-09 13:55:06 +02:00
|
|
|
Name: []byte("foo/bar"),
|
|
|
|
Target: []byte("foo"),
|
2022-12-27 16:11:42 +01:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.ErrorContains(t, d.Validate(), "invalid name")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid digest", func(t *testing.T) {
|
2023-09-21 21:32:44 +02:00
|
|
|
d := castorev1pb.Directory{
|
|
|
|
Directories: []*castorev1pb.DirectoryNode{{
|
2023-09-09 13:55:06 +02:00
|
|
|
Name: []byte("foo"),
|
2022-12-27 16:11:42 +01:00
|
|
|
Digest: nil,
|
|
|
|
Size: 42,
|
|
|
|
}},
|
2023-09-21 21:32:44 +02:00
|
|
|
Files: []*castorev1pb.FileNode{},
|
|
|
|
Symlinks: []*castorev1pb.SymlinkNode{},
|
2022-12-27 16:11:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.ErrorContains(t, d.Validate(), "invalid digest length")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("sorting", func(t *testing.T) {
|
|
|
|
// "b" comes before "a", bad.
|
|
|
|
{
|
2023-09-21 21:32:44 +02:00
|
|
|
d := castorev1pb.Directory{
|
|
|
|
Directories: []*castorev1pb.DirectoryNode{{
|
2023-09-09 13:55:06 +02:00
|
|
|
Name: []byte("b"),
|
2022-12-27 16:11:42 +01:00
|
|
|
Digest: dummyDigest,
|
|
|
|
Size: 42,
|
|
|
|
}, {
|
2023-09-09 13:55:06 +02:00
|
|
|
Name: []byte("a"),
|
2022-12-27 16:11:42 +01:00
|
|
|
Digest: dummyDigest,
|
|
|
|
Size: 42,
|
|
|
|
}},
|
2023-09-21 21:32:44 +02:00
|
|
|
Files: []*castorev1pb.FileNode{},
|
|
|
|
Symlinks: []*castorev1pb.SymlinkNode{},
|
2022-12-27 16:11:42 +01:00
|
|
|
}
|
|
|
|
assert.ErrorContains(t, d.Validate(), "is not in sorted order")
|
|
|
|
}
|
|
|
|
|
|
|
|
// "a" exists twice, bad.
|
|
|
|
{
|
2023-09-21 21:32:44 +02:00
|
|
|
d := castorev1pb.Directory{
|
|
|
|
Directories: []*castorev1pb.DirectoryNode{{
|
2023-09-09 13:55:06 +02:00
|
|
|
Name: []byte("a"),
|
2022-12-27 16:11:42 +01:00
|
|
|
Digest: dummyDigest,
|
|
|
|
Size: 42,
|
|
|
|
}},
|
2023-09-21 21:32:44 +02:00
|
|
|
Files: []*castorev1pb.FileNode{{
|
2023-09-09 13:55:06 +02:00
|
|
|
Name: []byte("a"),
|
2022-12-27 16:11:42 +01:00
|
|
|
Digest: dummyDigest,
|
|
|
|
Size: 42,
|
|
|
|
Executable: false,
|
|
|
|
}},
|
2023-09-21 21:32:44 +02:00
|
|
|
Symlinks: []*castorev1pb.SymlinkNode{},
|
2022-12-27 16:11:42 +01:00
|
|
|
}
|
|
|
|
assert.ErrorContains(t, d.Validate(), "duplicate name")
|
|
|
|
}
|
|
|
|
|
|
|
|
// "a" comes before "b", all good.
|
|
|
|
{
|
2023-09-21 21:32:44 +02:00
|
|
|
d := castorev1pb.Directory{
|
|
|
|
Directories: []*castorev1pb.DirectoryNode{{
|
2023-09-09 13:55:06 +02:00
|
|
|
Name: []byte("a"),
|
2022-12-27 16:11:42 +01:00
|
|
|
Digest: dummyDigest,
|
|
|
|
Size: 42,
|
|
|
|
}, {
|
2023-09-09 13:55:06 +02:00
|
|
|
Name: []byte("b"),
|
2022-12-27 16:11:42 +01:00
|
|
|
Digest: dummyDigest,
|
|
|
|
Size: 42,
|
|
|
|
}},
|
2023-09-21 21:32:44 +02:00
|
|
|
Files: []*castorev1pb.FileNode{},
|
|
|
|
Symlinks: []*castorev1pb.SymlinkNode{},
|
2022-12-27 16:11:42 +01:00
|
|
|
}
|
|
|
|
assert.NoError(t, d.Validate(), "shouldn't error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// [b, c] and [a] are both properly sorted.
|
|
|
|
{
|
2023-09-21 21:32:44 +02:00
|
|
|
d := castorev1pb.Directory{
|
|
|
|
Directories: []*castorev1pb.DirectoryNode{{
|
2023-09-09 13:55:06 +02:00
|
|
|
Name: []byte("b"),
|
2022-12-27 16:11:42 +01:00
|
|
|
Digest: dummyDigest,
|
|
|
|
Size: 42,
|
|
|
|
}, {
|
2023-09-09 13:55:06 +02:00
|
|
|
Name: []byte("c"),
|
2022-12-27 16:11:42 +01:00
|
|
|
Digest: dummyDigest,
|
|
|
|
Size: 42,
|
|
|
|
}},
|
2023-09-21 21:32:44 +02:00
|
|
|
Files: []*castorev1pb.FileNode{},
|
|
|
|
Symlinks: []*castorev1pb.SymlinkNode{{
|
2023-09-09 13:55:06 +02:00
|
|
|
Name: []byte("a"),
|
|
|
|
Target: []byte("foo"),
|
2022-12-27 16:11:42 +01:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
assert.NoError(t, d.Validate(), "shouldn't error")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|