andrewsbrown | aada393 | 2015-04-01 14:09:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * jndn-mock |
| 3 | * Copyright (c) 2015, Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU Lesser General Public License, |
| 7 | * version 3, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT ANY |
| 10 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 11 | * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
| 12 | * more details. |
| 13 | */ |
| 14 | package com.intel.jndn.mock; |
| 15 | |
| 16 | import net.named_data.jndn.Name; |
| 17 | import net.named_data.jndn.security.KeyChain; |
| 18 | import net.named_data.jndn.security.identity.IdentityManager; |
| 19 | import net.named_data.jndn.security.identity.IdentityStorage; |
| 20 | import net.named_data.jndn.security.identity.MemoryIdentityStorage; |
| 21 | import net.named_data.jndn.security.identity.MemoryPrivateKeyStorage; |
| 22 | import net.named_data.jndn.security.identity.PrivateKeyStorage; |
| 23 | import net.named_data.jndn.security.policy.SelfVerifyPolicyManager; |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame^] | 24 | import net.named_data.jndn.security.SecurityException; |
andrewsbrown | aada393 | 2015-04-01 14:09:51 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame^] | 27 | * Create an in-memory key chain for use in NDN-related tests. |
andrewsbrown | aada393 | 2015-04-01 14:09:51 -0700 | [diff] [blame] | 28 | * |
| 29 | * @author Andrew Brown <andrew.brown@intel.com> |
| 30 | */ |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame^] | 31 | public final class MockKeyChain { |
| 32 | /** |
| 33 | * Do not allow instances of this key chain. |
| 34 | */ |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 35 | private MockKeyChain() { |
andrewsbrown | 8e517fa | 2016-02-12 15:51:19 -0800 | [diff] [blame] | 36 | } |
| 37 | |
andrewsbrown | aada393 | 2015-04-01 14:09:51 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Build and configure an in-memory {@link KeyChain}. |
| 40 | * |
| 41 | * @param name the name of the default identity to create |
| 42 | * @return an in-memory {@link KeyChain} configured with the name as the |
| 43 | * default identity |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame^] | 44 | * @throws SecurityException if failed to create mock identity |
andrewsbrown | aada393 | 2015-04-01 14:09:51 -0700 | [diff] [blame] | 45 | */ |
Alexander Afanasyev | cbc4101 | 2016-02-19 20:10:57 -0800 | [diff] [blame^] | 46 | public static KeyChain configure(final Name name) throws SecurityException { |
andrewsbrown | aada393 | 2015-04-01 14:09:51 -0700 | [diff] [blame] | 47 | PrivateKeyStorage keyStorage = new MemoryPrivateKeyStorage(); |
| 48 | IdentityStorage identityStorage = new MemoryIdentityStorage(); |
| 49 | KeyChain keyChain = new KeyChain(new IdentityManager(identityStorage, keyStorage), |
| 50 | new SelfVerifyPolicyManager(identityStorage)); |
| 51 | |
| 52 | // create keys, certs if necessary |
| 53 | if (!identityStorage.doesIdentityExist(name)) { |
Alexander Afanasyev | 83a26d3 | 2016-01-26 01:04:32 -0800 | [diff] [blame] | 54 | keyChain.createIdentityAndCertificate(name); |
andrewsbrown | aada393 | 2015-04-01 14:09:51 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // set default identity |
| 58 | keyChain.getIdentityManager().setDefaultIdentity(name); |
| 59 | |
| 60 | return keyChain; |
| 61 | } |
| 62 | } |