87bc6807e979ccfa4cad5bb396a6f4c011e327c4
[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
19 public class AuthorizedKeysDecoderTest {
20
21     private AuthorizedKeysDecoder instance;
22
23     @Before
24     public void setup() {
25         instance = new AuthorizedKeysDecoder();
26     }
27
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     @Test(expected = Exception.class)
39     public void authorizedKeysDecoderInvalidRSAKey() throws GeneralSecurityException {
40         // given
41         String rsaStr = "AAAB3NzaC1yc2EAAAADAQABAAABAQCvLigTfPZMqOQwHp051Co4lwwPwO21NFIXWgjQmCPEgRTqQpei7qQaxlLGkrIPjZtJQRgCuC+Sg8HFw1YpUaMybN0nFInInQLp/qe0yc9ByDZM2G86NX6W5W3+j87I8Fh1dnMov1iJ0DFVn8RLwdEGjreiZCRyJOMuHghh6y4EG7W8BwmZrse17zhSpc2wFOVhxeZnYAQFEw6g48LutFRDpoTjGgz1nz/L4zcaUxxigs8wdY+qTTOHxSTxlLqwSZPFLyYrV2KJ9mKahMuYUy6o2b8snsjvnSjyK0kY+U0C6c8fmPDFUc0RqJqfdnsIUyh11U8d3NZdaFWg0UW0SNK3";
42         // when
43         instance.decodePublicKey(rsaStr);
44     }
45
46     @Test
47     public void authorizedKeysDecoderValidDSAKey() throws GeneralSecurityException {
48         // given
49         String dsaStr = "AAAAB3NzaC1kc3MAAACBANkM1e45lxlyV24QyWBAoESlHzhYYJUfk/yUd0+Dv28okyO71DmnJesYyUzsKDpnFLlnFhxTTUGSg90fdrdubLFkRTGnHhweegMCf6kU1xyE3U6bpyMdiOXH7fOS6Q2B+qtaQRB4R5TEhdoJX648Ng+YZvLwdbZh3r/et4P46b3DAAAAFQDcu6qp67XRpzMoOS2fIL+VOxvmDwAAAIAeT3d/hbvzPoL8wV52gPtWJMU2EGoX/LJwc86Vn52NlxXB1EQSzZI50PgCKEckS80lj4GXO1ZyuBhdsBEz4rDtAIdZGW5z7WxTfcz0G2dOWmNOBqvu7j9ngfPrgtDVHYV2VL/4VpbmoPgkQLfbA9NWb6US2RnTO46rGbGurigDMQAAAIEAiI3REuOJAmgDow6HxbN0FM+RCe1JYDwJIsCRRK4JA9oYV4Pg897xqypOeXogutVu9usfcOJI6uk5OwwLqIUSaU+flgmL0LOXv4lH4+URqs7Or8+ABFTcVGGCxg0I3gwhlY2Vjc9nyHY15wqBYdUxLbe8HC6EQp9uwlLlb8LQ6a0=";
50         // when
51         PublicKey serverKey = instance.decodePublicKey(dsaStr);
52         // then
53         assertEquals(serverKey.getAlgorithm(), "DSA");
54     }
55
56     @Test(expected = IllegalArgumentException.class)
57     public void authorizedKeysDecoderInvalidDSAKey() throws GeneralSecurityException {
58         // given
59         String dsaStr = "AAAAB3Nzakc3MAAACBANkM1e45lxlyV24QyWBAoESlHzhYYJUfk/yUd0+Dv28okyO71DmnJesYyUzsKDpnFLlnFhxTTUGSg90fdrdubLFkRTGnHhweegMCf6kU1xyE3U6bpyMdiOXH7fOS6Q2B+qtaQRB4R5TEhdoJX648Ng+YZvLwdbZh3r/et4P46b3DAAAAFQDcu6qp67XRpzMoOS2fIL+VOxvmDwAAAIAeT3d/hbvzPoL8wV52gPtWJMU2EGoX/LJwc86Vn52NlxXB1EQSzZI50PgCKEckS80lj4GXO1ZyuBhdsBEz4rDtAIdZGW5z7WxTfcz0G2dOWmNOBqvu7j9ngfPrgtDVHYV2VL/4VpbmoPgkQLfbA9NWb6US2RnTO46rGbGurigDMQAAAIEAiI3REuOJAmgDow6HxbN0FM+RCe1JYDwJIsCRRK4JA9oYV4Pg897xqypOeXogutVu9usfcOJI6uk5OwwLqIUSaU+flgmL0LOXv4lH4+URqs7Or8+ABFTcVGGCxg0I3gwhlY2Vjc9nyHY15wqBYdUxLbe8HC6EQp9uwlLlb8LQ6a0=";
60         // when
61         instance.decodePublicKey(dsaStr);
62     }
63
64     @Test
65     public void authorizedKeysDecoderValidECDSAKey() throws GeneralSecurityException {
66         // given
67         String ecdsaStr = "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBAP4dTrlwZmz8bZ1f901qWuFk7YelrL2WJG0jrCEAPo9UNM1wywpqjbaYUfoq+cevhLZaukDQ4N2Evux+YQ2zz0=";
68         // when
69         PublicKey serverKey = instance.decodePublicKey(ecdsaStr);
70         // then
71         assertEquals(serverKey.getAlgorithm(), "EC");
72     }
73
74     @Test(expected = IllegalArgumentException.class)
75     public void authorizedKeysDecoderInvalidECDSAKey() throws GeneralSecurityException {
76         // given
77         String ecdsaStr = "AAAAE2VjZHNhLXNoItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBAP4dTrlwZmz8bZ1f901qWuFk7YelrL2WJG0jrCEAPo9UNM1wywpqjbaYUfoq+cevhLZaukDQ4N2Evux+YQ2zz0=";
78         // when
79         instance.decodePublicKey(ecdsaStr);
80     }
81
82     @Test(expected = IllegalArgumentException.class)
83     public void authorizedKeysDecoderInvalidKeyType() throws GeneralSecurityException {
84         // given
85         String ed25519Str = "AAAAC3NzaC1lZDI1NTE5AAAAICIvyX9C+u3KZmJ8x4DuqJg1iAKOPObCgkX9plrvu29R";
86         // when
87         instance.decodePublicKey(ed25519Str);
88     }
89
90     @Test(expected = IllegalArgumentException.class)
91     public void decodingOfBlankInputIsCaughtAsAnError() throws GeneralSecurityException {
92         // when
93         instance.decodePublicKey("");
94     }
95 }