diff options
author | Dominik Schürmann <dominik@dominikschuermann.de> | 2015-06-11 00:05:13 +0200 |
---|---|---|
committer | Dominik Schürmann <dominik@dominikschuermann.de> | 2015-06-11 00:05:13 +0200 |
commit | d16b09b2a6be41319b993c27e69b85067a7f1c46 (patch) | |
tree | 78deb6554f9bd5f3505adb39bfbbd10e692dab9e /OpenKeychain/src/test/java/org/sufficientlysecure/keychain/util | |
parent | 05fcbcae7b816e72363030d499a43e0e6042b320 (diff) | |
download | open-keychain-d16b09b2a6be41319b993c27e69b85067a7f1c46.tar.gz open-keychain-d16b09b2a6be41319b993c27e69b85067a7f1c46.tar.bz2 open-keychain-d16b09b2a6be41319b993c27e69b85067a7f1c46.zip |
Use new officially supported way for local unit tests, many dependencies upgraded for this, temporary disabled separate debug builds
Diffstat (limited to 'OpenKeychain/src/test/java/org/sufficientlysecure/keychain/util')
3 files changed, 221 insertions, 0 deletions
diff --git a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/util/Iso7816TLVTest.java b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/util/Iso7816TLVTest.java new file mode 100644 index 000000000..c22f54715 --- /dev/null +++ b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/util/Iso7816TLVTest.java @@ -0,0 +1,102 @@ +/* Copyright (C) 2014 Vincent Breitmoser <v.breitmoser@mugenguild.com> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.sufficientlysecure.keychain.util; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricGradleTestRunner; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; +import org.robolectric.shadows.ShadowLog; +import org.spongycastle.util.encoders.Hex; +import org.sufficientlysecure.keychain.BuildConfig; +import org.sufficientlysecure.keychain.util.Iso7816TLV.Iso7816CompositeTLV; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(constants = BuildConfig.class, sdk = 21, manifest = "src/main/AndroidManifest.xml") +public class Iso7816TLVTest { + + @Before + public void setUp() throws Exception { + ShadowLog.stream = System.out; + } + + @Test + public void testDecode() throws Exception { + + // this is an Application Related Data packet, received from my Yubikey + String input = "6e81dd4f10d27600012401020000000000000100005f520f0073000080000000000000000000007300c00af00000ff04c000ff00ffc106010800001103c206010800001103c306010800001103c407007f7f7f030303c53c1efdb4845ca242ca6977fddb1f788094fd3b430af1114c28a08d8c5afda81191cc50ca9bf51bc99fe8e6ca03a9d4d40e7b5925cd154813df381655b2c63c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000cd0c5423590e5423590e5423590e9000"; + byte[] data = Hex.decode(input); + + Iso7816TLV tlv = Iso7816TLV.readSingle(data, true); + Assert.assertNotNull("tlv parse must succeed", tlv); + + Assert.assertEquals("top packet must be 'application related data' tag", 0x6e, tlv.mT); + Assert.assertEquals("length must be correct", 221, tlv.mL); + + Assert.assertTrue("top packet must be composite", tlv instanceof Iso7816CompositeTLV); + + Iso7816CompositeTLV ctlv = (Iso7816CompositeTLV) tlv; + + Assert.assertEquals("top packet must have 11 sub packets", 11, ctlv.mSubs.length); + + Assert.assertEquals("sub packet #1 must have expected tag", 0x4f, ctlv.mSubs[0].mT); + Assert.assertEquals("sub packet #1 must have expected length", 16, ctlv.mSubs[0].mL); + + Assert.assertEquals("sub packet #2 must have expected tag", 0x5f52, ctlv.mSubs[1].mT); + Assert.assertEquals("sub packet #2 must have expected length", 15, ctlv.mSubs[1].mL); + + Assert.assertEquals("sub packet #3 must have expected tag", 0x73, ctlv.mSubs[2].mT); + Assert.assertEquals("sub packet #3 must have expected length", 0, ctlv.mSubs[2].mL); + Assert.assertTrue("sub packet #3 muse be composite", ctlv.mSubs[2] instanceof Iso7816CompositeTLV); + + Assert.assertEquals("sub packet #4 must have expected tag", 0xc0, ctlv.mSubs[3].mT); + Assert.assertEquals("sub packet #4 must have expected length", 10, ctlv.mSubs[3].mL); + + Assert.assertEquals("sub packet #5 must have expected tag", 0xc1, ctlv.mSubs[4].mT); + Assert.assertEquals("sub packet #5 must have expected length", 6, ctlv.mSubs[4].mL); + + Assert.assertEquals("sub packet #6 must have expected tag", 0xc2, ctlv.mSubs[5].mT); + Assert.assertEquals("sub packet #6 must have expected length", 6, ctlv.mSubs[5].mL); + + Assert.assertEquals("sub packet #7 must have expected tag", 0xc3, ctlv.mSubs[6].mT); + Assert.assertEquals("sub packet #7 must have expected length", 6, ctlv.mSubs[6].mL); + + Assert.assertEquals("sub packet #8 must have expected tag", 0xc4, ctlv.mSubs[7].mT); + Assert.assertEquals("sub packet #8 must have expected length", 7, ctlv.mSubs[7].mL); + + Assert.assertEquals("sub packet #9 must have expected tag", 0xc5, ctlv.mSubs[8].mT); + Assert.assertEquals("sub packet #9 must have expected length", 60, ctlv.mSubs[8].mL); + + { + // this is my pubkey fingerprint + String fingerprint = "1efdb4845ca242ca6977fddb1f788094fd3b430a"; + byte[] V1 = new byte[20]; + System.arraycopy(ctlv.mSubs[8].mV, 0, V1, 0, 20); + Assert.assertArrayEquals("fingerprint must match", V1, Hex.decode(fingerprint)); + } + + Assert.assertEquals("sub packet #10 must have expected tag", 0xc6, ctlv.mSubs[9].mT); + Assert.assertEquals("sub packet #10 must have expected length", 60, ctlv.mSubs[9].mL); + + Assert.assertEquals("sub packet #11 must have expected tag", 0xcd, ctlv.mSubs[10].mT); + Assert.assertEquals("sub packet #11 must have expected length", 12, ctlv.mSubs[10].mL); + + } + +} diff --git a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/util/ParcelableFileCacheTest.java b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/util/ParcelableFileCacheTest.java new file mode 100644 index 000000000..10bee2a02 --- /dev/null +++ b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/util/ParcelableFileCacheTest.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de> + * Copyright (C) 2014 Vincent Breitmoser <v.breitmoser@mugenguild.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package org.sufficientlysecure.keychain.util; + +import android.os.Bundle; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricGradleTestRunner; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; +import org.robolectric.shadows.ShadowLog; +import org.sufficientlysecure.keychain.BuildConfig; +import org.sufficientlysecure.keychain.util.ParcelableFileCache.IteratorWithSize; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(constants = BuildConfig.class, sdk = 21, manifest = "src/main/AndroidManifest.xml") +public class ParcelableFileCacheTest { + + @Before + public void setUp() throws Exception { + ShadowLog.stream = System.out; + } + + @Test + public void testInputOutput() throws Exception { + + ParcelableFileCache<Bundle> cache = new ParcelableFileCache<Bundle>(RuntimeEnvironment.application, "test.pcl"); + + ArrayList<Bundle> list = new ArrayList<Bundle>(); + + for (int i = 0; i < 50; i++) { + Bundle b = new Bundle(); + b.putInt("key1", i); + b.putString("key2", Integer.toString(i)); + list.add(b); + } + + // write to cache file + cache.writeCache(list.size(), list.iterator()); + + // read back + IteratorWithSize<Bundle> it = cache.readCache(); + + Assert.assertEquals("number of entries must be correct", list.size(), it.getSize()); + + while (it.hasNext()) { + Bundle b = it.next(); + Assert.assertEquals("input values should be equal to output values", + b.getInt("key1"), b.getInt("key1")); + Assert.assertEquals("input values should be equal to output values", + b.getString("key2"), b.getString("key2")); + } + + } + +} diff --git a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/util/TestingUtils.java b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/util/TestingUtils.java new file mode 100644 index 000000000..1d7952464 --- /dev/null +++ b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/util/TestingUtils.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2014 Vincent Breitmoser <v.breitmoser@mugenguild.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package org.sufficientlysecure.keychain.util; + +import java.util.Random; + +public class TestingUtils { + public static Passphrase genPassphrase() { + return genPassphrase(false); + } + + public static Passphrase genPassphrase(boolean noEmpty) { + String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789!@#$%^&*()-_="; + Random r = new Random(); + StringBuilder passbuilder = new StringBuilder(); + // 20% chance for an empty passphrase + for(int i = 0, j = noEmpty || r.nextInt(10) > 2 ? r.nextInt(20)+1 : 0; i < j; i++) { + passbuilder.append(chars.charAt(r.nextInt(chars.length()))); + } + System.out.println("Generated passphrase: '" + passbuilder.toString() + "'"); + return new Passphrase(passbuilder.toString()); + } +} |