From c64741f9030383bc94961faea825d183ac0866e0 Mon Sep 17 00:00:00 2001 From: David Hill Date: Sat, 13 Dec 2014 10:29:36 -0500 Subject: [PATCH] Add PEM encode error checking --- certgen.go | 15 +++++++++++---- test_coverage.txt | 43 ++++++++++++++++++++++--------------------- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/certgen.go b/certgen.go index 76dc5e5..59d2373 100644 --- a/certgen.go +++ b/certgen.go @@ -112,18 +112,25 @@ func NewTLSCertPair(organization string, validUntil time.Time, extraHosts []stri derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv) if err != nil { - return nil, nil, fmt.Errorf("failed to create certificate: %v\n", err) + return nil, nil, fmt.Errorf("failed to create certificate: %v", err) } certBuf := &bytes.Buffer{} - pem.Encode(certBuf, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}) + err = pem.Encode(certBuf, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}) + if err != nil { + return nil, nil, fmt.Errorf("failed to encode certificate: %v", err) + } keybytes, err := x509.MarshalECPrivateKey(priv) if err != nil { - return nil, nil, err + return nil, nil, fmt.Errorf("failed to marshal private key: %v", err) } + keyBuf := &bytes.Buffer{} - pem.Encode(keyBuf, &pem.Block{Type: "EC PRIVATE KEY", Bytes: keybytes}) + err = pem.Encode(keyBuf, &pem.Block{Type: "EC PRIVATE KEY", Bytes: keybytes}) + if err != nil { + return nil, nil, fmt.Errorf("failed to encode private key: %v", err) + } return certBuf.Bytes(), keyBuf.Bytes(), nil } diff --git a/test_coverage.txt b/test_coverage.txt index 302b78a..e475fad 100644 --- a/test_coverage.txt +++ b/test_coverage.txt @@ -6,26 +6,33 @@ github.com/conformal/btcutil/wif.go WIF.String 100.00% (11/11) github.com/conformal/btcutil/block.go Block.Transactions 100.00% (11/11) github.com/conformal/btcutil/amount.go AmountUnit.String 100.00% (8/8) github.com/conformal/btcutil/tx.go NewTxFromReader 100.00% (6/6) -github.com/conformal/btcutil/block.go NewBlockFromReader 100.00% (6/6) github.com/conformal/btcutil/block.go NewBlockFromBytes 100.00% (6/6) +github.com/conformal/btcutil/block.go NewBlockFromReader 100.00% (6/6) github.com/conformal/btcutil/address.go encodeAddress 100.00% (6/6) -github.com/conformal/btcutil/block.go Block.Sha 100.00% (5/5) github.com/conformal/btcutil/address.go newAddressPubKeyHash 100.00% (5/5) github.com/conformal/btcutil/address.go newAddressScriptHashFromHash 100.00% (5/5) github.com/conformal/btcutil/tx.go Tx.Sha 100.00% (5/5) +github.com/conformal/btcutil/block.go Block.Sha 100.00% (5/5) github.com/conformal/btcutil/amount.go NewAmount 100.00% (5/5) github.com/conformal/btcutil/amount.go round 100.00% (3/3) -github.com/conformal/btcutil/hash160.go calcHash 100.00% (2/2) -github.com/conformal/btcutil/tx.go NewTxFromBytes 100.00% (2/2) github.com/conformal/btcutil/address.go NewAddressScriptHash 100.00% (2/2) github.com/conformal/btcutil/amount.go Amount.Format 100.00% (2/2) +github.com/conformal/btcutil/tx.go NewTxFromBytes 100.00% (2/2) +github.com/conformal/btcutil/hash160.go calcHash 100.00% (2/2) +github.com/conformal/btcutil/address.go AddressPubKeyHash.Hash160 100.00% (1/1) github.com/conformal/btcutil/block.go OutOfRangeError.Error 100.00% (1/1) github.com/conformal/btcutil/block.go Block.MsgBlock 100.00% (1/1) +github.com/conformal/btcutil/tx.go Tx.MsgTx 100.00% (1/1) +github.com/conformal/btcutil/hash160.go Hash160 100.00% (1/1) github.com/conformal/btcutil/block.go NewBlockFromBlockAndBytes 100.00% (1/1) +github.com/conformal/btcutil/block.go Block.Height 100.00% (1/1) +github.com/conformal/btcutil/block.go Block.SetHeight 100.00% (1/1) +github.com/conformal/btcutil/block.go NewBlock 100.00% (1/1) +github.com/conformal/btcutil/address.go AddressPubKeyHash.IsForNet 100.00% (1/1) +github.com/conformal/btcutil/address.go AddressPubKey.EncodeAddress 100.00% (1/1) github.com/conformal/btcutil/address.go NewAddressPubKeyHash 100.00% (1/1) github.com/conformal/btcutil/address.go AddressPubKeyHash.EncodeAddress 100.00% (1/1) github.com/conformal/btcutil/address.go AddressPubKeyHash.ScriptAddress 100.00% (1/1) -github.com/conformal/btcutil/address.go AddressPubKeyHash.IsForNet 100.00% (1/1) github.com/conformal/btcutil/address.go AddressPubKeyHash.String 100.00% (1/1) github.com/conformal/btcutil/address.go NewAddressScriptHashFromHash 100.00% (1/1) github.com/conformal/btcutil/address.go AddressScriptHash.EncodeAddress 100.00% (1/1) @@ -33,39 +40,33 @@ github.com/conformal/btcutil/address.go AddressScriptHash.ScriptAddress 100.00% github.com/conformal/btcutil/address.go AddressScriptHash.IsForNet 100.00% (1/1) github.com/conformal/btcutil/address.go AddressScriptHash.String 100.00% (1/1) github.com/conformal/btcutil/address.go AddressScriptHash.Hash160 100.00% (1/1) -github.com/conformal/btcutil/address.go AddressPubKey.EncodeAddress 100.00% (1/1) github.com/conformal/btcutil/address.go AddressPubKey.ScriptAddress 100.00% (1/1) github.com/conformal/btcutil/address.go AddressPubKey.IsForNet 100.00% (1/1) github.com/conformal/btcutil/address.go AddressPubKey.String 100.00% (1/1) +github.com/conformal/btcutil/tx.go NewTx 100.00% (1/1) +github.com/conformal/btcutil/tx.go Tx.SetIndex 100.00% (1/1) github.com/conformal/btcutil/amount.go Amount.ToUnit 100.00% (1/1) +github.com/conformal/btcutil/tx.go Tx.Index 100.00% (1/1) github.com/conformal/btcutil/amount.go Amount.String 100.00% (1/1) github.com/conformal/btcutil/amount.go Amount.MulF64 100.00% (1/1) -github.com/conformal/btcutil/address.go AddressPubKeyHash.Hash160 100.00% (1/1) -github.com/conformal/btcutil/block.go Block.Height 100.00% (1/1) -github.com/conformal/btcutil/block.go Block.SetHeight 100.00% (1/1) -github.com/conformal/btcutil/block.go NewBlock 100.00% (1/1) -github.com/conformal/btcutil/hash160.go Hash160 100.00% (1/1) -github.com/conformal/btcutil/tx.go Tx.MsgTx 100.00% (1/1) -github.com/conformal/btcutil/tx.go Tx.Index 100.00% (1/1) -github.com/conformal/btcutil/tx.go Tx.SetIndex 100.00% (1/1) -github.com/conformal/btcutil/tx.go NewTx 100.00% (1/1) github.com/conformal/btcutil/appdata.go appDataDir 92.00% (23/25) -github.com/conformal/btcutil/block.go Block.Bytes 88.89% (8/9) github.com/conformal/btcutil/block.go Block.TxLoc 88.89% (8/9) +github.com/conformal/btcutil/block.go Block.Bytes 88.89% (8/9) github.com/conformal/btcutil/address.go NewAddressPubKey 87.50% (7/8) github.com/conformal/btcutil/address.go DecodeAddress 85.00% (17/20) github.com/conformal/btcutil/wif.go DecodeWIF 85.00% (17/20) github.com/conformal/btcutil/address.go AddressPubKey.serialize 80.00% (4/5) github.com/conformal/btcutil/block.go Block.TxSha 75.00% (3/4) -github.com/conformal/btcutil/wif.go NewWIF 66.67% (2/3) github.com/conformal/btcutil/wif.go paddedAppend 66.67% (2/3) -github.com/conformal/btcutil/certgen.go NewTLSCertPair 0.00% (0/50) +github.com/conformal/btcutil/wif.go NewWIF 66.67% (2/3) +github.com/conformal/btcutil/certgen.go NewTLSCertPair 0.00% (0/54) github.com/conformal/btcutil/wif.go WIF.SerializePubKey 0.00% (0/4) github.com/conformal/btcutil/address.go AddressPubKey.AddressPubKeyHash 0.00% (0/3) -github.com/conformal/btcutil/address.go AddressPubKey.PubKey 0.00% (0/1) github.com/conformal/btcutil/address.go AddressPubKey.Format 0.00% (0/1) -github.com/conformal/btcutil/wif.go WIF.IsForNet 0.00% (0/1) +github.com/conformal/btcutil/address.go AddressPubKey.PubKey 0.00% (0/1) github.com/conformal/btcutil/address.go AddressPubKey.SetFormat 0.00% (0/1) +github.com/conformal/btcutil/wif.go WIF.IsForNet 0.00% (0/1) github.com/conformal/btcutil/appdata.go AppDataDir 0.00% (0/1) -github.com/conformal/btcutil ------------------------------- 77.01% (258/335) +github.com/conformal/btcutil/net.go interfaceAddrs 0.00% (0/1) +github.com/conformal/btcutil ------------------------------- 75.88% (258/340)