You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

83 lines
2.3 KiB

  1. // Copyright 2013 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. "testing"
  9. "time"
  10. )
  11. var pubKeyV3Test = struct {
  12. hexFingerprint string
  13. creationTime time.Time
  14. pubKeyAlgo PublicKeyAlgorithm
  15. keyId uint64
  16. keyIdString string
  17. keyIdShort string
  18. }{
  19. "103BECF5BD1E837C89D19E98487767F7",
  20. time.Unix(779753634, 0),
  21. PubKeyAlgoRSA,
  22. 0xDE0F188A5DA5E3C9,
  23. "DE0F188A5DA5E3C9",
  24. "5DA5E3C9"}
  25. func TestPublicKeyV3Read(t *testing.T) {
  26. i, test := 0, pubKeyV3Test
  27. packet, err := Read(v3KeyReader(t))
  28. if err != nil {
  29. t.Fatalf("#%d: Read error: %s", i, err)
  30. }
  31. pk, ok := packet.(*PublicKeyV3)
  32. if !ok {
  33. t.Fatalf("#%d: failed to parse, got: %#v", i, packet)
  34. }
  35. if pk.PubKeyAlgo != test.pubKeyAlgo {
  36. t.Errorf("#%d: bad public key algorithm got:%x want:%x", i, pk.PubKeyAlgo, test.pubKeyAlgo)
  37. }
  38. if !pk.CreationTime.Equal(test.creationTime) {
  39. t.Errorf("#%d: bad creation time got:%v want:%v", i, pk.CreationTime, test.creationTime)
  40. }
  41. expectedFingerprint, _ := hex.DecodeString(test.hexFingerprint)
  42. if !bytes.Equal(expectedFingerprint, pk.Fingerprint[:]) {
  43. t.Errorf("#%d: bad fingerprint got:%x want:%x", i, pk.Fingerprint[:], expectedFingerprint)
  44. }
  45. if pk.KeyId != test.keyId {
  46. t.Errorf("#%d: bad keyid got:%x want:%x", i, pk.KeyId, test.keyId)
  47. }
  48. if g, e := pk.KeyIdString(), test.keyIdString; g != e {
  49. t.Errorf("#%d: bad KeyIdString got:%q want:%q", i, g, e)
  50. }
  51. if g, e := pk.KeyIdShortString(), test.keyIdShort; g != e {
  52. t.Errorf("#%d: bad KeyIdShortString got:%q want:%q", i, g, e)
  53. }
  54. }
  55. func TestPublicKeyV3Serialize(t *testing.T) {
  56. //for i, test := range pubKeyV3Tests {
  57. i := 0
  58. packet, err := Read(v3KeyReader(t))
  59. if err != nil {
  60. t.Fatalf("#%d: Read error: %s", i, err)
  61. }
  62. pk, ok := packet.(*PublicKeyV3)
  63. if !ok {
  64. t.Fatalf("#%d: failed to parse, got: %#v", i, packet)
  65. }
  66. var serializeBuf bytes.Buffer
  67. if err = pk.Serialize(&serializeBuf); err != nil {
  68. t.Fatalf("#%d: failed to serialize: %s", i, err)
  69. }
  70. if packet, err = Read(bytes.NewBuffer(serializeBuf.Bytes())); err != nil {
  71. t.Fatalf("#%d: Read error (from serialized data): %s", i, err)
  72. }
  73. if pk, ok = packet.(*PublicKeyV3); !ok {
  74. t.Fatalf("#%d: failed to parse serialized data, got: %#v", i, packet)
  75. }
  76. }