github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/vm/isolated/isolated_test.go (about)

     1  // Copyright 2020 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package isolated
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/google/syzkaller/vm/vmimpl"
    10  )
    11  
    12  func TestEscapeDoubleQuotes(t *testing.T) {
    13  	testcases := []struct {
    14  		inp      string
    15  		expected string
    16  	}{
    17  		// Input with no quoting returns the same string.
    18  		{
    19  			"",
    20  			"",
    21  		},
    22  		{
    23  			"adsf",
    24  			"adsf",
    25  		},
    26  		// Inputs with escaping of characters other that double
    27  		// quotes returns the same input.
    28  		{
    29  			"\\$\\`\\\\\n", // \$\`\\\n
    30  			"\\$\\`\\\\\n", // \$\`\\\n
    31  		},
    32  		// Input with double quote.
    33  		{
    34  			`"`,
    35  			`\"`,
    36  		},
    37  		// Input with already escaped double quote.
    38  		{
    39  			`\"`,
    40  			`\\\"`,
    41  		},
    42  		// Input with already escaped backtick and already
    43  		// double quote. Should only re-escape the
    44  		// double quote.
    45  		{
    46  			"\\`something\"",   // \`something"
    47  			"\\`something\\\"", // \`something\"
    48  		},
    49  		// Input with already escaped backtick and already
    50  		// escaped double quote. Should only re-escape the
    51  		// escaped double quote.
    52  		{
    53  			"\\`something\\\"",     // \`something\"
    54  			"\\`something\\\\\\\"", // \`something\\\"
    55  		},
    56  		{
    57  			`touch \
    58      /tmp/OK
    59  touch '/tmp/OK2'
    60  touch "/tmp/OK3"
    61  touch /tmp/OK4
    62  bash -c "bash -c \"ls -al\""`,
    63  			`touch \
    64      /tmp/OK
    65  touch '/tmp/OK2'
    66  touch \"/tmp/OK3\"
    67  touch /tmp/OK4
    68  bash -c \"bash -c \\\"ls -al\\\"\"`,
    69  		},
    70  	}
    71  	for i, tc := range testcases {
    72  		output := vmimpl.EscapeDoubleQuotes(tc.inp)
    73  		if tc.expected != output {
    74  			t.Fatalf("%v: For input %v Expected escaped string %v got %v", i+1, tc.inp, tc.expected, output)
    75  		}
    76  	}
    77  }