aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--file.go27
-rw-r--r--file_test.go3
2 files changed, 21 insertions, 9 deletions
diff --git a/file.go b/file.go
index 68737ad..e8218d8 100644
--- a/file.go
+++ b/file.go
@@ -25,31 +25,42 @@ const (
sigFrom = "signature from %s"
)
+func checkComment(comment string) error {
+ // compatibility with original version
+ if len(comment) > 1024 {
+ return errors.New("comment line to long")
+ }
+ if !strings.HasPrefix(comment, commentHdr) {
+ return errors.New("expected untrusted comment")
+ }
+ return nil
+}
+
func Parse(r io.Reader) (File, error) {
buf := bufio.NewReader(r)
+
comment, err := buf.ReadString('\n')
if err != nil {
return File{}, err
}
- if !strings.HasPrefix(comment, commentHdr) {
- return File{}, errors.New("expected untrusted header")
- }
- // Backward compatibility with original signify
- if len(comment) > 1024 {
- return File{}, errors.New("comment line too long")
+ if err := checkComment(comment); err != nil {
+ return File{}, err
}
comment = comment[len(commentHdr):]
+
b64, err := buf.ReadBytes('\n')
if err != nil {
return File{}, err
}
+
body, err := ioutil.ReadAll(buf)
if err != nil {
return File{}, err
}
+
return File{
- Comment: strings.TrimRight(comment, "\r\n"),
- B64: bytes.TrimRight(b64, "\r\n"),
+ Comment: strings.TrimSpace(comment),
+ B64: bytes.TrimSpace(b64),
Body: body,
}, nil
}
diff --git a/file_test.go b/file_test.go
index e3a8bbb..b51c344 100644
--- a/file_test.go
+++ b/file_test.go
@@ -17,8 +17,9 @@ func TestParseFile(t *testing.T) {
if err != nil {
t.Error(err)
}
+ t.Logf("%v: %+v", tf, f)
buf := new(bytes.Buffer)
f.Encode(buf)
- t.Logf("%v", buf.String())
+ t.Logf("%v: %v", tf, buf.String())
}
}