James Moger
2014-04-11 436bd3f0ecdee282c503a9eb0f7a240b7a68ff49
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Copyright 2014 gitblit.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitblit.tests;
 
import java.security.KeyPair;
import java.util.List;
 
import org.junit.Test;
import org.parboiled.common.StringUtils;
 
import com.gitblit.Constants.AccessPermission;
import com.gitblit.transport.ssh.SshKey;
 
/**
 * Tests the Keys Dispatcher and it's commands.
 *
 * @author James Moger
 *
 */
public class SshKeysDispatcherTest extends SshUnitTest {
 
    @Test
    public void testKeysListCommand() throws Exception {
        String result = testSshCommand("keys ls -L");
        List<SshKey> keys = getKeyManager().getKeys(username);
        assertEquals(String.format("There are %d keys!", keys.size()), 2, keys.size());
        assertEquals(keys.get(0).getRawData() + "\n" + keys.get(1).getRawData(), result);
    }
 
    @Test
    public void testKeysWhichCommand() throws Exception {
        String result = testSshCommand("keys which -L");
        List<SshKey> keys = getKeyManager().getKeys(username);
        assertEquals(String.format("There are %d keys!", keys.size()), 2, keys.size());
        assertEquals(keys.get(0).getRawData(), result);
    }
 
    @Test
    public void testKeysRmCommand() throws Exception {
        testSshCommand("keys rm 2");
        String result = testSshCommand("keys ls -L");
        List<SshKey> keys = getKeyManager().getKeys(username);
        assertEquals(String.format("There are %d keys!", keys.size()), 1, keys.size());
        assertEquals(keys.get(0).getRawData(), result);
    }
 
    @Test
    public void testKeysRmAllByIndexCommand() throws Exception {
        testSshCommand("keys rm 1 2");
        List<SshKey> keys = getKeyManager().getKeys(username);
        assertEquals(String.format("There are %d keys!", keys.size()), 0, keys.size());
        try {
            testSshCommand("keys ls -L");
            assertTrue("Authentication worked without a public key?!", false);
        } catch (AssertionError e) {
            assertTrue(true);
        }
    }
 
    @Test
    public void testKeysRmAllCommand() throws Exception {
        testSshCommand("keys rm ALL");
        List<SshKey> keys = getKeyManager().getKeys(username);
        assertEquals(String.format("There are %d keys!", keys.size()), 0, keys.size());
        try {
            testSshCommand("keys ls -L");
            assertTrue("Authentication worked without a public key?!", false);
        } catch (AssertionError e) {
            assertTrue(true);
        }
    }
 
    @Test
    public void testKeysAddCommand() throws Exception {
        KeyPair kp = generator.generateKeyPair();
        SshKey key = new SshKey(kp.getPublic());
        testSshCommand("keys add --permission R", key.getRawData());
        List<SshKey> keys = getKeyManager().getKeys(username);
        assertEquals(String.format("There are %d keys!", keys.size()), 3, keys.size());
        assertEquals(AccessPermission.CLONE, keys.get(2).getPermission());
 
        String result = testSshCommand("keys ls -L");
        StringBuilder sb = new StringBuilder();
        for (SshKey sk : keys) {
            sb.append(sk.getRawData());
            sb.append('\n');
        }
        sb.setLength(sb.length() - 1);
        assertEquals(sb.toString(), result);
    }
 
    @Test
    public void testKeysCommentCommand() throws Exception {
        List<SshKey> keys = getKeyManager().getKeys(username);
        assertTrue(StringUtils.isEmpty(keys.get(0).getComment()));
        String comment = "this is my comment";
        testSshCommand(String.format("keys comment 1 %s", comment));
 
        keys = getKeyManager().getKeys(username);
        assertEquals(comment, keys.get(0).getComment());
    }
}