blob: b367808851f601f502a9638922acb16986b4eb6a [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;
24
25/**
Andrew Brown58191872016-02-05 22:16:57 -080026 * Create an in-memory key chain for use in NDN-related tests
andrewsbrownaada3932015-04-01 14:09:51 -070027 *
28 * @author Andrew Brown <andrew.brown@intel.com>
29 */
30public class MockKeyChain {
31
andrewsbrown8e517fa2016-02-12 15:51:19 -080032 private MockKeyChain() {
33 // do not allow instances of this key chain
34 }
35
andrewsbrownaada3932015-04-01 14:09:51 -070036 /**
37 * Build and configure an in-memory {@link KeyChain}.
38 *
39 * @param name the name of the default identity to create
40 * @return an in-memory {@link KeyChain} configured with the name as the
41 * default identity
42 * @throws net.named_data.jndn.security.SecurityException
43 */
44 public static KeyChain configure(Name name) throws net.named_data.jndn.security.SecurityException {
andrewsbrownaada3932015-04-01 14:09:51 -070045 PrivateKeyStorage keyStorage = new MemoryPrivateKeyStorage();
46 IdentityStorage identityStorage = new MemoryIdentityStorage();
47 KeyChain keyChain = new KeyChain(new IdentityManager(identityStorage, keyStorage),
48 new SelfVerifyPolicyManager(identityStorage));
49
50 // create keys, certs if necessary
51 if (!identityStorage.doesIdentityExist(name)) {
Alexander Afanasyev83a26d32016-01-26 01:04:32 -080052 keyChain.createIdentityAndCertificate(name);
andrewsbrownaada3932015-04-01 14:09:51 -070053 }
54
55 // set default identity
56 keyChain.getIdentityManager().setDefaultIdentity(name);
57
58 return keyChain;
59 }
60}