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.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertThrows;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.ArgumentMatchers.any;
15 import static org.mockito.ArgumentMatchers.anyString;
16 import static org.mockito.Mockito.doNothing;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.times;
19 import static org.mockito.Mockito.verify;
21 import java.security.KeyPair;
22 import java.security.PrivateKey;
23 import java.security.PublicKey;
24 import org.junit.Test;
25 import org.opendaylight.netconf.shaded.sshd.client.session.ClientSession;
26 import org.opendaylight.netconf.shaded.sshd.client.session.ClientSessionImpl;
28 public class CallHomeAuthorizationTest {
30 public void anAuthorizationOfRejectedIsNotAllowed() {
32 CallHomeAuthorization auth = CallHomeAuthorization.rejected();
34 assertFalse(auth.isServerAllowed());
38 public void anAuthorizationOfRejectedCannotBeAppliedToASession() {
40 CallHomeAuthorization auth = CallHomeAuthorization.rejected();
42 final var ex = assertThrows(IllegalStateException.class, () -> auth.applyTo(mock(ClientSession.class)));
43 assertEquals("Server is not allowed.", ex.getMessage());
47 public void anAuthorizationOfAcceptanceIsAllowed() {
49 String session = "some-session";
50 String user = "some-user-name";
51 ClientSessionImpl mockSession = mock(ClientSessionImpl.class);
52 doNothing().when(mockSession).setUsername(user);
55 CallHomeAuthorization auth = CallHomeAuthorization.serverAccepted(session, user).build();
57 auth.applyTo(mockSession);
59 assertTrue(auth.isServerAllowed());
63 public void anAuthorizationOfAcceptanceCanBeAppliedToASession() {
65 String session = "some-session";
66 String user = "some-user-name";
68 KeyPair pair = new KeyPair(mock(PublicKey.class), mock(PrivateKey.class));
69 ClientSessionImpl mockSession = mock(ClientSessionImpl.class);
70 doNothing().when(mockSession).setUsername(user);
71 doNothing().when(mockSession).addPasswordIdentity(pwd);
72 doNothing().when(mockSession).addPublicKeyIdentity(pair);
74 CallHomeAuthorization auth = CallHomeAuthorization.serverAccepted(session, user)
79 auth.applyTo(mockSession);
81 verify(mockSession, times(1)).addPasswordIdentity(anyString());
82 verify(mockSession, times(1)).addPublicKeyIdentity(any(KeyPair.class));