From 49c8c31e266c22b22e977522842684f0b5c7e961 Mon Sep 17 00:00:00 2001 From: v2ray Date: Mon, 16 May 2016 09:19:55 -0700 Subject: [PATCH] test case for dns config --- app/dns/config_json.go | 4 ++-- app/dns/config_json_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 app/dns/config_json_test.go diff --git a/app/dns/config_json.go b/app/dns/config_json.go index 8e869d25b..6e78336ce 100644 --- a/app/dns/config_json.go +++ b/app/dns/config_json.go @@ -10,7 +10,7 @@ import ( func (this *Config) UnmarshalJSON(data []byte) error { type JsonConfig struct { - Servers []v2net.Address `json:"servers"` + Servers []v2net.AddressJson `json:"servers"` } jsonConfig := new(JsonConfig) if err := json.Unmarshal(data, jsonConfig); err != nil { @@ -18,7 +18,7 @@ func (this *Config) UnmarshalJSON(data []byte) error { } this.NameServers = make([]v2net.Destination, len(jsonConfig.Servers)) for idx, server := range jsonConfig.Servers { - this.NameServers[idx] = v2net.UDPDestination(server, v2net.Port(53)) + this.NameServers[idx] = v2net.UDPDestination(server.Address, v2net.Port(53)) } return nil diff --git a/app/dns/config_json_test.go b/app/dns/config_json_test.go new file mode 100644 index 000000000..eccaea82a --- /dev/null +++ b/app/dns/config_json_test.go @@ -0,0 +1,30 @@ +// +build json + +package dns_test + +import ( + "encoding/json" + "testing" + + . "github.com/v2ray/v2ray-core/app/dns" + v2net "github.com/v2ray/v2ray-core/common/net" + netassert "github.com/v2ray/v2ray-core/common/net/testing/assert" + v2testing "github.com/v2ray/v2ray-core/testing" + "github.com/v2ray/v2ray-core/testing/assert" +) + +func TestConfigParsing(t *testing.T) { + v2testing.Current(t) + + rawJson := `{ + "servers": ["8.8.8.8"] + }` + + config := new(Config) + err := json.Unmarshal([]byte(rawJson), config) + assert.Error(err).IsNil() + assert.Int(len(config.NameServers)).Equals(1) + netassert.Destination(config.NameServers[0]).IsUDP() + netassert.Address(config.NameServers[0].Address()).Equals(v2net.IPAddress([]byte{8, 8, 8, 8})) + netassert.Port(config.NameServers[0].Port()).Equals(v2net.Port(53)) +}