github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/privilege/privilege_test.go (about)

     1  // Copyright 2015 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package privilege_test
    12  
    13  import (
    14  	"testing"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/sql/privilege"
    17  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    18  )
    19  
    20  func TestPrivilegeDecode(t *testing.T) {
    21  	defer leaktest.AfterTest(t)()
    22  	testCases := []struct {
    23  		raw              uint32
    24  		privileges       privilege.List
    25  		stringer, sorted string
    26  	}{
    27  		{0, privilege.List{}, "", ""},
    28  		// We avoid 0 as a privilege value even though we use 1 << privValue.
    29  		{1, privilege.List{}, "", ""},
    30  		{2, privilege.List{privilege.ALL}, "ALL", "ALL"},
    31  		{10, privilege.List{privilege.ALL, privilege.DROP}, "ALL, DROP", "ALL,DROP"},
    32  		{144, privilege.List{privilege.GRANT, privilege.DELETE}, "GRANT, DELETE", "DELETE,GRANT"},
    33  		{1022,
    34  			privilege.List{privilege.ALL, privilege.CREATE, privilege.DROP, privilege.GRANT,
    35  				privilege.SELECT, privilege.INSERT, privilege.DELETE, privilege.UPDATE, privilege.ZONECONFIG},
    36  			"ALL, CREATE, DROP, GRANT, SELECT, INSERT, DELETE, UPDATE, ZONECONFIG",
    37  			"ALL,CREATE,DELETE,DROP,GRANT,INSERT,SELECT,UPDATE,ZONECONFIG",
    38  		},
    39  	}
    40  
    41  	for _, tc := range testCases {
    42  		pl := privilege.ListFromBitField(tc.raw)
    43  		if len(pl) != len(tc.privileges) {
    44  			t.Fatalf("%+v: wrong privilege list from raw: %+v", tc, pl)
    45  		}
    46  		for i := 0; i < len(pl); i++ {
    47  			if pl[i] != tc.privileges[i] {
    48  				t.Fatalf("%+v: wrong privilege list from raw: %+v", tc, pl)
    49  			}
    50  		}
    51  		if pl.String() != tc.stringer {
    52  			t.Fatalf("%+v: wrong String() output: %q", tc, pl.String())
    53  		}
    54  		if pl.SortedString() != tc.sorted {
    55  			t.Fatalf("%+v: wrong SortedString() output: %q", tc, pl.SortedString())
    56  		}
    57  	}
    58  }