github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/environs/simplestreams/decode_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package simplestreams_test 5 6 import ( 7 "bytes" 8 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/juju/environs/simplestreams" 13 ) 14 15 type decodeSuite struct{} 16 17 var _ = gc.Suite(&decodeSuite{}) 18 19 func (s *decodeSuite) TestDecodeCheckValidSignature(c *gc.C) { 20 r := bytes.NewReader([]byte(signedData)) 21 txt, err := simplestreams.DecodeCheckSignature(r, testSigningKey) 22 c.Assert(err, jc.ErrorIsNil) 23 c.Assert(txt, gc.DeepEquals, []byte(unsignedData[1:])) 24 } 25 26 func (s *decodeSuite) TestDecodeCheckInvalidSignature(c *gc.C) { 27 r := bytes.NewReader([]byte(invalidClearsignInput + signSuffix)) 28 _, err := simplestreams.DecodeCheckSignature(r, testSigningKey) 29 c.Assert(err, gc.Not(gc.IsNil)) 30 _, ok := err.(*simplestreams.NotPGPSignedError) 31 c.Assert(ok, jc.IsFalse) 32 } 33 34 func (s *decodeSuite) TestDecodeCheckMissingSignature(c *gc.C) { 35 r := bytes.NewReader([]byte("foo")) 36 _, err := simplestreams.DecodeCheckSignature(r, testSigningKey) 37 _, ok := err.(*simplestreams.NotPGPSignedError) 38 c.Assert(ok, jc.IsTrue) 39 } 40 41 func (s *decodeSuite) TestDecodeCheckMissingKey(c *gc.C) { 42 r := bytes.NewReader([]byte(signedData)) 43 _, err := simplestreams.DecodeCheckSignature(r, "") 44 c.Assert(err, gc.ErrorMatches, "failed to parse public key: openpgp: invalid argument: no armored data found") 45 } 46 47 const ( 48 testSigningKey = `-----BEGIN PGP PRIVATE KEY BLOCK----- 49 Version: GnuPG v1.4.10 (GNU/Linux) 50 51 lQHYBE2rFNoBBADFwqWQIW/DSqcB4yCQqnAFTJ27qS5AnB46ccAdw3u4Greeu3Bp 52 id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0bE1jaaZKj6ndqcw86jd+QtD1SF+Cf21CWRNeLKnUds4FRRvclzTyUMuWPkUeX 69 TaNNsUOFqBsf6QQ2oHUBBK4VCHffHCW4ZEX2cd6umz7mpHW6XzN4DECEzOVksXtc 70 lUC1j4UB91DC/RNQqwX1IV2QLSwssVotPMPqhOi0ZLNY7wIA3n7DWKInxYZZ4K+6 71 rQ+POsz6brEoRHwr8x6XlHenq1Oki855pSa1yXIARoTrSJkBtn5oI+f8AzrnN0BN 72 oyeQAwIA/7E++3HDi5aweWrViiul9cd3rcsS0dEnksPhvS0ozCJiHsq/6GFmy7J8 73 QSHZPteedBnZyNp5jR+H7cIfVN3KgwH/Skq4PsuPhDq5TKK6i8Pc1WW8MA6DXTdU 74 nLkX7RGmMwjC0DBf7KWAlPjFaONAX3a8ndnz//fy1q7u2l9AZwrj1qa1iJ8EGAEC 75 AAkFAk2rFNoCGwwACgkQO9o98PRieSo2/QP/WTzr4ioINVsvN1akKuekmEMI3LAp 76 BfHwatufxxP1U+3Si/6YIk7kuPB9Hs+pRqCXzbvPRrI8NHZBmc8qIGthishdCYad 77 AHcVnXjtxrULkQFGbGvhKURLvS9WnzD/m1K2zzwxzkPTzT9/Yf06O6Mal5AdugPL 78 VrM0m72/jnpKo04= 79 =zNCn 80 -----END PGP PRIVATE KEY BLOCK----- 81 ` 82 83 signPrefix = ` 84 -----BEGIN PGP SIGNED MESSAGE----- 85 Hash: SHA1 86 ` 87 88 signSuffix = `-----BEGIN PGP SIGNATURE----- 89 Version: GnuPG v1.4.10 (GNU/Linux) 90 91 iJwEAQECAAYFAk8kMuEACgkQO9o98PRieSpMsAQAhmY/vwmNpflrPgmfWsYhk5O8 92 pjnBUzZwqTDoDeINjZEoPDSpQAHGhjFjgaDx/Gj4fAl0dM4D0wuUEBb6QOrwflog 93 2A2k9kfSOMOtk0IH/H5VuFN1Mie9L/erYXjTQIptv9t9J7NoRBMU0QOOaFU0JaO9 94 MyTpno24AjIAGb+mH1U= 95 =hIJ6 96 -----END PGP SIGNATURE----- 97 ` 98 99 unsignedData = ` 100 Hello world 101 line 2 102 ` 103 signedData = signPrefix + unsignedData + signSuffix 104 105 invalidClearsignInput = ` 106 -----BEGIN PGP SIGNED MESSAGE----- 107 Hash: SHA1 108 109 Invalid 110 ` 111 )