2019-11-29 22:12:38 +01:00
|
|
|
package alpine_builder
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/GehirnInc/crypt"
|
|
|
|
_ "github.com/GehirnInc/crypt/sha256_crypt"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
systemShadow = "test_shadow"
|
|
|
|
systemDropbearConfig = "test_dropbear"
|
|
|
|
systemDropbearRestart = "restart_dropbear_command"
|
|
|
|
systemReboot = "reboot_command"
|
|
|
|
systemShutdown = "shutdown_command"
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemSetRootPassword(t *testing.T) {
|
|
|
|
ass := assert.New(t)
|
|
|
|
|
|
|
|
ass.NoError(SystemSetRootPassword("password"))
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(systemShadow)
|
|
|
|
ass.NoError(err)
|
|
|
|
hash := strings.Split(string(data), ":")[1]
|
|
|
|
|
|
|
|
crypter := crypt.SHA256.New()
|
|
|
|
ass.NoError(crypter.Verify(hash, []byte("password")))
|
|
|
|
|
|
|
|
_ = os.Remove(systemShadow)
|
|
|
|
}
|
|
|
|
|
2019-12-10 18:57:01 +01:00
|
|
|
func TestSystemSSHEnabled(t *testing.T) {
|
|
|
|
ass := assert.New(t)
|
|
|
|
|
|
|
|
_, err := SystemSSHEnabled()
|
|
|
|
ass.EqualError(err, "failed to read ssh config: open test_dropbear: no such file or directory")
|
|
|
|
|
|
|
|
ass.NoError(ioutil.WriteFile(systemDropbearConfig, []byte("test"), os.ModePerm))
|
|
|
|
value, err := SystemSSHEnabled()
|
|
|
|
ass.NoError(err)
|
2019-12-15 16:24:44 +01:00
|
|
|
ass.True(value)
|
2019-12-10 18:57:01 +01:00
|
|
|
|
|
|
|
ass.NoError(ioutil.WriteFile(systemDropbearConfig, []byte("DROPBEAR_OPTS=\"-p 127.0.0.1:22\""), os.ModePerm))
|
|
|
|
value, err = SystemSSHEnabled()
|
|
|
|
ass.NoError(err)
|
2019-12-15 16:24:44 +01:00
|
|
|
ass.False(value)
|
2019-12-10 18:57:01 +01:00
|
|
|
|
|
|
|
_ = os.Remove(systemDropbearConfig)
|
|
|
|
}
|
|
|
|
|
2019-11-29 22:12:38 +01:00
|
|
|
func TestSystemEnableSSH(t *testing.T) {
|
|
|
|
ass := assert.New(t)
|
|
|
|
|
|
|
|
ass.EqualError(SystemEnableSSH(), "failed to restart ssh server: exec: \"restart_dropbear_command\": executable file not found in $PATH")
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(systemDropbearConfig)
|
|
|
|
ass.NoError(err)
|
|
|
|
|
|
|
|
ass.Equal("DROPBEAR_OPTS=\"\"", string(data))
|
|
|
|
|
|
|
|
_ = os.Remove(systemDropbearConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemDisableSSH(t *testing.T) {
|
|
|
|
ass := assert.New(t)
|
|
|
|
|
|
|
|
ass.EqualError(SystemDisableSSH(), "failed to restart ssh server: exec: \"restart_dropbear_command\": executable file not found in $PATH")
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(systemDropbearConfig)
|
|
|
|
ass.NoError(err)
|
|
|
|
|
|
|
|
ass.Equal("DROPBEAR_OPTS=\"-p 127.0.0.1:22\"", string(data))
|
|
|
|
|
|
|
|
_ = os.Remove(systemDropbearConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemReboot(t *testing.T) {
|
|
|
|
ass := assert.New(t)
|
|
|
|
|
|
|
|
ass.EqualError(SystemReboot(),
|
|
|
|
"failed to start system reboot: exec: \"reboot_command\": executable file not found in $PATH")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSystemShutdown(t *testing.T) {
|
|
|
|
ass := assert.New(t)
|
|
|
|
|
|
|
|
ass.EqualError(SystemShutdown(),
|
|
|
|
"failed to start system shutdown: exec: \"shutdown_command\": executable file not found in $PATH")
|
|
|
|
}
|