Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | package org.mozilla.gecko.background.fxa; |
michael@0 | 5 | |
michael@0 | 6 | import java.security.GeneralSecurityException; |
michael@0 | 7 | |
michael@0 | 8 | import org.mozilla.gecko.background.helpers.AndroidSyncTestCase; |
michael@0 | 9 | import org.mozilla.gecko.browserid.BrowserIDKeyPair; |
michael@0 | 10 | import org.mozilla.gecko.browserid.DSACryptoImplementation; |
michael@0 | 11 | import org.mozilla.gecko.browserid.JSONWebTokenUtils; |
michael@0 | 12 | import org.mozilla.gecko.browserid.RSACryptoImplementation; |
michael@0 | 13 | import org.mozilla.gecko.browserid.SigningPrivateKey; |
michael@0 | 14 | import org.mozilla.gecko.browserid.VerifyingPublicKey; |
michael@0 | 15 | import org.mozilla.gecko.sync.ExtendedJSONObject; |
michael@0 | 16 | import org.mozilla.gecko.sync.Utils; |
michael@0 | 17 | |
michael@0 | 18 | public class TestBrowserIDKeyPairGeneration extends AndroidSyncTestCase { |
michael@0 | 19 | public void doTestEncodeDecode(BrowserIDKeyPair keyPair) throws Exception { |
michael@0 | 20 | SigningPrivateKey privateKey = keyPair.getPrivate(); |
michael@0 | 21 | VerifyingPublicKey publicKey = keyPair.getPublic(); |
michael@0 | 22 | |
michael@0 | 23 | ExtendedJSONObject o = new ExtendedJSONObject(); |
michael@0 | 24 | o.put("key", Utils.generateGuid()); |
michael@0 | 25 | |
michael@0 | 26 | String token = JSONWebTokenUtils.encode(o.toJSONString(), privateKey); |
michael@0 | 27 | assertNotNull(token); |
michael@0 | 28 | |
michael@0 | 29 | String payload = JSONWebTokenUtils.decode(token, publicKey); |
michael@0 | 30 | assertEquals(o.toJSONString(), payload); |
michael@0 | 31 | |
michael@0 | 32 | try { |
michael@0 | 33 | JSONWebTokenUtils.decode(token + "x", publicKey); |
michael@0 | 34 | fail("Expected exception."); |
michael@0 | 35 | } catch (GeneralSecurityException e) { |
michael@0 | 36 | // Do nothing. |
michael@0 | 37 | } |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | public void testEncodeDecodeSuccessRSA() throws Exception { |
michael@0 | 41 | doTestEncodeDecode(RSACryptoImplementation.generateKeyPair(1024)); |
michael@0 | 42 | doTestEncodeDecode(RSACryptoImplementation.generateKeyPair(2048)); |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | public void testEncodeDecodeSuccessDSA() throws Exception { |
michael@0 | 46 | doTestEncodeDecode(DSACryptoImplementation.generateKeyPair(512)); |
michael@0 | 47 | doTestEncodeDecode(DSACryptoImplementation.generateKeyPair(1024)); |
michael@0 | 48 | } |
michael@0 | 49 | } |