Skip to content

Commit

Permalink
pass charset to String.getBytes() on tests (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
lbalmaceda authored and hzalaz committed Jan 4, 2017
1 parent 9148ca2 commit 24901e2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ public void shouldThrowOnSignWhenSignatureAlgorithmDoesNotExists() throws Except

ECKey key = mock(ECKey.class, withSettings().extraInterfaces(ECPrivateKey.class));
Algorithm algorithm = new ECDSAAlgorithm(crypto, "some-alg", "some-algorithm", 32, key);
algorithm.sign(ES256Header.getBytes());
algorithm.sign(ES256Header.getBytes(StandardCharsets.UTF_8));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void shouldGetStringBytes() throws Exception {
public void shouldPassHMAC256Verification() throws Exception {
String jwt = "eyJhbGciOiJIUzI1NiIsImN0eSI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.mZ0m_N1J4PgeqWmi903JuUoDRZDBPB7HwkS4nVyWH1M";
Algorithm algorithmString = Algorithm.HMAC256("secret");
Algorithm algorithmBytes = Algorithm.HMAC256("secret".getBytes());
Algorithm algorithmBytes = Algorithm.HMAC256("secret".getBytes(StandardCharsets.UTF_8));
AlgorithmUtils.verify(algorithmString, jwt);
AlgorithmUtils.verify(algorithmBytes, jwt);
}
Expand All @@ -57,15 +57,15 @@ public void shouldFailHMAC256VerificationWithInvalidSecretBytes() throws Excepti
exception.expect(SignatureVerificationException.class);
exception.expectMessage("The Token's Signature resulted invalid when verified using the Algorithm: HmacSHA256");
String jwt = "eyJhbGciOiJIUzI1NiIsImN0eSI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.mZ0m_N1J4PgeqWmi903JuUoDRZDBPB7HwkS4nVyWH1M";
Algorithm algorithm = Algorithm.HMAC256("not_real_secret".getBytes());
Algorithm algorithm = Algorithm.HMAC256("not_real_secret".getBytes(StandardCharsets.UTF_8));
AlgorithmUtils.verify(algorithm, jwt);
}

@Test
public void shouldPassHMAC384Verification() throws Exception {
String jwt = "eyJhbGciOiJIUzM4NCIsImN0eSI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.uztpK_wUMYJhrRv8SV-1LU4aPnwl-EM1q-wJnqgyb5DHoDteP6lN_gE1xnZJH5vw";
Algorithm algorithmString = Algorithm.HMAC384("secret");
Algorithm algorithmBytes = Algorithm.HMAC384("secret".getBytes());
Algorithm algorithmBytes = Algorithm.HMAC384("secret".getBytes(StandardCharsets.UTF_8));
AlgorithmUtils.verify(algorithmString, jwt);
AlgorithmUtils.verify(algorithmBytes, jwt);
}
Expand All @@ -84,15 +84,15 @@ public void shouldFailHMAC384VerificationWithInvalidSecretBytes() throws Excepti
exception.expect(SignatureVerificationException.class);
exception.expectMessage("The Token's Signature resulted invalid when verified using the Algorithm: HmacSHA384");
String jwt = "eyJhbGciOiJIUzM4NCIsImN0eSI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.uztpK_wUMYJhrRv8SV-1LU4aPnwl-EM1q-wJnqgyb5DHoDteP6lN_gE1xnZJH5vw";
Algorithm algorithm = Algorithm.HMAC384("not_real_secret".getBytes());
Algorithm algorithm = Algorithm.HMAC384("not_real_secret".getBytes(StandardCharsets.UTF_8));
AlgorithmUtils.verify(algorithm, jwt);
}

@Test
public void shouldPassHMAC512Verification() throws Exception {
String jwt = "eyJhbGciOiJIUzUxMiIsImN0eSI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.VUo2Z9SWDV-XcOc_Hr6Lff3vl7L9e5Vb8ThXpmGDFjHxe3Dr1ZBmUChYF-xVA7cAdX1P_D4ZCUcsv3IefpVaJw";
Algorithm algorithmString = Algorithm.HMAC512("secret");
Algorithm algorithmBytes = Algorithm.HMAC512("secret".getBytes());
Algorithm algorithmBytes = Algorithm.HMAC512("secret".getBytes(StandardCharsets.UTF_8));
AlgorithmUtils.verify(algorithmString, jwt);
AlgorithmUtils.verify(algorithmBytes, jwt);
}
Expand All @@ -111,7 +111,7 @@ public void shouldFailHMAC512VerificationWithInvalidSecretBytes() throws Excepti
exception.expect(SignatureVerificationException.class);
exception.expectMessage("The Token's Signature resulted invalid when verified using the Algorithm: HmacSHA512");
String jwt = "eyJhbGciOiJIUzUxMiIsImN0eSI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.VUo2Z9SWDV-XcOc_Hr6Lff3vl7L9e5Vb8ThXpmGDFjHxe3Dr1ZBmUChYF-xVA7cAdX1P_D4ZCUcsv3IefpVaJw";
Algorithm algorithm = Algorithm.HMAC512("not_real_secret".getBytes());
Algorithm algorithm = Algorithm.HMAC512("not_real_secret".getBytes(StandardCharsets.UTF_8));
AlgorithmUtils.verify(algorithm, jwt);
}

Expand All @@ -125,7 +125,7 @@ public void shouldThrowOnVerifyWhenSignatureAlgorithmDoesNotExists() throws Exce
when(crypto.verifySignatureFor(anyString(), any(byte[].class), any(byte[].class), any(byte[].class)))
.thenThrow(NoSuchAlgorithmException.class);

Algorithm algorithm = new HMACAlgorithm(crypto, "some-alg", "some-algorithm", "secret".getBytes());
Algorithm algorithm = new HMACAlgorithm(crypto, "some-alg", "some-algorithm", "secret".getBytes(StandardCharsets.UTF_8));
String jwt = "eyJhbGciOiJIUzI1NiIsImN0eSI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.mZ0m_N1J4PgeqWmi903JuUoDRZDBPB7HwkS4nVyWH1M";
AlgorithmUtils.verify(algorithm, jwt);
}
Expand All @@ -140,7 +140,7 @@ public void shouldThrowOnVerifyWhenTheSecretIsInvalid() throws Exception {
when(crypto.verifySignatureFor(anyString(), any(byte[].class), any(byte[].class), any(byte[].class)))
.thenThrow(InvalidKeyException.class);

Algorithm algorithm = new HMACAlgorithm(crypto, "some-alg", "some-algorithm", "secret".getBytes());
Algorithm algorithm = new HMACAlgorithm(crypto, "some-alg", "some-algorithm", "secret".getBytes(StandardCharsets.UTF_8));
String jwt = "eyJhbGciOiJIUzI1NiIsImN0eSI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.mZ0m_N1J4PgeqWmi903JuUoDRZDBPB7HwkS4nVyWH1M";
AlgorithmUtils.verify(algorithm, jwt);
}
Expand Down

0 comments on commit 24901e2

Please sign in to comment.