michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: package org.mozilla.gecko.background.fxa; michael@0: michael@0: import java.security.GeneralSecurityException; michael@0: michael@0: import org.mozilla.gecko.background.helpers.AndroidSyncTestCase; michael@0: import org.mozilla.gecko.browserid.BrowserIDKeyPair; michael@0: import org.mozilla.gecko.browserid.DSACryptoImplementation; michael@0: import org.mozilla.gecko.browserid.JSONWebTokenUtils; michael@0: import org.mozilla.gecko.browserid.RSACryptoImplementation; michael@0: import org.mozilla.gecko.browserid.SigningPrivateKey; michael@0: import org.mozilla.gecko.browserid.VerifyingPublicKey; michael@0: import org.mozilla.gecko.sync.ExtendedJSONObject; michael@0: import org.mozilla.gecko.sync.Utils; michael@0: michael@0: public class TestBrowserIDKeyPairGeneration extends AndroidSyncTestCase { michael@0: public void doTestEncodeDecode(BrowserIDKeyPair keyPair) throws Exception { michael@0: SigningPrivateKey privateKey = keyPair.getPrivate(); michael@0: VerifyingPublicKey publicKey = keyPair.getPublic(); michael@0: michael@0: ExtendedJSONObject o = new ExtendedJSONObject(); michael@0: o.put("key", Utils.generateGuid()); michael@0: michael@0: String token = JSONWebTokenUtils.encode(o.toJSONString(), privateKey); michael@0: assertNotNull(token); michael@0: michael@0: String payload = JSONWebTokenUtils.decode(token, publicKey); michael@0: assertEquals(o.toJSONString(), payload); michael@0: michael@0: try { michael@0: JSONWebTokenUtils.decode(token + "x", publicKey); michael@0: fail("Expected exception."); michael@0: } catch (GeneralSecurityException e) { michael@0: // Do nothing. michael@0: } michael@0: } michael@0: michael@0: public void testEncodeDecodeSuccessRSA() throws Exception { michael@0: doTestEncodeDecode(RSACryptoImplementation.generateKeyPair(1024)); michael@0: doTestEncodeDecode(RSACryptoImplementation.generateKeyPair(2048)); michael@0: } michael@0: michael@0: public void testEncodeDecodeSuccessDSA() throws Exception { michael@0: doTestEncodeDecode(DSACryptoImplementation.generateKeyPair(512)); michael@0: doTestEncodeDecode(DSACryptoImplementation.generateKeyPair(1024)); michael@0: } michael@0: }