@Test public void shouldCreateAnAESCipherFileWithTheCipherIfNotFound() throws IOException, CryptoException { assertThat(desCipherFile.exists()).isFalse(); assertThat(aesCipherFile.exists()).isFalse(); GoCipher goCipher = new GoCipher(systemEnvironment); assertThat(desCipherFile.exists()).isFalse(); assertThat(aesCipherFile.exists()).isTrue(); String plainText = goCipher.decrypt(goCipher.encrypt("user-password!")); assertThat(plainText).isEqualTo("user-password!"); assertThat(desCipherFile.exists()).isFalse(); assertThat(aesCipherFile.exists()).isTrue(); }
@Test public void shouldDecryptText() throws CryptoException { String plainText = desEncrypter.decrypt("mvcX9yrQsM4iPgm1tDxN1A=="); assertThat(plainText).isEqualTo("user-password!"); }
@Test public void shouldDecryptEncryptedText() throws CryptoException { String decrypt = aesEncrypter.decrypt("AES:lzcCuNSe4vUx+CsWgN11Uw==:DelRFu6mCN7kC/2oYmeLRA=="); assertThat(decrypt).isEqualTo("p@ssw0rd"); }
@Test public void testPrintBinaryNegative() { assertThat(AclFormattingUtils.printBinary(0x80000000)).isEqualTo( "*..............................."); }
@Test public void gettersReturnExpectedValues() throws Exception { ObjectIdentity obj = new ObjectIdentityImpl(DOMAIN_CLASS, Long.valueOf(1)); assertThat(obj.getIdentifier()).isEqualTo(Long.valueOf(1)); assertThat(obj.getType()).isEqualTo(MockIdDomainObject.class.getName()); }
@Test public void shouldReEncryptUsingNewKey() throws DecoderException, CryptoException { String originalCipherText = "mvcX9yrQsM4iPgm1tDxN1A=="; String newCipherText = DESEncrypter.reEncryptUsingNewKey(decodeHex("269298bc31c44620"), decodeHex("02644c13cb892962"), originalCipherText); assertThat(originalCipherText).isNotEqualTo(newCipherText); DESCipherProvider newCipher = mock(DESCipherProvider.class); when(newCipher.getKey()).thenReturn(decodeHex("02644c13cb892962")); DESEncrypter newEncrypter = new DESEncrypter(newCipher); assertThat(newEncrypter.decrypt(newCipherText)).isEqualTo("user-password!"); } }
@Test public void shouldNotKillLeadingAndTrailingSpacesDuringEncryption() throws CryptoException { String plainText = " \tfoobar\nbaz\t"; String encrypt = aesEncrypter.encrypt(plainText); String decrypt = aesEncrypter.decrypt(encrypt); assertThat(decrypt).isEqualTo(plainText); }
@Test public void getAuthenticationWhenAuthenticationManagerBeanThenAuthenticates() throws Exception { UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("user", "password"); this.spring.register(AuthenticationConfiguration.class, ObjectPostProcessorConfiguration.class, AuthenticationManagerBeanConfig.class).autowire(); AuthenticationManager authentication = this.spring.getContext().getBean(AuthenticationConfiguration.class).getAuthenticationManager(); when(authentication.authenticate(token)).thenReturn(TestAuthentication.authenticatedUser()); assertThat(authentication.authenticate(token).getName()).isEqualTo(token.getName()); }
@Test public void getAuthenticationManagerWhenProtectedPasswordEncoderBeanThenUsed() throws Exception { this.spring.register(PasswordEncoderGlobalConfig.class).autowire(); AuthenticationManager manager = this.spring.getContext() .getBean(AuthenticationConfiguration.class).getAuthenticationManager(); Authentication auth = manager.authenticate(new UsernamePasswordAuthenticationToken("user", "password")); assertThat(auth.getName()).isEqualTo("user"); assertThat(auth.getAuthorities()).extracting(GrantedAuthority::getAuthority).containsOnly("ROLE_USER"); }
@Test public void getServiceUrlNullQuery() throws Exception { this.details = new DefaultServiceAuthenticationDetails(this.casServiceUrl, this.request, this.artifactPattern); assertThat(this.details.getServiceUrl()) .isEqualTo(UrlUtils.buildFullRequestUrl(this.request)); }
@Test public void getServiceUrlDoesNotUseHostHeaderExplicit() { this.casServiceUrl = "https://example.com/j_spring_security_cas"; this.request.setServerName("evil.com"); ServiceAuthenticationDetails details = loadServiceAuthenticationDetails( "defaultserviceauthenticationdetails-explicit.xml"); assertThat(details.getServiceUrl()) .isEqualTo("https://example.com/cas-sample/secure/"); }
@Test public void testDetectsMissingLoginFormUrl() throws Exception { CasAuthenticationEntryPoint ep = new CasAuthenticationEntryPoint(); ep.setServiceProperties(new ServiceProperties()); try { ep.afterPropertiesSet(); fail("Should have thrown IllegalArgumentException"); } catch (IllegalArgumentException expected) { assertThat(expected.getMessage()).isEqualTo("loginUrl must be specified"); } }
@Test public void shouldSetConfiguration_whenPluginIsProvided() { final HashMap<Object, Object> configAttrs = new HashMap<>(); configAttrs.put(FetchPluggableArtifactTask.ARTIFACT_ID, "installers"); configAttrs.put(FetchPluggableArtifactTask.CONFIGURATION, Collections.singletonMap("NAME", "gocd.zip")); configAttrs.put("pluginId", "cd.go.artifact.s3"); FetchPluggableArtifactTask task = new FetchPluggableArtifactTask(new CaseInsensitiveString("#{pipeline}"), new CaseInsensitiveString("#{stage}"), new CaseInsensitiveString("#{job}"), "#{artifactId}"); task.setFetchTaskAttributes(configAttrs); Assertions.assertThat(task.getArtifactId()).isEqualTo("installers"); Assertions.assertThat(task.getConfiguration()) .hasSize(1) .contains(new ConfigurationProperty(new ConfigurationKey("NAME"), new ConfigurationValue("gocd.zip"))); }
@Test public void getServiceUrlTicketOnlyParam() throws Exception { this.request.setQueryString("ticket=123"); this.details = new DefaultServiceAuthenticationDetails(this.casServiceUrl, this.request, this.artifactPattern); String serviceUrl = this.details.getServiceUrl(); this.request.setQueryString(null); assertThat(serviceUrl).isEqualTo(UrlUtils.buildFullRequestUrl(this.request)); }
@Test public void getServiceUrlTicketFirstMultiParam() throws Exception { this.request.setQueryString("ticket=123&other=value"); this.details = new DefaultServiceAuthenticationDetails(this.casServiceUrl, this.request, this.artifactPattern); String serviceUrl = this.details.getServiceUrl(); this.request.setQueryString("other=value"); assertThat(serviceUrl).isEqualTo(UrlUtils.buildFullRequestUrl(this.request)); }
@Test public void getServiceUrlTicketMiddleMultiParam() throws Exception { this.request.setQueryString("other=value&ticket=123&last=this"); this.details = new DefaultServiceAuthenticationDetails(this.casServiceUrl, this.request, this.artifactPattern); String serviceUrl = this.details.getServiceUrl(); this.request.setQueryString("other=value&last=this"); assertThat(serviceUrl).isEqualTo(UrlUtils.buildFullRequestUrl(this.request)); }
@Test public void simpDestMatchersAnyRole() { messages.simpDestMatchers("admin/**", "location/**").hasAnyRole("ADMIN", "ROOT") .anyMessage().denyAll(); assertThat(getAttribute()).isEqualTo("hasAnyRole('ROLE_ADMIN','ROLE_ROOT')"); }
@Test public void simpDestMatchersAuthority() { messages.simpDestMatchers("admin/**", "location/**").hasAuthority("ROLE_ADMIN") .anyMessage().fullyAuthenticated(); assertThat(getAttribute()).isEqualTo("hasAuthority('ROLE_ADMIN')"); }
@Test public void simpDestMatchersAnyAuthority() { messages.simpDestMatchers("admin/**", "location/**") .hasAnyAuthority("ROLE_ADMIN", "ROLE_ROOT").anyMessage().denyAll(); assertThat(getAttribute()).isEqualTo("hasAnyAuthority('ROLE_ADMIN','ROLE_ROOT')"); }
@Test public void simpDestMessageMatchersMatch() { messages.simpMessageDestMatchers("location/**").denyAll().anyMessage() .permitAll(); assertThat(getAttribute()).isEqualTo("denyAll"); }