Added: 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/BypassAuthOnRequestMethodTest.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/BypassAuthOnRequestMethodTest.java?rev=1725214&view=auto
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/BypassAuthOnRequestMethodTest.java
 (added)
+++ 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/BypassAuthOnRequestMethodTest.java
 Mon Jan 18 10:18:15 2016
@@ -0,0 +1,107 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.james.jmap;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import javax.servlet.FilterChain;
+import javax.servlet.http.HttpServletRequest;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class BypassAuthOnRequestMethodTest {
+
+    private HttpServletRequest mockedRequest;
+    private AuthenticationFilter nestedFilter;
+    private BypassAuthOnRequestMethod sut;
+    private FilterChain filterChain;
+
+    @Before
+    public void setup() throws Exception {
+        mockedRequest = mock(HttpServletRequest.class);
+        nestedFilter = mock(AuthenticationFilter.class);
+        sut = 
BypassAuthOnRequestMethod.bypass(nestedFilter).on("POST").and("OPTIONS").only();
+        filterChain = mock(FilterChain.class);
+    }
+
+    @Test
+    public void filterShouldCallNestedFilterOnGet() throws Exception {
+        when(mockedRequest.getMethod())
+                .thenReturn("GET");
+
+        sut.doFilter(mockedRequest, null, filterChain);
+
+        verify(nestedFilter).doFilter(mockedRequest, null, filterChain);
+    }
+
+    @Test
+    public void filterShouldNotCallDirectlyChainOnGet() throws Exception {
+        when(mockedRequest.getMethod())
+                .thenReturn("GET");
+
+        sut.doFilter(mockedRequest, null, filterChain);
+
+        verify(filterChain, never()).doFilter(mockedRequest, null);
+    }
+
+    @Test
+    public void filterShouldNotCallNestedFilterOnPost() throws Exception {
+        when(mockedRequest.getMethod())
+                .thenReturn("POST");
+
+        sut.doFilter(mockedRequest, null, filterChain);
+
+        verify(nestedFilter, never()).doFilter(mockedRequest, null, 
filterChain);
+    }
+
+    @Test
+    public void filterShouldCallChainOnPost() throws Exception {
+        when(mockedRequest.getMethod())
+                .thenReturn("POST");
+
+        sut.doFilter(mockedRequest, null, filterChain);
+
+        verify(filterChain).doFilter(mockedRequest, null);
+    }
+
+    @Test
+    public void filterShouldNotCallNestedFilterOnOptions() throws Exception {
+        when(mockedRequest.getMethod())
+                .thenReturn("OPTIONS");
+
+        sut.doFilter(mockedRequest, null, filterChain);
+
+        verify(nestedFilter, never()).doFilter(mockedRequest, null, 
filterChain);
+    }
+
+    @Test
+    public void filterShouldCallChainOnOptions() throws Exception {
+        when(mockedRequest.getMethod())
+                .thenReturn("OPTIONS");
+
+        sut.doFilter(mockedRequest, null, filterChain);
+
+        verify(filterChain).doFilter(mockedRequest, null);
+    }
+
+}

Added: 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/JWTAuthenticationStrategyTest.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/JWTAuthenticationStrategyTest.java?rev=1725214&view=auto
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/JWTAuthenticationStrategyTest.java
 (added)
+++ 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/JWTAuthenticationStrategyTest.java
 Mon Jan 18 10:18:15 2016
@@ -0,0 +1,151 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.james.jmap;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.stream.Stream;
+
+import org.apache.james.jmap.crypto.JwtTokenVerifier;
+import org.apache.james.jmap.exceptions.MailboxSessionCreationException;
+import org.apache.james.jmap.exceptions.NoValidAuthHeaderException;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.Logger;
+
+
+public class JWTAuthenticationStrategyTest {
+
+    private JWTAuthenticationStrategy testee;
+    private MailboxManager mockedMailboxManager;
+    private JwtTokenVerifier stubTokenVerifier;
+
+    @Before
+    public void setup() {
+        mockedMailboxManager = mock(MailboxManager.class);
+
+        stubTokenVerifier = mock(JwtTokenVerifier.class);
+
+        testee = new JWTAuthenticationStrategy(stubTokenVerifier, 
mockedMailboxManager);
+    }
+
+
+    @Test
+    public void createMailboxSessionShouldThrowWhenAuthHeaderIsEmpty() throws 
Exception {
+        assertThatThrownBy(() -> testee.createMailboxSession(Stream.empty()))
+            .isExactlyInstanceOf(NoValidAuthHeaderException.class);
+    }
+
+    @Test
+    public void createMailboxSessionShouldReturnEmptyWhenAuthHeaderIsInvalid() 
throws Exception {
+        assertThatThrownBy(() -> testee.createMailboxSession(Stream.of("bad")))
+            .isExactlyInstanceOf(NoValidAuthHeaderException.class);
+    }
+
+    @Test
+    public void 
createMailboxSessionShouldThrowWhenMailboxExceptionHasOccurred() throws 
Exception {
+        String username = "username";
+        String validAuthHeader = "valid";
+        String fakeAuthHeaderWithPrefix = 
JWTAuthenticationStrategy.AUTHORIZATION_HEADER_PREFIX + validAuthHeader;
+
+        when(stubTokenVerifier.verify(validAuthHeader)).thenReturn(true);
+        
when(stubTokenVerifier.extractLogin(validAuthHeader)).thenReturn(username);
+        when(mockedMailboxManager.createSystemSession(eq(username), 
any(Logger.class)))
+                .thenThrow(new MailboxException());
+
+        assertThatThrownBy(() -> 
testee.createMailboxSession(Stream.of(fakeAuthHeaderWithPrefix)))
+                .isExactlyInstanceOf(MailboxSessionCreationException.class);
+    }
+
+    @Test
+    public void createMailboxSessionShouldReturnWhenAuthHeadersAreValid() 
throws Exception {
+        String username = "123456789";
+        String validAuthHeader = "valid";
+        String fakeAuthHeaderWithPrefix = 
JWTAuthenticationStrategy.AUTHORIZATION_HEADER_PREFIX + validAuthHeader;
+        MailboxSession fakeMailboxSession = mock(MailboxSession.class);
+
+        when(stubTokenVerifier.verify(validAuthHeader)).thenReturn(true);
+        
when(stubTokenVerifier.extractLogin(validAuthHeader)).thenReturn(username);
+        when(mockedMailboxManager.createSystemSession(eq(username), 
any(Logger.class)))
+                .thenReturn(fakeMailboxSession);
+
+        MailboxSession result = 
testee.createMailboxSession(Stream.of(fakeAuthHeaderWithPrefix));
+        assertThat(result).isEqualTo(fakeMailboxSession);
+    }
+
+    @Test
+    public void 
checkAuthorizationHeaderShouldReturnFalsewWhenAuthHeaderIsEmpty() {
+        assertThat(testee.checkAuthorizationHeader(Stream.empty())).isFalse();
+    }
+
+    @Test
+    public void 
checkAuthorizationHeaderShouldReturnFalseWhenAuthHeaderIsInvalid() {
+        String wrongAuthHeader = "invalid";
+        String fakeAuthHeaderWithPrefix = 
JWTAuthenticationStrategy.AUTHORIZATION_HEADER_PREFIX + wrongAuthHeader;
+
+        when(stubTokenVerifier.verify(wrongAuthHeader)).thenReturn(false);
+
+        
assertThat(testee.checkAuthorizationHeader(Stream.of(fakeAuthHeaderWithPrefix))).isFalse();
+    }
+
+    @Test
+    public void 
checkAuthorizationHeaderShouldReturnFalseWhenAuthHeadersAreInvalid() {
+        String wrongAuthHeader = "invalid";
+        String invalidAuthHeader = "INVALID";
+
+        when(stubTokenVerifier.verify(wrongAuthHeader)).thenReturn(false);
+        when(stubTokenVerifier.verify(invalidAuthHeader)).thenReturn(false);
+
+        Stream<String> authHeadersStream = Stream.of(wrongAuthHeader, 
invalidAuthHeader)
+                .map(h -> 
JWTAuthenticationStrategy.AUTHORIZATION_HEADER_PREFIX + h);
+        
assertThat(testee.checkAuthorizationHeader(authHeadersStream)).isFalse();
+    }
+
+    @Test
+    public void 
checkAuthorizationHeaderShouldReturnTrueWhenAuthHeaderIsValid() {
+        String validAuthHeader = "valid";
+        String validAuthHeaderWithPrefix = 
JWTAuthenticationStrategy.AUTHORIZATION_HEADER_PREFIX + validAuthHeader;
+
+        when(stubTokenVerifier.verify(validAuthHeader)).thenReturn(true);
+
+        
assertThat(testee.checkAuthorizationHeader(Stream.of(validAuthHeaderWithPrefix))).isTrue();
+    }
+
+    @Test
+    public void 
checkAuthorizationHeaderShouldReturnTrueWhenOneAuthHeaderIsValid() {
+        String dummyAuthHeader = "invalid";
+        String validAuthHeader = "correct";
+
+        when(stubTokenVerifier.verify(dummyAuthHeader)).thenReturn(false);
+        when(stubTokenVerifier.verify(validAuthHeader)).thenReturn(true);
+
+        Stream<String> authHeadersStream = Stream.of(dummyAuthHeader, 
validAuthHeader)
+                .map(h -> 
JWTAuthenticationStrategy.AUTHORIZATION_HEADER_PREFIX + h);
+        
assertThat(testee.checkAuthorizationHeader(authHeadersStream)).isTrue();
+    }
+
+}
\ No newline at end of file

Added: 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/JwtTokenVerifierTest.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/JwtTokenVerifierTest.java?rev=1725214&view=auto
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/JwtTokenVerifierTest.java
 (added)
+++ 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/JwtTokenVerifierTest.java
 Mon Jan 18 10:18:15 2016
@@ -0,0 +1,96 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.james.jmap.crypto;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.security.Security;
+import java.util.Optional;
+
+import org.apache.james.jmap.JMAPConfiguration;
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import io.jsonwebtoken.SignatureException;
+
+public class JwtTokenVerifierTest {
+
+    private static final String PUBLIC_PEM_KEY = "-----BEGIN PUBLIC 
KEY-----\n" +
+            
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtlChO/nlVP27MpdkG0Bh\n" +
+            
"16XrMRf6M4NeyGa7j5+1UKm42IKUf3lM28oe82MqIIRyvskPc11NuzSor8HmvH8H\n" +
+            
"lhDs5DyJtx2qp35AT0zCqfwlaDnlDc/QDlZv1CoRZGpQk1Inyh6SbZwYpxxwh0fi\n" +
+            
"+d/4RpE3LBVo8wgOaXPylOlHxsDizfkL8QwXItyakBfMO6jWQRrj7/9WDhGf4Hi+\n" +
+            
"GQur1tPGZDl9mvCoRHjFrD5M/yypIPlfMGWFVEvV5jClNMLAQ9bYFuOc7H1fEWw6\n" +
+            
"U1LZUUbJW9/CH45YXz82CYqkrfbnQxqRb2iVbVjs/sHopHd1NTiCfUtwvcYJiBVj\n" +
+            "kwIDAQAB\n" +
+            "-----END PUBLIC KEY-----";
+    private static final String VALID_TOKEN = 
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIn0.T04BTk"
 +
+            
"LXkJj24coSZkK13RfG25lpvmSl2MJ7N10KpBk9_-95EGYZdog-BDAn3PJzqVw52z-Bwjh4VOj1-j7cURu0cT4jXehhUrlCxS4n7QHZD"
 +
+            
"N_bsEYGu7KzjWTpTsUiHe-rN7izXVFxDGG1TGwlmBCBnPW-EFCf9ylUsJi0r2BKNdaaPRfMIrHptH1zJBkkUziWpBN1RNLjmvlAUf49"
 +
+            
"t1Tbv21ZqYM5Ht2vrhJWczFbuC-TD-8zJkXhjTmA1GVgomIX5dx1cH-dZX1wANNmshUJGHgepWlPU-5VIYxPEhb219RMLJIELMY2qN"
 +
+            "OR8Q31ydinyqzXvCSzVJOf6T60-w";
+
+    private JwtTokenVerifier sut;
+
+    @BeforeClass
+    public static void init() {
+        Security.addProvider(new BouncyCastleProvider());
+    }
+
+    @Before
+    public void setup() {
+        PublicKeyProvider pubKeyProvider = new 
PublicKeyProvider(getJWTConfiguration(), new PublicKeyReader());
+        sut = new JwtTokenVerifier(pubKeyProvider);
+    }
+
+    private JMAPConfiguration getJWTConfiguration() {
+
+        return JMAPConfiguration.builder()
+                .keystore(".")
+                .secret(".")
+                .jwtPublicKeyPem(Optional.ofNullable(PUBLIC_PEM_KEY))
+                .build();
+    }
+
+    @Test
+    public void shouldReturnTrueOnValidSignature() {
+
+        assertThat(sut.verify(VALID_TOKEN)).isTrue();
+    }
+
+    @Test
+    public void shouldThrowOnMismatchingSigningKey() {
+        String token = 
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIn0.Pd6t82"
 +
+                
"tPL3EZdkeYxw_DV2KimE1U2FvuLHmfR_mimJ5US3JFU4J2Gd94O7rwpSTGN1B9h-_lsTebo4ua4xHsTtmczZ9xa8a_kWKaSkqFjNFa"
 +
+                
"Fp6zcoD6ivCu03SlRqsQzSRHXo6TKbnqOt9D6Y2rNa3C4igSwoS0jUE4BgpXbc0";
+
+        assertThatThrownBy(() -> sut.verify(token))
+            .isInstanceOf(SignatureException.class);
+    }
+
+    @Test
+    public void shouldReturnUserLoginFromValidToken() {
+
+        assertThat(sut.extractLogin(VALID_TOKEN)).isEqualTo("1234567890");
+    }
+
+}

Added: 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/PublicKeyProviderTest.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/PublicKeyProviderTest.java?rev=1725214&view=auto
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/PublicKeyProviderTest.java
 (added)
+++ 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/PublicKeyProviderTest.java
 Mon Jan 18 10:18:15 2016
@@ -0,0 +1,74 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.james.jmap.crypto;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.security.Security;
+import java.security.interfaces.RSAPublicKey;
+import java.util.Optional;
+
+import org.apache.james.jmap.JMAPConfiguration;
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class PublicKeyProviderTest {
+
+    private static final String PUBLIC_PEM_KEY = "-----BEGIN PUBLIC 
KEY-----\n" +
+            
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtlChO/nlVP27MpdkG0Bh\n" +
+            
"16XrMRf6M4NeyGa7j5+1UKm42IKUf3lM28oe82MqIIRyvskPc11NuzSor8HmvH8H\n" +
+            
"lhDs5DyJtx2qp35AT0zCqfwlaDnlDc/QDlZv1CoRZGpQk1Inyh6SbZwYpxxwh0fi\n" +
+            
"+d/4RpE3LBVo8wgOaXPylOlHxsDizfkL8QwXItyakBfMO6jWQRrj7/9WDhGf4Hi+\n" +
+            
"GQur1tPGZDl9mvCoRHjFrD5M/yypIPlfMGWFVEvV5jClNMLAQ9bYFuOc7H1fEWw6\n" +
+            
"U1LZUUbJW9/CH45YXz82CYqkrfbnQxqRb2iVbVjs/sHopHd1NTiCfUtwvcYJiBVj\n" +
+            "kwIDAQAB\n" +
+            "-----END PUBLIC KEY-----";
+
+    @BeforeClass
+    public static void init() {
+        Security.addProvider(new BouncyCastleProvider());
+    }
+
+    @Test
+    public void getShouldNotThrowWhenPEMKeyProvided() {
+
+        JMAPConfiguration configWithPEMKey = JMAPConfiguration.builder()
+                .jwtPublicKeyPem(Optional.ofNullable(PUBLIC_PEM_KEY))
+                .keystore(".").secret(".")
+                .build();
+
+        PublicKeyProvider sut = new PublicKeyProvider(configWithPEMKey, new 
PublicKeyReader());
+
+        assertThat(sut.get()).isInstanceOf(RSAPublicKey.class);
+    }
+
+    @Test
+    public void getShouldThrowWhenPEMKeyNotProvided() {
+        JMAPConfiguration configWithPEMKey = JMAPConfiguration.builder()
+                .jwtPublicKeyPem(Optional.ofNullable(""))
+                .keystore(" ").secret(" ")
+                .build();
+
+        PublicKeyProvider sut = new PublicKeyProvider(configWithPEMKey, new 
PublicKeyReader());
+
+        assertThatThrownBy(() -> 
sut.get()).isExactlyInstanceOf(MissingOrInvalidKeyException.class);
+    }
+}
\ No newline at end of file

Added: 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/PublicKeyReaderTest.java
URL: 
http://svn.apache.org/viewvc/james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/PublicKeyReaderTest.java?rev=1725214&view=auto
==============================================================================
--- 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/PublicKeyReaderTest.java
 (added)
+++ 
james/project/trunk/server/protocols/jmap/src/test/java/org/apache/james/jmap/crypto/PublicKeyReaderTest.java
 Mon Jan 18 10:18:15 2016
@@ -0,0 +1,62 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james.jmap.crypto;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.security.Security;
+import java.util.Optional;
+
+import org.bouncycastle.jce.provider.BouncyCastleProvider;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class PublicKeyReaderTest {
+
+    private static final String PUBLIC_PEM_KEY = "-----BEGIN PUBLIC 
KEY-----\n" +
+            
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtlChO/nlVP27MpdkG0Bh\n" +
+            
"16XrMRf6M4NeyGa7j5+1UKm42IKUf3lM28oe82MqIIRyvskPc11NuzSor8HmvH8H\n" +
+            
"lhDs5DyJtx2qp35AT0zCqfwlaDnlDc/QDlZv1CoRZGpQk1Inyh6SbZwYpxxwh0fi\n" +
+            
"+d/4RpE3LBVo8wgOaXPylOlHxsDizfkL8QwXItyakBfMO6jWQRrj7/9WDhGf4Hi+\n" +
+            
"GQur1tPGZDl9mvCoRHjFrD5M/yypIPlfMGWFVEvV5jClNMLAQ9bYFuOc7H1fEWw6\n" +
+            
"U1LZUUbJW9/CH45YXz82CYqkrfbnQxqRb2iVbVjs/sHopHd1NTiCfUtwvcYJiBVj\n" +
+            "kwIDAQAB\n" +
+            "-----END PUBLIC KEY-----";
+
+    @BeforeClass
+    public static void init() {
+        Security.addProvider(new BouncyCastleProvider());
+    }
+
+    @Test
+    public void fromPEMShouldReturnEmptyWhenEmptyProvided() {
+        assertThat(new PublicKeyReader().fromPEM(Optional.empty())).isEmpty();
+    }
+
+    @Test
+    public void fromPEMShouldReturnEmptyWhenInvalidPEMKey() {
+        assertThat(new 
PublicKeyReader().fromPEM(Optional.of("blabla"))).isEmpty();
+    }
+
+    @Test
+    public void fromPEMShouldReturnRSAPublicKeyWhenValidPEMKey() {
+        assertThat(new 
PublicKeyReader().fromPEM(Optional.of(PUBLIC_PEM_KEY))).isPresent();
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to