blob: 1a0bafb1d599b31ca89ebac00b68533f6ea0e690 [file] [log] [blame]
andrewsbrownaada3932015-04-01 14:09:51 -07001/*
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 */
14package com.intel.jndn.mock;
15
16import net.named_data.jndn.Name;
17import net.named_data.jndn.security.KeyChain;
18import net.named_data.jndn.security.identity.IdentityManager;
19import net.named_data.jndn.security.identity.IdentityStorage;
20import net.named_data.jndn.security.identity.MemoryIdentityStorage;
21import net.named_data.jndn.security.identity.MemoryPrivateKeyStorage;
22import net.named_data.jndn.security.identity.PrivateKeyStorage;
23import net.named_data.jndn.security.policy.SelfVerifyPolicyManager;
Alexander Afanasyevcbc41012016-02-19 20:10:57 -080024import net.named_data.jndn.security.SecurityException;
andrewsbrownaada3932015-04-01 14:09:51 -070025
26/**
Alexander Afanasyevcbc41012016-02-19 20:10:57 -080027 * Create an in-memory key chain for use in NDN-related tests.
andrewsbrownaada3932015-04-01 14:09:51 -070028 *
29 * @author Andrew Brown <andrew.brown@intel.com>
30 */
Alexander Afanasyevcbc41012016-02-19 20:10:57 -080031public final class MockKeyChain {
32 /**
33 * Do not allow instances of this key chain.
34 */
andrewsbrown8e517fa2016-02-12 15:51:19 -080035 private MockKeyChain() {
andrewsbrown8e517fa2016-02-12 15:51:19 -080036 }
37
andrewsbrownaada3932015-04-01 14:09:51 -070038 /**
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 Afanasyevcbc41012016-02-19 20:10:57 -080044 * @throws SecurityException if failed to create mock identity
andrewsbrownaada3932015-04-01 14:09:51 -070045 */
Alexander Afanasyevcbc41012016-02-19 20:10:57 -080046 public static KeyChain configure(final Name name) throws SecurityException {
andrewsbrownaada3932015-04-01 14:09:51 -070047 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 Afanasyev83a26d32016-01-26 01:04:32 -080054 keyChain.createIdentityAndCertificate(name);
andrewsbrownaada3932015-04-01 14:09:51 -070055 }
56
57 // set default identity
58 keyChain.getIdentityManager().setDefaultIdentity(name);
59
60 return keyChain;
61 }
62}