Bug 8153: enforce check-style rules for netconf
[netconf.git] / netconf / callhome-protocol / src / test / java / org / opendaylight / netconf / callhome / protocol / AuthorizedKeysDecoderTest.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.assertEquals;
12
13 import java.security.GeneralSecurityException;
14 import java.security.PublicKey;
15 import org.junit.Before;
16 import org.junit.Test;
17
18 public class AuthorizedKeysDecoderTest {
19
20     private AuthorizedKeysDecoder instance;
21
22     @Before
23     public void setup() {
24         instance = new AuthorizedKeysDecoder();
25     }
26
27     @SuppressWarnings("checkstyle:lineLength")
28     @Test
29     public void authorizedKeysDecoderValidRSAKey() throws GeneralSecurityException {
30         // given
31         String rsaStr = "AAAAB3NzaC1yc2EAAAADAQABAAABAQCvLigTfPZMqOQwHp051Co4lwwPwO21NFIXWgjQmCPEgRTqQpei7qQaxlLGkrIPjZtJQRgCuC+Sg8HFw1YpUaMybN0nFInInQLp/qe0yc9ByDZM2G86NX6W5W3+j87I8Fh1dnMov1iJ0DFVn8RLwdEGjreiZCRyJOMuHghh6y4EG7W8BwmZrse17zhSpc2wFOVhxeZnYAQFEw6g48LutFRDpoTjGgz1nz/L4zcaUxxigs8wdY+qTTOHxSTxlLqwSZPFLyYrV2KJ9mKahMuYUy6o2b8snsjvnSjyK0kY+U0C6c8fmPDFUc0RqJqfdnsIUyh11U8d3NZdaFWg0UW0SNK3";
32         // when
33         PublicKey serverKey = instance.decodePublicKey(rsaStr);
34         // then
35         assertEquals(serverKey.getAlgorithm(), "RSA");
36     }
37
38     @SuppressWarnings("checkstyle:lineLength")
39     @Test(expected = Exception.class)
40     public void authorizedKeysDecoderInvalidRSAKey() throws GeneralSecurityException {
41         // given
42         String rsaStr = "AAAB3NzaC1yc2EAAAADAQABAAABAQCvLigTfPZMqOQwHp051Co4lwwPwO21NFIXWgjQmCPEgRTqQpei7qQaxlLGkrIPjZtJQRgCuC+Sg8HFw1YpUaMybN0nFInInQLp/qe0yc9ByDZM2G86NX6W5W3+j87I8Fh1dnMov1iJ0DFVn8RLwdEGjreiZCRyJOMuHghh6y4EG7W8BwmZrse17zhSpc2wFOVhxeZnYAQFEw6g48LutFRDpoTjGgz1nz/L4zcaUxxigs8wdY+qTTOHxSTxlLqwSZPFLyYrV2KJ9mKahMuYUy6o2b8snsjvnSjyK0kY+U0C6c8fmPDFUc0RqJqfdnsIUyh11U8d3NZdaFWg0UW0SNK3";
43         // when
44         instance.decodePublicKey(rsaStr);
45     }
46
47     @SuppressWarnings("checkstyle:lineLength")
48     @Test
49     public void authorizedKeysDecoderValidDSAKey() throws GeneralSecurityException {
50         // given
51         String dsaStr = "AAAAB3NzaC1kc3MAAACBANkM1e45lxlyV24QyWBAoESlHzhYYJUfk/yUd0+Dv28okyO71DmnJesYyUzsKDpnFLlnFhxTTUGSg90fdrdubLFkRTGnHhweegMCf6kU1xyE3U6bpyMdiOXH7fOS6Q2B+qtaQRB4R5TEhdoJX648Ng+YZvLwdbZh3r/et4P46b3DAAAAFQDcu6qp67XRpzMoOS2fIL+VOxvmDwAAAIAeT3d/hbvzPoL8wV52gPtWJMU2EGoX/LJwc86Vn52NlxXB1EQSzZI50PgCKEckS80lj4GXO1ZyuBhdsBEz4rDtAIdZGW5z7WxTfcz0G2dOWmNOBqvu7j9ngfPrgtDVHYV2VL/4VpbmoPgkQLfbA9NWb6US2RnTO46rGbGurigDMQAAAIEAiI3REuOJAmgDow6HxbN0FM+RCe1JYDwJIsCRRK4JA9oYV4Pg897xqypOeXogutVu9usfcOJI6uk5OwwLqIUSaU+flgmL0LOXv4lH4+URqs7Or8+ABFTcVGGCxg0I3gwhlY2Vjc9nyHY15wqBYdUxLbe8HC6EQp9uwlLlb8LQ6a0=";
52         // when
53         PublicKey serverKey = instance.decodePublicKey(dsaStr);
54         // then
55         assertEquals(serverKey.getAlgorithm(), "DSA");
56     }
57
58     @SuppressWarnings("checkstyle:lineLength")
59     @Test(expected = IllegalArgumentException.class)
60     public void authorizedKeysDecoderInvalidDSAKey() throws GeneralSecurityException {
61         // given
62         String dsaStr = "AAAAB3Nzakc3MAAACBANkM1e45lxlyV24QyWBAoESlHzhYYJUfk/yUd0+Dv28okyO71DmnJesYyUzsKDpnFLlnFhxTTUGSg90fdrdubLFkRTGnHhweegMCf6kU1xyE3U6bpyMdiOXH7fOS6Q2B+qtaQRB4R5TEhdoJX648Ng+YZvLwdbZh3r/et4P46b3DAAAAFQDcu6qp67XRpzMoOS2fIL+VOxvmDwAAAIAeT3d/hbvzPoL8wV52gPtWJMU2EGoX/LJwc86Vn52NlxXB1EQSzZI50PgCKEckS80lj4GXO1ZyuBhdsBEz4rDtAIdZGW5z7WxTfcz0G2dOWmNOBqvu7j9ngfPrgtDVHYV2VL/4VpbmoPgkQLfbA9NWb6US2RnTO46rGbGurigDMQAAAIEAiI3REuOJAmgDow6HxbN0FM+RCe1JYDwJIsCRRK4JA9oYV4Pg897xqypOeXogutVu9usfcOJI6uk5OwwLqIUSaU+flgmL0LOXv4lH4+URqs7Or8+ABFTcVGGCxg0I3gwhlY2Vjc9nyHY15wqBYdUxLbe8HC6EQp9uwlLlb8LQ6a0=";
63         // when
64         instance.decodePublicKey(dsaStr);
65     }
66
67     @SuppressWarnings("checkstyle:lineLength")
68     @Test
69     public void authorizedKeysDecoderValidEcDSAKey() throws GeneralSecurityException {
70         // given
71         String ecdsaStr = "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBAP4dTrlwZmz8bZ1f901qWuFk7YelrL2WJG0jrCEAPo9UNM1wywpqjbaYUfoq+cevhLZaukDQ4N2Evux+YQ2zz0=";
72         // when
73         PublicKey serverKey = instance.decodePublicKey(ecdsaStr);
74         // then
75         assertEquals(serverKey.getAlgorithm(), "EC");
76     }
77
78     @SuppressWarnings("checkstyle:lineLength")
79     @Test(expected = IllegalArgumentException.class)
80     public void authorizedKeysDecoderInvalidEcDSAKey() throws GeneralSecurityException {
81         // given
82         String ecdsaStr = "AAAAE2VjZHNhLXNoItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBAP4dTrlwZmz8bZ1f901qWuFk7YelrL2WJG0jrCEAPo9UNM1wywpqjbaYUfoq+cevhLZaukDQ4N2Evux+YQ2zz0=";
83         // when
84         instance.decodePublicKey(ecdsaStr);
85     }
86
87     @Test(expected = IllegalArgumentException.class)
88     public void authorizedKeysDecoderInvalidKeyType() throws GeneralSecurityException {
89         // given
90         String ed25519Str = "AAAAC3NzaC1lZDI1NTE5AAAAICIvyX9C+u3KZmJ8x4DuqJg1iAKOPObCgkX9plrvu29R";
91         // when
92         instance.decodePublicKey(ed25519Str);
93     }
94
95     @Test(expected = IllegalArgumentException.class)
96     public void decodingOfBlankInputIsCaughtAsAnError() throws GeneralSecurityException {
97         // when
98         instance.decodePublicKey("");
99     }
100 }