summaryrefslogtreecommitdiff
path: root/internal/config/args_test.go
blob: 5e351a5807df618983a9fdb4ac1d8a479db4c688 (plain)
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
package config

import (
	"encoding/base64"
	"strings"
	"testing"

	"github.com/mimecast/dtail/internal/lcontext"
)

func TestSerializeOptionsUsesStableOrdering(t *testing.T) {
	args := Args{
		LContext:   lcontextForTest(3, 1, 2),
		Plain:      true,
		Quiet:      true,
		Serverless: true,
	}

	got := args.SerializeOptions()
	want := "after=2:before=1:max=3:plain=true:quiet=true:serverless=true"
	if got != want {
		t.Fatalf("unexpected serialized options:\nwant %q\ngot  %q", want, got)
	}
}

func TestSerializeOptionsRoundTripsReservedValues(t *testing.T) {
	unsafeValue := "a:b=c|d"
	encodedValue := "base64%" + base64.StdEncoding.EncodeToString([]byte(unsafeValue))

	got := serializeOptions(map[string]string{
		"plain": "true",
		"note":  unsafeValue,
		"quiet": "false",
	})

	want := strings.Join([]string{
		"note=" + encodedValue,
		"plain=true",
		"quiet=false",
	}, ":")
	if got != want {
		t.Fatalf("unexpected serialized options:\nwant %q\ngot  %q", want, got)
	}

	options, ltx, err := DeserializeOptions(strings.Split(got, ":"))
	if err != nil {
		t.Fatalf("DeserializeOptions failed: %v", err)
	}
	if ltx != (lcontext.LContext{}) {
		t.Fatalf("unexpected lcontext: %#v", ltx)
	}
	if options["note"] != unsafeValue {
		t.Fatalf("expected note to round-trip, got %q", options["note"])
	}
	if options["plain"] != "true" {
		t.Fatalf("expected plain to round-trip, got %q", options["plain"])
	}
	if options["quiet"] != "false" {
		t.Fatalf("expected quiet to round-trip, got %q", options["quiet"])
	}
}

func TestDeserializeOptionsAcceptsRawSerializedBlob(t *testing.T) {
	unsafeValue := "a:b=c|d"
	serialized := serializeOptions(map[string]string{
		"note":  unsafeValue,
		"plain": "true",
		"quiet": "false",
	})

	options, ltx, err := DeserializeOptions([]string{serialized})
	if err != nil {
		t.Fatalf("DeserializeOptions failed: %v", err)
	}
	if ltx != (lcontext.LContext{}) {
		t.Fatalf("unexpected lcontext: %#v", ltx)
	}
	if options["note"] != unsafeValue {
		t.Fatalf("expected note to round-trip, got %q", options["note"])
	}
	if options["plain"] != "true" {
		t.Fatalf("expected plain to round-trip, got %q", options["plain"])
	}
	if options["quiet"] != "false" {
		t.Fatalf("expected quiet to round-trip, got %q", options["quiet"])
	}
}

func lcontextForTest(max, before, after int) lcontext.LContext {
	return lcontext.LContext{
		MaxCount:      max,
		BeforeContext: before,
		AfterContext:  after,
	}
}