blob: 9ee55791bfa35db3e2014d30392b235d1b815158 [file] [log] [blame]
Rickard von Essenab8cd7a2016-02-17 20:25:39 +01001// +build fixtures
2
Rickard von Essenc3d49b72016-02-16 20:59:18 +01003package servers
4
5import (
6 "crypto/rsa"
7 "encoding/json"
8 "fmt"
9 "testing"
10
11 "github.com/rackspace/gophercloud"
12 th "github.com/rackspace/gophercloud/testhelper"
jrperritt42b019f2016-05-02 14:02:55 -050013 "github.com/rackspace/gophercloud/testhelper/client"
Rickard von Essenc3d49b72016-02-16 20:59:18 +010014 "golang.org/x/crypto/ssh"
15)
16
17// Fail - No password in JSON.
18func TestExtractPassword_no_pwd_data(t *testing.T) {
19
20 var dejson interface{}
21 err := json.Unmarshal([]byte(`{ "Crappy data": ".-.-." }`), &dejson)
22 if err != nil {
23 t.Fatalf("%s", err)
24 }
25 resp := GetPasswordResult{gophercloud.Result{Body: dejson}}
26
27 pwd, err := resp.ExtractPassword(nil)
28 th.AssertEquals(t, pwd, "")
29}
30
31// Ok - return encrypted password when no private key is given.
32func TestExtractPassword_encrypted_pwd(t *testing.T) {
33
34 var dejson interface{}
35 sejson := []byte(`{"password":"PP8EnwPO9DhEc8+O/6CKAkPF379mKsUsfFY6yyw0734XXvKsSdV9KbiHQ2hrBvzeZxtGMrlFaikVunCRizyLLWLMuOi4hoH+qy9F9sQid61gQIGkxwDAt85d/7Eau2/KzorFnZhgxArl7IiqJ67X6xjKkR3zur+Yp3V/mtVIehpPYIaAvPbcp2t4mQXl1I9J8yrQfEZOctLL1L4heDEVXnxvNihVLK6pivlVggp6SZCtjj9cduZGrYGsxsOCso1dqJQr7GCojfwvuLOoG0OYwEGuWVTZppxWxi/q1QgeHFhGKA5QUXlz7pS71oqpjYsTeViuHnfvlqb5TVYZpQ1haw=="}`)
36
37 err := json.Unmarshal(sejson, &dejson)
38 fmt.Printf("%v\n", dejson)
39 if err != nil {
40 t.Fatalf("%s", err)
41 }
42 resp := GetPasswordResult{gophercloud.Result{Body: dejson}}
43
44 pwd, err := resp.ExtractPassword(nil)
45 th.AssertNoErr(t, err)
46 th.AssertEquals(t, "PP8EnwPO9DhEc8+O/6CKAkPF379mKsUsfFY6yyw0734XXvKsSdV9KbiHQ2hrBvzeZxtGMrlFaikVunCRizyLLWLMuOi4hoH+qy9F9sQid61gQIGkxwDAt85d/7Eau2/KzorFnZhgxArl7IiqJ67X6xjKkR3zur+Yp3V/mtVIehpPYIaAvPbcp2t4mQXl1I9J8yrQfEZOctLL1L4heDEVXnxvNihVLK6pivlVggp6SZCtjj9cduZGrYGsxsOCso1dqJQr7GCojfwvuLOoG0OYwEGuWVTZppxWxi/q1QgeHFhGKA5QUXlz7pS71oqpjYsTeViuHnfvlqb5TVYZpQ1haw==", pwd)
47}
48
49// Ok - return decrypted password when private key is given.
50// Decrytion can be verified by:
51// echo "<enc_pwd>" | base64 -D | openssl rsautl -decrypt -inkey <privateKey.pem>
52func TestExtractPassword_decrypted_pwd(t *testing.T) {
53
54 privateKey, err := ssh.ParseRawPrivateKey([]byte(`
55-----BEGIN RSA PRIVATE KEY-----
56MIIEpQIBAAKCAQEAo1ODZgwMVdTJYim9UYuYhowoPMhGEuV5IRZjcJ315r7RBSC+
57yEiBb1V+jhf+P8fzAyU35lkBzZGDr7E3jxSesbOuYT8cItQS4ErUnI1LGuqvMxwv
58X3GMyE/HmOcaiODF1XZN3Ur5pMJdVknnmczgUsW0hT98Udrh3MQn9WSuh/6LRy6+
59x1QsKHOCLFPnkhWa3LKyxmpQq/Gvhz+6NLe+gt8MFullA5mKQxBJ/K6laVHeaMlw
60JG3GCX0EZhRlvzoV8koIBKZtbKFolFr8ZtxBm3R5LvnyrtOvp22sa+xeItUT5kG1
61ZnbGNdK87oYW+VigEUfzT/+8R1i6E2QIXoeZiQIDAQABAoIBAQCVZ70IqbbTAW8j
62RAlyQh/J3Qal65LmkFJJKUDX8TfT1/Q/G6BKeMEmxm+Zrmsfj1pHI1HKftt+YEG1
63g4jOc09kQXkgbmnfll6aHPn3J+1vd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zbDyKQO23VJysFD46c
80KSde3I0ybDz7iS2EtceKB7m4C0slYd+oBkm4efuF00rCOKDwpFq45m0=
81-----END RSA PRIVATE KEY-----
82`))
83 if err != nil {
84 t.Fatalf("Error parsing private key: %s\n", err)
85 }
86
87 var dejson interface{}
88 sejson := []byte(`{"password":"PP8EnwPO9DhEc8+O/6CKAkPF379mKsUsfFY6yyw0734XXvKsSdV9KbiHQ2hrBvzeZxtGMrlFaikVunCRizyLLWLMuOi4hoH+qy9F9sQid61gQIGkxwDAt85d/7Eau2/KzorFnZhgxArl7IiqJ67X6xjKkR3zur+Yp3V/mtVIehpPYIaAvPbcp2t4mQXl1I9J8yrQfEZOctLL1L4heDEVXnxvNihVLK6pivlVggp6SZCtjj9cduZGrYGsxsOCso1dqJQr7GCojfwvuLOoG0OYwEGuWVTZppxWxi/q1QgeHFhGKA5QUXlz7pS71oqpjYsTeViuHnfvlqb5TVYZpQ1haw=="}`)
89
90 err = json.Unmarshal(sejson, &dejson)
91 fmt.Printf("%v\n", dejson)
92 if err != nil {
93 t.Fatalf("%s", err)
94 }
95 resp := GetPasswordResult{gophercloud.Result{Body: dejson}}
96
97 pwd, err := resp.ExtractPassword(privateKey.(*rsa.PrivateKey))
98 th.AssertNoErr(t, err)
99 th.AssertEquals(t, "ruZKK0tqxRfYm5t7lSJq", pwd)
100}
jrperritt057373d2016-05-02 14:01:39 -0500101
102func TestListAddressesAllPages(t *testing.T) {
103 th.SetupHTTP()
104 defer th.TeardownHTTP()
105 HandleAddressListSuccessfully(t)
106
107 allPages, err := ListAddresses(client.ServiceClient(), "asdfasdfasdf").AllPages()
108 th.AssertNoErr(t, err)
109 _, err = ExtractAddresses(allPages)
110 th.AssertNoErr(t, err)
111}