github.com/influxdata/influxql@v1.1.0/sanitize_test.go (about)

     1  package influxql_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/influxdata/influxql"
     7  )
     8  
     9  func TestSanitize(t *testing.T) {
    10  	var tests = []struct {
    11  		s    string
    12  		stmt string
    13  	}{
    14  		// Proper statements that should be redacted.
    15  		{
    16  			s:    `create user "admin" with password 'admin'`,
    17  			stmt: `create user "admin" with password [REDACTED]`,
    18  		},
    19  		{
    20  			s:    `set password for "admin" = 'admin'`,
    21  			stmt: `set password for "admin" = [REDACTED]`,
    22  		},
    23  
    24  		// Common invalid statements that should still be redacted.
    25  		{
    26  			s:    `create user "admin" with password "admin"`,
    27  			stmt: `create user "admin" with password [REDACTED]`,
    28  		},
    29  		{
    30  			s:    `set password for "admin" = "admin"`,
    31  			stmt: `set password for "admin" = [REDACTED]`,
    32  		},
    33  	}
    34  
    35  	for i, tt := range tests {
    36  		stmt := influxql.Sanitize(tt.s)
    37  		if tt.stmt != stmt {
    38  			t.Errorf("%d. %q\n\nsanitize mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
    39  		}
    40  	}
    41  }
    42  
    43  func BenchmarkSanitize(b *testing.B) {
    44  	b.ReportAllocs()
    45  	q := `create user "admin" with password 'admin'; set password for "admin" = 'admin'`
    46  	for i := 0; i < b.N; i++ {
    47  		influxql.Sanitize(q)
    48  	}
    49  }