Config tests
This commit is contained in:
parent
da1464009f
commit
cd13abdf50
|
@ -15,11 +15,11 @@ func NewConfEntry(key string, value *string) *ConfEntry {
|
||||||
return &ConfEntry{Key: key, Value: value}
|
return &ConfEntry{Key: key, Value: value}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Conn) SetConf(entries []*ConfEntry) error {
|
func (c *Conn) SetConf(entries ...*ConfEntry) error {
|
||||||
return c.sendSetConf("SETCONF", entries)
|
return c.sendSetConf("SETCONF", entries)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Conn) ResetConf(entries []*ConfEntry) error {
|
func (c *Conn) ResetConf(entries ...*ConfEntry) error {
|
||||||
return c.sendSetConf("RESETCONF", entries)
|
return c.sendSetConf("RESETCONF", entries)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -48,7 +48,7 @@ type InfoValue struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Conn) GetInfo(keys ...string) ([]*InfoValue, error) {
|
func (c *Conn) GetInfo(keys ...string) ([]*InfoValue, error) {
|
||||||
resp, err := c.SendRequest("GETCONF %v", strings.Join(keys, " "))
|
resp, err := c.SendRequest("GETINFO %v", strings.Join(keys, " "))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,12 +5,20 @@ import "testing"
|
||||||
func TestAuthenticateNull(t *testing.T) {
|
func TestAuthenticateNull(t *testing.T) {
|
||||||
ctx, conn := NewTestContextConnected(t)
|
ctx, conn := NewTestContextConnected(t)
|
||||||
defer ctx.CloseConnected(conn)
|
defer ctx.CloseConnected(conn)
|
||||||
|
// Verify auth methods before auth
|
||||||
|
info, err := conn.ProtocolInfo()
|
||||||
|
ctx.Require.NoError(err)
|
||||||
|
ctx.Require.ElementsMatch([]string{"NULL"}, info.AuthMethods)
|
||||||
ctx.Require.NoError(conn.Authenticate(""))
|
ctx.Require.NoError(conn.Authenticate(""))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAuthenticateSafeCookie(t *testing.T) {
|
func TestAuthenticateSafeCookie(t *testing.T) {
|
||||||
ctx, conn := NewTestContextConnected(t, "--CookieAuthentication", "1")
|
ctx, conn := NewTestContextConnected(t, "--CookieAuthentication", "1")
|
||||||
defer ctx.CloseConnected(conn)
|
defer ctx.CloseConnected(conn)
|
||||||
|
// Verify auth methods before auth
|
||||||
|
info, err := conn.ProtocolInfo()
|
||||||
|
ctx.Require.NoError(err)
|
||||||
|
ctx.Require.ElementsMatch([]string{"COOKIE", "SAFECOOKIE"}, info.AuthMethods)
|
||||||
ctx.Require.NoError(conn.Authenticate(""))
|
ctx.Require.NoError(conn.Authenticate(""))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,5 +27,9 @@ func TestAuthenticateHashedPassword(t *testing.T) {
|
||||||
ctx, conn := NewTestContextConnected(t, "--HashedControlPassword",
|
ctx, conn := NewTestContextConnected(t, "--HashedControlPassword",
|
||||||
"16:5417AE717521511A609921392778FFA8518EC089BF2162A199241AEB4A")
|
"16:5417AE717521511A609921392778FFA8518EC089BF2162A199241AEB4A")
|
||||||
defer ctx.CloseConnected(conn)
|
defer ctx.CloseConnected(conn)
|
||||||
|
// Verify auth methods before auth
|
||||||
|
info, err := conn.ProtocolInfo()
|
||||||
|
ctx.Require.NoError(err)
|
||||||
|
ctx.Require.ElementsMatch([]string{"HASHESPASSWORD"}, info.AuthMethods)
|
||||||
ctx.Require.NoError(conn.Authenticate("testpass"))
|
ctx.Require.NoError(conn.Authenticate("testpass"))
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,72 @@
|
||||||
|
package controltest
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/cretz/bine/control"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetSetAndResetConf(t *testing.T) {
|
||||||
|
ctx, conn := NewTestContextAuthenticated(t)
|
||||||
|
defer ctx.CloseConnected(conn)
|
||||||
|
// Simple get conf
|
||||||
|
assertConfVals := func(val string) {
|
||||||
|
entries, err := conn.GetConf("LogMessageDomains", "ProtocolWarnings")
|
||||||
|
ctx.Require.NoError(err)
|
||||||
|
ctx.Require.Len(entries, 2)
|
||||||
|
ctx.Require.Contains(entries, &control.ConfEntry{Key: "LogMessageDomains", Value: &val})
|
||||||
|
ctx.Require.Contains(entries, &control.ConfEntry{Key: "ProtocolWarnings", Value: &val})
|
||||||
|
}
|
||||||
|
assertConfVals("0")
|
||||||
|
// Change em both to 1
|
||||||
|
one := "1"
|
||||||
|
err := conn.SetConf(&control.ConfEntry{Key: "LogMessageDomains", Value: &one},
|
||||||
|
&control.ConfEntry{Key: "ProtocolWarnings", Value: &one})
|
||||||
|
ctx.Require.NoError(err)
|
||||||
|
// Check again
|
||||||
|
assertConfVals(one)
|
||||||
|
// Reset em both
|
||||||
|
err = conn.ResetConf(&control.ConfEntry{Key: "LogMessageDomains"}, &control.ConfEntry{Key: "ProtocolWarnings"})
|
||||||
|
ctx.Require.NoError(err)
|
||||||
|
// Make sure both back to zero
|
||||||
|
assertConfVals("0")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoadConf(t *testing.T) {
|
||||||
|
ctx, conn := NewTestContextAuthenticated(t)
|
||||||
|
defer ctx.CloseConnected(conn)
|
||||||
|
// Get entire conf text
|
||||||
|
vals, err := conn.GetInfo("config-text")
|
||||||
|
ctx.Require.NoError(err)
|
||||||
|
ctx.Require.Len(vals, 1)
|
||||||
|
ctx.Require.Equal("config-text", vals[0].Key)
|
||||||
|
confText := vals[0].Value
|
||||||
|
// Append new conf val and load
|
||||||
|
ctx.Require.NotContains(confText, "LogMessageDomains")
|
||||||
|
confText += "\r\nLogMessageDomains 1"
|
||||||
|
ctx.Require.NoError(conn.LoadConf(confText))
|
||||||
|
// Check the new val
|
||||||
|
vals, err = conn.GetInfo("config-text")
|
||||||
|
ctx.Require.NoError(err)
|
||||||
|
ctx.Require.Len(vals, 1)
|
||||||
|
ctx.Require.Equal("config-text", vals[0].Key)
|
||||||
|
ctx.Require.Contains(vals[0].Value, "LogMessageDomains 1")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSaveConf(t *testing.T) {
|
||||||
|
ctx, conn := NewTestContextAuthenticated(t)
|
||||||
|
defer ctx.CloseConnected(conn)
|
||||||
|
// Get conf filename
|
||||||
|
vals, err := conn.GetInfo("config-file")
|
||||||
|
ctx.Require.NoError(err)
|
||||||
|
ctx.Require.Len(vals, 1)
|
||||||
|
ctx.Require.Equal("config-file", vals[0].Key)
|
||||||
|
confFile := vals[0].Value
|
||||||
|
// Save it
|
||||||
|
ctx.Require.NoError(conn.SaveConf(false))
|
||||||
|
// Read and make sure, say, the DataDirectory is accurate
|
||||||
|
confText, err := ioutil.ReadFile(confFile)
|
||||||
|
ctx.Require.NoError(err)
|
||||||
|
ctx.Require.Contains(string(confText), "DataDirectory "+ctx.TestTor.DataDir)
|
||||||
|
}
|
|
@ -35,6 +35,16 @@ func NewTestContextConnected(t *testing.T, extraTorArgs ...string) (*TestContext
|
||||||
return ctx, conn
|
return ctx, conn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewTestContextAuthenticated(t *testing.T, extraTorArgs ...string) (*TestContext, *control.Conn) {
|
||||||
|
ctx, conn := NewTestContextConnected(t, extraTorArgs...)
|
||||||
|
if err := conn.Authenticate(""); err != nil {
|
||||||
|
conn.Close()
|
||||||
|
ctx.Close()
|
||||||
|
ctx.Fatal(err)
|
||||||
|
}
|
||||||
|
return ctx, conn
|
||||||
|
}
|
||||||
|
|
||||||
func (t *TestContext) EnsureTestTorStarted() {
|
func (t *TestContext) EnsureTestTorStarted() {
|
||||||
if t.TestTor == nil {
|
if t.TestTor == nil {
|
||||||
var err error
|
var err error
|
||||||
|
|
|
@ -15,7 +15,7 @@ import (
|
||||||
var torExePath string
|
var torExePath string
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flag.StringVar(&torExePath, "tor", "tor", "The TOR exe path")
|
flag.StringVar(&torExePath, "tor.path", "tor", "The TOR exe path")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,9 +33,15 @@ func StartTestTor(ctx context.Context, extraArgs ...string) (*TestTor, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
controlPortFile := filepath.Join(dataDir, "control-port")
|
controlPortFile := filepath.Join(dataDir, "control-port")
|
||||||
|
// We have to touch the torrc
|
||||||
|
torrcFile := filepath.Join(dataDir, "test-torrc")
|
||||||
|
if err = ioutil.WriteFile(torrcFile, nil, os.FileMode(0600)); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
ret := &TestTor{
|
ret := &TestTor{
|
||||||
DataDir: dataDir,
|
DataDir: dataDir,
|
||||||
OrigArgs: append([]string{
|
OrigArgs: append([]string{
|
||||||
|
"-f", torrcFile,
|
||||||
// "--quiet",
|
// "--quiet",
|
||||||
"--DisableNetwork", "1",
|
"--DisableNetwork", "1",
|
||||||
"--ControlPort", "auto",
|
"--ControlPort", "auto",
|
||||||
|
|
|
@ -82,13 +82,14 @@ func (c *Conn) ReadResponse() (*Response, error) {
|
||||||
resp.Data = append(resp.Data, line[4:])
|
resp.Data = append(resp.Data, line[4:])
|
||||||
case '+':
|
case '+':
|
||||||
// A "dot-encoded" payload follows.
|
// A "dot-encoded" payload follows.
|
||||||
resp.Data = append(resp.Data, line[4:])
|
|
||||||
dotBody, err := c.conn.ReadDotBytes()
|
dotBody, err := c.conn.ReadDotBytes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
resp.Data = append(resp.Data, strings.TrimRight(string(dotBody), "\n\r"))
|
dotBodyStr := strings.TrimRight(string(dotBody), "\n\r")
|
||||||
dotLines := strings.Split(string(dotBody), "\n")
|
c.debugf("Read dot body:\n---\n%v\n---", dotBodyStr)
|
||||||
|
resp.Data = append(resp.Data, line[4:]+dotBodyStr)
|
||||||
|
dotLines := strings.Split(dotBodyStr, "\n")
|
||||||
for _, dotLine := range dotLines[:len(dotLines)-1] {
|
for _, dotLine := range dotLines[:len(dotLines)-1] {
|
||||||
resp.RawLines = append(resp.RawLines, dotLine)
|
resp.RawLines = append(resp.RawLines, dotLine)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package process
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -17,5 +18,8 @@ func NewProcessCreator(exePath string) ProcessCreator {
|
||||||
return &exeProcessCreator{exePath}
|
return &exeProcessCreator{exePath}
|
||||||
}
|
}
|
||||||
func (e *exeProcessCreator) New(ctx context.Context, args ...string) (Process, error) {
|
func (e *exeProcessCreator) New(ctx context.Context, args ...string) (Process, error) {
|
||||||
return &exeProcess{Cmd: exec.CommandContext(ctx, e.exePath, args...)}, nil
|
proc := &exeProcess{Cmd: exec.CommandContext(ctx, e.exePath, args...)}
|
||||||
|
proc.Stdout = os.Stdout
|
||||||
|
proc.Stderr = os.Stderr
|
||||||
|
return proc, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue