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
9 package org.opendaylight.netconf.callhome.protocol;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Matchers.anyString;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
20 import java.security.KeyPair;
21 import java.security.PrivateKey;
22 import java.security.PublicKey;
23 import org.apache.sshd.ClientSession;
24 import org.apache.sshd.client.session.ClientSessionImpl;
25 import org.junit.Test;
28 public class CallHomeAuthorizationTest {
30 public void anAuthorizationOfRejectedIsNotAllowed() {
32 CallHomeAuthorization auth = CallHomeAuthorization.rejected();
34 assertFalse(auth.isServerAllowed());
37 @Test(expected = IllegalStateException.class)
38 public void anAuthorizationOfRejectedCannotBeAppliedToASession() {
40 CallHomeAuthorization auth = CallHomeAuthorization.rejected();
42 auth.applyTo(mock(ClientSession.class));
46 public void anAuthorizationOfAcceptanceIsAllowed() {
48 String session = "some-session";
49 String user = "some-user-name";
50 ClientSessionImpl mockSession = mock(ClientSessionImpl.class);
51 doNothing().when(mockSession).setUsername(user);
54 CallHomeAuthorization auth = CallHomeAuthorization.serverAccepted(session, user).build();
56 auth.applyTo(mockSession);
58 assertTrue(auth.isServerAllowed());
62 public void anAuthorizationOfAcceptanceCanBeAppliedToASession() {
64 String session = "some-session";
65 String user = "some-user-name";
67 KeyPair pair = new KeyPair(mock(PublicKey.class), mock(PrivateKey.class));
68 ClientSessionImpl mockSession = mock(ClientSessionImpl.class);
69 doNothing().when(mockSession).setUsername(user);
70 doNothing().when(mockSession).addPasswordIdentity(pwd);
71 doNothing().when(mockSession).addPublicKeyIdentity(pair);
73 CallHomeAuthorization auth = CallHomeAuthorization.serverAccepted(session, user)
78 auth.applyTo(mockSession);
80 verify(mockSession, times(1)).addPasswordIdentity(anyString());
81 verify(mockSession, times(1)).addPublicKeyIdentity(any(KeyPair.class));