github.com/EagleQL/Xray-core@v1.4.3/infra/conf/json/reader_test.go (about)

     1  package json_test
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"testing"
     7  
     8  	"github.com/google/go-cmp/cmp"
     9  
    10  	"github.com/xtls/xray-core/common"
    11  	. "github.com/xtls/xray-core/infra/conf/json"
    12  )
    13  
    14  func TestReader(t *testing.T) {
    15  	data := []struct {
    16  		input  string
    17  		output string
    18  	}{
    19  		{
    20  			`
    21  content #comment 1
    22  #comment 2
    23  content 2`,
    24  			`
    25  content 
    26  
    27  content 2`},
    28  		{`content`, `content`},
    29  		{" ", " "},
    30  		{`con/*abcd*/tent`, "content"},
    31  		{`
    32  text // adlkhdf /*
    33  //comment adfkj
    34  text 2*/`, `
    35  text 
    36  
    37  text 2*`},
    38  		{`"//"content`, `"//"content`},
    39  		{`abcd'//'abcd`, `abcd'//'abcd`},
    40  		{`"\""`, `"\""`},
    41  		{`\"/*abcd*/\"`, `\"\"`},
    42  	}
    43  
    44  	for _, testCase := range data {
    45  		reader := &Reader{
    46  			Reader: bytes.NewReader([]byte(testCase.input)),
    47  		}
    48  
    49  		actual := make([]byte, 1024)
    50  		n, err := reader.Read(actual)
    51  		common.Must(err)
    52  		if r := cmp.Diff(string(actual[:n]), testCase.output); r != "" {
    53  			t.Error(r)
    54  		}
    55  	}
    56  }
    57  
    58  func TestReader1(t *testing.T) {
    59  	type dataStruct struct {
    60  		input  string
    61  		output string
    62  	}
    63  
    64  	bufLen := 8
    65  
    66  	data := []dataStruct{
    67  		{"loooooooooooooooooooooooooooooooooooooooog", "loooooooooooooooooooooooooooooooooooooooog"},
    68  		{`{"t": "\/testlooooooooooooooooooooooooooooong"}`, `{"t": "\/testlooooooooooooooooooooooooooooong"}`},
    69  		{`{"t": "\/test"}`, `{"t": "\/test"}`},
    70  		{`"\// fake comment"`, `"\// fake comment"`},
    71  		{`"\/\/\/\/\/"`, `"\/\/\/\/\/"`},
    72  	}
    73  
    74  	for _, testCase := range data {
    75  		reader := &Reader{
    76  			Reader: bytes.NewReader([]byte(testCase.input)),
    77  		}
    78  		target := make([]byte, 0)
    79  		buf := make([]byte, bufLen)
    80  		var n int
    81  		var err error
    82  		for n, err = reader.Read(buf); err == nil; n, err = reader.Read(buf) {
    83  			if n > len(buf) {
    84  				t.Error("n: ", n)
    85  			}
    86  			target = append(target, buf[:n]...)
    87  			buf = make([]byte, bufLen)
    88  		}
    89  		if err != nil && err != io.EOF {
    90  			t.Error("error: ", err)
    91  		}
    92  		if string(target) != testCase.output {
    93  			t.Error("got ", string(target), " want ", testCase.output)
    94  		}
    95  	}
    96  }