public BlobCrypterTest() { crypter = new BasicBlobCrypter("0123456789abcdef".getBytes(),HMACType.HMACSHA1); timeSource = new FakeTimeSource(); crypter.timeSource = timeSource; }
@Test(expected=BlobExpiredException.class) public void testExpired() throws Exception { BlobCrypterSecurityToken t = new BlobCrypterSecurityToken(crypter, CONTAINER, DOMAIN); String token = t.encrypt(); // one hour plus clock skew timeSource.incrementSeconds(3600 + 181); String[] fields = StringUtils.split(token, ':'); // expect an exception BlobCrypterSecurityToken.decrypt(crypter, CONTAINER, DOMAIN, fields[1], "active"); } }
@Before public void setUp() { loader = new TestFeatureResourceLoader(); timeSource = new FakeTimeSource(); timeSource.setCurrentTimeMillis(0); loader.setTimeSource(timeSource); }
@Test public void testTimeStamping() throws Exception { long start = 1201917724000L; long skew = 180000; int maxAge = 300; // 5 minutes int realAge = 600; // 10 minutes try { timeSource.setCurrentTimeMillis(start); Map<String, String> in = ImmutableMap.of("a","b"); String blob = crypter.wrap(in); timeSource.incrementSeconds(realAge); crypter.unwrap(blob, maxAge); fail("Blob should have expired"); } catch (BlobExpiredException e) { assertEquals(start-skew, e.minDate.getTime()); assertEquals(start+realAge*1000L, e.used.getTime()); assertEquals(start+skew+maxAge*1000L, e.maxDate.getTime()); } }
@Test public void testNegativeCaching() { assertTrue("Bad HTTP responses must be cacheable!", HttpResponse.error().getCacheExpiration() > timeSource.currentTimeMillis()); assertTrue("Bad HTTP responses must be cacheable!", HttpResponse.notFound().getCacheExpiration() > timeSource.currentTimeMillis()); assertTrue("Bad HTTP responses must be cacheable!", HttpResponse.timeout().getCacheExpiration() > timeSource.currentTimeMillis()); long ttl = HttpResponse.error().getCacheTtl(); assertTrue(ttl <= HttpResponse.DEFAULT_TTL && ttl > 0); }
@Test public void testGeneralCacheExpiration() { SoftExpiringCache<String, String> expiringCache = makeSoftExpiringCache(); String key = "key1", val = "val1"; expiringCache.addElement(key, val, 240 * 1000); // Time is still 0: should be in the cache. assertEquals(val, expiringCache.getElement(key).obj); assertFalse(expiringCache.getElement(key).isExpired); // Time = 300 seconds: out of cache. timeSource.setCurrentTimeMillis(300 * 1000); assertEquals(val, expiringCache.getElement(key).obj); assertTrue(expiringCache.getElement(key).isExpired); }
@Test public void testTimeStamping() throws Exception { long start = 1201917724000L; long skew = 180000; int maxAge = 300; // 5 minutes int realAge = 600; // 10 minutes try { timeSource.setCurrentTimeMillis(start); Map<String, String> in = ImmutableMap.of("a","b"); String blob = crypter.wrap(in); timeSource.incrementSeconds(realAge); crypter.unwrap(blob, maxAge); fail("Blob should have expired"); } catch (BlobExpiredException e) { assertEquals(start-skew, e.minDate.getTime()); assertEquals(start+realAge*1000L, e.used.getTime()); assertEquals(start+skew+maxAge*1000L, e.maxDate.getTime()); } }
@Test public void testNegativeCaching() { assertTrue("Bad HTTP responses must be cacheable!", HttpResponse.error().getCacheExpiration() > timeSource.currentTimeMillis()); assertTrue("Bad HTTP responses must be cacheable!", HttpResponse.notFound().getCacheExpiration() > timeSource.currentTimeMillis()); assertTrue("Bad HTTP responses must be cacheable!", HttpResponse.timeout().getCacheExpiration() > timeSource.currentTimeMillis()); long ttl = HttpResponse.error().getCacheTtl(); assertTrue(ttl <= HttpResponse.DEFAULT_TTL && ttl > 0); }
@Test public void testGeneralCacheExpiration() { SoftExpiringCache<String, String> expiringCache = makeSoftExpiringCache(); String key = "key1", val = "val1"; expiringCache.addElement(key, val, 240 * 1000); // Time is still 0: should be in the cache. assertEquals(val, expiringCache.getElement(key).obj); assertFalse(expiringCache.getElement(key).isExpired); // Time = 300 seconds: out of cache. timeSource.setCurrentTimeMillis(300 * 1000); assertEquals(val, expiringCache.getElement(key).obj); assertTrue(expiringCache.getElement(key).isExpired); }
@Test(expected=BlobExpiredException.class) public void testExpired() throws Exception { BlobCrypterSecurityToken t = new BlobCrypterSecurityToken(crypter, CONTAINER, DOMAIN); String token = t.encrypt(); // one hour plus clock skew timeSource.incrementSeconds(3600 + 181); String[] fields = StringUtils.split(token, ':'); // expect an exception BlobCrypterSecurityToken.decrypt(crypter, CONTAINER, DOMAIN, fields[1], "active"); } }
public BlobCrypterTest() { crypter = new BasicBlobCrypter("0123456789abcdef".getBytes(),HMACType.HMACSHA1); timeSource = new FakeTimeSource(); crypter.timeSource = timeSource; }
@Before public void setUp() { fetcher = createMock(HttpFetcher.class); timeSource = new FakeTimeSource(); timeSource.setCurrentTimeMillis(0); loader = new TestFeatureResourceLoader(fetcher, timeSource, new DefaultFeatureFileSystem()); }
@Test public void testNegativeCaching() { assertTrue("Bad HTTP responses must be cacheable!", HttpResponse.error().getCacheExpiration() > timeSource.currentTimeMillis()); assertTrue("Bad HTTP responses must be cacheable!", HttpResponse.notFound().getCacheExpiration() > timeSource.currentTimeMillis()); assertTrue("Bad HTTP responses must be cacheable!", HttpResponse.timeout().getCacheExpiration() > timeSource.currentTimeMillis()); long ttl = HttpResponse.error().getCacheTtl(); assertTrue(ttl <= HttpResponse.DEFAULT_TTL && ttl > 0); }
@Test public void testGeneralCacheExpiration() { SoftExpiringCache<String, String> expiringCache = makeSoftExpiringCache(); String key = "key1", val = "val1"; expiringCache.addElement(key, val, 240 * 1000); // Time is still 0: should be in the cache. assertEquals(val, expiringCache.getElement(key).obj); assertFalse(expiringCache.getElement(key).isExpired); // Time = 300 seconds: out of cache. timeSource.setCurrentTimeMillis(300 * 1000); assertEquals(val, expiringCache.getElement(key).obj); assertTrue(expiringCache.getElement(key).isExpired); }
@Test public void testExpired() throws Exception { Map<String, String> values = new HashMap<String, String>(); values.put(Keys.APP_URL.getKey(), "http://www.example.com/gadget.xml"); values.put(Keys.MODULE_ID.getKey(), Long.toString(12345L, 10)); values.put(Keys.OWNER.getKey(), "owner"); values.put(Keys.VIEWER.getKey(), "viewer"); values.put(Keys.TRUSTED_JSON.getKey(), "trusted"); BlobCrypterSecurityToken token = new BlobCrypterSecurityToken("container", null, null, values); token.setTimeSource(timeSource); timeSource.incrementSeconds(-1 * (codec.getTokenTimeToLive("container") + 181)); // one hour plus clock skew String encrypted = codec.encodeToken(token); try { codec.createToken(ImmutableMap.of(SecurityTokenCodec.SECURITY_TOKEN_NAME, encrypted)); fail("should have expired"); } catch (SecurityTokenException e) { assertTrue(e.getMessage(), e.getMessage().contains("Blob expired")); } }
public BlobCrypterTest() { crypter = new BasicBlobCrypter("0123456789abcdef".getBytes()); timeSource = new FakeTimeSource(); crypter.timeSource = timeSource; }
@Before public void setUp() { fetcher = createMock(HttpFetcher.class); timeSource = new FakeTimeSource(); timeSource.setCurrentTimeMillis(0); loader = new TestFeatureResourceLoader(fetcher, timeSource, new DefaultFeatureFileSystem()); }
@Test public void testCacheExpirationForNegativeCacheExemptNoCacheControl() throws Exception { // Response should return a 401 or 403 (which are negative cache exempt) that don't have cache // control headers. They should still be cached for the negative ttl. HttpResponse response = new HttpResponseBuilder() .setHttpStatusCode(HttpResponse.SC_FORBIDDEN) .create(); assertTrue( "Response is cached for the negative TTL", response.getCacheExpiration() <= timeSource.currentTimeMillis() + response.getNegativeTtl()); } }
@Test public void testGeneralCacheExpiration() { SoftExpiringCache<String, String> expiringCache = makeSoftExpiringCache(); String key = "key1", val = "val1"; expiringCache.addElement(key, val, 240 * 1000); // Time is still 0: should be in the cache. assertEquals(val, expiringCache.getElement(key).obj); assertFalse(expiringCache.getElement(key).isExpired); // Time = 300 seconds: out of cache. timeSource.setCurrentTimeMillis(300 * 1000); assertEquals(val, expiringCache.getElement(key).obj); assertTrue(expiringCache.getElement(key).isExpired); }
@Test public void testExpired() throws Exception { Map<String, String> values = new HashMap<String, String>(); values.put(Keys.APP_URL.getKey(), "http://www.example.com/gadget.xml"); values.put(Keys.MODULE_ID.getKey(), Long.toString(12345L, 10)); values.put(Keys.OWNER.getKey(), "owner"); values.put(Keys.VIEWER.getKey(), "viewer"); values.put(Keys.TRUSTED_JSON.getKey(), "trusted"); BlobCrypterSecurityToken token = new BlobCrypterSecurityToken("container", null, null, values); token.setTimeSource(timeSource); timeSource.incrementSeconds(-1 * (codec.getTokenTimeToLive("container") + 181)); // one hour plus clock skew String encrypted = codec.encodeToken(token); try { codec.createToken(ImmutableMap.of(SecurityTokenCodec.SECURITY_TOKEN_NAME, encrypted)); fail("should have expired"); } catch (SecurityTokenException e) { assertTrue(e.getMessage(), e.getMessage().contains("Blob expired")); } }