From 07d4495b27f7979ddfaa5dde58a22fc3ccc9409a Mon Sep 17 00:00:00 2001 From: V2Ray Date: Sun, 20 Sep 2015 11:45:40 +0200 Subject: [PATCH] test cases for json config --- common/net/transport.go | 1 - config/json/json.go | 8 +++--- config/json/json_test.go | 47 +++++++++++++++++++++++++++++++++ testing/unit/assertions.go | 4 +++ testing/unit/pointersubject.go | 48 ++++++++++++++++++++++++++++++++++ testing/unit/uint16subject.go | 6 +++++ 6 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 config/json/json_test.go create mode 100644 testing/unit/pointersubject.go diff --git a/common/net/transport.go b/common/net/transport.go index 20741bdee..a4b66797a 100644 --- a/common/net/transport.go +++ b/common/net/transport.go @@ -19,7 +19,6 @@ func ReaderToChan(stream chan<- []byte, reader io.Reader) error { return err } } - return nil } func ChanToWriter(writer io.Writer, stream <-chan []byte) error { diff --git a/config/json/json.go b/config/json/json.go index 76ff0d0d8..5a8f3cbf8 100644 --- a/config/json/json.go +++ b/config/json/json.go @@ -3,6 +3,7 @@ package json import ( "encoding/json" "io/ioutil" + "os" "path/filepath" "github.com/v2ray/v2ray-core" @@ -49,7 +50,8 @@ func (config *Config) OutboundConfig() core.ConnectionConfig { } func LoadConfig(file string) (*Config, error) { - rawConfig, err := ioutil.ReadFile(file) + fixedFile := os.ExpandEnv(file) + rawConfig, err := ioutil.ReadFile(fixedFile) if err != nil { log.Error("Failed to read point config file (%s): %v", file, err) return nil, err @@ -59,11 +61,11 @@ func LoadConfig(file string) (*Config, error) { err = json.Unmarshal(rawConfig, config) if !filepath.IsAbs(config.InboundConfigValue.File) && len(config.InboundConfigValue.File) > 0 { - config.InboundConfigValue.File = filepath.Join(filepath.Dir(file), config.InboundConfigValue.File) + config.InboundConfigValue.File = filepath.Join(filepath.Dir(fixedFile), config.InboundConfigValue.File) } if !filepath.IsAbs(config.OutboundConfigValue.File) && len(config.OutboundConfigValue.File) > 0 { - config.OutboundConfigValue.File = filepath.Join(filepath.Dir(file), config.OutboundConfigValue.File) + config.OutboundConfigValue.File = filepath.Join(filepath.Dir(fixedFile), config.OutboundConfigValue.File) } return config, err diff --git a/config/json/json_test.go b/config/json/json_test.go new file mode 100644 index 000000000..52b10af0e --- /dev/null +++ b/config/json/json_test.go @@ -0,0 +1,47 @@ +package json + +import ( + "path/filepath" + "testing" + + "github.com/v2ray/v2ray-core/testing/unit" +) + +func TestClientSampleConfig(t *testing.T) { + assert := unit.Assert(t) + + // TODO: fix for Windows + baseDir := "$GOPATH/src/github.com/v2ray/v2ray-core/release/config" + + config, err := LoadConfig(filepath.Join(baseDir, "vpoint_socks_vmess.json")) + assert.Error(err).IsNil() + + assert.Uint16(config.PortValue).Positive() + assert.Pointer(config.InboundConfigValue).IsNotNil() + assert.Pointer(config.OutboundConfigValue).IsNotNil() + + assert.String(config.InboundConfigValue.ProtocolString).Equals("socks") + assert.Int(len(config.InboundConfigValue.Content())).GreaterThan(0) + + assert.String(config.OutboundConfigValue.ProtocolString).Equals("vmess") + assert.Int(len(config.OutboundConfigValue.Content())).GreaterThan(0) +} + +func TestServerSampleConfig(t *testing.T) { + assert := unit.Assert(t) + + // TODO: fix for Windows + baseDir := "$GOPATH/src/github.com/v2ray/v2ray-core/release/config" + + config, err := LoadConfig(filepath.Join(baseDir, "vpoint_vmess_freedom.json")) + assert.Error(err).IsNil() + + assert.Uint16(config.PortValue).Positive() + assert.Pointer(config.InboundConfigValue).IsNotNil() + assert.Pointer(config.OutboundConfigValue).IsNotNil() + + assert.String(config.InboundConfigValue.ProtocolString).Equals("vmess") + assert.Int(len(config.InboundConfigValue.Content())).GreaterThan(0) + + assert.String(config.OutboundConfigValue.ProtocolString).Equals("freedom") +} diff --git a/testing/unit/assertions.go b/testing/unit/assertions.go index fe5b2cfc4..1b10d1b9e 100644 --- a/testing/unit/assertions.go +++ b/testing/unit/assertions.go @@ -47,3 +47,7 @@ func (a *Assertion) Error(value error) *ErrorSubject { func (a *Assertion) Bool(value bool) *BoolSubject { return NewBoolSubject(NewSubject(a), value) } + +func (a *Assertion) Pointer(value interface{}) *PointerSubject { + return NewPointerSubject(NewSubject(a), value) +} diff --git a/testing/unit/pointersubject.go b/testing/unit/pointersubject.go new file mode 100644 index 000000000..e7ed60bfb --- /dev/null +++ b/testing/unit/pointersubject.go @@ -0,0 +1,48 @@ +package unit + +import ( + "fmt" +) + +type PointerSubject struct { + *Subject + value interface{} +} + +func NewPointerSubject(base *Subject, value interface{}) *PointerSubject { + return &PointerSubject{ + Subject: base, + value: value, + } +} + +func (subject *PointerSubject) Named(name string) *PointerSubject { + subject.Subject.Named(name) + return subject +} + +func (subject *PointerSubject) Fail(verb string, other interface{}) { + subject.FailWithMessage(fmt.Sprintf("Not true that %s %s <%v>.", subject.DisplayString(), verb, other)) +} + +func (subject *PointerSubject) DisplayString() string { + return subject.Subject.DisplayString(fmt.Sprintf("%v", subject.value)) +} + +func (subject *PointerSubject) Equals(expectation interface{}) { + if subject.value != expectation { + subject.Fail("is equal to", expectation) + } +} + +func (subject *PointerSubject) IsNil() { + if subject.value != nil { + subject.FailWithMessage("Not true that " + subject.DisplayString() + " is nil.") + } +} + +func (subject *PointerSubject) IsNotNil() { + if subject.value == nil { + subject.FailWithMessage("Not true that " + subject.DisplayString() + " is not nil.") + } +} diff --git a/testing/unit/uint16subject.go b/testing/unit/uint16subject.go index b1a3cace6..0d150c381 100644 --- a/testing/unit/uint16subject.go +++ b/testing/unit/uint16subject.go @@ -46,3 +46,9 @@ func (subject *Uint16Subject) LessThan(expectation uint16) { subject.Fail("is less than", expectation) } } + +func (subject *Uint16Subject) Positive() { + if subject.value <= 0 { + subject.FailWithMessage("Not true that " + subject.DisplayString() + " is positive.") + } +}