321 lines
7.9 KiB
Go
321 lines
7.9 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"os/user"
|
|
"testing"
|
|
"time"
|
|
|
|
openPrivacyLog "git.openprivacy.ca/openprivacy/log"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/x/exp/teatest"
|
|
)
|
|
|
|
const (
|
|
cairdeTestLogFile = "cairde-test.log"
|
|
cwtchTestLogFile = "cwtch-test.log"
|
|
)
|
|
|
|
var currentUser *user.User
|
|
var cairdeLogHandler *os.File
|
|
var cwtchLogHandler *openPrivacyLog.Logger
|
|
|
|
func TestMain(m *testing.M) {
|
|
defer setupAndTeardown()()
|
|
m.Run()
|
|
}
|
|
|
|
func setupAndTeardown() func() {
|
|
_, err := tea.LogToFile(cairdeTestLogFile, "debug")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
cairdeLogHandler, err = os.OpenFile(cairdeTestLogFile, os.O_RDONLY, 0644)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
cwtchLogHandler, err = openPrivacyLog.NewFile(openPrivacyLog.LevelDebug, cwtchTestLogFile)
|
|
if err == nil {
|
|
openPrivacyLog.SetStd(cwtchLogHandler)
|
|
}
|
|
|
|
currentUser, err = user.Current()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return func() {
|
|
if err := cairdeLogHandler.Close(); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if err := os.Remove(cairdeTestLogFile); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if err := os.Remove(cwtchTestLogFile); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestOnOff(t *testing.T) {
|
|
m := newModel(currentUser.Username, currentUser.HomeDir, true)
|
|
testModel := teatest.NewTestModel(t, m)
|
|
|
|
testModel.Send(tea.KeyMsg{
|
|
Type: tea.KeyRunes,
|
|
Runes: []rune("ctrl+c"),
|
|
})
|
|
|
|
testModel.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
|
|
}
|
|
|
|
func TestAcnOnOff(t *testing.T) {
|
|
m := newModel(currentUser.Username, currentUser.HomeDir, true)
|
|
testModel := teatest.NewTestModel(t, m)
|
|
|
|
testModel.Type("/acn on")
|
|
testModel.Send(tea.KeyMsg{Type: tea.KeyEnter})
|
|
|
|
teatest.WaitFor(
|
|
t, cairdeLogHandler,
|
|
func(bts []byte) bool {
|
|
return bytes.Contains(bts, []byte("ACN is up and running, all engines go"))
|
|
},
|
|
teatest.WithCheckInterval(time.Millisecond*100),
|
|
teatest.WithDuration(time.Second*10),
|
|
)
|
|
|
|
testModel.Type("/acn off")
|
|
testModel.Send(tea.KeyMsg{Type: tea.KeyEnter})
|
|
|
|
teatest.WaitFor(
|
|
t, cairdeLogHandler,
|
|
func(bts []byte) bool {
|
|
return bytes.Contains(bts, []byte("ACN successfully turned off"))
|
|
},
|
|
teatest.WithCheckInterval(time.Millisecond*100),
|
|
teatest.WithDuration(time.Second*3),
|
|
)
|
|
|
|
testModel.Send(tea.KeyMsg{
|
|
Type: tea.KeyRunes,
|
|
Runes: []rune("ctrl+c"),
|
|
})
|
|
|
|
testModel.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
|
|
}
|
|
|
|
func TestAcnOnThenQuit(t *testing.T) {
|
|
m := newModel(currentUser.Username, currentUser.HomeDir, true)
|
|
testModel := teatest.NewTestModel(t, m)
|
|
|
|
testModel.Type("/acn on")
|
|
testModel.Send(tea.KeyMsg{Type: tea.KeyEnter})
|
|
|
|
teatest.WaitFor(
|
|
t, cairdeLogHandler,
|
|
func(bts []byte) bool {
|
|
return bytes.Contains(bts, []byte("ACN is up and running, all engines go"))
|
|
},
|
|
teatest.WithCheckInterval(time.Millisecond*100),
|
|
teatest.WithDuration(time.Second*10),
|
|
)
|
|
|
|
testModel.Type("/quit")
|
|
testModel.Send(tea.KeyMsg{Type: tea.KeyEnter})
|
|
|
|
teatest.WaitFor(
|
|
t, cairdeLogHandler,
|
|
func(bts []byte) bool {
|
|
return bytes.Contains(bts, []byte("Shutting down application and ACN now"))
|
|
},
|
|
teatest.WithCheckInterval(time.Millisecond*100),
|
|
teatest.WithDuration(time.Second*3),
|
|
)
|
|
|
|
testModel.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
|
|
}
|
|
|
|
func TestAcnOnThenOnAsksToHold(t *testing.T) {
|
|
m := newModel(currentUser.Username, currentUser.HomeDir, true)
|
|
testModel := teatest.NewTestModel(t, m)
|
|
|
|
testModel.Type("/acn on")
|
|
testModel.Send(tea.KeyMsg{Type: tea.KeyEnter})
|
|
|
|
teatest.WaitFor(
|
|
t, cairdeLogHandler,
|
|
func(bts []byte) bool {
|
|
return bytes.Contains(bts, []byte("Initialising Tor ACN"))
|
|
},
|
|
teatest.WithCheckInterval(time.Millisecond*100),
|
|
teatest.WithDuration(time.Second*10),
|
|
)
|
|
|
|
testModel.Type("/acn on")
|
|
testModel.Send(tea.KeyMsg{Type: tea.KeyEnter})
|
|
|
|
teatest.WaitFor(
|
|
t, cairdeLogHandler,
|
|
func(bts []byte) bool {
|
|
return bytes.Contains(bts, []byte("ACN still initialising, please hold"))
|
|
},
|
|
teatest.WithCheckInterval(time.Millisecond*100),
|
|
teatest.WithDuration(time.Second*3),
|
|
)
|
|
|
|
teatest.WaitFor(
|
|
t, cairdeLogHandler,
|
|
func(bts []byte) bool {
|
|
return bytes.Contains(bts, []byte("ACN is up and running, all engines go"))
|
|
},
|
|
teatest.WithCheckInterval(time.Millisecond*100),
|
|
teatest.WithDuration(time.Second*10),
|
|
)
|
|
|
|
testModel.Type("/quit")
|
|
testModel.Send(tea.KeyMsg{Type: tea.KeyEnter})
|
|
|
|
teatest.WaitFor(
|
|
t, cairdeLogHandler,
|
|
func(bts []byte) bool {
|
|
return bytes.Contains(bts, []byte("Shutting down application and ACN now"))
|
|
},
|
|
teatest.WithCheckInterval(time.Millisecond*100),
|
|
teatest.WithDuration(time.Second*3),
|
|
)
|
|
|
|
testModel.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
|
|
}
|
|
|
|
func TestOnThenOffAsksToWait(t *testing.T) {
|
|
m := newModel(currentUser.Username, currentUser.HomeDir, true)
|
|
testModel := teatest.NewTestModel(t, m)
|
|
|
|
testModel.Type("/acn on")
|
|
testModel.Send(tea.KeyMsg{Type: tea.KeyEnter})
|
|
|
|
teatest.WaitFor(
|
|
t, cairdeLogHandler,
|
|
func(bts []byte) bool {
|
|
return bytes.Contains(bts, []byte("Initialising Tor ACN..."))
|
|
},
|
|
teatest.WithCheckInterval(time.Millisecond*100),
|
|
teatest.WithDuration(time.Second*10),
|
|
)
|
|
|
|
testModel.Type("/acn on")
|
|
testModel.Send(tea.KeyMsg{Type: tea.KeyEnter})
|
|
|
|
teatest.WaitFor(
|
|
t, cairdeLogHandler,
|
|
func(bts []byte) bool {
|
|
return bytes.Contains(bts, []byte("ACN still initialising, please hold"))
|
|
},
|
|
teatest.WithCheckInterval(time.Millisecond*100),
|
|
teatest.WithDuration(time.Second*3),
|
|
)
|
|
|
|
teatest.WaitFor(
|
|
t, cairdeLogHandler,
|
|
func(bts []byte) bool {
|
|
return bytes.Contains(bts, []byte("ACN is up and running, all engines go"))
|
|
},
|
|
teatest.WithCheckInterval(time.Millisecond*100),
|
|
teatest.WithDuration(time.Second*10),
|
|
)
|
|
|
|
testModel.Type("/quit")
|
|
testModel.Send(tea.KeyMsg{Type: tea.KeyEnter})
|
|
|
|
teatest.WaitFor(
|
|
t, cairdeLogHandler,
|
|
func(bts []byte) bool {
|
|
return bytes.Contains(bts, []byte("Shutting down application and ACN now"))
|
|
},
|
|
teatest.WithCheckInterval(time.Millisecond*100),
|
|
teatest.WithDuration(time.Second*3),
|
|
)
|
|
|
|
testModel.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
|
|
}
|
|
|
|
func TestAcnOnThenQuitAsksToWait(t *testing.T) {
|
|
m := newModel(currentUser.Username, currentUser.HomeDir, true)
|
|
testModel := teatest.NewTestModel(t, m)
|
|
|
|
testModel.Type("/acn on")
|
|
testModel.Send(tea.KeyMsg{Type: tea.KeyEnter})
|
|
|
|
teatest.WaitFor(
|
|
t, cairdeLogHandler,
|
|
func(bts []byte) bool {
|
|
return bytes.Contains(bts, []byte("Initialising Tor ACN..."))
|
|
},
|
|
teatest.WithCheckInterval(time.Millisecond*100),
|
|
teatest.WithDuration(time.Second*10),
|
|
)
|
|
|
|
testModel.Type("/quit")
|
|
testModel.Send(tea.KeyMsg{Type: tea.KeyEnter})
|
|
|
|
teatest.WaitFor(
|
|
t, cairdeLogHandler,
|
|
func(bts []byte) bool {
|
|
return bytes.Contains(bts, []byte("ACN still initialising, please hold"))
|
|
},
|
|
teatest.WithCheckInterval(time.Millisecond*100),
|
|
teatest.WithDuration(time.Second*3),
|
|
)
|
|
|
|
teatest.WaitFor(
|
|
t, cairdeLogHandler,
|
|
func(bts []byte) bool {
|
|
return bytes.Contains(bts, []byte("ACN is up and running, all engines go"))
|
|
},
|
|
teatest.WithCheckInterval(time.Millisecond*100),
|
|
teatest.WithDuration(time.Second*10),
|
|
)
|
|
|
|
testModel.Type("/quit")
|
|
testModel.Send(tea.KeyMsg{Type: tea.KeyEnter})
|
|
|
|
teatest.WaitFor(
|
|
t, cairdeLogHandler,
|
|
func(bts []byte) bool {
|
|
return bytes.Contains(bts, []byte("Shutting down application and ACN now"))
|
|
},
|
|
teatest.WithCheckInterval(time.Millisecond*100),
|
|
teatest.WithDuration(time.Second*3),
|
|
)
|
|
|
|
testModel.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
|
|
}
|
|
|
|
func TestAcnOffIsOff(t *testing.T) {
|
|
m := newModel(currentUser.Username, currentUser.HomeDir, true)
|
|
testModel := teatest.NewTestModel(t, m)
|
|
|
|
testModel.Type("/acn off")
|
|
testModel.Send(tea.KeyMsg{Type: tea.KeyEnter})
|
|
|
|
teatest.WaitFor(
|
|
t, cairdeLogHandler,
|
|
func(bts []byte) bool {
|
|
return bytes.Contains(bts, []byte("ACN is currently offline"))
|
|
},
|
|
teatest.WithCheckInterval(time.Millisecond*100),
|
|
teatest.WithDuration(time.Second*10),
|
|
)
|
|
|
|
testModel.Type("/quit")
|
|
testModel.Send(tea.KeyMsg{Type: tea.KeyEnter})
|
|
|
|
testModel.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
|
|
}
|