| 1 |
package v2 |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"strings" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/ProtonMail/go-crypto/openpgp/packet" |
| 9 |
) |
| 10 |
|
| 11 |
var foreignKeys = []string{ |
| 12 |
v5PrivKey, |
| 13 |
} |
| 14 |
|
| 15 |
func TestReadPrivateForeignV5Key(t *testing.T) { |
| 16 |
if packet.V5Disabled { |
| 17 |
t.Skip() |
| 18 |
} |
| 19 |
for _, str := range foreignKeys { |
| 20 |
kring, err := ReadArmoredKeyRing(strings.NewReader(str)) |
| 21 |
if err != nil { |
| 22 |
t.Fatal(err) |
| 23 |
} |
| 24 |
checkV5Key(t, kring[0]) |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
func TestReadPrivateSerializeForeignV5Key(t *testing.T) { |
| 29 |
if packet.V5Disabled { |
| 30 |
t.Skip() |
| 31 |
} |
| 32 |
for _, str := range foreignKeys { |
| 33 |
el, err := ReadArmoredKeyRing(strings.NewReader(str)) |
| 34 |
if err != nil { |
| 35 |
t.Fatal(err) |
| 36 |
} |
| 37 |
checkSerializeRead(t, el[0]) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
func checkV5Key(t *testing.T, ent *Entity) { |
| 42 |
key := ent.PrimaryKey |
| 43 |
if key.Version != 5 { |
| 44 |
t.Errorf("wrong key version %d", key.Version) |
| 45 |
} |
| 46 |
if len(key.Fingerprint) != 32 { |
| 47 |
t.Errorf("Wrong fingerprint length: %d", len(key.Fingerprint)) |
| 48 |
} |
| 49 |
signatures := ent.Revocations |
| 50 |
for _, id := range ent.Identities { |
| 51 |
signatures = append(signatures, id.SelfCertifications...) |
| 52 |
} |
| 53 |
for _, sig := range signatures { |
| 54 |
if sig == nil { |
| 55 |
continue |
| 56 |
} |
| 57 |
if sig.Packet.Version != 5 { |
| 58 |
t.Errorf("wrong signature version %d", sig.Packet.Version) |
| 59 |
} |
| 60 |
fgptLen := len(sig.Packet.IssuerFingerprint) |
| 61 |
if fgptLen != 32 { |
| 62 |
t.Errorf("Wrong fingerprint length in signature: %d", fgptLen) |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
func checkSerializeRead(t *testing.T, e *Entity) { |
| 68 |
// Entity serialize |
| 69 |
serializedEntity := bytes.NewBuffer(nil) |
| 70 |
err := e.Serialize(serializedEntity) |
| 71 |
if err != nil { |
| 72 |
t.Fatal(err) |
| 73 |
} |
| 74 |
el, err := ReadKeyRing(serializedEntity) |
| 75 |
if err != nil { |
| 76 |
t.Fatal(err) |
| 77 |
} |
| 78 |
checkV5Key(t, el[0]) |
| 79 |
|
| 80 |
// Without signing |
| 81 |
serializedEntity = bytes.NewBuffer(nil) |
| 82 |
err = e.SerializePrivateWithoutSigning(serializedEntity, nil) |
| 83 |
if err != nil { |
| 84 |
t.Fatal(err) |
| 85 |
} |
| 86 |
el, err = ReadKeyRing(serializedEntity) |
| 87 |
if err != nil { |
| 88 |
t.Fatal(err) |
| 89 |
} |
| 90 |
checkV5Key(t, el[0]) |
| 91 |
|
| 92 |
// Private |
| 93 |
serializedEntity = bytes.NewBuffer(nil) |
| 94 |
err = e.SerializePrivate(serializedEntity, nil) |
| 95 |
if err != nil { |
| 96 |
t.Fatal(err) |
| 97 |
} |
| 98 |
el, err = ReadKeyRing(serializedEntity) |
| 99 |
if err != nil { |
| 100 |
t.Fatal(err) |
| 101 |
} |
| 102 |
checkV5Key(t, el[0]) |
| 103 |
} |