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.ArgumentMatchers.any;
14 import static org.mockito.ArgumentMatchers.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.client.session.ClientSession;
24 import org.apache.sshd.client.session.ClientSessionImpl;
25 import org.junit.Test;
27 public class CallHomeAuthorizationTest {
29 public void anAuthorizationOfRejectedIsNotAllowed() {
31 CallHomeAuthorization auth = CallHomeAuthorization.rejected();
33 assertFalse(auth.isServerAllowed());
36 @Test(expected = IllegalStateException.class)
37 public void anAuthorizationOfRejectedCannotBeAppliedToASession() {
39 CallHomeAuthorization auth = CallHomeAuthorization.rejected();
41 auth.applyTo(mock(ClientSession.class));
45 public void anAuthorizationOfAcceptanceIsAllowed() {
47 String session = "some-session";
48 String user = "some-user-name";
49 ClientSessionImpl mockSession = mock(ClientSessionImpl.class);
50 doNothing().when(mockSession).setUsername(user);
53 CallHomeAuthorization auth = CallHomeAuthorization.serverAccepted(session, user).build();
55 auth.applyTo(mockSession);
57 assertTrue(auth.isServerAllowed());
61 public void anAuthorizationOfAcceptanceCanBeAppliedToASession() {
63 String session = "some-session";
64 String user = "some-user-name";
66 KeyPair pair = new KeyPair(mock(PublicKey.class), mock(PrivateKey.class));
67 ClientSessionImpl mockSession = mock(ClientSessionImpl.class);
68 doNothing().when(mockSession).setUsername(user);
69 doNothing().when(mockSession).addPasswordIdentity(pwd);
70 doNothing().when(mockSession).addPublicKeyIdentity(pair);
72 CallHomeAuthorization auth = CallHomeAuthorization.serverAccepted(session, user)
77 auth.applyTo(mockSession);
79 verify(mockSession, times(1)).addPasswordIdentity(anyString());
80 verify(mockSession, times(1)).addPublicKeyIdentity(any(KeyPair.class));