|
19 | 19 |
|
20 | 20 | package com.cloud.utils.ssh; |
21 | 21 |
|
| 22 | +import static org.junit.Assert.assertEquals; |
| 23 | +import static org.junit.Assert.assertNotNull; |
| 24 | +import static org.junit.Assert.assertNull; |
22 | 25 | import static org.junit.Assert.assertTrue; |
23 | 26 |
|
| 27 | +import java.nio.charset.StandardCharsets; |
| 28 | +import java.util.Base64; |
| 29 | + |
24 | 30 | import org.junit.Test; |
25 | 31 |
|
26 | 32 | public class SSHKeysHelperTest { |
@@ -70,4 +76,56 @@ public void dsaKeyTest() { |
70 | 76 | assertTrue("fc:6e:ef:31:93:f8:92:2b:a9:03:c7:06:90:f5:ec:bb".equals(fingerprint)); |
71 | 77 |
|
72 | 78 | } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void getPublicKeyFromKeyMaterialShouldHandleSupportedPrefixes() { |
| 82 | + assertEquals("ecdsa-sha2-nistp256 AAAA", SSHKeysHelper.getPublicKeyFromKeyMaterial("ecdsa-sha2-nistp256 AAAA comment")); |
| 83 | + assertEquals("ecdsa-sha2-nistp384 AAAA", SSHKeysHelper.getPublicKeyFromKeyMaterial("ecdsa-sha2-nistp384 AAAA comment")); |
| 84 | + assertEquals("ecdsa-sha2-nistp521 AAAA", SSHKeysHelper.getPublicKeyFromKeyMaterial("ecdsa-sha2-nistp521 AAAA comment")); |
| 85 | + assertEquals("ssh-ed25519 AAAA", SSHKeysHelper.getPublicKeyFromKeyMaterial("ssh-ed25519 AAAA comment")); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void getPublicKeyFromKeyMaterialShouldParseBase64EncodedMaterial() { |
| 90 | + String keyMaterial = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAITestKeyData comment"; |
| 91 | + String encoded = Base64.getEncoder().encodeToString(keyMaterial.getBytes(StandardCharsets.UTF_8)); |
| 92 | + |
| 93 | + assertEquals("ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAITestKeyData", SSHKeysHelper.getPublicKeyFromKeyMaterial(encoded)); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void getPublicKeyFromKeyMaterialShouldReturnNullForInvalidFormats() { |
| 98 | + assertNull(SSHKeysHelper.getPublicKeyFromKeyMaterial("not-a-valid-key")); |
| 99 | + assertNull(SSHKeysHelper.getPublicKeyFromKeyMaterial("ssh-unknown AAAA")); |
| 100 | + assertNull(SSHKeysHelper.getPublicKeyFromKeyMaterial("ssh-rsa")); |
| 101 | + } |
| 102 | + |
| 103 | + @Test(expected = RuntimeException.class) |
| 104 | + public void getPublicKeyFingerprintShouldThrowForInvalidPublicKey() { |
| 105 | + SSHKeysHelper.getPublicKeyFingerprint("invalid-key-format"); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void generatedKeysShouldBeWellFormedAndFingerprintConsistent() { |
| 110 | + SSHKeysHelper helper = new SSHKeysHelper(2048); |
| 111 | + |
| 112 | + String publicKey = helper.getPublicKey(); |
| 113 | + String privateKey = helper.getPrivateKey(); |
| 114 | + String fingerprint = helper.getPublicKeyFingerPrint(); |
| 115 | + |
| 116 | + assertNotNull(publicKey); |
| 117 | + assertTrue(publicKey.startsWith("ssh-rsa ")); |
| 118 | + |
| 119 | + String[] keyParts = publicKey.split(" "); |
| 120 | + assertEquals(2, keyParts.length); |
| 121 | + |
| 122 | + assertNotNull(privateKey); |
| 123 | + assertTrue(privateKey.contains("BEGIN RSA PRIVATE KEY")); |
| 124 | + assertTrue(privateKey.contains("END RSA PRIVATE KEY")); |
| 125 | + |
| 126 | + assertNotNull(fingerprint); |
| 127 | + assertEquals(SSHKeysHelper.getPublicKeyFingerprint(publicKey), fingerprint); |
| 128 | + |
| 129 | + assertTrue("Legacy MD5 fingerprint should be colon-separated hex", fingerprint.matches("^([0-9a-f]{2}:){15}[0-9a-f]{2}$")); |
| 130 | + } |
73 | 131 | } |
0 commit comments