github.com/StackExchange/blackbox/v2@v2.0.1-0.20220331193400-d84e904973ab/pkg/makesafe/makesafe_test.go (about)

     1  package makesafe
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestRedact(t *testing.T) {
     8  	for i, test := range []struct{ data, expected string }{
     9  		{"", `""`},
    10  		{"one", "one"},
    11  		{"has space.txt", `"has space.txt"`},
    12  		{"has\ttab.txt", `"hasXtab.txt"(redacted)`},
    13  		{"has\nnl.txt", `"hasXnl.txt"(redacted)`},
    14  		{"has\rret.txt", `"hasXret.txt"(redacted)`},
    15  		{"¡que!", `¡que!`},
    16  		{"thé", `thé`},
    17  		{"pound£", `pound£`},
    18  		{"*.go", `*.go`},
    19  		{"rm -rf / ; echo done", `"rm -rf / ; echo done"`},
    20  		{"smile\u263a", `smile☺`},
    21  		{"dub\U0001D4E6", `dub𝓦`},
    22  		{"four\U0010FFFF", `"fourX"(redacted)`},
    23  	} {
    24  		g := Redact(test.data)
    25  		if g == test.expected {
    26  			t.Logf("%03d: PASSED", i)
    27  		} else {
    28  			t.Errorf("%03d: FAILED data=%q got=(%s) wanted=(%s)", i, test.data, g, test.expected)
    29  		}
    30  	}
    31  }
    32  
    33  func TestRedactMany(t *testing.T) {
    34  	data := []string{
    35  		"",
    36  		"one",
    37  		"has space.txt",
    38  		"has\ttab.txt",
    39  	}
    40  	g := RedactMany(data)
    41  	if len(g) != 4 || g[0] != `""` || g[1] != `"has space.txt"` || g[2] != `"hasXtab.txt"(redacted)` {
    42  		t.Logf("PASSED")
    43  	} else {
    44  		t.Errorf("FAILED got=(%q)", g)
    45  	}
    46  }
    47  
    48  func TestShell(t *testing.T) {
    49  	for i, test := range []struct{ data, expected string }{
    50  		{"", `""`},
    51  		{"one", "one"},
    52  		{"two\n", `$(printf '%q' 'two\n')`},
    53  		{"ta	tab", `$(printf '%q' 'ta\ttab')`},
    54  		{"tab\ttab", `$(printf '%q' 'tab\ttab')`},
    55  		{"new\nline", `$(printf '%q' 'new\nline')`},
    56  		{"¡que!", `$(printf '%q' '\302\241que!')`},
    57  		{"thé", `$(printf '%q' 'th\303\251')`},
    58  		{"pound£", `$(printf '%q' 'pound\302\243')`},
    59  		{"*.go", `'*.go'`},
    60  		{"rm -rf / ; echo done", `'rm -rf / ; echo done'`},
    61  		{"smile\u263a", `$(printf '%q' 'smile\342\230\272')`},
    62  		{"dub\U0001D4E6", `$(printf '%q' 'dub\360\235\223\246')`},
    63  		{"four\U0010FFFF", `$(printf '%q' 'four\364\217\277\277')`},
    64  	} {
    65  		g := Shell(test.data)
    66  		if g == test.expected {
    67  			t.Logf("%03d: PASSED", i)
    68  			//t.Logf("%03d: PASSED go(%q) bash: %s", i, test.data, test.expected)
    69  		} else {
    70  			t.Errorf("%03d: FAILED data=%q got=`%s` wanted=`%s`", i, test.data, g, test.expected)
    71  		}
    72  	}
    73  }
    74  
    75  func TestEscapeRune(t *testing.T) {
    76  	for i, test := range []struct {
    77  		data     rune
    78  		expected string
    79  	}{
    80  		{'a', `\141`},
    81  		{'é', `\303\251`},
    82  		{'☺', `\342\230\272`},
    83  		{'글', `\352\270\200`},
    84  		{'𩸽', `\360\251\270\275`},
    85  		//{"\U0010FEDC", `"'\U0010fedc'"`},
    86  	} {
    87  		g := escapeRune(test.data)
    88  		if g == test.expected {
    89  			t.Logf("%03d: PASSED go=(%q) bash=(%s)", i, test.data, test.expected)
    90  		} else {
    91  			t.Errorf("%03d: FAILED data=%q got=(%s) wanted=(%s)", i, test.data, g, test.expected)
    92  		}
    93  	}
    94  }
    95  
    96  func TestShellMany(t *testing.T) {
    97  	data := []string{
    98  		"",
    99  		"one",
   100  		"has space.txt",
   101  		"¡que!",
   102  	}
   103  	g := ShellMany(data)
   104  	if len(g) != 4 || g[0] != `""` || g[1] != "one" || g[2] != `"has space.txt"` || g[3] != `$(printf '%q' '\302\241que!')` {
   105  		t.Logf("PASSED")
   106  	} else {
   107  		t.Errorf("FAILED got=(%q)", g)
   108  	}
   109  }
   110  
   111  func TestFirstFewFlag(t *testing.T) {
   112  	for i, test := range []struct {
   113  		data           []string
   114  		expectedFlag   bool
   115  		expectedString string
   116  	}{
   117  		{[]string{"", "one"}, false, ` one`},
   118  		{[]string{"one"}, false, `one`},
   119  		{[]string{"one", "two", "three", "longlonglong", "longlonglonglong", "manylonglonglog", "morelongonglonglong"}, true, ``},
   120  	} {
   121  		gs, gf := FirstFewFlag(test.data)
   122  		if test.expectedFlag {
   123  			if gf == test.expectedFlag {
   124  				t.Logf("%03d: PASSED", i)
   125  			} else {
   126  				t.Errorf("%03d: FAILED data=%q got=(%q) wanted=(%q)", i, test.data, gs, test.expectedString)
   127  			}
   128  		} else {
   129  			if gf == test.expectedFlag && gs == test.expectedString {
   130  				t.Logf("%03d: PASSED", i)
   131  			} else {
   132  				t.Errorf("%03d: FAILED data=%q got=(%q) wanted=(%q)", i, test.data, gs, test.expectedString)
   133  			}
   134  		}
   135  	}
   136  }