Skip to content

Commit

Permalink
tests: add armor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FiloSottile committed Jul 3, 2022
1 parent e84d742 commit 799c2bf
Show file tree
Hide file tree
Showing 60 changed files with 2,630 additions and 10 deletions.
37 changes: 36 additions & 1 deletion internal/testkit/testkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type TestFile struct {
comment string
identities []string
passphrases []string
armor bool
}

func NewTestFile() *TestFile {
Expand All @@ -87,7 +88,7 @@ func (f *TestFile) TextLine(s string) {

func (f *TestFile) UnreadLine() string {
buf := bytes.TrimSuffix(f.Buf.Bytes(), []byte("\n"))
idx := bytes.LastIndex(buf[:len(buf)-1], []byte("\n")) + 1
idx := bytes.LastIndex(buf, []byte("\n")) + 1
f.Buf.Reset()
f.Buf.Write(buf[:idx])
return string(buf[idx:])
Expand Down Expand Up @@ -122,6 +123,16 @@ func (f *TestFile) Body(body []byte) {
}
}

func (f *TestFile) Base64Padding() {
line := f.UnreadLine()
paddingLen := 4 - len(line)%4
if paddingLen == 4 {
paddingLen = 0
}
padding := strings.Repeat("=", paddingLen)
f.TextLine(line + padding)
}

func (f *TestFile) AEADBody(key, body []byte) {
aead, _ := chacha20poly1305.New(key)
f.Body(aead.Seal(nil, make([]byte, chacha20poly1305.NonceSize), body, nil))
Expand Down Expand Up @@ -231,6 +242,11 @@ func (f *TestFile) ExpectHeaderFailure() {
f.expect = "header failure"
}

func (f *TestFile) ExpectArmorFailure() {
f.armor = true
f.expect = "armor failure"
}

func (f *TestFile) ExpectPayloadFailure() {
f.expect = "payload failure"
f.payload.Reset()
Expand All @@ -255,6 +271,22 @@ func (f *TestFile) Comment(c string) {
f.comment = c
}

func (f *TestFile) BeginArmor(t string) {
f.armor = true
f.TextLine("-----BEGIN " + t + "-----")
}

func (f *TestFile) EndArmor(t string) {
f.armor = true
f.TextLine("-----END " + t + "-----")
}

func (f *TestFile) Bytes() []byte {
out := make([]byte, f.Buf.Len())
copy(out, f.Buf.Bytes())
return out
}

func (f *TestFile) Generate() {
fmt.Printf("expect: %s\n", f.expect)
if f.expect == "success" || f.expect == "payload failure" {
Expand All @@ -267,6 +299,9 @@ func (f *TestFile) Generate() {
for _, p := range f.passphrases {
fmt.Printf("passphrase: %s\n", p)
}
if f.armor {
fmt.Printf("armored: yes\n")
}
if f.comment != "" {
fmt.Printf("comment: %s\n", f.comment)
}
Expand Down
13 changes: 13 additions & 0 deletions testdata/testkit/armor
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
expect: success
payload: 013f54400c82da08037759ada907a8b864e97de81c088a182062c4b5622fd2ab
file key: 59454c4c4f57205355424d4152494e45
identity: AGE-SECRET-KEY-1XMWWC06LY3EE5RYTXM9MFLAZ2U56JJJ36S0MYPDRWSVLUL66MV4QX3S7F6
armored: yes

-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBURWlGMHlwcXIrYnB2Y3FY
TnlDVkpwTDdPdXdQZFZ3UEw3S1FFYkZET0NjCkVtRUNBRWNLTituL1ZzOVNiV2lW
K0h1MHIrRThSNzdEZFdZeWQ4M253N1UKLS0tIFZuKzU0anFpaVVDRStXWmNFVlkz
ZjFzcUhqbHUvejFMQ1EvVDdYbTdxSTAK7s9ix86RtDMnTmjU8vkTTLdMW/73vqpS
yPC8DpksHoMx+2Y=
-----END AGE ENCRYPTED FILE-----
14 changes: 14 additions & 0 deletions testdata/testkit/armor_crlf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
expect: success
payload: 013f54400c82da08037759ada907a8b864e97de81c088a182062c4b5622fd2ab
file key: 59454c4c4f57205355424d4152494e45
identity: AGE-SECRET-KEY-1XMWWC06LY3EE5RYTXM9MFLAZ2U56JJJ36S0MYPDRWSVLUL66MV4QX3S7F6
armored: yes
comment: CRLF is allowed as a end of line for armored files

-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBURWlGMHlwcXIrYnB2Y3FY
TnlDVkpwTDdPdXdQZFZ3UEw3S1FFYkZET0NjCkVtRUNBRWNLTituL1ZzOVNiV2lW
K0h1MHIrRThSNzdEZFdZeWQ4M253N1UKLS0tIFZuKzU0anFpaVVDRStXWmNFVlkz
ZjFzcUhqbHUvejFMQ1EvVDdYbTdxSTAK7s9ix86RtDMnTmjU8vkTTLdMW/73vqpS
yPC8DpksHoMx+2Y=
-----END AGE ENCRYPTED FILE-----
13 changes: 13 additions & 0 deletions testdata/testkit/armor_empty_line_begin
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
expect: armor failure
file key: 59454c4c4f57205355424d4152494e45
identity: AGE-SECRET-KEY-1XMWWC06LY3EE5RYTXM9MFLAZ2U56JJJ36S0MYPDRWSVLUL66MV4QX3S7F6
armored: yes

-----BEGIN AGE ENCRYPTED FILE-----

YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBURWlGMHlwcXIrYnB2Y3FY
TnlDVkpwTDdPdXdQZFZ3UEw3S1FFYkZET0NjCkVtRUNBRWNLTituL1ZzOVNiV2lW
K0h1MHIrRThSNzdEZFdZeWQ4M253N1UKLS0tIFZuKzU0anFpaVVDRStXWmNFVlkz
ZjFzcUhqbHUvejFMQ1EvVDdYbTdxSTAK7s9ix86RtDMnTmjU8vkTTLdMW/73vqpS
yPC8DpksHoMx+2Y=
-----END AGE ENCRYPTED FILE-----
13 changes: 13 additions & 0 deletions testdata/testkit/armor_empty_line_end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
expect: armor failure
file key: 59454c4c4f57205355424d4152494e45
identity: AGE-SECRET-KEY-1XMWWC06LY3EE5RYTXM9MFLAZ2U56JJJ36S0MYPDRWSVLUL66MV4QX3S7F6
armored: yes

-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBURWlGMHlwcXIrYnB2Y3FY
TnlDVkpwTDdPdXdQZFZ3UEw3S1FFYkZET0NjCkVtRUNBRWNLTituL1ZzOVNiV2lW
K0h1MHIrRThSNzdEZFdZeWQ4M253N1UKLS0tIFZuKzU0anFpaVVDRStXWmNFVlkz
ZjFzcUhqbHUvejFMQ1EvVDdYbTdxSTAK7s9ix86RtDMnTmjU8vkTTLdMW/73vqpS
yPC8DpksHoMx+2Y=

-----END AGE ENCRYPTED FILE-----
13 changes: 13 additions & 0 deletions testdata/testkit/armor_eol_between_padding
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
expect: armor failure
file key: 59454c4c4f57205355424d4152494e45
identity: AGE-SECRET-KEY-1XMWWC06LY3EE5RYTXM9MFLAZ2U56JJJ36S0MYPDRWSVLUL66MV4QX3S7F6
armored: yes

-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBURWlGMHlwcXIrYnB2Y3FY
TnlDVkpwTDdPdXdQZFZ3UEw3S1FFYkZET0NjCkVtRUNBRWNLTituL1ZzOVNiV2lW
K0h1MHIrRThSNzdEZFdZeWQ4M253N1UKLS0tIFZuKzU0anFpaVVDRStXWmNFVlkz
ZjFzcUhqbHUvejFMQ1EvVDdYbTdxSTAK7s9ix86RtDMnTmjU8vkTTLdMW2ewwwqo
mNlxYv6gMOKyDNzgiw=
=
-----END AGE ENCRYPTED FILE-----
13 changes: 13 additions & 0 deletions testdata/testkit/armor_full_last_line
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
expect: success
payload: 724a112a2cac139a4fca3ea0f799f2e5ccd1d0db46af654dee40567bff16ee33
file key: 59454c4c4f57205355424d4152494e45
identity: AGE-SECRET-KEY-1XMWWC06LY3EE5RYTXM9MFLAZ2U56JJJ36S0MYPDRWSVLUL66MV4QX3S7F6
armored: yes

-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBURWlGMHlwcXIrYnB2Y3FY
TnlDVkpwTDdPdXdQZFZ3UEw3S1FFYkZET0NjCkVtRUNBRWNLTituL1ZzOVNiV2lW
K0h1MHIrRThSNzdEZFdZeWQ4M253N1UKLS0tIFZuKzU0anFpaVVDRStXWmNFVlkz
ZjFzcUhqbHUvejFMQ1EvVDdYbTdxSTAK7s9ix86RtDMnTmjU8vkTTLdMW3bj4iHS
YS3WWUtZB5wJqKgEe8kpsp0iOnD2CNG4DVKBC0Z7SAcCFb8xdwV9CRavSEE7OU1c
-----END AGE ENCRYPTED FILE-----
Loading

0 comments on commit 799c2bf

Please sign in to comment.