Rickard von Essen | ab8cd7a | 2016-02-17 20:25:39 +0100 | [diff] [blame] | 1 | // +build fixtures |
| 2 | |
Rickard von Essen | c3d49b7 | 2016-02-16 20:59:18 +0100 | [diff] [blame] | 3 | package servers |
| 4 | |
| 5 | import ( |
| 6 | "crypto/rsa" |
| 7 | "encoding/json" |
| 8 | "fmt" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/rackspace/gophercloud" |
| 12 | th "github.com/rackspace/gophercloud/testhelper" |
jrperritt | 42b019f | 2016-05-02 14:02:55 -0500 | [diff] [blame] | 13 | "github.com/rackspace/gophercloud/testhelper/client" |
Rickard von Essen | c3d49b7 | 2016-02-16 20:59:18 +0100 | [diff] [blame] | 14 | "golang.org/x/crypto/ssh" |
| 15 | ) |
| 16 | |
| 17 | // Fail - No password in JSON. |
| 18 | func 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. |
| 32 | func 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> |
| 52 | func TestExtractPassword_decrypted_pwd(t *testing.T) { |
| 53 | |
| 54 | privateKey, err := ssh.ParseRawPrivateKey([]byte(` |
| 55 | -----BEGIN RSA PRIVATE KEY----- |
| 56 | MIIEpQIBAAKCAQEAo1ODZgwMVdTJYim9UYuYhowoPMhGEuV5IRZjcJ315r7RBSC+ |
| 57 | yEiBb1V+jhf+P8fzAyU35lkBzZGDr7E3jxSesbOuYT8cItQS4ErUnI1LGuqvMxwv |
| 58 | X3GMyE/HmOcaiODF1XZN3Ur5pMJdVknnmczgUsW0hT98Udrh3MQn9WSuh/6LRy6+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/4Bb |
| 74 | eOT3hZXOqLwhvE6XgI8Rzd95bClwgXekDoh80dqeKMdmta961BQGlKskaPiacmsF |
| 75 | G1yhZV70P9Mwwy8vpbLB4GUNAoGAbTwbjsWkNfa0qCF3J8NZoszjCvnBQfSW2J1R |
| 76 | 1+8ZKyNwt0yFi3Ajr3TibNiZzPzp1T9lj29FvfpJxA9Y+sXZvthxmcFxizix5GB1 |
| 77 | ha5yCNtA8VSOI7lJkAFDpL+j1lyYyjD6N9JE2KqEyKoh6J+8F7sXsqW7CqRRDfQX |
| 78 | mKNfey0CgYEAxcEoNoADN2hRl7qY9rbQfVvQb3RkoQkdHhl9gpLFCcV32IP8R4xg |
| 79 | 09NbQK5OmgcIuZhLVNzTmUHJbabEGeXqIFIV0DsqECAt3WzbDyKQO23VJysFD46c |
| 80 | KSde3I0ybDz7iS2EtceKB7m4C0slYd+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 | } |
jrperritt | 057373d | 2016-05-02 14:01:39 -0500 | [diff] [blame] | 101 | |
| 102 | func 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 | } |