Skip to content

Commit

Permalink
adjust private key tests
Browse files Browse the repository at this point in the history
This commit adjusts and simplifies the private key tests slightly.

Signed-off-by: Andreas Auernhammer <[email protected]>
  • Loading branch information
aead committed May 19, 2024
1 parent 54b1462 commit 989464f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
1 change: 0 additions & 1 deletion cmd/minisign/minisign.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,6 @@ func isTerm(fd *os.File) bool { return term.IsTerminal(int(fd.Fd())) }
// exit formats and prints its args to stderr before exiting
// the program.
func exit(args ...any) {
fmt.Println()
fmt.Fprintln(os.Stderr, args...)
os.Exit(1)
}
Expand Down
File renamed without changes.
File renamed without changes.
44 changes: 27 additions & 17 deletions private_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"bytes"
"encoding/base64"
"os"
"strconv"
"testing"
)

Expand All @@ -18,13 +17,13 @@ var marshalPrivateKeyTests = []struct {
Bytes []byte
}{
{
File: "./internal/testdata/unencrypted-0.key",
ID: htoi("3728470A8118E56E"),
File: "./internal/testdata/minisign-nopassword-0.key",
ID: 0x3728470A8118E56E,
Bytes: b64("JpjEI/XIKqIVl99tT611AxXwlVjlw2afJC8Nv6o7uuipyNvC3DmgO2csDT+bw1bZR3ss4rd5cXqoq0uftlCJqw=="),
},
{
File: "./internal/testdata/unencrypted-1.key",
ID: htoi("D7E531EE76B2FC6F"),
File: "./internal/testdata/minisign-nopassword-1.key",
ID: 0xD7E531EE76B2FC6F,
Bytes: b64("L24Gi2UbWOb/MBb4MzJLysgC1F1FnE/m72qhb7r5FMlHzHe6M6mCLPMzmj6ln+hI51kqpDqTkIg9VCaToAhZtA=="),
},
}
Expand Down Expand Up @@ -53,14 +52,26 @@ func TestPrivateKey_Marshal(t *testing.T) {
}
}

var unmarshalPrivateKeyTests = []string{
"./internal/testdata/unencrypted-0.key",
"./internal/testdata/unencrypted-1.key",
var unmarshalPrivateKeyTests = []struct {
File string
ID uint64
Bytes []byte
}{
{
File: "./internal/testdata/minisign-nopassword-0.key",
ID: 0x3728470A8118E56E,
Bytes: b64("JpjEI/XIKqIVl99tT611AxXwlVjlw2afJC8Nv6o7uuipyNvC3DmgO2csDT+bw1bZR3ss4rd5cXqoq0uftlCJqw=="),
},
{
File: "./internal/testdata/minisign-nopassword-1.key",
ID: 0xD7E531EE76B2FC6F,
Bytes: b64("L24Gi2UbWOb/MBb4MzJLysgC1F1FnE/m72qhb7r5FMlHzHe6M6mCLPMzmj6ln+hI51kqpDqTkIg9VCaToAhZtA=="),
},
}

func TestPrivateKey_Unmarshal(t *testing.T) {
for i, file := range unmarshalPrivateKeyTests {
raw, err := os.ReadFile(file)
for i, test := range unmarshalPrivateKeyTests {
raw, err := os.ReadFile(test.File)
if err != nil {
t.Fatalf("Test %d: failed to read private key: %v", i, err)
}
Expand All @@ -72,15 +83,14 @@ func TestPrivateKey_Unmarshal(t *testing.T) {

// Print test vector for marshaling:
// t.Logf("\n{\n\tID: htoi(\"%X\"),\n\tBytes: b64(\"%s\"),\n}", key.id, base64.StdEncoding.EncodeToString(key.bytes[:]))
}
}

func htoi(s string) uint64 {
i, err := strconv.ParseUint(s, 16, 64)
if err != nil {
panic(err)
if key.ID() != test.ID {
t.Fatalf("Test %d: ID mismatch: got '%x' - want '%x'", i, key.ID(), test.ID)
}
if !bytes.Equal(key.bytes[:], test.Bytes) {
t.Fatalf("Test %d: private key mismatch: got '%x' - want '%x'", i, key.bytes, test.Bytes)
}
}
return i
}

func b64(s string) []byte {
Expand Down

0 comments on commit 989464f

Please sign in to comment.