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 5package packet 6 7import ( 8 "bytes" 9 "encoding/hex" 10 "io" 11 "testing" 12) 13 14// Test packet.Read error handling in OpaquePacket.Parse, 15// which attempts to re-read an OpaquePacket as a supported 16// Packet type. 17func TestOpaqueParseReason(t *testing.T) { 18 buf, err := hex.DecodeString(UnsupportedKeyHex) 19 if err != nil { 20 t.Fatal(err) 21 } 22 or := NewOpaqueReader(bytes.NewBuffer(buf)) 23 count := 0 24 badPackets := 0 25 var uid *UserId 26 for { 27 op, err := or.Next() 28 if err == io.EOF { 29 break 30 } else if err != nil { 31 t.Errorf("#%d: opaque read error: %v", count, err) 32 break 33 } 34 // try to parse opaque packet 35 p, _ := op.Parse() 36 switch pkt := p.(type) { 37 case *UserId: 38 uid = pkt 39 case *OpaquePacket: 40 // If an OpaquePacket can't re-parse, packet.Read 41 // certainly had its reasons. 42 if pkt.Reason == nil { 43 t.Errorf("#%d: opaque packet, no reason", count) 44 } else { 45 badPackets++ 46 } 47 } 48 count++ 49 } 50 51 const expectedBad = 3 52 // Test post-conditions, make sure we actually parsed packets as expected. 53 if badPackets != expectedBad { 54 t.Errorf("unexpected # unparseable packets: %d (want %d)", badPackets, expectedBad) 55 } 56 if uid == nil { 57 t.Errorf("failed to find expected UID in unsupported keyring") 58 } else if uid.Id != "Armin M. Warda <warda@nephilim.ruhr.de>" { 59 t.Errorf("unexpected UID: %v", uid.Id) 60 } 61} 62 63// This key material has public key and signature packet versions modified to 64// an unsupported value (1), so that trying to parse the OpaquePacket to 65// a typed packet will get an error. It also contains a GnuPG trust packet. 66// (Created with: od -An -t x1 pubring.gpg | xargs | sed 's/ //g') 67const UnsupportedKeyHex = `988d012e7a18a20000010400d6ac00d92b89c1f4396c243abb9b76d2e9673ad63483291fed88e22b82e255e441c078c6abbbf7d2d195e50b62eeaa915b85b0ec20c225ce2c64c167cacb6e711daf2e45da4a8356a059b8160e3b3628ac0dd8437b31f06d53d6e8ea4214d4a26406a6b63e1001406ef23e0bb3069fac9a99a91f77dfafd5de0f188a5da5e3c9000511b42741726d696e204d2e205761726461203c7761726461406e657068696c696d2e727568722e64653e8900950105102e8936c705d1eb399e58489901013f0e03ff5a0c4f421e34fcfa388129166420c08cd76987bcdec6f01bd0271459a85cc22048820dd4e44ac2c7d23908d540f54facf1b36b0d9c20488781ce9dca856531e76e2e846826e9951338020a03a09b57aa5faa82e9267458bd76105399885ac35af7dc1cbb6aaed7c39e1039f3b5beda2c0e916bd38560509bab81235d1a0ead83b0020000` 68