Skip to content

Commit

Permalink
jwtk#18: extracted Android-specific logic out of the Base64Codec to i…
Browse files Browse the repository at this point in the history
…ts own class. Created a factory that can return either depending on environment. Ensured all code other than the lang package is at 100% test coverage.
  • Loading branch information
lhazlewood committed May 9, 2015
1 parent c18e4ee commit 66b30e2
Show file tree
Hide file tree
Showing 30 changed files with 330 additions and 127 deletions.
47 changes: 29 additions & 18 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@
<!-- Test Dependencies: Only required for testing when building. Not required by users at runtime: -->
<groovy.version>2.3.0-beta-2</groovy.version>
<logback.version>1.0.7</logback.version>
<easymock.version>3.1</easymock.version>
<testng.version>6.8</testng.version>
<easymock.version>3.3.1</easymock.version>
<junit.version>4.12</junit.version>
<powermock.version>1.6.2</powermock.version>
<failsafe.plugin.version>2.12.4</failsafe.plugin.version>

</properties>
Expand All @@ -83,14 +84,6 @@
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!--
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
<scope>runtime</scope>
</dependency>
-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand All @@ -107,8 +100,8 @@
</dependency>

<!-- Provided scope: only used in Android environments - not downloaded as a transitive dependency.
This dependency is only a stub of the actual implementation, which means we can't use it at test time
during TestNG tests. An Android environment is required to test for real. -->
This dependency is only a stub of the actual implementation, which means we can't use it at test time.
An Android environment is required to test for real. -->
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
Expand All @@ -129,12 +122,6 @@
<version>${logback.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
Expand All @@ -147,6 +134,30 @@
<version>${easymock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-easymock</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
30 changes: 30 additions & 0 deletions src/main/java/io/jsonwebtoken/impl/AndroidBase64Codec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2015 jsonwebtoken.io
*
* Licensed 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 io.jsonwebtoken.impl;

public class AndroidBase64Codec extends AbstractTextCodec {

@Override
public String encode(byte[] data) {
int flags = android.util.Base64.NO_PADDING | android.util.Base64.NO_WRAP;
return android.util.Base64.encodeToString(data, flags);
}

@Override
public byte[] decode(String encoded) {
return android.util.Base64.decode(encoded, android.util.Base64.DEFAULT);
}
}
33 changes: 0 additions & 33 deletions src/main/java/io/jsonwebtoken/impl/Base64Codec.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,12 @@

public class Base64Codec extends AbstractTextCodec {

private static final boolean ANDROID = isAndroid();

private static boolean isAndroid() {

String name = System.getProperty("java.vm.name");
if (name != null) {
String lcase = name.toLowerCase();
return lcase.contains("dalvik");
}

name = System.getProperty("java.vm.vendor");
if (name != null) {
String lcase = name.toLowerCase();
return lcase.contains("android");
}

return false;
}

public String encode(byte[] data) {

if (ANDROID) {
int flags = android.util.Base64.NO_PADDING | android.util.Base64.NO_WRAP;
return android.util.Base64.encodeToString(data, flags);
}

//else, assume standard JVM
return javax.xml.bind.DatatypeConverter.printBase64Binary(data);
}

@Override
public byte[] decode(String encoded) {

if (ANDROID) {
return android.util.Base64.decode(encoded, android.util.Base64.DEFAULT);
}

//else assume standard JVM:
return javax.xml.bind.DatatypeConverter.parseBase64Binary(encoded);
}

}
51 changes: 51 additions & 0 deletions src/main/java/io/jsonwebtoken/impl/DefaultTextCodecFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2015 jsonwebtoken.io
*
* Licensed 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 io.jsonwebtoken.impl;

public class DefaultTextCodecFactory implements TextCodecFactory {

protected String getSystemProperty(String key) {
return System.getProperty(key);
}

protected boolean isAndroid() {

String name = getSystemProperty("java.vm.name");
if (name != null) {
String lcase = name.toLowerCase();
return lcase.contains("dalvik");
}

name = getSystemProperty("java.vm.vendor");
if (name != null) {
String lcase = name.toLowerCase();
return lcase.contains("android");
}

return false;
}


@Override
public TextCodec getTextCodec() {

if (isAndroid()) {
return new AndroidBase64Codec();
}

return new Base64Codec();
}
}
2 changes: 1 addition & 1 deletion src/main/java/io/jsonwebtoken/impl/TextCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

public interface TextCodec {

public static final TextCodec BASE64 = new Base64Codec();
public static final TextCodec BASE64 = new DefaultTextCodecFactory().getTextCodec();
public static final TextCodec BASE64URL = new Base64UrlCodec();

String encode(String data);
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/io/jsonwebtoken/impl/TextCodecFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (C) 2015 jsonwebtoken.io
*
* Licensed 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 io.jsonwebtoken.impl;

public interface TextCodecFactory {

TextCodec getTextCodec();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
*/
package io.jsonwebtoken

import org.testng.annotations.Test

import static org.testng.Assert.*
import org.junit.Test
import static org.junit.Assert.*

class ExpiredJwtExceptionTest {

Expand Down
5 changes: 2 additions & 3 deletions src/test/groovy/io/jsonwebtoken/JwtHandlerAdapterTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
*/
package io.jsonwebtoken

import org.testng.annotations.Test

import static org.testng.Assert.*
import org.junit.Test
import static org.junit.Assert.*

class JwtHandlerAdapterTest {

Expand Down
7 changes: 3 additions & 4 deletions src/test/groovy/io/jsonwebtoken/JwtParserTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
package io.jsonwebtoken

import io.jsonwebtoken.impl.TextCodec
import org.testng.annotations.Test

import javax.crypto.spec.SecretKeySpec
import java.security.SecureRandom

import static org.testng.Assert.*

import org.junit.Test
import static org.junit.Assert.*

class JwtParserTest {

Expand Down Expand Up @@ -97,7 +96,7 @@ class JwtParserTest {
Jwts.parser().setSigningKey(randomKey()).parse(bad);
fail()
} catch (SignatureException se) {
assertEquals se.getMessage(), "Unsupported signature algorithm '$badAlgorithmName'"
assertEquals se.getMessage(), "Unsupported signature algorithm '$badAlgorithmName'".toString()
}
}

Expand Down
28 changes: 12 additions & 16 deletions src/test/groovy/io/jsonwebtoken/JwtsTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import io.jsonwebtoken.impl.TextCodec
import io.jsonwebtoken.impl.crypto.EllipticCurveProvider
import io.jsonwebtoken.impl.crypto.MacProvider
import io.jsonwebtoken.impl.crypto.RsaProvider
import org.testng.annotations.Test

import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
Expand All @@ -31,7 +30,8 @@ import java.security.KeyPair
import java.security.PrivateKey
import java.security.PublicKey

import static org.testng.Assert.*
import org.junit.Test
import static org.junit.Assert.*

class JwtsTest {

Expand Down Expand Up @@ -108,20 +108,20 @@ class JwtsTest {

def token = Jwts.parser().parse(jwt);

assertEquals token.body, claims
assert token.body == claims
}

@Test(expectedExceptions = IllegalArgumentException)
@Test(expected = IllegalArgumentException)
void testParseNull() {
Jwts.parser().parse(null)
}

@Test(expectedExceptions = IllegalArgumentException)
@Test(expected = IllegalArgumentException)
void testParseEmptyString() {
Jwts.parser().parse('')
}

@Test(expectedExceptions = IllegalArgumentException)
@Test(expected = IllegalArgumentException)
void testParseWhitespaceString() {
Jwts.parser().parse(' ')
}
Expand Down Expand Up @@ -537,10 +537,8 @@ class JwtsTest {

def token = Jwts.parser().setSigningKey(key).parse(jwt);

assertEquals token.header, [alg: alg.name()]

assertEquals token.body, claims

assert [alg: alg.name()] == token.header
assert token.body == claims
}

static void testHmac(SignatureAlgorithm alg) {
Expand All @@ -553,9 +551,8 @@ class JwtsTest {

def token = Jwts.parser().setSigningKey(key).parse(jwt)

assertEquals token.header, [alg: alg.name()]

assertEquals token.body, claims
assert token.header == [alg: alg.name()]
assert token.body == claims
}

static void testEC(SignatureAlgorithm alg, boolean verifyWithPrivateKey=false) {
Expand All @@ -575,9 +572,8 @@ class JwtsTest {

def token = Jwts.parser().setSigningKey(key).parse(jwt);

assertEquals token.header, [alg: alg.name()]

assertEquals token.body, claims
assert token.header == [alg: alg.name()]
assert token.body == claims
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
*/
package io.jsonwebtoken

import org.testng.annotations.Test

import static org.testng.Assert.*
import org.junit.Test
import static org.junit.Assert.*

class PrematureJwtExceptionTest {

Expand Down
Loading

0 comments on commit 66b30e2

Please sign in to comment.