You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SshKeysDispatcherTest.java 4.8KB

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