...

Source file src/golang.org/x/crypto/openpgp/packet/signature_test.go

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

     1  // Copyright 2011 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 packet
     6  
     7  import (
     8  	"bytes"
     9  	"crypto"
    10  	"encoding/hex"
    11  	"testing"
    12  )
    13  
    14  func TestSignatureRead(t *testing.T) {
    15  	packet, err := Read(readerFromHex(signatureDataHex))
    16  	if err != nil {
    17  		t.Error(err)
    18  		return
    19  	}
    20  	sig, ok := packet.(*Signature)
    21  	if !ok || sig.SigType != SigTypeBinary || sig.PubKeyAlgo != PubKeyAlgoRSA || sig.Hash != crypto.SHA1 {
    22  		t.Errorf("failed to parse, got: %#v", packet)
    23  	}
    24  }
    25  
    26  func TestSignatureReserialize(t *testing.T) {
    27  	packet, _ := Read(readerFromHex(signatureDataHex))
    28  	sig := packet.(*Signature)
    29  	out := new(bytes.Buffer)
    30  	err := sig.Serialize(out)
    31  	if err != nil {
    32  		t.Errorf("error reserializing: %s", err)
    33  		return
    34  	}
    35  
    36  	expected, _ := hex.DecodeString(signatureDataHex)
    37  	if !bytes.Equal(expected, out.Bytes()) {
    38  		t.Errorf("output doesn't match input (got vs expected):\n%s\n%s", hex.Dump(out.Bytes()), hex.Dump(expected))
    39  	}
    40  }
    41  
    42  func TestSignUserId(t *testing.T) {
    43  	sig := &Signature{
    44  		SigType:    SigTypeGenericCert,
    45  		PubKeyAlgo: PubKeyAlgoRSA,
    46  		Hash:       0, // invalid hash function
    47  	}
    48  
    49  	packet, err := Read(readerFromHex(rsaPkDataHex))
    50  	if err != nil {
    51  		t.Fatalf("failed to deserialize public key: %v", err)
    52  	}
    53  	pubKey := packet.(*PublicKey)
    54  
    55  	packet, err = Read(readerFromHex(privKeyRSAHex))
    56  	if err != nil {
    57  		t.Fatalf("failed to deserialize private key: %v", err)
    58  	}
    59  	privKey := packet.(*PrivateKey)
    60  
    61  	err = sig.SignUserId("", pubKey, privKey, nil)
    62  	if err == nil {
    63  		t.Errorf("did not receive an error when expected")
    64  	}
    65  
    66  	sig.Hash = crypto.SHA256
    67  	err = privKey.Decrypt([]byte("testing"))
    68  	if err != nil {
    69  		t.Fatalf("failed to decrypt private key: %v", err)
    70  	}
    71  
    72  	err = sig.SignUserId("", pubKey, privKey, nil)
    73  	if err != nil {
    74  		t.Errorf("failed to sign user id: %v", err)
    75  	}
    76  }
    77  
    78  const signatureDataHex = "c2c05c04000102000605024cb45112000a0910ab105c91af38fb158f8d07ff5596ea368c5efe015bed6e78348c0f033c931d5f2ce5db54ce7f2a7e4b4ad64db758d65a7a71773edeab7ba2a9e0908e6a94a1175edd86c1d843279f045b021a6971a72702fcbd650efc393c5474d5b59a15f96d2eaad4c4c426797e0dcca2803ef41c6ff234d403eec38f31d610c344c06f2401c262f0993b2e66cad8a81ebc4322c723e0d4ba09fe917e8777658307ad8329adacba821420741009dfe87f007759f0982275d028a392c6ed983a0d846f890b36148c7358bdb8a516007fac760261ecd06076813831a36d0459075d1befa245ae7f7fb103d92ca759e9498fe60ef8078a39a3beda510deea251ea9f0a7f0df6ef42060f20780360686f3e400e"
    79  

View as plain text