From 3ea4e1f202102d0d5145e6b3c54335e5bd8f10ff Mon Sep 17 00:00:00 2001 From: Adam Hughes Date: Wed, 23 Jun 2021 21:14:41 +0000 Subject: [PATCH 1/8] test: add unit test for Header --- internal/app/siftool/info_test.go | 87 +++++++++++++++++++ .../testdata/TestApp_Header/Empty.golden | 14 +++ .../testdata/TestApp_Header/OneGroup.golden | 14 +++ .../TestApp_Header/OneGroupSigned.golden | 14 +++ .../OneGroupSignedLegacy.golden | 14 +++ .../OneGroupSignedLegacyAll.golden | 14 +++ .../OneGroupSignedLegacyGroup.golden | 14 +++ .../testdata/TestApp_Header/TwoGroups.golden | 14 +++ .../TestApp_Header/TwoGroupsSigned.golden | 14 +++ .../TwoGroupsSignedLegacy.golden | 14 +++ .../TwoGroupsSignedLegacyAll.golden | 14 +++ .../TwoGroupsSignedLegacyGroup.golden | 14 +++ 12 files changed, 241 insertions(+) create mode 100644 internal/app/siftool/info_test.go create mode 100644 internal/app/siftool/testdata/TestApp_Header/Empty.golden create mode 100644 internal/app/siftool/testdata/TestApp_Header/OneGroup.golden create mode 100644 internal/app/siftool/testdata/TestApp_Header/OneGroupSigned.golden create mode 100644 internal/app/siftool/testdata/TestApp_Header/OneGroupSignedLegacy.golden create mode 100644 internal/app/siftool/testdata/TestApp_Header/OneGroupSignedLegacyAll.golden create mode 100644 internal/app/siftool/testdata/TestApp_Header/OneGroupSignedLegacyGroup.golden create mode 100644 internal/app/siftool/testdata/TestApp_Header/TwoGroups.golden create mode 100644 internal/app/siftool/testdata/TestApp_Header/TwoGroupsSigned.golden create mode 100644 internal/app/siftool/testdata/TestApp_Header/TwoGroupsSignedLegacy.golden create mode 100644 internal/app/siftool/testdata/TestApp_Header/TwoGroupsSignedLegacyAll.golden create mode 100644 internal/app/siftool/testdata/TestApp_Header/TwoGroupsSignedLegacyGroup.golden diff --git a/internal/app/siftool/info_test.go b/internal/app/siftool/info_test.go new file mode 100644 index 00000000..bcc3edb5 --- /dev/null +++ b/internal/app/siftool/info_test.go @@ -0,0 +1,87 @@ +// Copyright (c) 2021, Sylabs Inc. All rights reserved. +// This software is licensed under a 3-clause BSD license. Please consult the +// LICENSE file distributed with the sources of this project regarding your +// rights to use or distribute this software. + +package siftool + +import ( + "bytes" + "errors" + "path/filepath" + "testing" + + "github.com/sebdah/goldie/v2" +) + +var corpus = filepath.Join("..", "..", "..", "pkg", "integrity", "testdata", "images") + +func TestApp_Header(t *testing.T) { + tests := []struct { + name string + path string + wantErr error + }{ + { + name: "Empty", + path: filepath.Join(corpus, "empty.sif"), + }, + { + name: "OneGroup", + path: filepath.Join(corpus, "one-group.sif"), + }, + { + name: "OneGroupSigned", + path: filepath.Join(corpus, "one-group-signed.sif"), + }, + { + name: "OneGroupSignedLegacy", + path: filepath.Join(corpus, "one-group-signed-legacy.sif"), + }, + { + name: "OneGroupSignedLegacyAll", + path: filepath.Join(corpus, "one-group-signed-legacy-all.sif"), + }, + { + name: "OneGroupSignedLegacyGroup", + path: filepath.Join(corpus, "one-group-signed-legacy-group.sif"), + }, + { + name: "TwoGroups", + path: filepath.Join(corpus, "two-groups.sif"), + }, + { + name: "TwoGroupsSigned", + path: filepath.Join(corpus, "two-groups-signed.sif"), + }, + { + name: "TwoGroupsSignedLegacy", + path: filepath.Join(corpus, "two-groups-signed-legacy.sif"), + }, + { + name: "TwoGroupsSignedLegacyAll", + path: filepath.Join(corpus, "two-groups-signed-legacy-all.sif"), + }, + { + name: "TwoGroupsSignedLegacyGroup", + path: filepath.Join(corpus, "two-groups-signed-legacy-group.sif"), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var b bytes.Buffer + + a, err := New(OptAppOutput(&b)) + if err != nil { + t.Fatalf("failed to create app: %v", err) + } + + if got, want := a.Header(tt.path), tt.wantErr; !errors.Is(got, want) { + t.Fatalf("got error %v, want %v", got, want) + } + + g := goldie.New(t, goldie.WithTestNameForDir(true)) + g.Assert(t, tt.name, b.Bytes()) + }) + } +} diff --git a/internal/app/siftool/testdata/TestApp_Header/Empty.golden b/internal/app/siftool/testdata/TestApp_Header/Empty.golden new file mode 100644 index 00000000..ed6507ef --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_Header/Empty.golden @@ -0,0 +1,14 @@ +Launch: #!/usr/bin/env run-singularity + +Magic: SIF_MAGIC +Version: 01 +Arch: unknown +ID: 3fa802cc-358b-45e3-bcc0-69dc7a45f9f8 +Ctime: 2020-05-22 19:30:59 +0000 UTC +Mtime: 2020-05-22 19:30:59 +0000 UTC +Dfree: 48 +Dtotal: 48 +Descoff: 4096 +Descrlen: 27KB +Dataoff: 32768 +Datalen: 0 diff --git a/internal/app/siftool/testdata/TestApp_Header/OneGroup.golden b/internal/app/siftool/testdata/TestApp_Header/OneGroup.golden new file mode 100644 index 00000000..64fbeb95 --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_Header/OneGroup.golden @@ -0,0 +1,14 @@ +Launch: #!/usr/bin/env run-singularity + +Magic: SIF_MAGIC +Version: 01 +Arch: 386 +ID: 6ecc76b7-a497-4f7f-9ebd-8da2a04c6be1 +Ctime: 2020-05-22 19:30:59 +0000 UTC +Mtime: 2020-05-22 19:30:59 +0000 UTC +Dfree: 46 +Dtotal: 48 +Descoff: 4096 +Descrlen: 27KB +Dataoff: 32768 +Datalen: 4KB diff --git a/internal/app/siftool/testdata/TestApp_Header/OneGroupSigned.golden b/internal/app/siftool/testdata/TestApp_Header/OneGroupSigned.golden new file mode 100644 index 00000000..d9ab455b --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_Header/OneGroupSigned.golden @@ -0,0 +1,14 @@ +Launch: #!/usr/bin/env run-singularity + +Magic: SIF_MAGIC +Version: 01 +Arch: 386 +ID: 73e1c5c3-5c41-41ed-ad7c-2504d669f140 +Ctime: 2020-06-30 00:01:56 +0000 UTC +Mtime: 2020-06-30 00:01:56 +0000 UTC +Dfree: 45 +Dtotal: 48 +Descoff: 4096 +Descrlen: 27KB +Dataoff: 32768 +Datalen: 8KB diff --git a/internal/app/siftool/testdata/TestApp_Header/OneGroupSignedLegacy.golden b/internal/app/siftool/testdata/TestApp_Header/OneGroupSignedLegacy.golden new file mode 100644 index 00000000..1e11aff6 --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_Header/OneGroupSignedLegacy.golden @@ -0,0 +1,14 @@ +Launch: #!/usr/bin/env run-singularity + +Magic: SIF_MAGIC +Version: 01 +Arch: 386 +ID: 6ecc76b7-a497-4f7f-9ebd-8da2a04c6be1 +Ctime: 2020-05-22 19:30:59 +0000 UTC +Mtime: 2020-06-20 20:16:39 +0000 UTC +Dfree: 45 +Dtotal: 48 +Descoff: 4096 +Descrlen: 27KB +Dataoff: 32768 +Datalen: 8KB diff --git a/internal/app/siftool/testdata/TestApp_Header/OneGroupSignedLegacyAll.golden b/internal/app/siftool/testdata/TestApp_Header/OneGroupSignedLegacyAll.golden new file mode 100644 index 00000000..6821e21f --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_Header/OneGroupSignedLegacyAll.golden @@ -0,0 +1,14 @@ +Launch: #!/usr/bin/env run-singularity + +Magic: SIF_MAGIC +Version: 01 +Arch: 386 +ID: 6ecc76b7-a497-4f7f-9ebd-8da2a04c6be1 +Ctime: 2020-05-22 19:30:59 +0000 UTC +Mtime: 2020-06-20 20:17:15 +0000 UTC +Dfree: 44 +Dtotal: 48 +Descoff: 4096 +Descrlen: 27KB +Dataoff: 32768 +Datalen: 12KB diff --git a/internal/app/siftool/testdata/TestApp_Header/OneGroupSignedLegacyGroup.golden b/internal/app/siftool/testdata/TestApp_Header/OneGroupSignedLegacyGroup.golden new file mode 100644 index 00000000..1cd7b870 --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_Header/OneGroupSignedLegacyGroup.golden @@ -0,0 +1,14 @@ +Launch: #!/usr/bin/env run-singularity + +Magic: SIF_MAGIC +Version: 01 +Arch: 386 +ID: 6ecc76b7-a497-4f7f-9ebd-8da2a04c6be1 +Ctime: 2020-05-22 19:30:59 +0000 UTC +Mtime: 2020-06-20 20:16:55 +0000 UTC +Dfree: 45 +Dtotal: 48 +Descoff: 4096 +Descrlen: 27KB +Dataoff: 32768 +Datalen: 8KB diff --git a/internal/app/siftool/testdata/TestApp_Header/TwoGroups.golden b/internal/app/siftool/testdata/TestApp_Header/TwoGroups.golden new file mode 100644 index 00000000..394187c1 --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_Header/TwoGroups.golden @@ -0,0 +1,14 @@ +Launch: #!/usr/bin/env run-singularity + +Magic: SIF_MAGIC +Version: 01 +Arch: 386 +ID: 0b19ec2c-0b08-46c9-95ae-fa88cd9e48a1 +Ctime: 2020-05-22 19:30:59 +0000 UTC +Mtime: 2020-05-22 19:30:59 +0000 UTC +Dfree: 45 +Dtotal: 48 +Descoff: 4096 +Descrlen: 27KB +Dataoff: 32768 +Datalen: 8KB diff --git a/internal/app/siftool/testdata/TestApp_Header/TwoGroupsSigned.golden b/internal/app/siftool/testdata/TestApp_Header/TwoGroupsSigned.golden new file mode 100644 index 00000000..fa14cd02 --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_Header/TwoGroupsSigned.golden @@ -0,0 +1,14 @@ +Launch: #!/usr/bin/env run-singularity + +Magic: SIF_MAGIC +Version: 01 +Arch: 386 +ID: 610cf3a3-18b0-4622-8b08-772d3510d7b5 +Ctime: 2020-06-30 00:01:56 +0000 UTC +Mtime: 2020-06-30 00:01:56 +0000 UTC +Dfree: 43 +Dtotal: 48 +Descoff: 4096 +Descrlen: 27KB +Dataoff: 32768 +Datalen: 16KB diff --git a/internal/app/siftool/testdata/TestApp_Header/TwoGroupsSignedLegacy.golden b/internal/app/siftool/testdata/TestApp_Header/TwoGroupsSignedLegacy.golden new file mode 100644 index 00000000..a4e0aa06 --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_Header/TwoGroupsSignedLegacy.golden @@ -0,0 +1,14 @@ +Launch: #!/usr/bin/env run-singularity + +Magic: SIF_MAGIC +Version: 01 +Arch: 386 +ID: 0b19ec2c-0b08-46c9-95ae-fa88cd9e48a1 +Ctime: 2020-05-22 19:30:59 +0000 UTC +Mtime: 2020-06-20 20:16:44 +0000 UTC +Dfree: 44 +Dtotal: 48 +Descoff: 4096 +Descrlen: 27KB +Dataoff: 32768 +Datalen: 12KB diff --git a/internal/app/siftool/testdata/TestApp_Header/TwoGroupsSignedLegacyAll.golden b/internal/app/siftool/testdata/TestApp_Header/TwoGroupsSignedLegacyAll.golden new file mode 100644 index 00000000..3cdf05b1 --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_Header/TwoGroupsSignedLegacyAll.golden @@ -0,0 +1,14 @@ +Launch: #!/usr/bin/env run-singularity + +Magic: SIF_MAGIC +Version: 01 +Arch: 386 +ID: 0b19ec2c-0b08-46c9-95ae-fa88cd9e48a1 +Ctime: 2020-05-22 19:30:59 +0000 UTC +Mtime: 2020-06-20 20:17:19 +0000 UTC +Dfree: 43 +Dtotal: 48 +Descoff: 4096 +Descrlen: 27KB +Dataoff: 32768 +Datalen: 16KB diff --git a/internal/app/siftool/testdata/TestApp_Header/TwoGroupsSignedLegacyGroup.golden b/internal/app/siftool/testdata/TestApp_Header/TwoGroupsSignedLegacyGroup.golden new file mode 100644 index 00000000..cac483ea --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_Header/TwoGroupsSignedLegacyGroup.golden @@ -0,0 +1,14 @@ +Launch: #!/usr/bin/env run-singularity + +Magic: SIF_MAGIC +Version: 01 +Arch: 386 +ID: 0b19ec2c-0b08-46c9-95ae-fa88cd9e48a1 +Ctime: 2020-05-22 19:30:59 +0000 UTC +Mtime: 2020-06-20 20:16:58 +0000 UTC +Dfree: 44 +Dtotal: 48 +Descoff: 4096 +Descrlen: 27KB +Dataoff: 32768 +Datalen: 12KB From 392f2ca0b5953fc555283dcfc2a5bbd0975aaff6 Mon Sep 17 00:00:00 2001 From: Adam Hughes Date: Wed, 23 Jun 2021 21:19:56 +0000 Subject: [PATCH 2/8] test: add unit test for List --- internal/app/siftool/info_test.go | 72 +++++++++++++++++++ .../testdata/TestApp_List/Empty.golden | 7 ++ .../testdata/TestApp_List/OneGroup.golden | 9 +++ .../TestApp_List/OneGroupSigned.golden | 10 +++ .../TestApp_List/OneGroupSignedLegacy.golden | 10 +++ .../OneGroupSignedLegacyAll.golden | 11 +++ .../OneGroupSignedLegacyGroup.golden | 10 +++ .../testdata/TestApp_List/TwoGroups.golden | 10 +++ .../TestApp_List/TwoGroupsSigned.golden | 12 ++++ .../TestApp_List/TwoGroupsSignedLegacy.golden | 11 +++ .../TwoGroupsSignedLegacyAll.golden | 12 ++++ .../TwoGroupsSignedLegacyGroup.golden | 11 +++ 12 files changed, 185 insertions(+) create mode 100644 internal/app/siftool/testdata/TestApp_List/Empty.golden create mode 100644 internal/app/siftool/testdata/TestApp_List/OneGroup.golden create mode 100644 internal/app/siftool/testdata/TestApp_List/OneGroupSigned.golden create mode 100644 internal/app/siftool/testdata/TestApp_List/OneGroupSignedLegacy.golden create mode 100644 internal/app/siftool/testdata/TestApp_List/OneGroupSignedLegacyAll.golden create mode 100644 internal/app/siftool/testdata/TestApp_List/OneGroupSignedLegacyGroup.golden create mode 100644 internal/app/siftool/testdata/TestApp_List/TwoGroups.golden create mode 100644 internal/app/siftool/testdata/TestApp_List/TwoGroupsSigned.golden create mode 100644 internal/app/siftool/testdata/TestApp_List/TwoGroupsSignedLegacy.golden create mode 100644 internal/app/siftool/testdata/TestApp_List/TwoGroupsSignedLegacyAll.golden create mode 100644 internal/app/siftool/testdata/TestApp_List/TwoGroupsSignedLegacyGroup.golden diff --git a/internal/app/siftool/info_test.go b/internal/app/siftool/info_test.go index bcc3edb5..5ad0fb92 100644 --- a/internal/app/siftool/info_test.go +++ b/internal/app/siftool/info_test.go @@ -16,6 +16,7 @@ import ( var corpus = filepath.Join("..", "..", "..", "pkg", "integrity", "testdata", "images") +//nolint:dupl func TestApp_Header(t *testing.T) { tests := []struct { name string @@ -85,3 +86,74 @@ func TestApp_Header(t *testing.T) { }) } } + +//nolint:dupl +func TestApp_List(t *testing.T) { + tests := []struct { + name string + path string + wantErr error + }{ + { + name: "Empty", + path: filepath.Join(corpus, "empty.sif"), + }, + { + name: "OneGroup", + path: filepath.Join(corpus, "one-group.sif"), + }, + { + name: "OneGroupSigned", + path: filepath.Join(corpus, "one-group-signed.sif"), + }, + { + name: "OneGroupSignedLegacy", + path: filepath.Join(corpus, "one-group-signed-legacy.sif"), + }, + { + name: "OneGroupSignedLegacyAll", + path: filepath.Join(corpus, "one-group-signed-legacy-all.sif"), + }, + { + name: "OneGroupSignedLegacyGroup", + path: filepath.Join(corpus, "one-group-signed-legacy-group.sif"), + }, + { + name: "TwoGroups", + path: filepath.Join(corpus, "two-groups.sif"), + }, + { + name: "TwoGroupsSigned", + path: filepath.Join(corpus, "two-groups-signed.sif"), + }, + { + name: "TwoGroupsSignedLegacy", + path: filepath.Join(corpus, "two-groups-signed-legacy.sif"), + }, + { + name: "TwoGroupsSignedLegacyAll", + path: filepath.Join(corpus, "two-groups-signed-legacy-all.sif"), + }, + { + name: "TwoGroupsSignedLegacyGroup", + path: filepath.Join(corpus, "two-groups-signed-legacy-group.sif"), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var b bytes.Buffer + + a, err := New(OptAppOutput(&b)) + if err != nil { + t.Fatalf("failed to create app: %v", err) + } + + if got, want := a.List(tt.path), tt.wantErr; !errors.Is(got, want) { + t.Fatalf("got error %v, want %v", got, want) + } + + g := goldie.New(t, goldie.WithTestNameForDir(true)) + g.Assert(t, tt.name, b.Bytes()) + }) + } +} diff --git a/internal/app/siftool/testdata/TestApp_List/Empty.golden b/internal/app/siftool/testdata/TestApp_List/Empty.golden new file mode 100644 index 00000000..6ec5e3b9 --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_List/Empty.golden @@ -0,0 +1,7 @@ +Container id: 3fa802cc-358b-45e3-bcc0-69dc7a45f9f8 +Created on: 2020-05-22 19:30:59 +0000 UTC +Modified on: 2020-05-22 19:30:59 +0000 UTC +---------------------------------------------------- +Descriptor list: +ID |GROUP |LINK |SIF POSITION (start-end) |TYPE +------------------------------------------------------------------------------ diff --git a/internal/app/siftool/testdata/TestApp_List/OneGroup.golden b/internal/app/siftool/testdata/TestApp_List/OneGroup.golden new file mode 100644 index 00000000..ef78088e --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_List/OneGroup.golden @@ -0,0 +1,9 @@ +Container id: 6ecc76b7-a497-4f7f-9ebd-8da2a04c6be1 +Created on: 2020-05-22 19:30:59 +0000 UTC +Modified on: 2020-05-22 19:30:59 +0000 UTC +---------------------------------------------------- +Descriptor list: +ID |GROUP |LINK |SIF POSITION (start-end) |TYPE +------------------------------------------------------------------------------ +1 |1 |NONE |32768-32772 |FS (Raw/System/386) +2 |1 |NONE |36864-36868 |FS (Squashfs/*System/386) diff --git a/internal/app/siftool/testdata/TestApp_List/OneGroupSigned.golden b/internal/app/siftool/testdata/TestApp_List/OneGroupSigned.golden new file mode 100644 index 00000000..c61e4e86 --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_List/OneGroupSigned.golden @@ -0,0 +1,10 @@ +Container id: 73e1c5c3-5c41-41ed-ad7c-2504d669f140 +Created on: 2020-06-30 00:01:56 +0000 UTC +Modified on: 2020-06-30 00:01:56 +0000 UTC +---------------------------------------------------- +Descriptor list: +ID |GROUP |LINK |SIF POSITION (start-end) |TYPE +------------------------------------------------------------------------------ +1 |1 |NONE |32768-32772 |FS (Raw/System/386) +2 |1 |NONE |36864-36868 |FS (Squashfs/*System/386) +3 |NONE |1 (G) |40960-41981 |Signature (SHA256) diff --git a/internal/app/siftool/testdata/TestApp_List/OneGroupSignedLegacy.golden b/internal/app/siftool/testdata/TestApp_List/OneGroupSignedLegacy.golden new file mode 100644 index 00000000..36015e5a --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_List/OneGroupSignedLegacy.golden @@ -0,0 +1,10 @@ +Container id: 6ecc76b7-a497-4f7f-9ebd-8da2a04c6be1 +Created on: 2020-05-22 19:30:59 +0000 UTC +Modified on: 2020-06-20 20:16:39 +0000 UTC +---------------------------------------------------- +Descriptor list: +ID |GROUP |LINK |SIF POSITION (start-end) |TYPE +------------------------------------------------------------------------------ +1 |1 |NONE |32768-32772 |FS (Raw/System/386) +2 |1 |NONE |36864-36868 |FS (Squashfs/*System/386) +3 |1 |2 |40960-41569 |Signature (SHA384) diff --git a/internal/app/siftool/testdata/TestApp_List/OneGroupSignedLegacyAll.golden b/internal/app/siftool/testdata/TestApp_List/OneGroupSignedLegacyAll.golden new file mode 100644 index 00000000..716ab967 --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_List/OneGroupSignedLegacyAll.golden @@ -0,0 +1,11 @@ +Container id: 6ecc76b7-a497-4f7f-9ebd-8da2a04c6be1 +Created on: 2020-05-22 19:30:59 +0000 UTC +Modified on: 2020-06-20 20:17:15 +0000 UTC +---------------------------------------------------- +Descriptor list: +ID |GROUP |LINK |SIF POSITION (start-end) |TYPE +------------------------------------------------------------------------------ +1 |1 |NONE |32768-32772 |FS (Raw/System/386) +2 |1 |NONE |36864-36868 |FS (Squashfs/*System/386) +3 |1 |1 |40960-41569 |Signature (SHA384) +4 |1 |2 |45056-45665 |Signature (SHA384) diff --git a/internal/app/siftool/testdata/TestApp_List/OneGroupSignedLegacyGroup.golden b/internal/app/siftool/testdata/TestApp_List/OneGroupSignedLegacyGroup.golden new file mode 100644 index 00000000..ef9b1f80 --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_List/OneGroupSignedLegacyGroup.golden @@ -0,0 +1,10 @@ +Container id: 6ecc76b7-a497-4f7f-9ebd-8da2a04c6be1 +Created on: 2020-05-22 19:30:59 +0000 UTC +Modified on: 2020-06-20 20:16:55 +0000 UTC +---------------------------------------------------- +Descriptor list: +ID |GROUP |LINK |SIF POSITION (start-end) |TYPE +------------------------------------------------------------------------------ +1 |1 |NONE |32768-32772 |FS (Raw/System/386) +2 |1 |NONE |36864-36868 |FS (Squashfs/*System/386) +3 |NONE |1 (G) |40960-41569 |Signature (SHA384) diff --git a/internal/app/siftool/testdata/TestApp_List/TwoGroups.golden b/internal/app/siftool/testdata/TestApp_List/TwoGroups.golden new file mode 100644 index 00000000..92809f4d --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_List/TwoGroups.golden @@ -0,0 +1,10 @@ +Container id: 0b19ec2c-0b08-46c9-95ae-fa88cd9e48a1 +Created on: 2020-05-22 19:30:59 +0000 UTC +Modified on: 2020-05-22 19:30:59 +0000 UTC +---------------------------------------------------- +Descriptor list: +ID |GROUP |LINK |SIF POSITION (start-end) |TYPE +------------------------------------------------------------------------------ +1 |1 |NONE |32768-32772 |FS (Raw/System/386) +2 |1 |NONE |36864-36868 |FS (Squashfs/*System/386) +3 |2 |NONE |40960-40964 |FS (Ext3/System/amd64) diff --git a/internal/app/siftool/testdata/TestApp_List/TwoGroupsSigned.golden b/internal/app/siftool/testdata/TestApp_List/TwoGroupsSigned.golden new file mode 100644 index 00000000..8faa8697 --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_List/TwoGroupsSigned.golden @@ -0,0 +1,12 @@ +Container id: 610cf3a3-18b0-4622-8b08-772d3510d7b5 +Created on: 2020-06-30 00:01:56 +0000 UTC +Modified on: 2020-06-30 00:01:56 +0000 UTC +---------------------------------------------------- +Descriptor list: +ID |GROUP |LINK |SIF POSITION (start-end) |TYPE +------------------------------------------------------------------------------ +1 |1 |NONE |32768-32772 |FS (Raw/System/386) +2 |1 |NONE |36864-36868 |FS (Squashfs/*System/386) +3 |2 |NONE |40960-40964 |FS (Ext3/System/amd64) +4 |NONE |1 (G) |45056-46077 |Signature (SHA256) +5 |NONE |2 (G) |49152-49974 |Signature (SHA256) diff --git a/internal/app/siftool/testdata/TestApp_List/TwoGroupsSignedLegacy.golden b/internal/app/siftool/testdata/TestApp_List/TwoGroupsSignedLegacy.golden new file mode 100644 index 00000000..3a613fc9 --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_List/TwoGroupsSignedLegacy.golden @@ -0,0 +1,11 @@ +Container id: 0b19ec2c-0b08-46c9-95ae-fa88cd9e48a1 +Created on: 2020-05-22 19:30:59 +0000 UTC +Modified on: 2020-06-20 20:16:44 +0000 UTC +---------------------------------------------------- +Descriptor list: +ID |GROUP |LINK |SIF POSITION (start-end) |TYPE +------------------------------------------------------------------------------ +1 |1 |NONE |32768-32772 |FS (Raw/System/386) +2 |1 |NONE |36864-36868 |FS (Squashfs/*System/386) +3 |2 |NONE |40960-40964 |FS (Ext3/System/amd64) +4 |1 |2 |45056-45665 |Signature (SHA384) diff --git a/internal/app/siftool/testdata/TestApp_List/TwoGroupsSignedLegacyAll.golden b/internal/app/siftool/testdata/TestApp_List/TwoGroupsSignedLegacyAll.golden new file mode 100644 index 00000000..26f55016 --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_List/TwoGroupsSignedLegacyAll.golden @@ -0,0 +1,12 @@ +Container id: 0b19ec2c-0b08-46c9-95ae-fa88cd9e48a1 +Created on: 2020-05-22 19:30:59 +0000 UTC +Modified on: 2020-06-20 20:17:19 +0000 UTC +---------------------------------------------------- +Descriptor list: +ID |GROUP |LINK |SIF POSITION (start-end) |TYPE +------------------------------------------------------------------------------ +1 |1 |NONE |32768-32772 |FS (Raw/System/386) +2 |1 |NONE |36864-36868 |FS (Squashfs/*System/386) +3 |2 |NONE |40960-40964 |FS (Ext3/System/amd64) +4 |1 |1 |45056-45665 |Signature (SHA384) +5 |1 |2 |49152-49761 |Signature (SHA384) diff --git a/internal/app/siftool/testdata/TestApp_List/TwoGroupsSignedLegacyGroup.golden b/internal/app/siftool/testdata/TestApp_List/TwoGroupsSignedLegacyGroup.golden new file mode 100644 index 00000000..d2f3795b --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_List/TwoGroupsSignedLegacyGroup.golden @@ -0,0 +1,11 @@ +Container id: 0b19ec2c-0b08-46c9-95ae-fa88cd9e48a1 +Created on: 2020-05-22 19:30:59 +0000 UTC +Modified on: 2020-06-20 20:16:58 +0000 UTC +---------------------------------------------------- +Descriptor list: +ID |GROUP |LINK |SIF POSITION (start-end) |TYPE +------------------------------------------------------------------------------ +1 |1 |NONE |32768-32772 |FS (Raw/System/386) +2 |1 |NONE |36864-36868 |FS (Squashfs/*System/386) +3 |2 |NONE |40960-40964 |FS (Ext3/System/amd64) +4 |NONE |1 (G) |45056-45665 |Signature (SHA384) From 3bc1d3b8721faa748f2d1666b983935156028c4c Mon Sep 17 00:00:00 2001 From: Adam Hughes Date: Wed, 23 Jun 2021 21:34:54 +0000 Subject: [PATCH 3/8] test: add unit test for Info --- internal/app/siftool/info_test.go | 42 +++++++++++++++++++ .../siftool/testdata/TestApp_Info/One.golden | 16 +++++++ .../testdata/TestApp_Info/Three.golden | 15 +++++++ .../siftool/testdata/TestApp_Info/Two.golden | 16 +++++++ 4 files changed, 89 insertions(+) create mode 100644 internal/app/siftool/testdata/TestApp_Info/One.golden create mode 100644 internal/app/siftool/testdata/TestApp_Info/Three.golden create mode 100644 internal/app/siftool/testdata/TestApp_Info/Two.golden diff --git a/internal/app/siftool/info_test.go b/internal/app/siftool/info_test.go index 5ad0fb92..e4c7db7e 100644 --- a/internal/app/siftool/info_test.go +++ b/internal/app/siftool/info_test.go @@ -157,3 +157,45 @@ func TestApp_List(t *testing.T) { }) } } + +func TestApp_Info(t *testing.T) { + tests := []struct { + name string + path string + id uint32 + wantErr error + }{ + { + name: "One", + path: filepath.Join(corpus, "one-group-signed.sif"), + id: 1, + }, + { + name: "Two", + path: filepath.Join(corpus, "one-group-signed.sif"), + id: 2, + }, + { + name: "Three", + path: filepath.Join(corpus, "one-group-signed.sif"), + id: 3, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var b bytes.Buffer + + a, err := New(OptAppOutput(&b)) + if err != nil { + t.Fatalf("failed to create app: %v", err) + } + + if got, want := a.Info(tt.path, tt.id), tt.wantErr; !errors.Is(got, want) { + t.Fatalf("got error %v, want %v", got, want) + } + + g := goldie.New(t, goldie.WithTestNameForDir(true)) + g.Assert(t, tt.name, b.Bytes()) + }) + } +} diff --git a/internal/app/siftool/testdata/TestApp_Info/One.golden b/internal/app/siftool/testdata/TestApp_Info/One.golden new file mode 100644 index 00000000..e16d9ec9 --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_Info/One.golden @@ -0,0 +1,16 @@ +Descr slot#: 0 + Datatype: FS + ID: 1 + Used: true + Groupid: 1 + Link: NONE + Fileoff: 32768 + Filelen: 4 + Ctime: 2020-06-30 00:01:56 +0000 UTC + Mtime: 2020-06-30 00:01:56 +0000 UTC + UID: 501 + Gid: 20 + Name: . + Fstype: Raw + Parttype: System + Arch: 386 diff --git a/internal/app/siftool/testdata/TestApp_Info/Three.golden b/internal/app/siftool/testdata/TestApp_Info/Three.golden new file mode 100644 index 00000000..c5fbbf50 --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_Info/Three.golden @@ -0,0 +1,15 @@ +Descr slot#: 2 + Datatype: Signature + ID: 3 + Used: true + Groupid: NONE + Link: 1 (G) + Fileoff: 40960 + Filelen: 1021 + Ctime: 2020-06-30 00:01:56 +0000 UTC + Mtime: 2020-06-30 00:01:56 +0000 UTC + UID: 501 + Gid: 20 + Name: . + Hashtype: SHA256 + Entity: 12045C8C0B1004D058DE4BEDA20C27EE7FF7BA84 diff --git a/internal/app/siftool/testdata/TestApp_Info/Two.golden b/internal/app/siftool/testdata/TestApp_Info/Two.golden new file mode 100644 index 00000000..1224f658 --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_Info/Two.golden @@ -0,0 +1,16 @@ +Descr slot#: 1 + Datatype: FS + ID: 2 + Used: true + Groupid: 1 + Link: NONE + Fileoff: 36864 + Filelen: 4 + Ctime: 2020-06-30 00:01:56 +0000 UTC + Mtime: 2020-06-30 00:01:56 +0000 UTC + UID: 501 + Gid: 20 + Name: . + Fstype: Squashfs + Parttype: *System + Arch: 386 From 13f589baeef10d395f26cc3fdb055871b4f640fb Mon Sep 17 00:00:00 2001 From: Adam Hughes Date: Wed, 23 Jun 2021 21:36:12 +0000 Subject: [PATCH 4/8] test: add unit test for Dump --- internal/app/siftool/info_test.go | 44 +++++++++++++++++++ .../siftool/testdata/TestApp_Dump/One.golden | 1 + .../testdata/TestApp_Dump/Three.golden | 14 ++++++ .../siftool/testdata/TestApp_Dump/Two.golden | 1 + 4 files changed, 60 insertions(+) create mode 100644 internal/app/siftool/testdata/TestApp_Dump/One.golden create mode 100644 internal/app/siftool/testdata/TestApp_Dump/Three.golden create mode 100644 internal/app/siftool/testdata/TestApp_Dump/Two.golden diff --git a/internal/app/siftool/info_test.go b/internal/app/siftool/info_test.go index e4c7db7e..2b54220a 100644 --- a/internal/app/siftool/info_test.go +++ b/internal/app/siftool/info_test.go @@ -158,6 +158,7 @@ func TestApp_List(t *testing.T) { } } +//nolint:dupl func TestApp_Info(t *testing.T) { tests := []struct { name string @@ -199,3 +200,46 @@ func TestApp_Info(t *testing.T) { }) } } + +//nolint:dupl +func TestApp_Dump(t *testing.T) { + tests := []struct { + name string + path string + id uint32 + wantErr error + }{ + { + name: "One", + path: filepath.Join(corpus, "one-group-signed.sif"), + id: 1, + }, + { + name: "Two", + path: filepath.Join(corpus, "one-group-signed.sif"), + id: 2, + }, + { + name: "Three", + path: filepath.Join(corpus, "one-group-signed.sif"), + id: 3, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var b bytes.Buffer + + a, err := New(OptAppOutput(&b)) + if err != nil { + t.Fatalf("failed to create app: %v", err) + } + + if got, want := a.Dump(tt.path, tt.id), tt.wantErr; !errors.Is(got, want) { + t.Fatalf("got error %v, want %v", got, want) + } + + g := goldie.New(t, goldie.WithTestNameForDir(true)) + g.Assert(t, tt.name, b.Bytes()) + }) + } +} diff --git a/internal/app/siftool/testdata/TestApp_Dump/One.golden b/internal/app/siftool/testdata/TestApp_Dump/One.golden new file mode 100644 index 00000000..1d386480 --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_Dump/One.golden @@ -0,0 +1 @@ +úÎþí \ No newline at end of file diff --git a/internal/app/siftool/testdata/TestApp_Dump/Three.golden b/internal/app/siftool/testdata/TestApp_Dump/Three.golden new file mode 100644 index 00000000..16df6199 --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_Dump/Three.golden @@ -0,0 +1,14 @@ +-----BEGIN PGP SIGNED MESSAGE----- +Hash: SHA256 + +{"version":1,"header":{"digest":"sha256:2086c56f4fc4b733d256d56b780a16c3c906578ab4a90158306de17798bc3f82"},"objects":[{"relativeId":0,"descriptorDigest":"sha256:72e10abae30a69c4eccae514d3d2217d1a03820767c50273b224b0ed69c3cf89","objectDigest":"sha256:004dfc8da678c309de28b5386a1e9efd57f536b150c40d29b31506aa0fb17ec2"},{"relativeId":1,"descriptorDigest":"sha256:9a2fa8bc044f51d56ee818101828d1f5f9fdd5ed012cad8231aca8551d886d07","objectDigest":"sha256:5f78c33274e43fa9de5659265c1d917e25c03722dcb0b8d27db8d5feaa813953"}]} +-----BEGIN PGP SIGNATURE----- + +wsBcBAEBCAAQBQJe+oD0CRCiDCfuf/e6hAAAifcIAH0bGbjQMNKQOqkRlvKUEGt/ +ogs1iMAGAvxbfLCfrhxKLdpBaYCHhEnSamtlt40oOJW5GYvnZRK5pG1ULbngDBd3 +I9UFQgEzj6rz5apwEMg9KZbBEZyTADxBCMleQ7AXzqTkhyyrKKcz2uuJi674YNEV +t8fce1QDzdmU112/nObBpS/3hOK16VxkwFZHfhfnLYbz5nm41a6Cc3ZxDxU5ULEW +HgbkPdZ8fz1g/68c/qRXn5td0kVW5ZnmJyDFZwXt5h+/93YNewGIyF2JUwngGe87 +l9jOl7oNSugTwei3tTJmh95zl2zRrQN37CttXP0njTgkm5RKWfPzW6o9FSYg7B8= +=5Vi5 +-----END PGP SIGNATURE----- \ No newline at end of file diff --git a/internal/app/siftool/testdata/TestApp_Dump/Two.golden b/internal/app/siftool/testdata/TestApp_Dump/Two.golden new file mode 100644 index 00000000..7d174b13 --- /dev/null +++ b/internal/app/siftool/testdata/TestApp_Dump/Two.golden @@ -0,0 +1 @@ +Þ­¾ï \ No newline at end of file From e664b46504c908aee594121ed4e3903a1a2a5d95 Mon Sep 17 00:00:00 2001 From: Adam Hughes Date: Wed, 23 Jun 2021 21:51:54 +0000 Subject: [PATCH 5/8] test: add unit test for New --- internal/app/siftool/modif_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 internal/app/siftool/modif_test.go diff --git a/internal/app/siftool/modif_test.go b/internal/app/siftool/modif_test.go new file mode 100644 index 00000000..1694959d --- /dev/null +++ b/internal/app/siftool/modif_test.go @@ -0,0 +1,29 @@ +// Copyright (c) 2021, Sylabs Inc. All rights reserved. +// This software is licensed under a 3-clause BSD license. Please consult the +// LICENSE file distributed with the sources of this project regarding your +// rights to use or distribute this software. + +package siftool + +import ( + "os" + "testing" +) + +func TestApp_New(t *testing.T) { + a, err := New() + if err != nil { + t.Fatalf("failed to create app: %v", err) + } + + tf, err := os.CreateTemp("", "sif-test-*") + if err != nil { + t.Fatal(err) + } + defer os.Remove(tf.Name()) + tf.Close() + + if err := a.New(tf.Name()); err != nil { + t.Fatal(err) + } +} From ab8115146fb2596507b66b53674b2468fe05f071 Mon Sep 17 00:00:00 2001 From: Adam Hughes Date: Wed, 23 Jun 2021 22:08:36 +0000 Subject: [PATCH 6/8] test: add unit test for Add --- internal/app/siftool/modif_test.go | 55 ++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/internal/app/siftool/modif_test.go b/internal/app/siftool/modif_test.go index 1694959d..eba70cee 100644 --- a/internal/app/siftool/modif_test.go +++ b/internal/app/siftool/modif_test.go @@ -6,8 +6,12 @@ package siftool import ( + "bytes" + "errors" "os" "testing" + + "github.com/sylabs/sif/v2/pkg/sif" ) func TestApp_New(t *testing.T) { @@ -27,3 +31,54 @@ func TestApp_New(t *testing.T) { t.Fatal(err) } } + +func TestApp_Add(t *testing.T) { + a, err := New() + if err != nil { + t.Fatalf("failed to create app: %v", err) + } + + tests := []struct { + name string + opts AddOptions + wantErr error + }{ + { + name: "DataPartition", + opts: AddOptions{ + Datatype: sif.DataPartition, + Parttype: sif.PartPrimSys, + Partfs: sif.FsSquash, + Partarch: sif.HdrArch386, + Fp: bytes.NewBuffer([]byte{0xde, 0xad, 0xbe, 0xef}), + }, + }, + { + name: "DataSignature", + opts: AddOptions{ + Datatype: sif.DataSignature, + Signhash: sif.HashSHA256, + Signentity: "12045C8C0B1004D058DE4BEDA20C27EE7FF7BA84", + Fp: bytes.NewBuffer([]byte{0xde, 0xad, 0xbe, 0xef}), + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tf, err := os.CreateTemp("", "sif-test-*") + if err != nil { + t.Fatal(err) + } + defer os.Remove(tf.Name()) + tf.Close() + + if err := a.New(tf.Name()); err != nil { + t.Fatal(err) + } + + if got, want := a.Add(tf.Name(), tt.opts), tt.wantErr; !errors.Is(got, want) { + t.Fatalf("got error %v, want %v", got, want) + } + }) + } +} From 5c69b61ed289f5df27970c45851a9d44f67e4814 Mon Sep 17 00:00:00 2001 From: Adam Hughes Date: Wed, 23 Jun 2021 22:11:58 +0000 Subject: [PATCH 7/8] test: add unit test for Del --- internal/app/siftool/modif_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/internal/app/siftool/modif_test.go b/internal/app/siftool/modif_test.go index eba70cee..8f2a250f 100644 --- a/internal/app/siftool/modif_test.go +++ b/internal/app/siftool/modif_test.go @@ -82,3 +82,33 @@ func TestApp_Add(t *testing.T) { }) } } + +func TestApp_Del(t *testing.T) { + a, err := New() + if err != nil { + t.Fatalf("failed to create app: %v", err) + } + + tf, err := os.CreateTemp("", "sif-test-*") + if err != nil { + t.Fatal(err) + } + defer os.Remove(tf.Name()) + tf.Close() + + if err := a.New(tf.Name()); err != nil { + t.Fatal(err) + } + + opts := AddOptions{ + Datatype: sif.DataGeneric, + Fp: bytes.NewBuffer([]byte{0xde, 0xad, 0xbe, 0xef}), + } + if err := a.Add(tf.Name(), opts); err != nil { + t.Fatal(err) + } + + if err := a.Del(tf.Name(), 1); err != nil { + t.Fatal(err) + } +} From 716cc9f512877ee8f6c15c112e09132d0d8bd625 Mon Sep 17 00:00:00 2001 From: Adam Hughes Date: Wed, 23 Jun 2021 22:14:23 +0000 Subject: [PATCH 8/8] test: add unit test for Setprim --- internal/app/siftool/modif_test.go | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/internal/app/siftool/modif_test.go b/internal/app/siftool/modif_test.go index 8f2a250f..e5414b50 100644 --- a/internal/app/siftool/modif_test.go +++ b/internal/app/siftool/modif_test.go @@ -112,3 +112,36 @@ func TestApp_Del(t *testing.T) { t.Fatal(err) } } + +func TestApp_Setprim(t *testing.T) { + a, err := New() + if err != nil { + t.Fatalf("failed to create app: %v", err) + } + + tf, err := os.CreateTemp("", "sif-test-*") + if err != nil { + t.Fatal(err) + } + defer os.Remove(tf.Name()) + tf.Close() + + if err := a.New(tf.Name()); err != nil { + t.Fatal(err) + } + + opts := AddOptions{ + Datatype: sif.DataPartition, + Parttype: sif.PartSystem, + Partfs: sif.FsSquash, + Partarch: sif.HdrArch386, + Fp: bytes.NewBuffer([]byte{0xde, 0xad, 0xbe, 0xef}), + } + if err := a.Add(tf.Name(), opts); err != nil { + t.Fatal(err) + } + + if err := a.Setprim(tf.Name(), 1); err != nil { + t.Fatal(err) + } +}