Skip to content

Commit

Permalink
wip: organizing
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed Mar 8, 2022
1 parent e3df8ed commit 0185d3a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
36 changes: 19 additions & 17 deletions cmd/melt/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"os"
"path/filepath"
"strings"
Expand All @@ -14,36 +13,27 @@ import (

func TestBackupRestoreKnownKey(t *testing.T) {
const expectedMnemonic = `
model tone century code pilot
ball polar sauce machine crisp
plate soccer salon awake monkey
own install all broccoli marine
print smart square impact
alter gap broom kitten orient over settle work honey rule
coach system wage effort mask void solid devote divert
quarter quote broccoli jaguar lady
`
const expectedSum = "4ec2b1e65bb86ef635991c3e31341c3bdaf6862e9b1efcde0a9c0307081ffc4c"
const expectedSum = "ba34175ef608633b29f046b40cce596dd221347b77abba40763eef2e7ae51fe9"

t.Run("backup", func(t *testing.T) {
is := is.New(t)
mnemonic, err := backup("testdata/test_ed25519")
mnemonic, err := backup("testdata/id_ed25519")
is.NoErr(err)
is.Equal(mnemonic, strings.Join(strings.Fields(expectedMnemonic), " "))
})

t.Run("restore", func(t *testing.T) {
is := is.New(t)
path := filepath.Join(t.TempDir(), "key")
is.NoErr(restore(path, expectedMnemonic))
is.NoErr(restore(expectedMnemonic, path))
is.Equal(expectedSum, sha256sum(t, path+".pub"))
})
}

func sha256sum(bts []byte) (string, error) {
digest := sha256.New()
if _, err := digest.Write(bts); err != nil {
return "", fmt.Errorf("failed to sha256sum key: %w", err)
}
return hex.EncodeToString(digest.Sum(nil)), nil
}

func TestMaybeFile(t *testing.T) {
t.Run("is a file", func(t *testing.T) {
is := is.New(t)
Expand All @@ -58,3 +48,15 @@ func TestMaybeFile(t *testing.T) {
is.Equal("strings", maybeFile("strings"))
})
}

func sha256sum(tb testing.TB, path string) string {
tb.Helper()
is := is.New(tb)
bts, err := os.ReadFile(path)
is.NoErr(err)
tb.Log(string(bts))
digest := sha256.New()
_, err = digest.Write(bts)
is.NoErr(err)
return hex.EncodeToString(digest.Sum(nil))
}
7 changes: 7 additions & 0 deletions cmd/melt/testdata/id_ed25519
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACDJQSoksziSwoefzN4O7jzRv6vJpyOW9CnGXgkUjVajHAAAAIgvWUstL1lL
LQAAAAtzc2gtZWQyNTUxOQAAACDJQSoksziSwoefzN4O7jzRv6vJpyOW9CnGXgkUjVajHA
AAAEAHa+hzPanJO/Ef7G03pLLub2KNYh+szqeY/1e7ADjdy8lBKiSzOJLCh5/M3g7uPNG/
q8mnI5b0KcZeCRSNVqMcAAAAAAECAwQF
-----END OPENSSH PRIVATE KEY-----
1 change: 1 addition & 0 deletions cmd/melt/testdata/id_ed25519.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMlBKiSzOJLCh5/M3g7uPNG/q8mnI5b0KcZeCRSNVqMc
7 changes: 0 additions & 7 deletions cmd/melt/testdata/test_ed25519

This file was deleted.

0 comments on commit 0185d3a

Please sign in to comment.