github.com/coreos/mantle@v0.13.0/network/ssh_test.go (about) 1 // Copyright 2015 CoreOS, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package network 16 17 import ( 18 "bytes" 19 "fmt" 20 "net" 21 "testing" 22 23 "golang.org/x/crypto/ssh" 24 ) 25 26 var ( 27 testHostKeyBytes = []byte(`-----BEGIN RSA PRIVATE KEY----- 28 MIIBOwIBAAJBALdGZxkXDAjsYk10ihwU6Id2KeILz1TAJuoq4tOgDWxEEGeTrcld 29 r/ZwVaFzjWzxaf6zQIJbfaSEAhqD5yo72+sCAwEAAQJBAK8PEVU23Wj8mV0QjwcJ 30 tZ4GcTUYQL7cF4+ezTCE9a1NrGnCP2RuQkHEKxuTVrxXt+6OF15/1/fuXnxKjmJC 31 nxkCIQDaXvPPBi0c7vAxGwNY9726x01/dNbHCE0CBtcotobxpwIhANbbQbh3JHVW 32 2haQh4fAG5mhesZKAGcxTyv4mQ7uMSQdAiAj+4dzMpJWdSzQ+qGHlHMIBvVHLkqB 33 y2VdEyF7DPCZewIhAI7GOI/6LDIFOvtPo6Bj2nNmyQ1HU6k/LRtNIXi4c9NJAiAr 34 rrxx26itVhJmcvoUhOjwuzSlP2bE5VHAvkGB352YBg== 35 -----END RSA PRIVATE KEY----- 36 `) 37 ) 38 39 func TestEnsurePortSuffix(t *testing.T) { 40 tests := map[string]string{ 41 "host": "host:22", 42 "host:9": "host:9", 43 "[host]": "[host]:22", 44 "[host]:9": "[host]:9", 45 "::1": "[::1]:22", 46 "[::1]:9": "[::1]:9", 47 "127.0.0.1": "127.0.0.1:22", 48 "127.0.0.1:9": "127.0.0.1:9", 49 "[127.0.0.1]": "[127.0.0.1]:22", 50 "[127.0.0.1]:9": "[127.0.0.1]:9", 51 } 52 53 for input, expect := range tests { 54 output := ensurePortSuffix(input, defaultPort) 55 if output != expect { 56 t.Errorf("Got result %q, expected %q", output, expect) 57 } 58 } 59 } 60 61 func TestSSHNewClient(t *testing.T) { 62 m, err := NewSSHAgent(&net.Dialer{}) 63 if err != nil { 64 t.Fatalf("NewSSHAgent failed: %v", err) 65 } 66 67 keys, err := m.List() 68 if err != nil { 69 t.Fatalf("Keys failed: %v", err) 70 } 71 72 cfg := ssh.ServerConfig{ 73 PublicKeyCallback: func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) { 74 if conn.User() == "core" && bytes.Equal(key.Marshal(), keys[0].Marshal()) { 75 return nil, nil 76 } 77 return nil, fmt.Errorf("pubkey rejected") 78 }, 79 } 80 81 hostKey, err := ssh.ParsePrivateKey(testHostKeyBytes) 82 if err != nil { 83 t.Fatalf("ParsePrivateKey failed: %v", err) 84 } 85 cfg.AddHostKey(hostKey) 86 87 listener, err := net.Listen("tcp", "localhost:0") 88 if err != nil { 89 t.Fatalf("Listen failed: %v", err) 90 } 91 defer listener.Close() 92 93 // Oh god... I give up for now. 94 t.Skip("Implementation incomplete") 95 }