2019-03-13 04:14:30 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-11-03 11:06:11 -08:00
|
|
|
type sessionArgs struct {
|
|
|
|
userInfoSeparator string
|
|
|
|
username string
|
|
|
|
hostname string
|
|
|
|
goos string
|
|
|
|
connection string
|
|
|
|
client string
|
|
|
|
sshIcon string
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupSession(args *sessionArgs) session {
|
2019-03-13 04:14:30 -07:00
|
|
|
env := new(MockedEnvironment)
|
2020-11-03 11:06:11 -08:00
|
|
|
env.On("getCurrentUser", nil).Return(args.username)
|
|
|
|
env.On("getHostName", nil).Return(args.hostname, nil)
|
|
|
|
env.On("getRuntimeGOOS", nil).Return(args.goos)
|
|
|
|
env.On("getenv", "SSH_CONNECTION").Return(args.connection)
|
|
|
|
env.On("getenv", "SSH_CLIENT").Return(args.client)
|
2019-03-13 04:14:30 -07:00
|
|
|
props := &properties{
|
2020-11-03 11:06:11 -08:00
|
|
|
values: map[Property]interface{}{
|
|
|
|
UserInfoSeparator: args.userInfoSeparator,
|
|
|
|
SSHIcon: args.sshIcon,
|
|
|
|
},
|
2019-03-13 04:14:30 -07:00
|
|
|
foreground: "#fff",
|
|
|
|
background: "#000",
|
|
|
|
}
|
|
|
|
s := session{
|
|
|
|
env: env,
|
|
|
|
props: props,
|
|
|
|
}
|
2020-10-07 08:14:26 -07:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-11-03 11:06:11 -08:00
|
|
|
func testUserInfoWriter(args *sessionArgs) string {
|
|
|
|
s := setupSession(args)
|
2020-12-02 10:00:46 -08:00
|
|
|
_ = s.enabled()
|
2019-03-13 04:14:30 -07:00
|
|
|
return s.getFormattedText()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteUserInfo(t *testing.T) {
|
|
|
|
want := "<#fff>bill</>@<#fff>surface</>"
|
2020-11-03 11:06:11 -08:00
|
|
|
args := &sessionArgs{
|
|
|
|
userInfoSeparator: "@",
|
|
|
|
username: "bill",
|
|
|
|
hostname: "surface",
|
|
|
|
goos: "windows",
|
|
|
|
}
|
|
|
|
got := testUserInfoWriter(args)
|
2019-03-13 04:14:30 -07:00
|
|
|
assert.EqualValues(t, want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteUserInfoWindowsIncludingHostname(t *testing.T) {
|
|
|
|
want := "<#fff>bill</>@<#fff>surface</>"
|
2020-11-03 11:06:11 -08:00
|
|
|
args := &sessionArgs{
|
|
|
|
userInfoSeparator: "@",
|
|
|
|
username: "surface\\bill",
|
|
|
|
hostname: "surface",
|
|
|
|
goos: "windows",
|
|
|
|
}
|
|
|
|
got := testUserInfoWriter(args)
|
2019-03-13 04:14:30 -07:00
|
|
|
assert.EqualValues(t, want, got)
|
|
|
|
}
|
2020-10-07 08:14:26 -07:00
|
|
|
|
|
|
|
func TestWriteOnlyUsername(t *testing.T) {
|
2020-11-03 11:06:11 -08:00
|
|
|
args := &sessionArgs{
|
|
|
|
userInfoSeparator: "@",
|
|
|
|
username: "surface\\bill",
|
|
|
|
hostname: "surface",
|
|
|
|
goos: "windows",
|
|
|
|
}
|
|
|
|
s := setupSession(args)
|
2020-10-07 08:14:26 -07:00
|
|
|
s.props.values[DisplayHost] = false
|
|
|
|
want := "<#fff>bill</><#fff></>"
|
2020-12-02 10:00:46 -08:00
|
|
|
assert.True(t, s.enabled())
|
2020-10-07 08:14:26 -07:00
|
|
|
got := s.getFormattedText()
|
|
|
|
assert.EqualValues(t, want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteOnlyHostname(t *testing.T) {
|
2020-11-03 11:06:11 -08:00
|
|
|
args := &sessionArgs{
|
|
|
|
userInfoSeparator: "@",
|
|
|
|
username: "surface\\bill",
|
|
|
|
hostname: "surface",
|
|
|
|
goos: "windows",
|
|
|
|
}
|
|
|
|
s := setupSession(args)
|
2020-10-07 08:14:26 -07:00
|
|
|
s.props.values[DisplayUser] = false
|
|
|
|
want := "<#fff></><#fff>surface</>"
|
2020-12-02 10:00:46 -08:00
|
|
|
assert.True(t, s.enabled())
|
2020-10-07 08:14:26 -07:00
|
|
|
got := s.getFormattedText()
|
|
|
|
assert.EqualValues(t, want, got)
|
|
|
|
}
|
2020-10-12 03:53:54 -07:00
|
|
|
|
2020-11-03 11:06:11 -08:00
|
|
|
func TestWriteActiveSSHSession(t *testing.T) {
|
|
|
|
want := "ssh <#fff>bill</>@<#fff>surface</>"
|
|
|
|
args := &sessionArgs{
|
|
|
|
userInfoSeparator: "@",
|
|
|
|
username: "bill",
|
|
|
|
hostname: "surface",
|
|
|
|
goos: "windows",
|
|
|
|
sshIcon: "ssh ",
|
|
|
|
connection: "1.1.1.1",
|
|
|
|
}
|
|
|
|
got := testUserInfoWriter(args)
|
|
|
|
assert.EqualValues(t, want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestActiveSSHSessionInactive(t *testing.T) {
|
|
|
|
env := new(MockedEnvironment)
|
|
|
|
env.On("getenv", "SSH_CONNECTION").Return("")
|
|
|
|
env.On("getenv", "SSH_CLIENT").Return("")
|
|
|
|
s := &session{
|
|
|
|
env: env,
|
|
|
|
}
|
|
|
|
assert.False(t, s.activeSSHSession())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestActiveSSHSessionActiveConnection(t *testing.T) {
|
|
|
|
env := new(MockedEnvironment)
|
|
|
|
env.On("getenv", "SSH_CONNECTION").Return("1.1.1.1")
|
|
|
|
env.On("getenv", "SSH_CLIENT").Return("")
|
|
|
|
s := &session{
|
|
|
|
env: env,
|
|
|
|
}
|
|
|
|
assert.True(t, s.activeSSHSession())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestActiveSSHSessionActiveClient(t *testing.T) {
|
|
|
|
env := new(MockedEnvironment)
|
|
|
|
env.On("getenv", "SSH_CONNECTION").Return("")
|
|
|
|
env.On("getenv", "SSH_CLIENT").Return("1.1.1.1")
|
|
|
|
s := &session{
|
|
|
|
env: env,
|
|
|
|
}
|
|
|
|
assert.True(t, s.activeSSHSession())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestActiveSSHSessionActiveBoth(t *testing.T) {
|
|
|
|
env := new(MockedEnvironment)
|
|
|
|
env.On("getenv", "SSH_CONNECTION").Return("2.2.2.2")
|
|
|
|
env.On("getenv", "SSH_CLIENT").Return("1.1.1.1")
|
2020-10-12 03:53:54 -07:00
|
|
|
s := &session{
|
2020-11-03 11:06:11 -08:00
|
|
|
env: env,
|
2020-10-12 03:53:54 -07:00
|
|
|
}
|
2020-11-03 11:06:11 -08:00
|
|
|
assert.True(t, s.activeSSHSession())
|
2020-10-12 03:53:54 -07:00
|
|
|
}
|
2021-02-15 13:01:09 -08:00
|
|
|
|
|
|
|
func TestSessionSegmentTemplate(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Case string
|
|
|
|
ExpectedString string
|
|
|
|
UserName string
|
|
|
|
ComputerName string
|
|
|
|
SSHSession bool
|
|
|
|
Root bool
|
|
|
|
Template string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Case: "user and computer",
|
|
|
|
ExpectedString: "john@company-laptop",
|
|
|
|
ComputerName: "company-laptop",
|
|
|
|
UserName: "john",
|
|
|
|
Template: "{{.UserName}}{{if .ComputerName}}@{{.ComputerName}}{{end}}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "user only",
|
|
|
|
ExpectedString: "john",
|
|
|
|
UserName: "john",
|
|
|
|
Template: "{{.UserName}}{{if .ComputerName}}@{{.ComputerName}}{{end}}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "user with ssh",
|
|
|
|
ExpectedString: "john on remote",
|
|
|
|
UserName: "john",
|
|
|
|
SSHSession: true,
|
|
|
|
ComputerName: "remote",
|
|
|
|
Template: "{{.UserName}}{{if .SSHSession}} on {{.ComputerName}}{{end}}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "user without ssh",
|
|
|
|
ExpectedString: "john",
|
|
|
|
UserName: "john",
|
|
|
|
SSHSession: false,
|
|
|
|
ComputerName: "remote",
|
|
|
|
Template: "{{.UserName}}{{if .SSHSession}} on {{.ComputerName}}{{end}}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "user with root and ssh",
|
|
|
|
ExpectedString: "super john on remote",
|
|
|
|
UserName: "john",
|
|
|
|
SSHSession: true,
|
|
|
|
ComputerName: "remote",
|
|
|
|
Root: true,
|
|
|
|
Template: "{{if .Root}}super {{end}}{{.UserName}}{{if .SSHSession}} on {{.ComputerName}}{{end}}",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Case: "no template",
|
|
|
|
ExpectedString: "\uf817 <>john</>@<>remote</>",
|
|
|
|
UserName: "john",
|
|
|
|
SSHSession: true,
|
|
|
|
ComputerName: "remote",
|
|
|
|
Root: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
env := new(MockedEnvironment)
|
|
|
|
env.On("getCurrentUser", nil).Return(tc.UserName)
|
|
|
|
env.On("getRuntimeGOOS", nil).Return("burp")
|
|
|
|
env.On("getHostName", nil).Return(tc.ComputerName, nil)
|
|
|
|
var SSHSession string
|
|
|
|
if tc.SSHSession {
|
|
|
|
SSHSession = "zezzion"
|
|
|
|
}
|
|
|
|
env.On("getenv", "SSH_CONNECTION").Return(SSHSession)
|
|
|
|
env.On("getenv", "SSH_CLIENT").Return(SSHSession)
|
|
|
|
env.On("isRunningAsRoot", nil).Return(tc.Root)
|
|
|
|
props := &properties{
|
|
|
|
values: map[Property]interface{}{
|
|
|
|
SegmentTemplate: tc.Template,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
session := &session{
|
|
|
|
env: env,
|
|
|
|
props: props,
|
|
|
|
}
|
|
|
|
_ = session.enabled()
|
|
|
|
assert.Equal(t, tc.ExpectedString, session.string(), tc.Case)
|
|
|
|
}
|
|
|
|
}
|