PageRenderTime 348ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/test/java/org/jclouds/rest/CredentialStoreModuleTest.java

http://github.com/jclouds/jclouds
Java | 228 lines | 169 code | 37 blank | 22 comment | 1 complexity | 721b1d0ee98bca7ef45c358fb7395bb3 MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.jclouds.rest;
  18. import static org.testng.Assert.assertEquals;
  19. import static org.testng.Assert.assertFalse;
  20. import static org.testng.Assert.assertTrue;
  21. import java.io.IOException;
  22. import java.util.Map;
  23. import java.util.concurrent.ConcurrentHashMap;
  24. import org.jclouds.crypto.PemsTest;
  25. import org.jclouds.domain.Credentials;
  26. import org.jclouds.domain.LoginCredentials;
  27. import org.jclouds.json.Json;
  28. import org.jclouds.json.config.GsonModule;
  29. import org.jclouds.rest.config.CredentialStoreModule;
  30. import org.testng.annotations.DataProvider;
  31. import org.testng.annotations.Test;
  32. import com.google.common.base.Charsets;
  33. import com.google.common.base.Function;
  34. import com.google.common.io.ByteSource;
  35. import com.google.inject.Guice;
  36. import com.google.inject.Injector;
  37. import com.google.inject.Key;
  38. import com.google.inject.TypeLiteral;
  39. @Test(groups = "unit", singleThreaded = true)
  40. public class CredentialStoreModuleTest {
  41. Json json = createInjector().getInstance(Json.class);
  42. @DataProvider(name = "credentials")
  43. public Object[][] createData() {
  44. return new Object[][] {
  45. { "root", PemsTest.PRIVATE_KEY },
  46. { "identity", "Base64==" },
  47. { "user@domain", "pa$sw@rd" },
  48. { "user", "unic₪de" }
  49. };
  50. }
  51. @Test(dataProvider = "credentials")
  52. public void deleteObject(String identity, String credential) throws InterruptedException, IOException {
  53. Injector injector = createInjector();
  54. Map<String, ByteSource> map = getMap(injector);
  55. check(map, getStore(injector), "i-20312", new Credentials(identity, credential));
  56. }
  57. public void testProvidedMapWithValue() throws IOException {
  58. Map<String, ByteSource> map = new ConcurrentHashMap<String, ByteSource>();
  59. map.put("test", ByteSource.wrap(json.toJson(new Credentials("user", "pass")).getBytes()));
  60. checkConsistent(map, getStore(createInjectorWithProvidedMap(map)), "test", new Credentials("user", "pass"));
  61. checkConsistent(map, getStore(createInjectorWithProvidedMap(map)), "test", new Credentials("user", "pass"));
  62. remove(map, getStore(createInjectorWithProvidedMap(map)), "test");
  63. }
  64. public void testProvidedConsistentAcrossRepeatedWrites() throws IOException {
  65. Map<String, ByteSource> map = new ConcurrentHashMap<String, ByteSource>();
  66. Injector injector = createInjectorWithProvidedMap(map);
  67. assertEquals(injector.getInstance(Key.get(new TypeLiteral<Map<String, ByteSource>>() {
  68. })), map);
  69. Map<String, Credentials> store = getStore(injector);
  70. for (int i = 0; i < 10; i++)
  71. check(map, store, "test" + i, new Credentials("user" + i, "pass" + i));
  72. }
  73. public void testProvidedConsistentAcrossMultipleInjectors() throws IOException {
  74. Map<String, ByteSource> map = new ConcurrentHashMap<String, ByteSource>();
  75. put(map, getStore(createInjectorWithProvidedMap(map)), "test", new Credentials("user", "pass"));
  76. checkConsistent(map, getStore(createInjectorWithProvidedMap(map)), "test", new Credentials("user", "pass"));
  77. checkConsistent(map, getStore(createInjectorWithProvidedMap(map)), "test", new Credentials("user", "pass"));
  78. remove(map, getStore(createInjectorWithProvidedMap(map)), "test");
  79. }
  80. public void testDefaultDifferentAcrossMultipleInjectors() throws IOException {
  81. Injector injector = createInjector();
  82. Map<String, ByteSource> map = getMap(injector);
  83. put(map, getStore(injector), "test", new Credentials("user", "pass"));
  84. Map<String, Credentials> anotherStore = getStore(createInjector());
  85. assertEquals(anotherStore.size(), 0);
  86. assertFalse(anotherStore.containsKey("test"));
  87. }
  88. public void testLoginDifferentAcrossMultipleInjectorsAndLooksNice() throws IOException {
  89. Injector injector = createInjector();
  90. Map<String, ByteSource> map = getMap(injector);
  91. LoginCredentials creds = LoginCredentials.builder().user("user").password("pass").build();
  92. Map<String, Credentials> store = getStore(injector);
  93. put(map, store, "test", creds);
  94. checkConsistent(map, store, "test", creds, "{\"user\":\"user\",\"password\":\"pass\"}");
  95. checkConsistent(map, store, "test", creds, "{\"user\":\"user\",\"password\":\"pass\"}");
  96. remove(map, store, "test");
  97. }
  98. public void testLoginDifferentAcrossMultipleInjectorsAndLooksNiceWithSudo() throws IOException {
  99. Injector injector = createInjector();
  100. Map<String, ByteSource> map = getMap(injector);
  101. LoginCredentials creds = LoginCredentials.builder().user("user").password("pass").authenticateSudo(true).build();
  102. Map<String, Credentials> store = getStore(injector);
  103. put(map, store, "test", creds);
  104. checkConsistent(map, store, "test", creds,
  105. "{\"user\":\"user\",\"password\":\"pass\",\"authenticateSudo\":true}");
  106. checkConsistent(map, store, "test", creds,
  107. "{\"user\":\"user\",\"password\":\"pass\",\"authenticateSudo\":true}");
  108. remove(map, store, "test");
  109. }
  110. public void testCredentialsToByteSourceConversion() throws Exception {
  111. Function<Credentials, ByteSource> toBytesFunc = getCredentialsToByteStoreFunction(createInjector());
  112. Function<ByteSource, Credentials> fromBytesFunc = getByteStoreToCredentialsFunction(createInjector());
  113. LoginCredentials creds = LoginCredentials.builder().user("myuser").password("mypass").authenticateSudo(true).build();
  114. ByteSource bytes = toBytesFunc.apply(creds);
  115. LoginCredentials deserializedCreds = (LoginCredentials) fromBytesFunc.apply(bytes);
  116. String json = bytes.asCharSource(Charsets.UTF_8).read();
  117. assertEquals(json, "{\"user\":\"myuser\",\"password\":\"mypass\",\"authenticateSudo\":true}");
  118. assertEquals(deserializedCreds.identity, creds.identity);
  119. assertEquals(deserializedCreds.credential, creds.credential);
  120. assertEquals(deserializedCreds.getUser(), creds.getUser());
  121. assertEquals(deserializedCreds.getOptionalPassword(), creds.getOptionalPassword());
  122. assertEquals(deserializedCreds.getOptionalPrivateKey(), creds.getOptionalPrivateKey());
  123. assertEquals(deserializedCreds.shouldAuthenticateSudo(), creds.shouldAuthenticateSudo());
  124. }
  125. protected Map<String, Credentials> getStore(Injector injector) {
  126. return injector.getInstance(Key.get(new TypeLiteral<Map<String, Credentials>>() {
  127. }));
  128. }
  129. protected Map<String, ByteSource> getMap(Injector injector) {
  130. return injector.getInstance(Key.get(new TypeLiteral<Map<String, ByteSource>>() {
  131. }));
  132. }
  133. protected Function<ByteSource, Credentials> getByteStoreToCredentialsFunction(Injector injector) {
  134. return injector.getInstance(Key.get(new TypeLiteral<Function<ByteSource, Credentials>>() {
  135. }));
  136. }
  137. protected Function<Credentials, ByteSource> getCredentialsToByteStoreFunction(Injector injector) {
  138. return injector.getInstance(Key.get(new TypeLiteral<Function<Credentials, ByteSource>>() {
  139. }));
  140. }
  141. protected Injector createInjectorWithProvidedMap(Map<String, ByteSource> map) {
  142. return Guice.createInjector(new CredentialStoreModule(map), new GsonModule());
  143. }
  144. protected Injector createInjector() {
  145. return Guice.createInjector(new CredentialStoreModule(), new GsonModule());
  146. }
  147. protected void check(Map<String, ByteSource> map, Map<String, Credentials> store, String key, Credentials creds)
  148. throws IOException {
  149. put(map, store, key, creds);
  150. checkConsistent(map, store, key, creds);
  151. remove(map, store, key);
  152. }
  153. protected void remove(Map<String, ByteSource> map, Map<String, Credentials> store, String key) {
  154. store.remove(key);
  155. assertEquals(store.size(), 0);
  156. assertEquals(map.size(), 0);
  157. assertEquals(store.get(key), null);
  158. assertEquals(map.get(key), null);
  159. }
  160. protected void checkConsistent(Map<String, ByteSource> map, Map<String, Credentials> store, String key,
  161. Credentials creds) throws IOException {
  162. checkConsistent(map, store, key, creds, json.toJson(creds));
  163. }
  164. protected void checkConsistent(Map<String, ByteSource> map, Map<String, Credentials> store, String key,
  165. Credentials creds, String expected) throws IOException {
  166. assertEquals(store.size(), 1);
  167. assertEquals(map.size(), 1);
  168. assertTrue(store.containsKey(key));
  169. //System.out.println("YYYYYY " + store.get(key));
  170. //System.err.println("YYYYYY " + store.get(key));
  171. assertTrue(store.containsValue(creds));
  172. // checkRepeatedRead
  173. assertEquals(store.get(key), creds);
  174. assertEquals(store.get(key), creds);
  175. // checkRepeatedRead
  176. checkToJson(map, key, expected);
  177. checkToJson(map, key, expected);
  178. }
  179. protected void checkToJson(Map<String, ByteSource> map, String key, String expected) throws IOException {
  180. assertEquals(map.get(key).asCharSource(Charsets.UTF_8).read(), expected);
  181. }
  182. protected void put(Map<String, ByteSource> map, Map<String, Credentials> store, String key, Credentials creds) {
  183. assertEquals(store.size(), 0);
  184. assertEquals(map.size(), 0);
  185. assertFalse(store.containsKey(key));
  186. assertFalse(store.containsValue(creds));
  187. store.put(key, creds);
  188. //System.err.printf("XXXXXXXXXX\n\nStore has %n: %s\n\nXXXXXXXXXX\n", store.size(), Joiner.on(", ").withKeyValueSeparator("=").useForNull("<<EMPTY>>").join(store));
  189. //System.out.printf("XXXXXXXXXX\n\nStore has %n: %s\n\nXXXXXXXXXX\n", store.size(), Joiner.on(", ").withKeyValueSeparator("=").useForNull("<<EMPTY>>").join(store));
  190. }
  191. }