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