Fix periodic NETCONF Call Home connection dropping
[netconf.git] / netconf / callhome-protocol / src / test / java / org / opendaylight / netconf / callhome / protocol / CallHomeAuthorizationTest.java
1 /*
2  * Copyright (c) 2016 Brocade Communication Systems and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.callhome.protocol;
9
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;
18
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;
25
26 public class CallHomeAuthorizationTest {
27     @Test
28     public void anAuthorizationOfRejectedIsNotAllowed() {
29         // given
30         CallHomeAuthorization auth = CallHomeAuthorization.rejected();
31         // expect
32         assertFalse(auth.isServerAllowed());
33     }
34
35     @Test(expected = IllegalStateException.class)
36     public void anAuthorizationOfRejectedCannotBeAppliedToASession() {
37         // given
38         CallHomeAuthorization auth = CallHomeAuthorization.rejected();
39         // when
40         auth.applyTo(mock(ClientSession.class));
41     }
42
43     @Test
44     public void anAuthorizationOfAcceptanceIsAllowed() {
45         // given
46         String session = "some-session";
47         String user = "some-user-name";
48         ClientSessionImpl mockSession = mock(ClientSessionImpl.class);
49         doNothing().when(mockSession).setUsername(user);
50
51         // and
52         CallHomeAuthorization auth = CallHomeAuthorization.serverAccepted(session, user).build();
53         // when
54         auth.applyTo(mockSession);
55         // then
56         assertTrue(auth.isServerAllowed());
57     }
58
59     @Test
60     public void anAuthorizationOfAcceptanceCanBeAppliedToASession() {
61         // given
62         String session = "some-session";
63         String user = "some-user-name";
64         String pwd = "pwd1";
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);
70         // and
71         CallHomeAuthorization auth = CallHomeAuthorization.serverAccepted(session, user)
72                 .addPassword(pwd)
73                 .addClientKeys(pair)
74                 .build();
75         // when
76         auth.applyTo(mockSession);
77         // then
78         verify(mockSession, times(1)).addPasswordIdentity(anyString());
79         verify(mockSession, times(1)).addPublicKeyIdentity(any(KeyPair.class));
80     }
81 }