1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-20 22:45:24 +00:00

test cases for json config

This commit is contained in:
V2Ray 2015-09-20 11:45:40 +02:00
parent a887fd01b8
commit 07d4495b27
6 changed files with 110 additions and 4 deletions

View File

@ -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 {

View File

@ -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

47
config/json/json_test.go Normal file
View File

@ -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")
}

View File

@ -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)
}

View File

@ -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.")
}
}

View File

@ -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.")
}
}