1package gpg 2 3import ( 4 "fmt" 5 "testing" 6 "time" 7 8 "github.com/stretchr/testify/assert" 9) 10 11func genTestKey(args ...string) Key { 12 first := "John" 13 if len(args) > 0 && args[0] != "" { 14 first = args[0] 15 } 16 nick := "johnny" 17 if len(args) > 1 && args[1] != "" { 18 nick = args[1] 19 } 20 last := "Doe" 21 if len(args) > 2 && args[2] != "" { 22 last = args[2] 23 } 24 email := "john.doe@example.org" 25 if len(args) > 3 && args[3] != "" { 26 email = args[3] 27 } 28 fp := "25FF1614B8F87B52FFFF99B962AF4031C82E0039" 29 if len(args) > 4 && args[4] != "" { 30 fp = args[4] 31 } 32 validity := "u" 33 if len(args) > 5 && args[5] != "" { 34 validity = args[5] 35 } 36 trust := "ultimate" 37 if len(args) > 6 && args[6] != "" { 38 trust = args[6] 39 } 40 41 creation := time.Date(2018, 1, 1, 1, 1, 1, 0, time.UTC) 42 expiration := time.Date(2218, 1, 1, 1, 1, 1, 0, time.UTC) 43 return Key{ 44 KeyType: "sec", 45 KeyLength: 2048, 46 Validity: validity, 47 CreationDate: creation, 48 ExpirationDate: expiration, 49 Ownertrust: trust, 50 Fingerprint: fp, 51 Identities: map[string]Identity{ 52 fmt.Sprintf("%s %s (%s) <%s>", first, last, nick, email): { 53 Name: fmt.Sprintf("%s %s", first, last), 54 Comment: nick, 55 Email: email, 56 CreationDate: creation, 57 ExpirationDate: expiration, 58 }, 59 }, 60 } 61} 62 63func TestKey(t *testing.T) { 64 k := Key{ 65 Identities: map[string]Identity{}, 66 } 67 assert.Equal(t, "(invalid:)", k.OneLine()) 68 assert.Equal(t, "", k.Identity().Name) 69 k = genTestKey() 70 assert.Equal(t, k.IsUseable(false), true) 71 assert.Equal(t, "sec 2048D/0x62AF4031C82E0039 2018-01-01 [expires: 2218-01-01]\n Key fingerprint = 25FF1614B8F87B52FFFF99B962AF4031C82E0039\nuid John Doe (johnny) <john.doe@example.org>", k.String()) 72 assert.Equal(t, "0x62AF4031C82E0039 - John Doe (johnny) <john.doe@example.org>", k.OneLine()) 73 assert.Equal(t, "0x62AF4031C82E0039", k.ID()) 74} 75 76func TestIdentitySort(t *testing.T) { 77 creation := time.Date(2017, 1, 1, 1, 1, 1, 0, time.UTC) 78 expiration := time.Date(2018, 1, 1, 1, 1, 1, 0, time.UTC) 79 80 k := genTestKey() 81 k.Identities["Foo Bar"] = Identity{ 82 Name: "Foo Bar", 83 Comment: "foo", 84 Email: "foo.bar@example.com", 85 CreationDate: creation, 86 ExpirationDate: expiration, 87 } 88 assert.Equal(t, "0x62AF4031C82E0039 - John Doe (johnny) <john.doe@example.org>", k.OneLine()) 89} 90 91func TestUseability(t *testing.T) { 92 // invalid 93 for _, k := range []Key{ 94 {}, 95 { 96 ExpirationDate: time.Now().Add(-time.Second), 97 }, 98 { 99 ExpirationDate: time.Now().Add(time.Hour), 100 Validity: "z", 101 }, 102 } { 103 assert.False(t, k.IsUseable(false)) 104 } 105 // valid 106 for _, k := range []Key{ 107 { 108 ExpirationDate: time.Now().Add(time.Hour), 109 Validity: "m", 110 }, 111 { 112 ExpirationDate: time.Now().Add(time.Hour), 113 Validity: "f", 114 }, 115 { 116 ExpirationDate: time.Now().Add(time.Hour), 117 Validity: "u", 118 }, 119 } { 120 assert.True(t, k.IsUseable(false)) 121 } 122} 123