08f9a5fb8568e26afc1054203ad5cab65737654d
[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
9 package org.opendaylight.netconf.callhome.protocol;
10
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;
19
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;
26
27
28 public class CallHomeAuthorizationTest {
29     @Test
30     public void anAuthorizationOfRejectedIsNotAllowed() {
31         // given
32         CallHomeAuthorization auth = CallHomeAuthorization.rejected();
33         // expect
34         assertFalse(auth.isServerAllowed());
35     }
36
37     @Test(expected = IllegalStateException.class)
38     public void anAuthorizationOfRejectedCannotBeAppliedToASession() {
39         // given
40         CallHomeAuthorization auth = CallHomeAuthorization.rejected();
41         // when
42         auth.applyTo(mock(ClientSession.class));
43     }
44
45     @Test
46     public void anAuthorizationOfAcceptanceIsAllowed() {
47         // given
48         String session = "some-session";
49         String user = "some-user-name";
50         ClientSessionImpl mockSession = mock(ClientSessionImpl.class);
51         doNothing().when(mockSession).setUsername(user);
52
53         // and
54         CallHomeAuthorization auth = CallHomeAuthorization.serverAccepted(session, user).build();
55         // when
56         auth.applyTo(mockSession);
57         // then
58         assertTrue(auth.isServerAllowed());
59     }
60
61     @Test
62     public void anAuthorizationOfAcceptanceCanBeAppliedToASession() {
63         // given
64         String session = "some-session";
65         String user = "some-user-name";
66         String pwd = "pwd1";
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);
72         // and
73         CallHomeAuthorization auth = CallHomeAuthorization.serverAccepted(session, user)
74                 .addPassword(pwd)
75                 .addClientKeys(pair)
76                 .build();
77         // when
78         auth.applyTo(mockSession);
79         // then
80         verify(mockSession, times(1)).addPasswordIdentity(anyString());
81         verify(mockSession, times(1)).addPublicKeyIdentity(any(KeyPair.class));
82     }
83 }