PageRenderTime 64ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

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

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