PageRenderTime 33ms CodeModel.GetById 18ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/richardcloudsoft/legacy-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 */
 19package org.jclouds.rest;
 20
 21import static org.testng.Assert.assertEquals;
 22import static org.testng.Assert.assertFalse;
 23import static org.testng.Assert.assertTrue;
 24
 25import java.io.ByteArrayInputStream;
 26import java.io.IOException;
 27import java.io.InputStream;
 28import java.util.Map;
 29import java.util.concurrent.ConcurrentHashMap;
 30
 31import org.jclouds.crypto.PemsTest;
 32import org.jclouds.domain.Credentials;
 33import org.jclouds.domain.LoginCredentials;
 34import org.jclouds.io.CopyInputStreamInputSupplierMap;
 35import org.jclouds.json.Json;
 36import org.jclouds.json.config.GsonModule;
 37import org.jclouds.rest.config.CredentialStoreModule;
 38import org.jclouds.util.Strings2;
 39import org.testng.annotations.DataProvider;
 40import org.testng.annotations.Test;
 41
 42import com.google.common.io.InputSupplier;
 43import com.google.inject.Guice;
 44import com.google.inject.Injector;
 45import com.google.inject.Key;
 46import com.google.inject.TypeLiteral;
 47
 48/**
 49 * 
 50 * @author Adrian Cole
 51 */
 52@Test(groups = "unit", singleThreaded = true)
 53public class CredentialStoreModuleTest {
 54   Json json = createInjector().getInstance(Json.class);
 55
 56   @DataProvider(name = "credentials")
 57   public Object[][] createData() {
 58      return new Object[][] {
 59            { "root", PemsTest.PRIVATE_KEY },
 60            { "identity", "Base64==" },
 61            { "user@domain", "pa$sw@rd" },
 62            { "user", "unic₪de" }
 63      };
 64   }
 65
 66   @Test(dataProvider = "credentials")
 67   public void deleteObject(String identity, String credential) throws InterruptedException, IOException {
 68      Injector injector = createInjector();
 69      Map<String, InputStream> map = getMap(injector);
 70      check(map, getStore(injector), "i-20312", new Credentials(identity, credential));
 71   }
 72
 73   public void testProvidedMapWithValue() throws IOException {
 74      Map<String, InputStream> map = new CopyInputStreamInputSupplierMap(
 75            new ConcurrentHashMap<String, InputSupplier<InputStream>>());
 76
 77      map.put("test", new ByteArrayInputStream(json.toJson(new Credentials("user", "pass")).getBytes()));
 78      checkConsistent(map, getStore(createInjectorWithProvidedMap(map)), "test", new Credentials("user", "pass"));
 79      checkConsistent(map, getStore(createInjectorWithProvidedMap(map)), "test", new Credentials("user", "pass"));
 80      remove(map, getStore(createInjectorWithProvidedMap(map)), "test");
 81
 82   }
 83
 84   public void testProvidedConsistentAcrossRepeatedWrites() throws IOException {
 85      Map<String, InputStream> map = new CopyInputStreamInputSupplierMap(
 86            new ConcurrentHashMap<String, InputSupplier<InputStream>>());
 87
 88      Injector injector = createInjectorWithProvidedMap(map);
 89      assertEquals(injector.getInstance(Key.get(new TypeLiteral<Map<String, InputStream>>() {
 90      })), map);
 91      Map<String, Credentials> store = getStore(injector);
 92
 93      for (int i = 0; i < 10; i++)
 94         check(map, store, "test" + i, new Credentials("user" + i, "pass" + i));
 95
 96   }
 97
 98   public void testProvidedConsistentAcrossMultipleInjectors() throws IOException {
 99      Map<String, InputStream> map = new CopyInputStreamInputSupplierMap(
100            new ConcurrentHashMap<String, InputSupplier<InputStream>>());
101
102      put(map, getStore(createInjectorWithProvidedMap(map)), "test", new Credentials("user", "pass"));
103      checkConsistent(map, getStore(createInjectorWithProvidedMap(map)), "test", new Credentials("user", "pass"));
104      checkConsistent(map, getStore(createInjectorWithProvidedMap(map)), "test", new Credentials("user", "pass"));
105      remove(map, getStore(createInjectorWithProvidedMap(map)), "test");
106
107   }
108
109   public void testDefaultConsistentAcrossMultipleInjectors() throws IOException {
110      Map<String, InputStream> map = getMap(createInjector());
111
112      put(map, getStore(createInjector()), "test", new Credentials("user", "pass"));
113      checkConsistent(map, getStore(createInjector()), "test", new Credentials("user", "pass"));
114      checkConsistent(map, getStore(createInjector()), "test", new Credentials("user", "pass"));
115      remove(map, getStore(createInjector()), "test");
116
117   }
118
119   public void testLoginConsistentAcrossMultipleInjectorsAndLooksNice() throws IOException {
120      Map<String, InputStream> map = getMap(createInjector());
121      LoginCredentials creds = LoginCredentials.builder().user("user").password("pass").build();
122      put(map, getStore(createInjector()), "test", creds);
123      checkConsistent(map, getStore(createInjector()), "test", creds, "{\"user\":\"user\",\"password\":\"pass\"}");
124      checkConsistent(map, getStore(createInjector()), "test", creds, "{\"user\":\"user\",\"password\":\"pass\"}");
125      remove(map, getStore(createInjector()), "test");
126   }
127
128   public void testLoginConsistentAcrossMultipleInjectorsAndLooksNiceWithSudo() throws IOException {
129      Map<String, InputStream> map = getMap(createInjector());
130      LoginCredentials creds = LoginCredentials.builder().user("user").password("pass").authenticateSudo(true).build();
131      put(map, getStore(createInjector()), "test", creds);
132      checkConsistent(map, getStore(createInjector()), "test", creds,
133            "{\"user\":\"user\",\"password\":\"pass\",\"authenticateSudo\":true}");
134      checkConsistent(map, getStore(createInjector()), "test", creds,
135            "{\"user\":\"user\",\"password\":\"pass\",\"authenticateSudo\":true}");
136      remove(map, getStore(createInjector()), "test");
137   }
138
139   protected Map<String, Credentials> getStore(Injector injector) {
140      return injector.getInstance(Key.get(new TypeLiteral<Map<String, Credentials>>() {
141      }));
142   }
143
144   protected Map<String, InputStream> getMap(Injector injector) {
145      return injector.getInstance(Key.get(new TypeLiteral<Map<String, InputStream>>() {
146      }));
147   }
148
149   protected Injector createInjectorWithProvidedMap(Map<String, InputStream> map) {
150      return Guice.createInjector(new CredentialStoreModule(map), new GsonModule());
151   }
152
153   protected Injector createInjector() {
154      return Guice.createInjector(new CredentialStoreModule(), new GsonModule());
155   }
156
157   protected void check(Map<String, InputStream> map, Map<String, Credentials> store, String key, Credentials creds)
158         throws IOException {
159      put(map, store, key, creds);
160      checkConsistent(map, store, key, creds);
161      remove(map, store, key);
162   }
163
164   protected void remove(Map<String, InputStream> map, Map<String, Credentials> store, String key) {
165      store.remove(key);
166      assertEquals(store.size(), 0);
167      assertEquals(map.size(), 0);
168      assertEquals(store.get(key), null);
169      assertEquals(map.get(key), null);
170   }
171
172   protected void checkConsistent(Map<String, InputStream> map, Map<String, Credentials> store, String key,
173         Credentials creds) throws IOException {
174      checkConsistent(map, store, key, creds, json.toJson(creds));
175   }
176
177   protected void checkConsistent(Map<String, InputStream> map, Map<String, Credentials> store, String key,
178         Credentials creds, String expected) throws IOException {
179      assertEquals(store.size(), 1);
180      assertEquals(map.size(), 1);
181      assertTrue(store.containsKey(key));
182      //System.out.println("YYYYYY " + store.get(key));
183      //System.err.println("YYYYYY " + store.get(key));
184      assertTrue(store.containsValue(creds));
185      // checkRepeatedRead
186      assertEquals(store.get(key), creds);
187      assertEquals(store.get(key), creds);
188      // checkRepeatedRead
189      checkToJson(map, key, expected);
190      checkToJson(map, key, expected);
191   }
192
193   protected void checkToJson(Map<String, InputStream> map, String key, String expected) throws IOException {
194      assertEquals(Strings2.toStringAndClose(map.get(key)), expected);
195   }
196
197   protected void put(Map<String, InputStream> map, Map<String, Credentials> store, String key, Credentials creds) {
198      assertEquals(store.size(), 0);
199      assertEquals(map.size(), 0);
200      assertFalse(store.containsKey(key));
201      assertFalse(store.containsValue(creds));
202      store.put(key, creds);
203      //System.err.printf("XXXXXXXXXX\n\nStore has %n: %s\n\nXXXXXXXXXX\n", store.size(), Joiner.on(", ").withKeyValueSeparator("=").useForNull("<<EMPTY>>").join(store));
204      //System.out.printf("XXXXXXXXXX\n\nStore has %n: %s\n\nXXXXXXXXXX\n", store.size(), Joiner.on(", ").withKeyValueSeparator("=").useForNull("<<EMPTY>>").join(store));
205   }
206}