| 1 |
package sasquatch |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"io/ioutil" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/mitchellh/go-homedir" |
| 9 |
) |
| 10 |
|
| 11 |
func TestFindSSHKeys(t *testing.T) { |
| 12 |
keys, err := FindSSHKeys() |
| 13 |
if err != nil { |
| 14 |
t.Error(err) |
| 15 |
} |
| 16 |
|
| 17 |
if len(keys) == 0 { |
| 18 |
t.Fatal("no keys found") |
| 19 |
} |
| 20 |
// fmt.Println(keys) |
| 21 |
} |
| 22 |
|
| 23 |
func TestFindSSHAgentSigners(t *testing.T) { |
| 24 |
signers, err := SSHAgentSigners() |
| 25 |
if err != nil { |
| 26 |
t.Error(err) |
| 27 |
} |
| 28 |
|
| 29 |
if len(signers) == 0 { |
| 30 |
t.Fatal("no signers found") |
| 31 |
} |
| 32 |
|
| 33 |
/* |
| 34 |
for _, k := range signers { |
| 35 |
s := EncodeToString(k.PublicKey().Marshal()) |
| 36 |
fmt.Println(s) |
| 37 |
} |
| 38 |
*/ |
| 39 |
} |
| 40 |
|
| 41 |
func TestFindRecipients(t *testing.T) { |
| 42 |
rcp := FindRecipients() |
| 43 |
|
| 44 |
if len(rcp) == 0 { |
| 45 |
t.Fatal("no recipients found") |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
func TestFindIdentities(t *testing.T) { |
| 50 |
identities := FindIdentities() |
| 51 |
|
| 52 |
if len(identities) == 0 { |
| 53 |
t.Fatal("no identities found") |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
func TestIdentityFromFile(t *testing.T) { |
| 58 |
key, _ := homedir.Expand("~/.ssh/id_rsa") |
| 59 |
i, err := ParseIdentitiesFile(key) |
| 60 |
if err != nil { |
| 61 |
t.Fatal(err) |
| 62 |
} |
| 63 |
|
| 64 |
if len(i) == 0 { |
| 65 |
t.Fatal("no key found") |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
func TestRecipientFromPubKey(t *testing.T) { |
| 70 |
pubkey := `ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCnpGXlDvHC7YbQTBhxYnQVFlPjhG9NJzJA+DvucLjyEhoFZT7FRd1Zpy1vX3asswolBW+BkfI7y0opamhWoJbUKZFXsQnymfLknqMZ10FcXBpkbGj/wE9NdDu36CORb9W9mItwhqrIY5eAhktMmVnqaq3bqRW08y8BiTzbt17ulxr81/OFymKPnX5qKfGErKXkT8L8umuGcNknpumyDHvxDHWQ8El4KmLDHFb7UsZxY272dMuFXXGVCN90V5aEzXNklPhkHcQnaCvGhPmBwQBCjaBYc+0oQFLO7+s1mXkS92pt4hiX1Srui+VYtTN6Laqcu/HIwFjKC65EmNCW/6t9XzP+lZbu9U5gRPLS3tDEUkp5j3yMyM/E28fdsGvYnb8Fyj7ifV6fCcWvCkEfj+vDK4iJn9NmkPfsQvt4Hi4p5voPyCZw6PvCyQZW1jpIDDBBtGZxOxAn9aVQicJ34ZIusbCgygLonkgZ/C67Kd9ewIsNLGYjtKvo8k6l6vqWjqt5HmQCIT6SPMEhlh9hitcLbUh7EvcpqfMpClSpEDwnbh7k1g4Izv073ky16ACT0IA2ocGXnZGcjUYNiPhYry4hzajSTCveJJrOWi0j13BSSZJa9j0EsVSGHzln+H/Oz8V4QqOOnlw8Kuud9FPnOgA6e7+jRAk3aSpulNosUtsMUw==` |
| 71 |
_, err := ParseRecipient(pubkey) |
| 72 |
if err != nil { |
| 73 |
t.Fatal(err) |
| 74 |
} |
| 75 |
|
| 76 |
pubkey = `ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBEpRrRQBpDXgGZqrUUEHN2U0phczPsnJGJgllvTZMH2` |
| 77 |
_, err = ParseRecipient(pubkey) |
| 78 |
if err != nil { |
| 79 |
t.Fatal(err) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
func TestSSHRSAEncrypt(t *testing.T) { |
| 84 |
buf := bytes.NewBuffer(nil) |
| 85 |
|
| 86 |
key, _ := homedir.Expand("~/.ssh/id_rsa.pub") |
| 87 |
c, err := ioutil.ReadFile(key) |
| 88 |
if err != nil { |
| 89 |
t.Fatal(err) |
| 90 |
} |
| 91 |
|
| 92 |
r, err := ParseRecipient(string(c)) |
| 93 |
if err != nil { |
| 94 |
t.Fatal(err) |
| 95 |
} |
| 96 |
|
| 97 |
w, err := Encrypt(buf, r) |
| 98 |
if err != nil { |
| 99 |
t.Fatal(err) |
| 100 |
} |
| 101 |
|
| 102 |
data := []byte("Hello World!") |
| 103 |
_, err = w.Write(data) |
| 104 |
if err != nil { |
| 105 |
t.Fatal(err) |
| 106 |
} |
| 107 |
err = w.Close() |
| 108 |
if err != nil { |
| 109 |
t.Fatal(err) |
| 110 |
} |
| 111 |
|
| 112 |
// _ = ioutil.WriteFile("/tmp/sasquatch.crypted.rsa", buf.Bytes(), 0644) |
| 113 |
|
| 114 |
ids := FindIdentities() |
| 115 |
dr, err := Decrypt(buf, ids...) |
| 116 |
if err != nil { |
| 117 |
t.Fatal(err) |
| 118 |
} |
| 119 |
|
| 120 |
dbuf, _ := ioutil.ReadAll(dr) |
| 121 |
if !bytes.Equal(dbuf, data) { |
| 122 |
t.Fatalf("Decrypted data does not match!") |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
func TestSSHEd25519Encrypt(t *testing.T) { |
| 127 |
buf := bytes.NewBuffer(nil) |
| 128 |
|
| 129 |
key, _ := homedir.Expand("~/.ssh/id_ed25519.pub") |
| 130 |
c, err := ioutil.ReadFile(key) |
| 131 |
if err != nil { |
| 132 |
t.Fatal(err) |
| 133 |
} |
| 134 |
|
| 135 |
r, err := ParseRecipient(string(c)) |
| 136 |
if err != nil { |
| 137 |
t.Fatal(err) |
| 138 |
} |
| 139 |
|
| 140 |
w, err := Encrypt(buf, r) |
| 141 |
if err != nil { |
| 142 |
t.Fatal(err) |
| 143 |
} |
| 144 |
|
| 145 |
data := []byte("Hello World!") |
| 146 |
_, err = w.Write(data) |
| 147 |
if err != nil { |
| 148 |
t.Fatal(err) |
| 149 |
} |
| 150 |
err = w.Close() |
| 151 |
if err != nil { |
| 152 |
t.Fatal(err) |
| 153 |
} |
| 154 |
|
| 155 |
// _ = ioutil.WriteFile("/tmp/sasquatch.crypted.ed25519", buf.Bytes(), 0644) |
| 156 |
|
| 157 |
ids := FindIdentities() |
| 158 |
dr, err := Decrypt(buf, ids...) |
| 159 |
if err != nil { |
| 160 |
t.Fatal(err) |
| 161 |
} |
| 162 |
|
| 163 |
dbuf, _ := ioutil.ReadAll(dr) |
| 164 |
if !bytes.Equal(dbuf, data) { |
| 165 |
t.Fatalf("Decrypted data does not match!") |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
func TestChallengeEncrypt(t *testing.T) { |
| 170 |
buf := bytes.NewBuffer(nil) |
| 171 |
|
| 172 |
signers, err := SSHAgentSigners() |
| 173 |
if err != nil { |
| 174 |
t.Fatal(err) |
| 175 |
} |
| 176 |
|
| 177 |
r, err := NewChallengeRecipient(signers[0]) |
| 178 |
if err != nil { |
| 179 |
t.Fatal(err) |
| 180 |
} |
| 181 |
|
| 182 |
w, err := Encrypt(buf, r) |
| 183 |
if err != nil { |
| 184 |
t.Fatal(err) |
| 185 |
} |
| 186 |
|
| 187 |
data := []byte("Hello World!") |
| 188 |
_, err = w.Write(data) |
| 189 |
if err != nil { |
| 190 |
t.Fatal(err) |
| 191 |
} |
| 192 |
err = w.Close() |
| 193 |
if err != nil { |
| 194 |
t.Fatal(err) |
| 195 |
} |
| 196 |
|
| 197 |
// _ = ioutil.WriteFile("/tmp/sasquatch.crypted.agent", buf.Bytes(), 0644) |
| 198 |
|
| 199 |
ids := FindIdentities() |
| 200 |
dr, err := Decrypt(buf, ids...) |
| 201 |
if err != nil { |
| 202 |
t.Fatal(err) |
| 203 |
} |
| 204 |
|
| 205 |
dbuf, _ := ioutil.ReadAll(dr) |
| 206 |
if !bytes.Equal(dbuf, data) { |
| 207 |
t.Fatalf("Decrypted data does not match!") |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
func TestScryptEncrypt(t *testing.T) { |
| 212 |
buf := bytes.NewBuffer(nil) |
| 213 |
|
| 214 |
r, err := NewScryptRecipient("password") |
| 215 |
if err != nil { |
| 216 |
t.Fatal(err) |
| 217 |
} |
| 218 |
|
| 219 |
w, err := Encrypt(buf, r) |
| 220 |
if err != nil { |
| 221 |
t.Fatal(err) |
| 222 |
} |
| 223 |
|
| 224 |
data := []byte("Hello World!") |
| 225 |
_, err = w.Write(data) |
| 226 |
if err != nil { |
| 227 |
t.Fatal(err) |
| 228 |
} |
| 229 |
err = w.Close() |
| 230 |
if err != nil { |
| 231 |
t.Fatal(err) |
| 232 |
} |
| 233 |
|
| 234 |
id, err := NewScryptIdentity("password") |
| 235 |
if err != nil { |
| 236 |
t.Fatal(err) |
| 237 |
} |
| 238 |
|
| 239 |
dr, err := Decrypt(buf, id) |
| 240 |
if err != nil { |
| 241 |
t.Fatal(err) |
| 242 |
} |
| 243 |
|
| 244 |
dbuf, _ := ioutil.ReadAll(dr) |
| 245 |
if !bytes.Equal(dbuf, data) { |
| 246 |
t.Fatalf("Decrypted data does not match!") |
| 247 |
} |
| 248 |
} |