File: public_test.go

package info (click to toggle)
golang-github-aead-minisign 0.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 236 kB
  • sloc: makefile: 45
file content (94 lines) | stat: -rw-r--r-- 3,103 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) 2021 Andreas Auernhammer. All rights reserved.
// Use of this source code is governed by a license that can be
// found in the LICENSE file.

package minisign

import "testing"

var marshalPublicKeyTests = []struct {
	PublicKey PublicKey
	Text      string
}{
	{
		PublicKey: PublicKey{
			id:    0xe7620f1842b4e81f,
			bytes: [32]byte{121, 165, 97, 231, 14, 224, 140, 211, 231, 84, 198, 62, 155, 214, 185, 195, 82, 10, 29, 66, 4, 205, 16, 77, 162, 231, 239, 118, 59, 24, 83, 183},
		},
		Text: "untrusted comment: minisign public key: E7620F1842B4E81F" + "\n" + "RWQf6LRCGA9i53mlYecO4IzT51TGPpvWucNSCh1CBM0QTaLn73Y7GFO3",
	},
	{
		PublicKey: PublicKey{
			id:    0x6f7add142cdc7edb,
			bytes: [32]byte{121, 165, 97, 231, 14, 224, 140, 211, 231, 84, 198, 62, 155, 214, 185, 195, 82, 10, 29, 66, 4, 205, 16, 77, 162, 231, 239, 118, 59, 24, 83, 183},
		},
		Text: "untrusted comment: minisign public key: 6F7ADD142CDC7EDB" + "\n" + "RWTbftwsFN16b3mlYecO4IzT51TGPpvWucNSCh1CBM0QTaLn73Y7GFO3",
	},
}

func TestMarshalPublicKey(t *testing.T) {
	for i, test := range marshalPublicKeyTests {
		text, err := test.PublicKey.MarshalText()
		if err != nil {
			t.Fatalf("Test %d: failed to marshal public key: %v", i, err)
		}
		if string(text) != test.Text {
			t.Fatalf("Test %d: got '%s' - want '%s'", i, string(text), test.Text)
		}
	}
}

var unmarshalPublicKeyTests = []struct {
	Text       string
	PublicKey  PublicKey
	ShouldFail bool
}{
	{
		Text: "RWQf6LRCGA9i53mlYecO4IzT51TGPpvWucNSCh1CBM0QTaLn73Y7GFO3",
		PublicKey: PublicKey{
			id:    0xe7620f1842b4e81f,
			bytes: [32]byte{121, 165, 97, 231, 14, 224, 140, 211, 231, 84, 198, 62, 155, 214, 185, 195, 82, 10, 29, 66, 4, 205, 16, 77, 162, 231, 239, 118, 59, 24, 83, 183},
		},
	},
	{
		Text: "RWQf6LRCGA9i53mlYecO4IzT51TGPpvWucNSCh1CBM0QTaLn73Y7GFO3\r\n\n",
		PublicKey: PublicKey{
			id:    0xe7620f1842b4e81f,
			bytes: [32]byte{121, 165, 97, 231, 14, 224, 140, 211, 231, 84, 198, 62, 155, 214, 185, 195, 82, 10, 29, 66, 4, 205, 16, 77, 162, 231, 239, 118, 59, 24, 83, 183},
		},
	},
	{ // Invalid algorithm
		Text:       "RmQf6LRCGA9i53mlYecO4IzT51TGPpvWucNSCh1CBM0QTaLn73Y7GFO3",
		ShouldFail: true,
	},
	{ // Invalid public key b/c too long
		Text:       "RWQf6LRCGA9i53mlYecO4IzT51TGPpvWucNSCh1CBM0QTaLn73Y7GFO3bhQ=",
		ShouldFail: true,
	},
}

func TestUnmarshalPublicKey(t *testing.T) {
	for i, test := range unmarshalPublicKeyTests {
		var key PublicKey

		err := key.UnmarshalText([]byte(test.Text))
		if err == nil && test.ShouldFail {
			t.Fatalf("Test %d: should have failed but passed", i)
		}
		if err != nil && !test.ShouldFail {
			t.Fatalf("Test %d: failed to unmarshal public key: %v", i, err)
		}

		if err == nil {
			if key.ID() != test.PublicKey.ID() {
				t.Fatalf("Test %d: key ID mismatch: got '%x' - want '%x'", i, key.ID(), test.PublicKey.ID())
			}
			if key.bytes != test.PublicKey.bytes {
				t.Fatalf("Test %d: raw public key mismatch: got '%v' - want '%v'", i, key.bytes, test.PublicKey.bytes)
			}
			if !key.Equal(test.PublicKey) {
				t.Fatalf("Test %d: public keys are not equal", i)
			}
		}
	}
}