package common

import (
	"io/ioutil"
	"os"
	"testing"

	"github.com/mitchellh/packer/helper/communicator"
)

func testSSHConfig() *SSHConfig {
	return &SSHConfig{
		Comm: communicator.Config{
			SSHUsername: "foo",
		},
	}
}

func TestSSHConfigPrepare(t *testing.T) {
	c := testSSHConfig()
	errs := c.Prepare(testConfigTemplate(t))
	if len(errs) > 0 {
		t.Fatalf("err: %#v", errs)
	}

	if c.SSHHostPortMin != 2222 {
		t.Errorf("bad min ssh host port: %d", c.SSHHostPortMin)
	}

	if c.SSHHostPortMax != 4444 {
		t.Errorf("bad max ssh host port: %d", c.SSHHostPortMax)
	}

	if c.Comm.SSHPort != 22 {
		t.Errorf("bad ssh port: %d", c.Comm.SSHPort)
	}
}

func TestSSHConfigPrepare_SSHHostPort(t *testing.T) {
	var c *SSHConfig
	var errs []error

	// Bad
	c = testSSHConfig()
	c.SSHHostPortMin = 1000
	c.SSHHostPortMax = 500
	errs = c.Prepare(testConfigTemplate(t))
	if len(errs) == 0 {
		t.Fatalf("bad: %#v", errs)
	}

	// Good
	c = testSSHConfig()
	c.SSHHostPortMin = 50
	c.SSHHostPortMax = 500
	errs = c.Prepare(testConfigTemplate(t))
	if len(errs) > 0 {
		t.Fatalf("should not have error: %s", errs)
	}
}

func TestSSHConfigPrepare_SSHPrivateKey(t *testing.T) {
	var c *SSHConfig
	var errs []error

	c = testSSHConfig()
	c.Comm.SSHPrivateKey = ""
	errs = c.Prepare(testConfigTemplate(t))
	if len(errs) > 0 {
		t.Fatalf("should not have error: %#v", errs)
	}

	c = testSSHConfig()
	c.Comm.SSHPrivateKey = "/i/dont/exist"
	errs = c.Prepare(testConfigTemplate(t))
	if len(errs) == 0 {
		t.Fatal("should have error")
	}

	// Test bad contents
	tf, err := ioutil.TempFile("", "packer")
	if err != nil {
		t.Fatalf("err: %s", err)
	}
	defer os.Remove(tf.Name())
	defer tf.Close()

	if _, err := tf.Write([]byte("HELLO!")); err != nil {
		t.Fatalf("err: %s", err)
	}

	c = testSSHConfig()
	c.Comm.SSHPrivateKey = tf.Name()
	errs = c.Prepare(testConfigTemplate(t))
	if len(errs) == 0 {
		t.Fatal("should have error")
	}

	// Test good contents
	tf.Seek(0, 0)
	tf.Truncate(0)
	tf.Write([]byte(testPem))
	c = testSSHConfig()
	c.Comm.SSHPrivateKey = tf.Name()
	errs = c.Prepare(testConfigTemplate(t))
	if len(errs) > 0 {
		t.Fatalf("should not have error: %#v", errs)
	}
}

func TestSSHConfigPrepare_SSHUser(t *testing.T) {
	var c *SSHConfig
	var errs []error

	c = testSSHConfig()
	c.Comm.SSHUsername = ""
	errs = c.Prepare(testConfigTemplate(t))
	if len(errs) == 0 {
		t.Fatalf("should have error")
	}

	c = testSSHConfig()
	c.Comm.SSHUsername = "exists"
	errs = c.Prepare(testConfigTemplate(t))
	if len(errs) > 0 {
		t.Fatalf("should not have error: %#v", errs)
	}
}

const testPem = `
-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEAxd4iamvrwRJvtNDGQSIbNvvIQN8imXTRWlRY62EvKov60vqu
hh+rDzFYAIIzlmrJopvOe0clqmi3mIP9dtkjPFrYflq52a2CF5q+BdwsJXuRHbJW
LmStZUwW1khSz93DhvhmK50nIaczW63u4EO/jJb3xj+wxR1Nkk9bxi3DDsYFt8SN
AzYx9kjlEYQ/+sI4/ATfmdV9h78SVotjScupd9KFzzi76gWq9gwyCBLRynTUWlyD
2UOfJRkOvhN6/jKzvYfVVwjPSfA9IMuooHdScmC4F6KBKJl/zf/zETM0XyzIDNmH
uOPbCiljq2WoRM+rY6ET84EO0kVXbfx8uxUsqQIDAQABAoIBAQCkPj9TF0IagbM3
5BSs/CKbAWS4dH/D4bPlxx4IRCNirc8GUg+MRb04Xz0tLuajdQDqeWpr6iLZ0RKV
BvreLF+TOdV7DNQ4XE4gSdJyCtCaTHeort/aordL3l0WgfI7mVk0L/yfN1PEG4YG
E9q1TYcyrB3/8d5JwIkjabxERLglCcP+geOEJp+QijbvFIaZR/n2irlKW4gSy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