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.
 
 

50 lines
2.2 KiB

package main
import (
"log"
"testing"
)
const validSSHKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDIIEDwge6dWGIMsV2SN+soLHo7o8MbUcHH99u+mhkIkUxsxdIIrm0Q5MrmCzALXCIVO1jl0Y4TFQxtOlS1sPo+CX+4WvHNWM11qu+PccW8u17MFBDinSVSm8V4ZnB6FoZIP5jVNXFW/hTa2uiHrKMbPIHq3Qix6ucEMQHExWrlWk8aaQi2uiRFWtEacfBiWI9XTBiui2IurN+Q47gggibdyi+CaQiKvA921UwC4gVPeBjq/HZp1R5QgD1fdh8OttIzG5ti0py3hoBJnXU6uFFgwsQLX7ex6iZRb8c5/N7C4snsMpowNRorqcyat2SkEB8HNL7qHk3zqd2FF46UISMF"
const validSSHKeyWithComment = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDIIEDwge6dWGIMsV2SN+soLHo7o8MbUcHH99u+mhkIkUxsxdIIrm0Q5MrmCzALXCIVO1jl0Y4TFQxtOlS1sPo+CX+4WvHNWM11qu+PccW8u17MFBDinSVSm8V4ZnB6FoZIP5jVNXFW/hTa2uiHrKMbPIHq3Qix6ucEMQHExWrlWk8aaQi2uiRFWtEacfBiWI9XTBiui2IurN+Q47gggibdyi+CaQiKvA921UwC4gVPeBjq/HZp1R5QgD1fdh8OttIzG5ti0py3hoBJnXU6uFFgwsQLX7ex6iZRb8c5/N7C4snsMpowNRorqcyat2SkEB8HNL7qHk3zqd2FF46UISMF user@host"
const SSHKeyWithNewline = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDIIEDwge6dWGIMsV2SN+soLHo7o8MbUcHH99u+mhkIkUxsxdIIrm0Q5MrmCzALXCIVO1jl0Y4TFQxtOlS1sPo+CX+4WvHNWM11qu+PccW8u17MFBDinSVSm8V4ZnB6FoZIP5jVNXFW/hTa2uiHrKMbPIHq3Qix6ucEMQHExWrlWk8aaQi2uiRFWtEacfBiWI9XTBiui2IurN+Q47gggibdyi+CaQiKvA921UwC4gVPeBjq/HZp1R5QgD1fdh8OttIzG5ti0py3hoBJnXU6uFFgwsQLX7ex6iZRb8c5/N7C4snsMpowNRorqcyat2SkEB8HNL7qHk3zqd2FF46UISMF\n"
func TestValidateSSHKey(t *testing.T) {
var err error
err = validateSSHKey(validSSHKey)
if err != nil {
t.Errorf("Raised error when we should not have: %s", err.Error())
} else {
log.Print("Valid SSH key is valid.")
}
err = validateSSHKey(validSSHKeyWithComment)
if err != nil {
t.Errorf("Raised error when we should not have: %s", err.Error())
} else {
log.Print("Valid SSH key is valid.")
}
err = validateSSHKey("")
if err == nil {
t.Errorf("We think an empty string is valid.")
} else {
log.Printf("Empty string is invalid: %s", err.Error())
}
err = validateSSHKey("dsa AAAAAAAAAA")
if err == nil {
t.Errorf("We don't check properly for the prefix.")
} else {
log.Printf("Invalid prefix makes it invalid: %s", err.Error())
}
err = validateSSHKey(SSHKeyWithNewline)
if err == nil {
t.Errorf("We think it is valid even with a newline.")
} else {
log.Printf("Newline makes the key invalid: %s", err.Error())
}
}