2020-11-28 14:06:03 +00:00
|
|
|
// Copyright 2020 Jebbs. All rights reserved.
|
|
|
|
// Use of this source code is governed by MIT
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package merge_test
|
|
|
|
|
|
|
|
import (
|
improve commands (#648)
* stat show json refactor
> will show {} since scripts should expect it, its the json style of blank value
* combine statistics commands to one
* code optimize
* fix runtime flag
* remove json indent
* change overridden to override
* api bi -json
* convert stdin support
code optimize
* writeRow() code optimize
add stats tittle
revert back to restartlogger
* api log -restart
* follow log
* codeql
* move -json to shared flags
* flags optimize
* update flag descriptions
* change "-v" of "api bo" to duration
* change "-expire" of "tls cert" to days
* cmds short description optimize
* fix multiple log followers
* Format loader refactor
* "infra/conf/merge" refactor
* "LoadConfig" refactor
* add "infra/conf/mergers"
* contribute to it will benifit `v2ray run`,`v2ray test`,`v2ray convert`
* easily add new formats, by just adding a converter like json.FromTOML
* default format auto, to all cmds above
* auto detect input format
* mixed formats support
* better stdin behavior
* don't wait if no content
* don't use 'stdin:' placeholder
* `v2ray test` now behaves exactly the same with `v2ray run`, including stdin reading
* api ado, adi, rmo, rmi refactor
* support folders to files resolving, mixed formats
* remove remaining 'stdin:' placeholders
* fix tests
* os.Stdin.Stat() behaves different in platforms, removed
* code optimize
Co-authored-by: loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
2021-02-21 15:02:42 +00:00
|
|
|
"bytes"
|
2020-11-28 14:06:03 +00:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2021-02-17 00:07:12 +00:00
|
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/merge"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/infra/conf/serial"
|
2020-11-28 14:06:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMergeV2Style(t *testing.T) {
|
|
|
|
json1 := `
|
|
|
|
{
|
|
|
|
"log": {"access": "some_value", "loglevel": "debug"},
|
|
|
|
"inbounds": [{"tag": "in-1"}],
|
|
|
|
"outbounds": [{"_priority": 100, "tag": "out-1"}],
|
|
|
|
"routing": {"rules": [
|
|
|
|
{"_tag":"default_route","inboundTag":["in-1"],"outboundTag":"out-1"}
|
|
|
|
]}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
json2 := `
|
|
|
|
{
|
|
|
|
"log": {"loglevel": "error"},
|
|
|
|
"inbounds": [{"tag": "in-2"}],
|
|
|
|
"outbounds": [{"_priority": -100, "tag": "out-2"}],
|
|
|
|
"routing": {"rules": [
|
|
|
|
{"inboundTag":["in-2"],"outboundTag":"out-2"},
|
|
|
|
{"_tag":"default_route","inboundTag":["in-1.1"],"outboundTag":"out-1.1"}
|
|
|
|
]}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
expected := `
|
|
|
|
{
|
|
|
|
"log": {"access": "some_value", "loglevel": "error"},
|
|
|
|
"inbounds": [{"tag": "in-1"},{"tag": "in-2"}],
|
|
|
|
"outbounds": [
|
|
|
|
{"tag": "out-2"},
|
|
|
|
{"tag": "out-1"}
|
|
|
|
],
|
|
|
|
"routing": {"rules": [
|
|
|
|
{"inboundTag":["in-1","in-1.1"],"outboundTag":"out-1.1"},
|
|
|
|
{"inboundTag":["in-2"],"outboundTag":"out-2"}
|
|
|
|
]}
|
|
|
|
}
|
|
|
|
`
|
improve commands (#648)
* stat show json refactor
> will show {} since scripts should expect it, its the json style of blank value
* combine statistics commands to one
* code optimize
* fix runtime flag
* remove json indent
* change overridden to override
* api bi -json
* convert stdin support
code optimize
* writeRow() code optimize
add stats tittle
revert back to restartlogger
* api log -restart
* follow log
* codeql
* move -json to shared flags
* flags optimize
* update flag descriptions
* change "-v" of "api bo" to duration
* change "-expire" of "tls cert" to days
* cmds short description optimize
* fix multiple log followers
* Format loader refactor
* "infra/conf/merge" refactor
* "LoadConfig" refactor
* add "infra/conf/mergers"
* contribute to it will benifit `v2ray run`,`v2ray test`,`v2ray convert`
* easily add new formats, by just adding a converter like json.FromTOML
* default format auto, to all cmds above
* auto detect input format
* mixed formats support
* better stdin behavior
* don't wait if no content
* don't use 'stdin:' placeholder
* `v2ray test` now behaves exactly the same with `v2ray run`, including stdin reading
* api ado, adi, rmo, rmi refactor
* support folders to files resolving, mixed formats
* remove remaining 'stdin:' placeholders
* fix tests
* os.Stdin.Stat() behaves different in platforms, removed
* code optimize
Co-authored-by: loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
2021-02-21 15:02:42 +00:00
|
|
|
m, err := merge.JSONs([][]byte{[]byte(json1), []byte(json2)})
|
2020-11-28 14:06:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
assertResult(t, m, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMergeTag(t *testing.T) {
|
|
|
|
json1 := `
|
|
|
|
{
|
|
|
|
"routing": {
|
|
|
|
"rules": [{
|
|
|
|
"tag":"1",
|
|
|
|
"inboundTag": ["in-1"],
|
|
|
|
"outboundTag": "out-1"
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
json2 := `
|
|
|
|
{
|
|
|
|
"routing": {
|
|
|
|
"rules": [{
|
|
|
|
"_tag":"1",
|
|
|
|
"inboundTag": ["in-2"],
|
|
|
|
"outboundTag": "out-2"
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
expected := `
|
|
|
|
{
|
|
|
|
"routing": {
|
|
|
|
"rules": [{
|
|
|
|
"tag":"1",
|
|
|
|
"inboundTag": ["in-1", "in-2"],
|
|
|
|
"outboundTag": "out-2"
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
improve commands (#648)
* stat show json refactor
> will show {} since scripts should expect it, its the json style of blank value
* combine statistics commands to one
* code optimize
* fix runtime flag
* remove json indent
* change overridden to override
* api bi -json
* convert stdin support
code optimize
* writeRow() code optimize
add stats tittle
revert back to restartlogger
* api log -restart
* follow log
* codeql
* move -json to shared flags
* flags optimize
* update flag descriptions
* change "-v" of "api bo" to duration
* change "-expire" of "tls cert" to days
* cmds short description optimize
* fix multiple log followers
* Format loader refactor
* "infra/conf/merge" refactor
* "LoadConfig" refactor
* add "infra/conf/mergers"
* contribute to it will benifit `v2ray run`,`v2ray test`,`v2ray convert`
* easily add new formats, by just adding a converter like json.FromTOML
* default format auto, to all cmds above
* auto detect input format
* mixed formats support
* better stdin behavior
* don't wait if no content
* don't use 'stdin:' placeholder
* `v2ray test` now behaves exactly the same with `v2ray run`, including stdin reading
* api ado, adi, rmo, rmi refactor
* support folders to files resolving, mixed formats
* remove remaining 'stdin:' placeholders
* fix tests
* os.Stdin.Stat() behaves different in platforms, removed
* code optimize
Co-authored-by: loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
2021-02-21 15:02:42 +00:00
|
|
|
m, err := merge.JSONs([][]byte{[]byte(json1), []byte(json2)})
|
2020-11-28 14:06:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
assertResult(t, m, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMergeTagValueTypes(t *testing.T) {
|
|
|
|
json1 := `
|
|
|
|
{
|
|
|
|
"array_1": [{
|
|
|
|
"_tag":"1",
|
|
|
|
"array_2": [{
|
|
|
|
"_tag":"2",
|
|
|
|
"array_3.1": ["string",true,false],
|
|
|
|
"array_3.2": [1,2,3],
|
|
|
|
"number_1": 1,
|
|
|
|
"number_2": 1,
|
|
|
|
"bool_1": true,
|
|
|
|
"bool_2": true
|
|
|
|
}]
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
json2 := `
|
|
|
|
{
|
|
|
|
"array_1": [{
|
|
|
|
"_tag":"1",
|
|
|
|
"array_2": [{
|
|
|
|
"_tag":"2",
|
|
|
|
"array_3.1": [0,1,null],
|
|
|
|
"array_3.2": null,
|
|
|
|
"number_1": 0,
|
|
|
|
"number_2": 1,
|
|
|
|
"bool_1": true,
|
|
|
|
"bool_2": false,
|
|
|
|
"null_1": null
|
|
|
|
}]
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
expected := `
|
|
|
|
{
|
|
|
|
"array_1": [{
|
|
|
|
"array_2": [{
|
|
|
|
"array_3.1": ["string",true,false,0,1,null],
|
|
|
|
"array_3.2": [1,2,3],
|
|
|
|
"number_1": 0,
|
|
|
|
"number_2": 1,
|
|
|
|
"bool_1": true,
|
|
|
|
"bool_2": false,
|
|
|
|
"null_1": null
|
|
|
|
}]
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
`
|
improve commands (#648)
* stat show json refactor
> will show {} since scripts should expect it, its the json style of blank value
* combine statistics commands to one
* code optimize
* fix runtime flag
* remove json indent
* change overridden to override
* api bi -json
* convert stdin support
code optimize
* writeRow() code optimize
add stats tittle
revert back to restartlogger
* api log -restart
* follow log
* codeql
* move -json to shared flags
* flags optimize
* update flag descriptions
* change "-v" of "api bo" to duration
* change "-expire" of "tls cert" to days
* cmds short description optimize
* fix multiple log followers
* Format loader refactor
* "infra/conf/merge" refactor
* "LoadConfig" refactor
* add "infra/conf/mergers"
* contribute to it will benifit `v2ray run`,`v2ray test`,`v2ray convert`
* easily add new formats, by just adding a converter like json.FromTOML
* default format auto, to all cmds above
* auto detect input format
* mixed formats support
* better stdin behavior
* don't wait if no content
* don't use 'stdin:' placeholder
* `v2ray test` now behaves exactly the same with `v2ray run`, including stdin reading
* api ado, adi, rmo, rmi refactor
* support folders to files resolving, mixed formats
* remove remaining 'stdin:' placeholders
* fix tests
* os.Stdin.Stat() behaves different in platforms, removed
* code optimize
Co-authored-by: loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
2021-02-21 15:02:42 +00:00
|
|
|
m, err := merge.JSONs([][]byte{[]byte(json1), []byte(json2)})
|
2020-11-28 14:06:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
assertResult(t, m, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMergeTagDeep(t *testing.T) {
|
|
|
|
json1 := `
|
|
|
|
{
|
|
|
|
"array_1": [{
|
|
|
|
"_tag":"1",
|
|
|
|
"array_2": [{
|
|
|
|
"_tag":"2",
|
|
|
|
"array_3": [true,false,"string"]
|
|
|
|
}]
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
json2 := `
|
|
|
|
{
|
|
|
|
"array_1": [{
|
|
|
|
"_tag":"1",
|
|
|
|
"array_2": [{
|
|
|
|
"_tag":"2",
|
|
|
|
"_priority":-100,
|
|
|
|
"array_3": [0,1,null]
|
|
|
|
}]
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
expected := `
|
|
|
|
{
|
|
|
|
"array_1": [{
|
|
|
|
"array_2": [{
|
|
|
|
"array_3": [0,1,null,true,false,"string"]
|
|
|
|
}]
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
`
|
improve commands (#648)
* stat show json refactor
> will show {} since scripts should expect it, its the json style of blank value
* combine statistics commands to one
* code optimize
* fix runtime flag
* remove json indent
* change overridden to override
* api bi -json
* convert stdin support
code optimize
* writeRow() code optimize
add stats tittle
revert back to restartlogger
* api log -restart
* follow log
* codeql
* move -json to shared flags
* flags optimize
* update flag descriptions
* change "-v" of "api bo" to duration
* change "-expire" of "tls cert" to days
* cmds short description optimize
* fix multiple log followers
* Format loader refactor
* "infra/conf/merge" refactor
* "LoadConfig" refactor
* add "infra/conf/mergers"
* contribute to it will benifit `v2ray run`,`v2ray test`,`v2ray convert`
* easily add new formats, by just adding a converter like json.FromTOML
* default format auto, to all cmds above
* auto detect input format
* mixed formats support
* better stdin behavior
* don't wait if no content
* don't use 'stdin:' placeholder
* `v2ray test` now behaves exactly the same with `v2ray run`, including stdin reading
* api ado, adi, rmo, rmi refactor
* support folders to files resolving, mixed formats
* remove remaining 'stdin:' placeholders
* fix tests
* os.Stdin.Stat() behaves different in platforms, removed
* code optimize
Co-authored-by: loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
2021-02-21 15:02:42 +00:00
|
|
|
m, err := merge.JSONs([][]byte{[]byte(json1), []byte(json2)})
|
2020-11-28 14:06:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
assertResult(t, m, expected)
|
|
|
|
}
|
improve commands (#648)
* stat show json refactor
> will show {} since scripts should expect it, its the json style of blank value
* combine statistics commands to one
* code optimize
* fix runtime flag
* remove json indent
* change overridden to override
* api bi -json
* convert stdin support
code optimize
* writeRow() code optimize
add stats tittle
revert back to restartlogger
* api log -restart
* follow log
* codeql
* move -json to shared flags
* flags optimize
* update flag descriptions
* change "-v" of "api bo" to duration
* change "-expire" of "tls cert" to days
* cmds short description optimize
* fix multiple log followers
* Format loader refactor
* "infra/conf/merge" refactor
* "LoadConfig" refactor
* add "infra/conf/mergers"
* contribute to it will benifit `v2ray run`,`v2ray test`,`v2ray convert`
* easily add new formats, by just adding a converter like json.FromTOML
* default format auto, to all cmds above
* auto detect input format
* mixed formats support
* better stdin behavior
* don't wait if no content
* don't use 'stdin:' placeholder
* `v2ray test` now behaves exactly the same with `v2ray run`, including stdin reading
* api ado, adi, rmo, rmi refactor
* support folders to files resolving, mixed formats
* remove remaining 'stdin:' placeholders
* fix tests
* os.Stdin.Stat() behaves different in platforms, removed
* code optimize
Co-authored-by: loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
2021-02-21 15:02:42 +00:00
|
|
|
func assertResult(t *testing.T, value []byte, expected string) {
|
|
|
|
v := make(map[string]interface{})
|
|
|
|
err := serial.DecodeJSON(bytes.NewReader(value), &v)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2020-11-28 14:06:03 +00:00
|
|
|
e := make(map[string]interface{})
|
improve commands (#648)
* stat show json refactor
> will show {} since scripts should expect it, its the json style of blank value
* combine statistics commands to one
* code optimize
* fix runtime flag
* remove json indent
* change overridden to override
* api bi -json
* convert stdin support
code optimize
* writeRow() code optimize
add stats tittle
revert back to restartlogger
* api log -restart
* follow log
* codeql
* move -json to shared flags
* flags optimize
* update flag descriptions
* change "-v" of "api bo" to duration
* change "-expire" of "tls cert" to days
* cmds short description optimize
* fix multiple log followers
* Format loader refactor
* "infra/conf/merge" refactor
* "LoadConfig" refactor
* add "infra/conf/mergers"
* contribute to it will benifit `v2ray run`,`v2ray test`,`v2ray convert`
* easily add new formats, by just adding a converter like json.FromTOML
* default format auto, to all cmds above
* auto detect input format
* mixed formats support
* better stdin behavior
* don't wait if no content
* don't use 'stdin:' placeholder
* `v2ray test` now behaves exactly the same with `v2ray run`, including stdin reading
* api ado, adi, rmo, rmi refactor
* support folders to files resolving, mixed formats
* remove remaining 'stdin:' placeholders
* fix tests
* os.Stdin.Stat() behaves different in platforms, removed
* code optimize
Co-authored-by: loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
2021-02-21 15:02:42 +00:00
|
|
|
err = serial.DecodeJSON(strings.NewReader(expected), &e)
|
2020-11-28 14:06:03 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
improve commands (#648)
* stat show json refactor
> will show {} since scripts should expect it, its the json style of blank value
* combine statistics commands to one
* code optimize
* fix runtime flag
* remove json indent
* change overridden to override
* api bi -json
* convert stdin support
code optimize
* writeRow() code optimize
add stats tittle
revert back to restartlogger
* api log -restart
* follow log
* codeql
* move -json to shared flags
* flags optimize
* update flag descriptions
* change "-v" of "api bo" to duration
* change "-expire" of "tls cert" to days
* cmds short description optimize
* fix multiple log followers
* Format loader refactor
* "infra/conf/merge" refactor
* "LoadConfig" refactor
* add "infra/conf/mergers"
* contribute to it will benifit `v2ray run`,`v2ray test`,`v2ray convert`
* easily add new formats, by just adding a converter like json.FromTOML
* default format auto, to all cmds above
* auto detect input format
* mixed formats support
* better stdin behavior
* don't wait if no content
* don't use 'stdin:' placeholder
* `v2ray test` now behaves exactly the same with `v2ray run`, including stdin reading
* api ado, adi, rmo, rmi refactor
* support folders to files resolving, mixed formats
* remove remaining 'stdin:' placeholders
* fix tests
* os.Stdin.Stat() behaves different in platforms, removed
* code optimize
Co-authored-by: loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
2021-02-21 15:02:42 +00:00
|
|
|
if !reflect.DeepEqual(v, e) {
|
|
|
|
t.Fatalf("expected:\n%s\n\nactual:\n%s", expected, string(value))
|
2020-11-28 14:06:03 +00:00
|
|
|
}
|
|
|
|
}
|