...

Source file src/golang.org/x/crypto/openpgp/armor/armor_test.go

Documentation: golang.org/x/crypto/openpgp/armor

     1  // Copyright 2010 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package armor
     6  
     7  import (
     8  	"bytes"
     9  	"hash/adler32"
    10  	"io"
    11  	"testing"
    12  )
    13  
    14  func TestDecodeEncode(t *testing.T) {
    15  	buf := bytes.NewBuffer([]byte(armorExample1))
    16  	result, err := Decode(buf)
    17  	if err != nil {
    18  		t.Error(err)
    19  	}
    20  	expectedType := "PGP SIGNATURE"
    21  	if result.Type != expectedType {
    22  		t.Errorf("result.Type: got:%s want:%s", result.Type, expectedType)
    23  	}
    24  	if len(result.Header) != 1 {
    25  		t.Errorf("len(result.Header): got:%d want:1", len(result.Header))
    26  	}
    27  	v, ok := result.Header["Version"]
    28  	if !ok || v != "GnuPG v1.4.10 (GNU/Linux)" {
    29  		t.Errorf("result.Header: got:%#v", result.Header)
    30  	}
    31  
    32  	contents, err := io.ReadAll(result.Body)
    33  	if err != nil {
    34  		t.Error(err)
    35  	}
    36  
    37  	if adler32.Checksum(contents) != 0x27b144be {
    38  		t.Errorf("contents: got: %x", contents)
    39  	}
    40  
    41  	buf = bytes.NewBuffer(nil)
    42  	w, err := Encode(buf, result.Type, result.Header)
    43  	if err != nil {
    44  		t.Error(err)
    45  	}
    46  	_, err = w.Write(contents)
    47  	if err != nil {
    48  		t.Error(err)
    49  	}
    50  	w.Close()
    51  
    52  	if !bytes.Equal(buf.Bytes(), []byte(armorExample1)) {
    53  		t.Errorf("got: %s\nwant: %s", string(buf.Bytes()), armorExample1)
    54  	}
    55  }
    56  
    57  func TestLongHeader(t *testing.T) {
    58  	buf := bytes.NewBuffer([]byte(armorLongLine))
    59  	result, err := Decode(buf)
    60  	if err != nil {
    61  		t.Error(err)
    62  		return
    63  	}
    64  	value, ok := result.Header["Version"]
    65  	if !ok {
    66  		t.Errorf("missing Version header")
    67  	}
    68  	if value != longValueExpected {
    69  		t.Errorf("got: %s want: %s", value, longValueExpected)
    70  	}
    71  }
    72  
    73  const armorExample1 = `-----BEGIN PGP SIGNATURE-----
    74  Version: GnuPG v1.4.10 (GNU/Linux)
    75  
    76  iJwEAAECAAYFAk1Fv/0ACgkQo01+GMIMMbsYTwQAiAw+QAaNfY6WBdplZ/uMAccm
    77  4g+81QPmTSGHnetSb6WBiY13kVzK4HQiZH8JSkmmroMLuGeJwsRTEL4wbjRyUKEt
    78  p1xwUZDECs234F1xiG5enc5SGlRtP7foLBz9lOsjx+LEcA4sTl5/2eZR9zyFZqWW
    79  TxRjs+fJCIFuo71xb1g=
    80  =/teI
    81  -----END PGP SIGNATURE-----`
    82  
    83  const armorLongLine = `-----BEGIN PGP SIGNATURE-----
    84  Version: 0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz
    85  
    86  iQEcBAABAgAGBQJMtFESAAoJEKsQXJGvOPsVj40H/1WW6jaMXv4BW+1ueDSMDwM8
    87  kx1fLOXbVM5/Kn5LStZNt1jWWnpxdz7eq3uiqeCQjmqUoRde3YbB2EMnnwRbAhpp
    88  cacnAvy9ZQ78OTxUdNW1mhX5bS6q1MTEJnl+DcyigD70HG/yNNQD7sOPMdYQw0TA
    89  byQBwmLwmTsuZsrYqB68QyLHI+DUugn+kX6Hd2WDB62DKa2suoIUIHQQCd/ofwB3
    90  WfCYInXQKKOSxu2YOg2Eb4kLNhSMc1i9uKUWAH+sdgJh7NBgdoE4MaNtBFkHXRvv
    91  okWuf3+xA9ksp1npSY/mDvgHijmjvtpRDe6iUeqfCn8N9u9CBg8geANgaG8+QA4=
    92  =wfQG
    93  -----END PGP SIGNATURE-----`
    94  
    95  const longValueExpected = "0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz"
    96  

View as plain text