distrobuilder/shared/util_test.go
Thomas Hipp 4b91f650a4
*: add keys and keyserver to source
Signed-off-by: Thomas Hipp <thomas.hipp@canonical.com>
2018-02-16 17:17:25 +01:00

91 lines
1.7 KiB
Go

package shared
import (
"fmt"
"log"
"os"
"path/filepath"
"testing"
)
func TestVerifyFile(t *testing.T) {
wd, err := os.Getwd()
if err != nil {
fmt.Errorf("Failed to retrieve working directory: %s", err)
}
testdataDir := filepath.Join(wd, "..", "testdata")
keys := []string{"0x5DE8949A899C8D99"}
keyserver := "keys.gnupg.net"
tests := []struct {
name string
signedFile string
signatureFile string
keys []string
keyserver string
shouldFail bool
}{
{
"testfile with detached signature",
filepath.Join(testdataDir, "testfile"),
filepath.Join(testdataDir, "testfile.sig"),
keys,
keyserver,
false,
},
{
"testfile with cleartext signature",
filepath.Join(testdataDir, "testfile.asc"),
"",
keys,
keyserver,
false,
},
{
"testfile with invalid cleartext signature",
filepath.Join(testdataDir, "testfile-invalid.asc"),
"",
keys,
keyserver,
true,
},
{
"testfile with normal signature",
filepath.Join(testdataDir, "testfile.gpg"),
"",
keys,
keyserver,
false,
},
{
"no keys",
filepath.Join(testdataDir, "testfile"),
filepath.Join(testdataDir, "testfile.sig"),
[]string{},
keyserver,
true,
},
{
"invalid key",
filepath.Join(testdataDir, "testfile.asc"),
"",
[]string{"0x46181433FBB75451"},
keyserver,
true,
},
}
for i, tt := range tests {
log.Printf("Running test #%d: %s", i, tt.name)
valid, err := VerifyFile(tt.signedFile, tt.signatureFile, tt.keys,
tt.keyserver)
if !tt.shouldFail && !valid {
t.Fatalf("Failed to verify: %s\n%s", tt.name, err)
}
if tt.shouldFail && valid {
t.Fatalf("Expected to fail: %s", tt.name)
}
}
}