Skip to content

Commit

Permalink
Merge pull request #342 from RyanBard/issue-333_add_ascii_base64_test…
Browse files Browse the repository at this point in the history
…_vector

tests #333: Add ascii base64 test vector
  • Loading branch information
lhazlewood authored Jul 8, 2018
2 parents 4b9a3ca + f215e39 commit d61916f
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/test/groovy/io/jsonwebtoken/codec/impl/Base64Test.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,18 @@ class Base64Test {
assertEquals "Zm9vYmE=", BASE64("fooba")

assertEquals "Zm9vYmFy", BASE64("foobar")

def input = 'special: [\r\n \t], ascii[32..126]: [ !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~]\n'
def expected = "c3BlY2lhbDogWw0KIAldLCBhc2NpaVszMi4uMTI2XTogWyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5Ojs8PT4/QEFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaW1xdXl9gYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXp7fH1+XQo="
assertEquals expected, BASE64(input)
}

private static String BASE64URL(String s) {
byte[] bytes = s.getBytes(Strings.UTF_8);
return Base64.URL_SAFE.encodeToString(bytes, false)
}

@Test //same test vectors above, but with padding removed
@Test //same test vectors above, but with padding removed & some specials swapped: https://brockallen.com/2014/10/17/base64url-encoding/
void testRfc4648Base64UrlTestVectors() {

assertEquals "", BASE64URL("")
Expand All @@ -127,5 +131,11 @@ class Base64Test {

assertEquals "Zm9vYmFy", BASE64URL("foobar")

def input = 'special: [\r\n \t], ascii[32..126]: [ !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~]\n'
def expected = "c3BlY2lhbDogWw0KIAldLCBhc2NpaVszMi4uMTI2XTogWyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5Ojs8PT4/QEFCQ0RFRkdISUpLTE1OT1BRUlNUVVZXWFlaW1xdXl9gYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXp7fH1+XQo="
.replace("=", "")
.replace("+", "-")
.replace("/", "_")
assertEquals expected, BASE64URL(input)
}
}

0 comments on commit d61916f

Please sign in to comment.