mirror of
https://github.com/FiloSottile/age.git
synced 2026-01-05 03:43:57 +00:00
armor: don't leave an empty line before the footer
Closes #264 Fixes #263
This commit is contained in:
@@ -28,7 +28,7 @@ const (
|
||||
|
||||
type armoredWriter struct {
|
||||
started, closed bool
|
||||
encoder io.WriteCloser
|
||||
encoder *format.WrappedBase64Encoder
|
||||
dst io.Writer
|
||||
}
|
||||
|
||||
@@ -50,15 +50,20 @@ func (a *armoredWriter) Close() error {
|
||||
if err := a.encoder.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := io.WriteString(a.dst, "\n"+Footer+"\n")
|
||||
footer := Footer + "\n"
|
||||
if !a.encoder.LastLineIsEmpty() {
|
||||
footer = "\n" + footer
|
||||
}
|
||||
_, err := io.WriteString(a.dst, footer)
|
||||
return err
|
||||
}
|
||||
|
||||
func NewWriter(dst io.Writer) io.WriteCloser {
|
||||
// TODO: write a test with aligned and misaligned sizes, and 8 and 10 steps.
|
||||
return &armoredWriter{dst: dst,
|
||||
encoder: base64.NewEncoder(base64.StdEncoding.Strict(),
|
||||
format.NewlineWriter(dst))}
|
||||
return &armoredWriter{
|
||||
dst: dst,
|
||||
encoder: format.NewWrappedBase64Encoder(base64.StdEncoding, dst),
|
||||
}
|
||||
}
|
||||
|
||||
type armoredReader struct {
|
||||
|
||||
@@ -8,6 +8,7 @@ package armor_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -18,6 +19,7 @@ import (
|
||||
|
||||
"filippo.io/age"
|
||||
"filippo.io/age/armor"
|
||||
"filippo.io/age/internal/format"
|
||||
)
|
||||
|
||||
func ExampleNewWriter() {
|
||||
@@ -87,9 +89,15 @@ kB/RRusYjn+KVJ+KTioxj0THtzZPXcjFKuQ1
|
||||
}
|
||||
|
||||
func TestArmor(t *testing.T) {
|
||||
t.Run("PartialLine", func(t *testing.T) { testArmor(t, 611) })
|
||||
t.Run("FullLine", func(t *testing.T) { testArmor(t, 10*format.BytesPerLine) })
|
||||
}
|
||||
|
||||
func testArmor(t *testing.T, size int) {
|
||||
buf := &bytes.Buffer{}
|
||||
w := armor.NewWriter(buf)
|
||||
plain := make([]byte, 611)
|
||||
plain := make([]byte, size)
|
||||
rand.Read(plain)
|
||||
if _, err := w.Write(plain); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -101,9 +109,18 @@ func TestArmor(t *testing.T) {
|
||||
if block == nil {
|
||||
t.Fatal("PEM decoding failed")
|
||||
}
|
||||
if len(block.Headers) != 0 {
|
||||
t.Error("unexpected headers")
|
||||
}
|
||||
if block.Type != "AGE ENCRYPTED FILE" {
|
||||
t.Errorf("unexpected type %q", block.Type)
|
||||
}
|
||||
if !bytes.Equal(block.Bytes, plain) {
|
||||
t.Error("PEM decoded value doesn't match")
|
||||
}
|
||||
if !bytes.Equal(buf.Bytes(), pem.EncodeToMemory(block)) {
|
||||
t.Error("PEM re-encoded value doesn't match")
|
||||
}
|
||||
|
||||
r := armor.NewReader(buf)
|
||||
out, err := ioutil.ReadAll(r)
|
||||
|
||||
Reference in New Issue
Block a user