|
| 1 | +package clientconfig |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/netsec-ethz/scion-apps/ssh/config" |
| 9 | + . "github.com/smartystreets/goconvey/convey" |
| 10 | +) |
| 11 | + |
| 12 | +func TestDefaultConfig(t *testing.T) { |
| 13 | + Convey("Given an example SSH config file", t, func() { |
| 14 | + configString := ` |
| 15 | + Host * |
| 16 | + ForwardAgent no |
| 17 | + ForwardX11 no |
| 18 | + ForwardX11Trusted yes |
| 19 | + RhostsRSAAuthentication no |
| 20 | + RSAAuthentication yes |
| 21 | + PasswordAuthentication no |
| 22 | + HostbasedAuthentication yes |
| 23 | + GSSAPIAuthentication no |
| 24 | + GSSAPIDelegateCredentials no |
| 25 | + GSSAPIKeyExchange no |
| 26 | + GSSAPITrustDNS no |
| 27 | + BatchMode no |
| 28 | + CheckHostIP yes |
| 29 | + AddressFamily any |
| 30 | + ConnectTimeout 0 |
| 31 | + StrictHostKeyChecking no |
| 32 | + IdentityFile ~/.ssh/identity |
| 33 | + IdentityFile ~/.ssh/id_rsa |
| 34 | + IdentityFile ~/.ssh/id_dsa |
| 35 | + IdentityFile ~/.ssh/id_ecdsa |
| 36 | + IdentityFile ~/.ssh/id_ed25519 |
| 37 | + Port 65535 |
| 38 | + Protocol 2 |
| 39 | + Cipher 3des |
| 40 | + Ciphers aes128-ctr,aes192-ctr,aes256-ctr$ |
| 41 | + MACs hmac-md5,hmac-sha1,umac-64@openssh.$ |
| 42 | + EscapeChar ~ |
| 43 | + Tunnel no |
| 44 | + TunnelDevice any:any |
| 45 | + PermitLocalCommand no |
| 46 | + VisualHostKey no |
| 47 | + ProxyCommand ssh -q -W %h:%p gateway.exa$ |
| 48 | + RekeyLimit 1G 1h |
| 49 | + SendEnv LANG LC_* |
| 50 | + HashKnownHosts yes |
| 51 | + GSSAPIAuthentication yes |
| 52 | + GSSAPIDelegateCredentials no |
| 53 | + ` |
| 54 | + |
| 55 | + Convey("The new values are read correctly", func() { |
| 56 | + conf := &ClientConfig{} |
| 57 | + config.UpdateFromReader(conf, strings.NewReader(configString)) |
| 58 | + So(conf.HostAddress, ShouldEqual, "") |
| 59 | + So(conf.PasswordAuthentication, ShouldEqual, "no") |
| 60 | + So(conf.StrictHostKeyChecking, ShouldEqual, "no") |
| 61 | + So(conf.Port, ShouldEqual, "65535") |
| 62 | + So(conf.IdentityFile[len(conf.IdentityFile)-1], ShouldEqual, "~/.ssh/identity") |
| 63 | + So(conf.IdentityFile[len(conf.IdentityFile)-2], ShouldEqual, "~/.ssh/id_rsa") |
| 64 | + So(conf.IdentityFile[len(conf.IdentityFile)-3], ShouldEqual, "~/.ssh/id_dsa") |
| 65 | + So(conf.IdentityFile[len(conf.IdentityFile)-4], ShouldEqual, "~/.ssh/id_ecdsa") |
| 66 | + So(conf.IdentityFile[len(conf.IdentityFile)-5], ShouldEqual, "~/.ssh/id_ed25519") |
| 67 | + }) |
| 68 | + |
| 69 | + }) |
| 70 | +} |
| 71 | + |
| 72 | +func TestPortRegex(t *testing.T) { |
| 73 | + Convey("Given a default config file", t, func() { |
| 74 | + conf := &ClientConfig{} |
| 75 | + |
| 76 | + Convey("Valid port numbers are accepted", func() { |
| 77 | + nums := []int{1, 2, 3, 10, 100, 1000, 10000, 45, 652, 3486, 43621, 6554, 66, 65535} |
| 78 | + for _, i := range nums { |
| 79 | + err := config.Set(conf, "Port", i) |
| 80 | + So(err, ShouldEqual, nil) |
| 81 | + So(conf.Port, ShouldEqual, fmt.Sprintf("%v", i)) |
| 82 | + } |
| 83 | + }) |
| 84 | + |
| 85 | + Convey("Invalid port numbers are not accepted", func() { |
| 86 | + nums := []int{-1, 2, 3, 351000, 10064300, 455635, 65345632, 34845636, 6436554, 65536, 70000} |
| 87 | + for _, i := range nums { |
| 88 | + if i >= 0 && i <= 65535 { |
| 89 | + continue |
| 90 | + } |
| 91 | + initialPort := conf.Port |
| 92 | + err := config.Set(conf, "Port", i) |
| 93 | + So(err, ShouldNotEqual, nil) |
| 94 | + So(conf.Port, ShouldEqual, fmt.Sprintf("%v", initialPort)) |
| 95 | + } |
| 96 | + }) |
| 97 | + |
| 98 | + }) |
| 99 | +} |
0 commit comments