You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

538 lines
10 KiB

  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package fastjson
  5. import (
  6. "bytes"
  7. "math"
  8. "reflect"
  9. "testing"
  10. "unicode"
  11. )
  12. type Optionals struct {
  13. Sr string `json:"sr"`
  14. So string `json:"so,omitempty"`
  15. Sw string `json:"-"`
  16. Ir int `json:"omitempty"` // actually named omitempty, not an option
  17. Io int `json:"io,omitempty"`
  18. Slr []string `json:"slr,random"`
  19. Slo []string `json:"slo,omitempty"`
  20. Mr map[string]interface{} `json:"mr"`
  21. Mo map[string]interface{} `json:",omitempty"`
  22. Fr float64 `json:"fr"`
  23. Fo float64 `json:"fo,omitempty"`
  24. Br bool `json:"br"`
  25. Bo bool `json:"bo,omitempty"`
  26. Ur uint `json:"ur"`
  27. Uo uint `json:"uo,omitempty"`
  28. Str struct{} `json:"str"`
  29. Sto struct{} `json:"sto,omitempty"`
  30. }
  31. var optionalsExpected = `{
  32. "sr": "",
  33. "omitempty": 0,
  34. "slr": null,
  35. "mr": {},
  36. "fr": 0,
  37. "br": false,
  38. "ur": 0,
  39. "str": {},
  40. "sto": {}
  41. }`
  42. func TestOmitEmpty(t *testing.T) {
  43. var o Optionals
  44. o.Sw = "something"
  45. o.Mr = map[string]interface{}{}
  46. o.Mo = map[string]interface{}{}
  47. got, err := MarshalIndent(&o, "", " ")
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. if got := string(got); got != optionalsExpected {
  52. t.Errorf(" got: %s\nwant: %s\n", got, optionalsExpected)
  53. }
  54. }
  55. type StringTag struct {
  56. BoolStr bool `json:",string"`
  57. IntStr int64 `json:",string"`
  58. StrStr string `json:",string"`
  59. }
  60. var stringTagExpected = `{
  61. "BoolStr": "true",
  62. "IntStr": "42",
  63. "StrStr": "\"xzbit\""
  64. }`
  65. func TestStringTag(t *testing.T) {
  66. var s StringTag
  67. s.BoolStr = true
  68. s.IntStr = 42
  69. s.StrStr = "xzbit"
  70. got, err := MarshalIndent(&s, "", " ")
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. if got := string(got); got != stringTagExpected {
  75. t.Fatalf(" got: %s\nwant: %s\n", got, stringTagExpected)
  76. }
  77. // Verify that it round-trips.
  78. var s2 StringTag
  79. err = NewDecoder(bytes.NewReader(got)).Decode(&s2)
  80. if err != nil {
  81. t.Fatalf("Decode: %v", err)
  82. }
  83. if !reflect.DeepEqual(s, s2) {
  84. t.Fatalf("decode didn't match.\nsource: %#v\nEncoded as:\n%s\ndecode: %#v", s, string(got), s2)
  85. }
  86. }
  87. // byte slices are special even if they're renamed types.
  88. type renamedByte byte
  89. type renamedByteSlice []byte
  90. type renamedRenamedByteSlice []renamedByte
  91. func TestEncodeRenamedByteSlice(t *testing.T) {
  92. s := renamedByteSlice("abc")
  93. result, err := Marshal(s)
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. expect := `"YWJj"`
  98. if string(result) != expect {
  99. t.Errorf(" got %s want %s", result, expect)
  100. }
  101. r := renamedRenamedByteSlice("abc")
  102. result, err = Marshal(r)
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. if string(result) != expect {
  107. t.Errorf(" got %s want %s", result, expect)
  108. }
  109. }
  110. var unsupportedValues = []interface{}{
  111. math.NaN(),
  112. math.Inf(-1),
  113. math.Inf(1),
  114. }
  115. func TestUnsupportedValues(t *testing.T) {
  116. for _, v := range unsupportedValues {
  117. if _, err := Marshal(v); err != nil {
  118. if _, ok := err.(*UnsupportedValueError); !ok {
  119. t.Errorf("for %v, got %T want UnsupportedValueError", v, err)
  120. }
  121. } else {
  122. t.Errorf("for %v, expected error", v)
  123. }
  124. }
  125. }
  126. // Ref has Marshaler and Unmarshaler methods with pointer receiver.
  127. type Ref int
  128. func (*Ref) MarshalJSON() ([]byte, error) {
  129. return []byte(`"ref"`), nil
  130. }
  131. func (r *Ref) UnmarshalJSON([]byte) error {
  132. *r = 12
  133. return nil
  134. }
  135. // Val has Marshaler methods with value receiver.
  136. type Val int
  137. func (Val) MarshalJSON() ([]byte, error) {
  138. return []byte(`"val"`), nil
  139. }
  140. // RefText has Marshaler and Unmarshaler methods with pointer receiver.
  141. type RefText int
  142. func (*RefText) MarshalText() ([]byte, error) {
  143. return []byte(`"ref"`), nil
  144. }
  145. func (r *RefText) UnmarshalText([]byte) error {
  146. *r = 13
  147. return nil
  148. }
  149. // ValText has Marshaler methods with value receiver.
  150. type ValText int
  151. func (ValText) MarshalText() ([]byte, error) {
  152. return []byte(`"val"`), nil
  153. }
  154. func TestRefValMarshal(t *testing.T) {
  155. var s = struct {
  156. R0 Ref
  157. R1 *Ref
  158. R2 RefText
  159. R3 *RefText
  160. V0 Val
  161. V1 *Val
  162. V2 ValText
  163. V3 *ValText
  164. }{
  165. R0: 12,
  166. R1: new(Ref),
  167. R2: 14,
  168. R3: new(RefText),
  169. V0: 13,
  170. V1: new(Val),
  171. V2: 15,
  172. V3: new(ValText),
  173. }
  174. const want = `{"R0":"ref","R1":"ref","R2":"\"ref\"","R3":"\"ref\"","V0":"val","V1":"val","V2":"\"val\"","V3":"\"val\""}`
  175. b, err := Marshal(&s)
  176. if err != nil {
  177. t.Fatalf("Marshal: %v", err)
  178. }
  179. if got := string(b); got != want {
  180. t.Errorf("got %q, want %q", got, want)
  181. }
  182. }
  183. // C implements Marshaler and returns unescaped JSON.
  184. type C int
  185. func (C) MarshalJSON() ([]byte, error) {
  186. return []byte(`"<&>"`), nil
  187. }
  188. // CText implements Marshaler and returns unescaped text.
  189. type CText int
  190. func (CText) MarshalText() ([]byte, error) {
  191. return []byte(`"<&>"`), nil
  192. }
  193. func TestMarshalerEscaping(t *testing.T) {
  194. var c C
  195. want := `"\u003c\u0026\u003e"`
  196. b, err := Marshal(c)
  197. if err != nil {
  198. t.Fatalf("Marshal(c): %v", err)
  199. }
  200. if got := string(b); got != want {
  201. t.Errorf("Marshal(c) = %#q, want %#q", got, want)
  202. }
  203. var ct CText
  204. want = `"\"\u003c\u0026\u003e\""`
  205. b, err = Marshal(ct)
  206. if err != nil {
  207. t.Fatalf("Marshal(ct): %v", err)
  208. }
  209. if got := string(b); got != want {
  210. t.Errorf("Marshal(ct) = %#q, want %#q", got, want)
  211. }
  212. }
  213. type IntType int
  214. type MyStruct struct {
  215. IntType
  216. }
  217. func TestAnonymousNonstruct(t *testing.T) {
  218. var i IntType = 11
  219. a := MyStruct{i}
  220. const want = `{"IntType":11}`
  221. b, err := Marshal(a)
  222. if err != nil {
  223. t.Fatalf("Marshal: %v", err)
  224. }
  225. if got := string(b); got != want {
  226. t.Errorf("got %q, want %q", got, want)
  227. }
  228. }
  229. type BugA struct {
  230. S string
  231. }
  232. type BugB struct {
  233. BugA
  234. S string
  235. }
  236. type BugC struct {
  237. S string
  238. }
  239. // Legal Go: We never use the repeated embedded field (S).
  240. type BugX struct {
  241. A int
  242. BugA
  243. BugB
  244. }
  245. // Issue 5245.
  246. func TestEmbeddedBug(t *testing.T) {
  247. v := BugB{
  248. BugA{"A"},
  249. "B",
  250. }
  251. b, err := Marshal(v)
  252. if err != nil {
  253. t.Fatal("Marshal:", err)
  254. }
  255. want := `{"S":"B"}`
  256. got := string(b)
  257. if got != want {
  258. t.Fatalf("Marshal: got %s want %s", got, want)
  259. }
  260. // Now check that the duplicate field, S, does not appear.
  261. x := BugX{
  262. A: 23,
  263. }
  264. b, err = Marshal(x)
  265. if err != nil {
  266. t.Fatal("Marshal:", err)
  267. }
  268. want = `{"A":23}`
  269. got = string(b)
  270. if got != want {
  271. t.Fatalf("Marshal: got %s want %s", got, want)
  272. }
  273. }
  274. type BugD struct { // Same as BugA after tagging.
  275. XXX string `json:"S"`
  276. }
  277. // BugD's tagged S field should dominate BugA's.
  278. type BugY struct {
  279. BugA
  280. BugD
  281. }
  282. // Test that a field with a tag dominates untagged fields.
  283. func TestTaggedFieldDominates(t *testing.T) {
  284. v := BugY{
  285. BugA{"BugA"},
  286. BugD{"BugD"},
  287. }
  288. b, err := Marshal(v)
  289. if err != nil {
  290. t.Fatal("Marshal:", err)
  291. }
  292. want := `{"S":"BugD"}`
  293. got := string(b)
  294. if got != want {
  295. t.Fatalf("Marshal: got %s want %s", got, want)
  296. }
  297. }
  298. // There are no tags here, so S should not appear.
  299. type BugZ struct {
  300. BugA
  301. BugC
  302. BugY // Contains a tagged S field through BugD; should not dominate.
  303. }
  304. func TestDuplicatedFieldDisappears(t *testing.T) {
  305. v := BugZ{
  306. BugA{"BugA"},
  307. BugC{"BugC"},
  308. BugY{
  309. BugA{"nested BugA"},
  310. BugD{"nested BugD"},
  311. },
  312. }
  313. b, err := Marshal(v)
  314. if err != nil {
  315. t.Fatal("Marshal:", err)
  316. }
  317. want := `{}`
  318. got := string(b)
  319. if got != want {
  320. t.Fatalf("Marshal: got %s want %s", got, want)
  321. }
  322. }
  323. func TestStringBytes(t *testing.T) {
  324. // Test that encodeState.stringBytes and encodeState.string use the same encoding.
  325. es := &encodeState{}
  326. var r []rune
  327. for i := '\u0000'; i <= unicode.MaxRune; i++ {
  328. r = append(r, i)
  329. }
  330. s := string(r) + "\xff\xff\xffhello" // some invalid UTF-8 too
  331. es.string(s)
  332. esBytes := &encodeState{}
  333. esBytes.stringBytes([]byte(s))
  334. enc := es.Buffer.String()
  335. encBytes := esBytes.Buffer.String()
  336. if enc != encBytes {
  337. i := 0
  338. for i < len(enc) && i < len(encBytes) && enc[i] == encBytes[i] {
  339. i++
  340. }
  341. enc = enc[i:]
  342. encBytes = encBytes[i:]
  343. i = 0
  344. for i < len(enc) && i < len(encBytes) && enc[len(enc)-i-1] == encBytes[len(encBytes)-i-1] {
  345. i++
  346. }
  347. enc = enc[:len(enc)-i]
  348. encBytes = encBytes[:len(encBytes)-i]
  349. if len(enc) > 20 {
  350. enc = enc[:20] + "..."
  351. }
  352. if len(encBytes) > 20 {
  353. encBytes = encBytes[:20] + "..."
  354. }
  355. t.Errorf("encodings differ at %#q vs %#q", enc, encBytes)
  356. }
  357. }
  358. func TestIssue6458(t *testing.T) {
  359. type Foo struct {
  360. M RawMessage
  361. }
  362. x := Foo{RawMessage(`"foo"`)}
  363. b, err := Marshal(&x)
  364. if err != nil {
  365. t.Fatal(err)
  366. }
  367. if want := `{"M":"foo"}`; string(b) != want {
  368. t.Errorf("Marshal(&x) = %#q; want %#q", b, want)
  369. }
  370. b, err = Marshal(x)
  371. if err != nil {
  372. t.Fatal(err)
  373. }
  374. if want := `{"M":"ImZvbyI="}`; string(b) != want {
  375. t.Errorf("Marshal(x) = %#q; want %#q", b, want)
  376. }
  377. }
  378. func TestIssue10281(t *testing.T) {
  379. type Foo struct {
  380. N Number
  381. }
  382. x := Foo{Number(`invalid`)}
  383. b, err := Marshal(&x)
  384. if err == nil {
  385. t.Errorf("Marshal(&x) = %#q; want error", b)
  386. }
  387. }
  388. func TestHTMLEscape(t *testing.T) {
  389. var b, want bytes.Buffer
  390. m := `{"M":"<html>foo &` + "\xe2\x80\xa8 \xe2\x80\xa9" + `</html>"}`
  391. want.Write([]byte(`{"M":"\u003chtml\u003efoo \u0026\u2028 \u2029\u003c/html\u003e"}`))
  392. HTMLEscape(&b, []byte(m))
  393. if !bytes.Equal(b.Bytes(), want.Bytes()) {
  394. t.Errorf("HTMLEscape(&b, []byte(m)) = %s; want %s", b.Bytes(), want.Bytes())
  395. }
  396. }
  397. // golang.org/issue/8582
  398. func TestEncodePointerString(t *testing.T) {
  399. type stringPointer struct {
  400. N *int64 `json:"n,string"`
  401. }
  402. var n int64 = 42
  403. b, err := Marshal(stringPointer{N: &n})
  404. if err != nil {
  405. t.Fatalf("Marshal: %v", err)
  406. }
  407. if got, want := string(b), `{"n":"42"}`; got != want {
  408. t.Errorf("Marshal = %s, want %s", got, want)
  409. }
  410. var back stringPointer
  411. err = Unmarshal(b, &back)
  412. if err != nil {
  413. t.Fatalf("Unmarshal: %v", err)
  414. }
  415. if back.N == nil {
  416. t.Fatalf("Unmarshalled nil N field")
  417. }
  418. if *back.N != 42 {
  419. t.Fatalf("*N = %d; want 42", *back.N)
  420. }
  421. }
  422. var encodeStringTests = []struct {
  423. in string
  424. out string
  425. }{
  426. {"\x00", `"\u0000"`},
  427. {"\x01", `"\u0001"`},
  428. {"\x02", `"\u0002"`},
  429. {"\x03", `"\u0003"`},
  430. {"\x04", `"\u0004"`},
  431. {"\x05", `"\u0005"`},
  432. {"\x06", `"\u0006"`},
  433. {"\x07", `"\u0007"`},
  434. {"\x08", `"\u0008"`},
  435. {"\x09", `"\t"`},
  436. {"\x0a", `"\n"`},
  437. {"\x0b", `"\u000b"`},
  438. {"\x0c", `"\u000c"`},
  439. {"\x0d", `"\r"`},
  440. {"\x0e", `"\u000e"`},
  441. {"\x0f", `"\u000f"`},
  442. {"\x10", `"\u0010"`},
  443. {"\x11", `"\u0011"`},
  444. {"\x12", `"\u0012"`},
  445. {"\x13", `"\u0013"`},
  446. {"\x14", `"\u0014"`},
  447. {"\x15", `"\u0015"`},
  448. {"\x16", `"\u0016"`},
  449. {"\x17", `"\u0017"`},
  450. {"\x18", `"\u0018"`},
  451. {"\x19", `"\u0019"`},
  452. {"\x1a", `"\u001a"`},
  453. {"\x1b", `"\u001b"`},
  454. {"\x1c", `"\u001c"`},
  455. {"\x1d", `"\u001d"`},
  456. {"\x1e", `"\u001e"`},
  457. {"\x1f", `"\u001f"`},
  458. }
  459. func TestEncodeString(t *testing.T) {
  460. for _, tt := range encodeStringTests {
  461. b, err := Marshal(tt.in)
  462. if err != nil {
  463. t.Errorf("Marshal(%q): %v", tt.in, err)
  464. continue
  465. }
  466. out := string(b)
  467. if out != tt.out {
  468. t.Errorf("Marshal(%q) = %#q, want %#q", tt.in, out, tt.out)
  469. }
  470. }
  471. }