Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit Tests for Internal siftool Package #41

Merged
merged 8 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
245 changes: 245 additions & 0 deletions internal/app/siftool/info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
// 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")

//nolint:dupl
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())
})
}
}

//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())
})
}
}

//nolint:dupl
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())
})
}
}

//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())
})
}
}
147 changes: 147 additions & 0 deletions internal/app/siftool/modif_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// 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"
"os"
"testing"

"github.com/sylabs/sif/v2/pkg/sif"
)

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)
}
}

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)
}
})
}
}

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)
}
}

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)
}
}
1 change: 1 addition & 0 deletions internal/app/siftool/testdata/TestApp_Dump/One.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
����
Loading