File: command_test.go

package info (click to toggle)
kitty 0.45.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,468 kB
  • sloc: ansic: 84,285; python: 57,992; objc: 5,432; sh: 1,333; xml: 364; makefile: 144; javascript: 78
file content (102 lines) | stat: -rw-r--r-- 2,762 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>

package graphics

import (
	"bytes"
	"compress/zlib"
	"encoding/base64"
	"fmt"
	"io"
	"strings"
	"testing"

	"github.com/google/go-cmp/cmp"
	"golang.org/x/exp/rand"
)

var _ = fmt.Print

func from_full_apc_escape_code(raw string) *GraphicsCommand {
	return GraphicsCommandFromAPC([]byte(raw[2 : len(raw)-2]))
}

func TestGraphicsCommandSerialization(t *testing.T) {
	gc := &GraphicsCommand{}

	test_serialize := func(payload string, vals ...string) {
		expected := "\033_G" + strings.Join(vals, ",")
		if payload != "" {
			expected += ";" + base64.RawStdEncoding.EncodeToString([]byte(payload))
		}
		expected += "\033\\"
		if diff := cmp.Diff(expected, gc.AsAPC([]byte(payload))); diff != "" {
			t.Fatalf("Failed to write vals: %#v with payload: %#v\n%s", vals, payload, diff)
		}
	}

	test_chunked_payload := func(payload []byte) {
		c := &GraphicsCommand{}
		data := c.AsAPC([]byte(payload))
		encoded := strings.Builder{}
		compressed := false
		is_first := true
		for {
			idx := strings.Index(data, "\033_")
			if idx < 0 {
				break
			}
			l := strings.Index(data, "\033\\")
			apc := data[idx+2 : l]
			data = data[l+2:]
			g := GraphicsCommandFromAPC([]byte(apc))
			if is_first {
				compressed = g.Compression() != 0
				is_first = false
			}
			encoded.WriteString(g.ResponseMessage())
			if g.m == GRT_more_nomore {
				break
			}
		}
		if len(data) > 0 {
			t.Fatalf("Unparsed remnant: %#v", string(data))
		}
		decoded, err := base64.RawStdEncoding.DecodeString(encoded.String())
		if err != nil {
			t.Fatalf("Encoded data not valid base-64 with error: %v", err)
		}
		if compressed {
			b := bytes.Buffer{}
			b.Write(decoded)
			r, _ := zlib.NewReader(&b)
			o := bytes.Buffer{}
			if _, err = io.Copy(&o, r); err != nil {
				t.Fatal(err)
			}
			r.Close()
			decoded = o.Bytes()
		}
		if diff := cmp.Diff(payload, decoded); diff != "" {
			t.Fatalf("Decoded payload does not match original\nlen decoded=%d len payload=%d", len(decoded), len(payload))
		}
	}

	test_serialize("")
	gc.SetTransmission(GRT_transmission_sharedmem).SetAction(GRT_action_query).SetZIndex(-3).SetWidth(33).SetImageId(11)
	test_serialize("abcd", "a=q", "t=s", "w=33", "i=11", "z=-3")
	q := from_full_apc_escape_code(gc.AsAPC([]byte("abcd")))
	if diff := cmp.Diff(gc.AsAPC(nil), q.AsAPC(nil)); diff != "" {
		t.Fatalf("Parsing failed:\n%s", diff)
	}
	if diff := cmp.Diff(q.response_message, base64.RawStdEncoding.EncodeToString([]byte("abcd"))); diff != "" {
		t.Fatalf("Failed to parse payload:\n%s", diff)
	}

	test_chunked_payload([]byte("abcd"))
	data := make([]byte, 8111)
	_, _ = rand.Read(data)
	test_chunked_payload(data)
	test_chunked_payload([]byte(strings.Repeat("a", 8007)))

}