loader_test.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package serial_test
  2. import (
  3. "bytes"
  4. "strings"
  5. "testing"
  6. "github.com/xtls/xray-core/infra/conf/serial"
  7. )
  8. func TestLoaderError(t *testing.T) {
  9. testCases := []struct {
  10. Input string
  11. Output string
  12. }{
  13. {
  14. Input: `{
  15. "log": {
  16. // abcd
  17. 0,
  18. "loglevel": "info"
  19. }
  20. }`,
  21. Output: "line 4 char 6",
  22. },
  23. {
  24. Input: `{
  25. "log": {
  26. // abcd
  27. "loglevel": "info",
  28. }
  29. }`,
  30. Output: "line 5 char 5",
  31. },
  32. {
  33. Input: `{
  34. "port": 1,
  35. "inbounds": [{
  36. "protocol": "test"
  37. }]
  38. }`,
  39. Output: "parse json config",
  40. },
  41. {
  42. Input: `{
  43. "inbounds": [{
  44. "port": 1,
  45. "listen": 0,
  46. "protocol": "test"
  47. }]
  48. }`,
  49. Output: "line 1 char 1",
  50. },
  51. }
  52. for _, testCase := range testCases {
  53. reader := bytes.NewReader([]byte(testCase.Input))
  54. _, err := serial.LoadJSONConfig(reader)
  55. errString := err.Error()
  56. if !strings.Contains(errString, testCase.Output) {
  57. t.Error("unexpected output from json: ", testCase.Input, ". expected ", testCase.Output, ", but actually ", errString)
  58. }
  59. }
  60. }