2 * Copyright (c) 2016 Brocade Communication Systems and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netconf.callhome.protocol;
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.anyString;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
19 import java.security.KeyPair;
20 import java.security.PrivateKey;
21 import java.security.PublicKey;
22 import org.junit.Test;
23 import org.opendaylight.netconf.shaded.sshd.client.session.ClientSession;
24 import org.opendaylight.netconf.shaded.sshd.client.session.ClientSessionImpl;
26 public class CallHomeAuthorizationTest {
28 public void anAuthorizationOfRejectedIsNotAllowed() {
30 CallHomeAuthorization auth = CallHomeAuthorization.rejected();
32 assertFalse(auth.isServerAllowed());
35 @Test(expected = IllegalStateException.class)
36 public void anAuthorizationOfRejectedCannotBeAppliedToASession() {
38 CallHomeAuthorization auth = CallHomeAuthorization.rejected();
40 auth.applyTo(mock(ClientSession.class));
44 public void anAuthorizationOfAcceptanceIsAllowed() {
46 String session = "some-session";
47 String user = "some-user-name";
48 ClientSessionImpl mockSession = mock(ClientSessionImpl.class);
49 doNothing().when(mockSession).setUsername(user);
52 CallHomeAuthorization auth = CallHomeAuthorization.serverAccepted(session, user).build();
54 auth.applyTo(mockSession);
56 assertTrue(auth.isServerAllowed());
60 public void anAuthorizationOfAcceptanceCanBeAppliedToASession() {
62 String session = "some-session";
63 String user = "some-user-name";
65 KeyPair pair = new KeyPair(mock(PublicKey.class), mock(PrivateKey.class));
66 ClientSessionImpl mockSession = mock(ClientSessionImpl.class);
67 doNothing().when(mockSession).setUsername(user);
68 doNothing().when(mockSession).addPasswordIdentity(pwd);
69 doNothing().when(mockSession).addPublicKeyIdentity(pair);
71 CallHomeAuthorization auth = CallHomeAuthorization.serverAccepted(session, user)
76 auth.applyTo(mockSession);
78 verify(mockSession, times(1)).addPasswordIdentity(anyString());
79 verify(mockSession, times(1)).addPublicKeyIdentity(any(KeyPair.class));