aboutsummaryrefslogtreecommitdiffstats
path: root/vm/isolated/isolated_test.go
blob: f1b4dbda07352beece02b5dece4722844d46d217 (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
// Copyright 2020 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

package isolated

import (
	"testing"

	"github.com/google/syzkaller/vm/vmimpl"
)

func TestEscapeDoubleQuotes(t *testing.T) {
	testcases := []struct {
		inp      string
		expected string
	}{
		// Input with no quoting returns the same string.
		{
			"",
			"",
		},
		{
			"adsf",
			"adsf",
		},
		// Inputs with escaping of characters other that double
		// quotes returns the same input.
		{
			"\\$\\`\\\\\n", // \$\`\\\n
			"\\$\\`\\\\\n", // \$\`\\\n
		},
		// Input with double quote.
		{
			`"`,
			`\"`,
		},
		// Input with already escaped double quote.
		{
			`\"`,
			`\\\"`,
		},
		// Input with already escaped backtick and already
		// double quote. Should only re-escape the
		// double quote.
		{
			"\\`something\"",   // \`something"
			"\\`something\\\"", // \`something\"
		},
		// Input with already escaped backtick and already
		// escaped double quote. Should only re-escape the
		// escaped double quote.
		{
			"\\`something\\\"",     // \`something\"
			"\\`something\\\\\\\"", // \`something\\\"
		},
		{
			`touch \
    /tmp/OK
touch '/tmp/OK2'
touch "/tmp/OK3"
touch /tmp/OK4
bash -c "bash -c \"ls -al\""`,
			`touch \
    /tmp/OK
touch '/tmp/OK2'
touch \"/tmp/OK3\"
touch /tmp/OK4
bash -c \"bash -c \\\"ls -al\\\"\"`,
		},
	}
	for i, tc := range testcases {
		output := vmimpl.EscapeDoubleQuotes(tc.inp)
		if tc.expected != output {
			t.Fatalf("%v: For input %v Expected escaped string %v got %v", i+1, tc.inp, tc.expected, output)
		}
	}
}