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