1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 package org.mozilla.gecko.sync.crypto.test; 5 6 import org.junit.Test; 7 import org.junit.runner.RunWith; 8 import org.mozilla.apache.commons.codec.binary.Base64; 9 import org.mozilla.gecko.background.testhelpers.TestRunner; 10 import org.mozilla.gecko.sync.crypto.CryptoException; 11 import org.mozilla.gecko.sync.crypto.KeyBundle; 12 13 import java.io.UnsupportedEncodingException; 14 import java.util.Arrays; 15 16 import static org.junit.Assert.assertArrayEquals; 17 import static org.junit.Assert.assertEquals; 18 import static org.junit.Assert.assertFalse; 19 import static org.junit.Assert.assertTrue; 20 21 @RunWith(TestRunner.class) 22 public class TestKeyBundle { 23 @Test testCreateKeyBundle()24 public void testCreateKeyBundle() throws UnsupportedEncodingException, CryptoException { 25 String username = "smqvooxj664hmrkrv6bw4r4vkegjhkns"; 26 String friendlyBase32SyncKey = "gbh7teqqcgyzd65svjgibd7tqy"; 27 String base64EncryptionKey = "069EnS3EtDK4y1tZ1AyKX+U7WEsWRp9b" + 28 "RIKLdW/7aoE="; 29 String base64HmacKey = "LF2YCS1QCgSNCf0BCQvQ06SGH8jqJDi9" + 30 "dKj0O+b0fwI="; 31 32 KeyBundle keys = new KeyBundle(username, friendlyBase32SyncKey); 33 assertArrayEquals(keys.getEncryptionKey(), Base64.decodeBase64(base64EncryptionKey.getBytes("UTF-8"))); 34 assertArrayEquals(keys.getHMACKey(), Base64.decodeBase64(base64HmacKey.getBytes("UTF-8"))); 35 } 36 37 /* 38 * Basic sanity check to make sure length of keys is correct (32 bytes). 39 * Also make sure that the two keys are different. 40 */ 41 @Test testGenerateRandomKeys()42 public void testGenerateRandomKeys() throws CryptoException { 43 KeyBundle keys = KeyBundle.withRandomKeys(); 44 45 assertEquals(32, keys.getEncryptionKey().length); 46 assertEquals(32, keys.getHMACKey().length); 47 48 boolean equal = Arrays.equals(keys.getEncryptionKey(), keys.getHMACKey()); 49 assertEquals(false, equal); 50 } 51 52 @Test testEquals()53 public void testEquals() throws CryptoException { 54 KeyBundle k = KeyBundle.withRandomKeys(); 55 KeyBundle o = KeyBundle.withRandomKeys(); 56 assertFalse(k.equals("test")); 57 assertFalse(k.equals(o)); 58 assertTrue(k.equals(k)); 59 assertTrue(o.equals(o)); 60 o.setHMACKey(k.getHMACKey()); 61 assertFalse(o.equals(k)); 62 o.setEncryptionKey(k.getEncryptionKey()); 63 assertTrue(o.equals(k)); 64 } 65 } 66