command_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package graphics
  3. import (
  4. "bytes"
  5. "compress/zlib"
  6. "encoding/base64"
  7. "fmt"
  8. "io"
  9. "strings"
  10. "testing"
  11. "github.com/google/go-cmp/cmp"
  12. "golang.org/x/exp/rand"
  13. )
  14. var _ = fmt.Print
  15. func from_full_apc_escape_code(raw string) *GraphicsCommand {
  16. return GraphicsCommandFromAPC([]byte(raw[2 : len(raw)-2]))
  17. }
  18. func TestGraphicsCommandSerialization(t *testing.T) {
  19. gc := &GraphicsCommand{}
  20. test_serialize := func(payload string, vals ...string) {
  21. expected := "\033_G" + strings.Join(vals, ",")
  22. if payload != "" {
  23. expected += ";" + base64.RawStdEncoding.EncodeToString([]byte(payload))
  24. }
  25. expected += "\033\\"
  26. if diff := cmp.Diff(expected, gc.AsAPC([]byte(payload))); diff != "" {
  27. t.Fatalf("Failed to write vals: %#v with payload: %#v\n%s", vals, payload, diff)
  28. }
  29. }
  30. test_chunked_payload := func(payload []byte) {
  31. c := &GraphicsCommand{}
  32. data := c.AsAPC([]byte(payload))
  33. encoded := strings.Builder{}
  34. compressed := false
  35. is_first := true
  36. for {
  37. idx := strings.Index(data, "\033_")
  38. if idx < 0 {
  39. break
  40. }
  41. l := strings.Index(data, "\033\\")
  42. apc := data[idx+2 : l]
  43. data = data[l+2:]
  44. g := GraphicsCommandFromAPC([]byte(apc))
  45. if is_first {
  46. compressed = g.Compression() != 0
  47. is_first = false
  48. }
  49. encoded.WriteString(g.ResponseMessage())
  50. if g.m == GRT_more_nomore {
  51. break
  52. }
  53. }
  54. if len(data) > 0 {
  55. t.Fatalf("Unparsed remnant: %#v", string(data))
  56. }
  57. decoded, err := base64.RawStdEncoding.DecodeString(encoded.String())
  58. if err != nil {
  59. t.Fatalf("Encoded data not valid base-64 with error: %v", err)
  60. }
  61. if compressed {
  62. b := bytes.Buffer{}
  63. b.Write(decoded)
  64. r, _ := zlib.NewReader(&b)
  65. o := bytes.Buffer{}
  66. if _, err = io.Copy(&o, r); err != nil {
  67. t.Fatal(err)
  68. }
  69. r.Close()
  70. decoded = o.Bytes()
  71. }
  72. if diff := cmp.Diff(payload, decoded); diff != "" {
  73. t.Fatalf("Decoded payload does not match original\nlen decoded=%d len payload=%d", len(decoded), len(payload))
  74. }
  75. }
  76. test_serialize("")
  77. gc.SetTransmission(GRT_transmission_sharedmem).SetAction(GRT_action_query).SetZIndex(-3).SetWidth(33).SetImageId(11)
  78. test_serialize("abcd", "a=q", "t=s", "w=33", "i=11", "z=-3")
  79. q := from_full_apc_escape_code(gc.AsAPC([]byte("abcd")))
  80. if diff := cmp.Diff(gc.AsAPC(nil), q.AsAPC(nil)); diff != "" {
  81. t.Fatalf("Parsing failed:\n%s", diff)
  82. }
  83. if diff := cmp.Diff(q.response_message, base64.RawStdEncoding.EncodeToString([]byte("abcd"))); diff != "" {
  84. t.Fatalf("Failed to parse payload:\n%s", diff)
  85. }
  86. test_chunked_payload([]byte("abcd"))
  87. data := make([]byte, 8111)
  88. _, _ = rand.Read(data)
  89. test_chunked_payload(data)
  90. test_chunked_payload([]byte(strings.Repeat("a", 8007)))
  91. }