github.com/xtls/xray-core@v1.8.12-0.20240518155711-3168d27b0bdb/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 "github.com/xtls/xray-core/common" 10 . "github.com/xtls/xray-core/infra/conf/json" 11 ) 12 13 func TestReader(t *testing.T) { 14 data := []struct { 15 input string 16 output string 17 }{ 18 { 19 ` 20 content #comment 1 21 #comment 2 22 content 2`, 23 ` 24 content 25 26 content 2`, 27 }, 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 }