opaque_test.go 2.4 KB

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