...

Source file src/github.com/opencontainers/runc/libcontainer/nsenter/test/escape.go

Documentation: github.com/opencontainers/runc/libcontainer/nsenter/test

     1  package escapetest
     2  
     3  // This file is part of escape_json_string unit test.
     4  // It is in a separate package so cgo can be used together
     5  // with go test.
     6  
     7  // #include <stdlib.h>
     8  // extern char *escape_json_string(char *str);
     9  // #cgo CFLAGS: -DESCAPE_TEST=1
    10  import "C"
    11  
    12  import (
    13  	"testing"
    14  	"unsafe"
    15  )
    16  
    17  func testEscapeJSONString(t *testing.T, input, want string) {
    18  	in := C.CString(input)
    19  	out := C.escape_json_string(in)
    20  	got := C.GoString(out)
    21  	C.free(unsafe.Pointer(out))
    22  	t.Logf("input: %q, output: %q", input, got)
    23  	if got != want {
    24  		t.Errorf("Failed on input: %q, want %q, got %q", input, want, got)
    25  	}
    26  }
    27  
    28  func testEscapeJSON(t *testing.T) {
    29  	testCases := []struct {
    30  		input, output string
    31  	}{
    32  		{"", ""},
    33  		{"abcdef", "abcdef"},
    34  		{`\\\\\\`, `\\\\\\\\\\\\`},
    35  		{`with"quote`, `with\"quote`},
    36  		{"\n\r\b\t\f\\", `\n\r\b\t\f\\`},
    37  		{"\007", "\\u0007"},
    38  		{"\017 \020 \037", "\\u000f \\u0010 \\u001f"},
    39  		{"\033", "\\u001b"},
    40  		{`<->`, `<->`},
    41  		{"\176\177\200", "~\\u007f\200"},
    42  		{"\000", ""},
    43  		{"a\x7fxc", "a\\u007fxc"},
    44  		{"a\033xc", "a\\u001bxc"},
    45  		{"a\nxc", "a\\nxc"},
    46  		{"a\\xc", "a\\\\xc"},
    47  		{"Barney B\303\244r", "Barney B\303\244r"},
    48  	}
    49  
    50  	for _, tc := range testCases {
    51  		testEscapeJSONString(t, tc.input, tc.output)
    52  	}
    53  }
    54  

View as plain text