diff options
author | Florian Klink <flokli@flokli.de> | 2022-12-27T15·11+0100 |
---|---|---|
committer | flokli <flokli@flokli.de> | 2022-12-27T21·31+0000 |
commit | dfd9286f680ef69ff89ab9a9081b2beaabda92be (patch) | |
tree | 4fc5d04e33dff412c4bfddea004a960586b65759 /tvix/store/protos/castore_test.go | |
parent | c3fb6d22187fc759563b265cfab65a48a6e4fa08 (diff) |
feat(tvix/store/protos): implement Directory.Validate() r/5509
Validate thecks the Directory message for invalid data, such as: - violations of name restrictions - invalid digest lengths - not properly sorted lists - duplicate names in the three lists Change-Id: I8d43a13797793c64097e526ef3bd482c9606c87b Reviewed-on: https://cl.tvl.fyi/c/depot/+/7648 Reviewed-by: tazjin <tazjin@tvl.su> Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/store/protos/castore_test.go')
-rw-r--r-- | tvix/store/protos/castore_test.go | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/tvix/store/protos/castore_test.go b/tvix/store/protos/castore_test.go index cac73f29c7f0..61fe535366d0 100644 --- a/tvix/store/protos/castore_test.go +++ b/tvix/store/protos/castore_test.go @@ -98,3 +98,174 @@ func TestDirectoryDigest(t *testing.T) { 0xe4, 0x1f, 0x32, 0x62, }, dgst) } + +func TestDirectoryValidate(t *testing.T) { + t.Run("empty", func(t *testing.T) { + d := storev1pb.Directory{ + Directories: []*storev1pb.DirectoryNode{}, + Files: []*storev1pb.FileNode{}, + Symlinks: []*storev1pb.SymlinkNode{}, + } + + assert.NoError(t, d.Validate()) + }) + + t.Run("invalid names", func(t *testing.T) { + { + d := storev1pb.Directory{ + Directories: []*storev1pb.DirectoryNode{{ + Name: "", + Digest: dummyDigest, + Size: 42, + }}, + Files: []*storev1pb.FileNode{}, + Symlinks: []*storev1pb.SymlinkNode{}, + } + + assert.ErrorContains(t, d.Validate(), "invalid name") + } + { + d := storev1pb.Directory{ + Directories: []*storev1pb.DirectoryNode{{ + Name: ".", + Digest: dummyDigest, + Size: 42, + }}, + Files: []*storev1pb.FileNode{}, + Symlinks: []*storev1pb.SymlinkNode{}, + } + + assert.ErrorContains(t, d.Validate(), "invalid name") + } + { + d := storev1pb.Directory{ + Directories: []*storev1pb.DirectoryNode{}, + Files: []*storev1pb.FileNode{{ + Name: "..", + Digest: dummyDigest, + Size: 42, + Executable: false, + }}, + Symlinks: []*storev1pb.SymlinkNode{}, + } + + assert.ErrorContains(t, d.Validate(), "invalid name") + } + { + d := storev1pb.Directory{ + Directories: []*storev1pb.DirectoryNode{}, + Files: []*storev1pb.FileNode{}, + Symlinks: []*storev1pb.SymlinkNode{{ + Name: "\x00", + Target: "foo", + }}, + } + + assert.ErrorContains(t, d.Validate(), "invalid name") + } + { + d := storev1pb.Directory{ + Directories: []*storev1pb.DirectoryNode{}, + Files: []*storev1pb.FileNode{}, + Symlinks: []*storev1pb.SymlinkNode{{ + Name: "foo/bar", + Target: "foo", + }}, + } + + assert.ErrorContains(t, d.Validate(), "invalid name") + } + }) + + t.Run("invalid digest", func(t *testing.T) { + d := storev1pb.Directory{ + Directories: []*storev1pb.DirectoryNode{{ + Name: "foo", + Digest: nil, + Size: 42, + }}, + Files: []*storev1pb.FileNode{}, + Symlinks: []*storev1pb.SymlinkNode{}, + } + + assert.ErrorContains(t, d.Validate(), "invalid digest length") + }) + + t.Run("sorting", func(t *testing.T) { + // "b" comes before "a", bad. + { + d := storev1pb.Directory{ + Directories: []*storev1pb.DirectoryNode{{ + Name: "b", + Digest: dummyDigest, + Size: 42, + }, { + Name: "a", + Digest: dummyDigest, + Size: 42, + }}, + Files: []*storev1pb.FileNode{}, + Symlinks: []*storev1pb.SymlinkNode{}, + } + assert.ErrorContains(t, d.Validate(), "is not in sorted order") + } + + // "a" exists twice, bad. + { + d := storev1pb.Directory{ + Directories: []*storev1pb.DirectoryNode{{ + Name: "a", + Digest: dummyDigest, + Size: 42, + }}, + Files: []*storev1pb.FileNode{{ + Name: "a", + Digest: dummyDigest, + Size: 42, + Executable: false, + }}, + Symlinks: []*storev1pb.SymlinkNode{}, + } + assert.ErrorContains(t, d.Validate(), "duplicate name") + } + + // "a" comes before "b", all good. + { + d := storev1pb.Directory{ + Directories: []*storev1pb.DirectoryNode{{ + Name: "a", + Digest: dummyDigest, + Size: 42, + }, { + Name: "b", + Digest: dummyDigest, + Size: 42, + }}, + Files: []*storev1pb.FileNode{}, + Symlinks: []*storev1pb.SymlinkNode{}, + } + assert.NoError(t, d.Validate(), "shouldn't error") + } + + // [b, c] and [a] are both properly sorted. + { + d := storev1pb.Directory{ + Directories: []*storev1pb.DirectoryNode{{ + Name: "b", + Digest: dummyDigest, + Size: 42, + }, { + Name: "c", + Digest: dummyDigest, + Size: 42, + }}, + Files: []*storev1pb.FileNode{}, + Symlinks: []*storev1pb.SymlinkNode{{ + Name: "a", + Target: "foo", + }}, + } + assert.NoError(t, d.Validate(), "shouldn't error") + } + }) +} |