88 "encoding/binary"
99 "math/rand"
1010 "slices"
11+ "strconv"
1112 "testing"
1213
1314 "github.com/stretchr/testify/require"
@@ -135,7 +136,7 @@ func TestKeysMarshalingSimple(t *testing.T) {
135136 require .True (keys .Add ("key1" , Read ))
136137 bytes , err := keys .MarshalJSON ()
137138 require .NoError (err )
138- require .Equal ([] byte { 0x7b , 0x22 , 0x50 , 0x65 , 0x72 , 0x6d , 0x73 , 0x22 , 0x3a , 0x5b , 0x6e , 0x75 , 0x6c , 0x6c , 0x2c , 0x5b , 0x22 , 0x36 , 0x62 , 0x36 , 0x35 , 0x37 , 0x39 , 0x33 , 0x31 , 0x22 , 0x5d , 0x2c , 0x6e , 0x75 , 0x6c , 0x6c , 0x2c , 0x6e , 0x75 , 0x6c , 0x6c , 0x2c , 0x6e , 0x75 , 0x6c , 0x6c , 0x2c , 0x6e , 0x75 , 0x6c , 0x6c , 0x2c , 0x6e , 0x75 , 0x6c , 0x6c , 0x2c , 0x6e , 0x75 , 0x6c , 0x6c , 0x5d , 0x7d }, bytes )
139+ require .Equal (`{"6b657931":"read"}` , string ( bytes ) )
139140 keys = Keys {}
140141 require .NoError (keys .UnmarshalJSON (bytes ))
141142 require .Len (keys , 1 )
@@ -146,7 +147,7 @@ func TestKeysMarshalingSimple(t *testing.T) {
146147 require .True (keys .Add ("key2" , Read | Write ))
147148 bytes , err = keys .MarshalJSON ()
148149 require .NoError (err )
149- require .Equal ([] byte { 0x7b , 0x22 , 0x50 , 0x65 , 0x72 , 0x6d , 0x73 , 0x22 , 0x3a , 0x5b , 0x6e , 0x75 , 0x6c , 0x6c , 0x2c , 0x6e , 0x75 , 0x6c , 0x6c , 0x2c , 0x6e , 0x75 , 0x6c , 0x6c , 0x2c , 0x6e , 0x75 , 0x6c , 0x6c , 0x2c , 0x6e , 0x75 , 0x6c , 0x6c , 0x2c , 0x5b , 0x22 , 0x36 , 0x62 , 0x36 , 0x35 , 0x37 , 0x39 , 0x33 , 0x32 , 0x22 , 0x5d , 0x2c , 0x6e , 0x75 , 0x6c , 0x6c , 0x2c , 0x6e , 0x75 , 0x6c , 0x6c , 0x5d , 0x7d }, bytes )
150+ require .Equal (`{"6b657932":"write"}` , string ( bytes ) )
150151 keys = Keys {}
151152 require .NoError (keys .UnmarshalJSON (bytes ))
152153 require .Len (keys , 1 )
@@ -181,3 +182,64 @@ func TestKeysMarshalingFuzz(t *testing.T) {
181182 require .True (keys .compare (decodedKeys ))
182183 }
183184}
185+
186+ func TestNewPermissionFromString (t * testing.T ) {
187+ tests := []struct {
188+ strPerm string
189+ perm Permissions
190+ expectedErr error
191+ }{
192+ {
193+ strPerm : "read" ,
194+ perm : Read ,
195+ },
196+ {
197+ strPerm : "write" ,
198+ perm : Write ,
199+ },
200+ {
201+ strPerm : "allocate" ,
202+ perm : Allocate ,
203+ },
204+ {
205+ strPerm : "all" ,
206+ perm : All ,
207+ },
208+ {
209+ strPerm : "none" ,
210+ perm : None ,
211+ },
212+ {
213+ strPerm : "09" ,
214+ perm : Permissions (9 ),
215+ },
216+ {
217+ strPerm : "010A" ,
218+ expectedErr : errInvalidHexadecimalString ,
219+ },
220+ }
221+
222+ for i , test := range tests {
223+ t .Run (strconv .Itoa (i ), func (t * testing.T ) {
224+ require := require .New (t )
225+ var perm Permissions
226+ err := perm .UnmarshalText ([]byte (test .strPerm ))
227+ if test .expectedErr != nil {
228+ require .ErrorIs (err , test .expectedErr )
229+ } else {
230+ require .NoError (err )
231+ require .Equal (test .perm , perm )
232+ }
233+ })
234+ }
235+ }
236+
237+ func TestPermissionStringer (t * testing.T ) {
238+ require := require .New (t )
239+ require .Equal ("read" , Read .String ())
240+ require .Equal ("write" , Write .String ())
241+ require .Equal ("allocate" , Allocate .String ())
242+ require .Equal ("all" , All .String ())
243+ require .Equal ("none" , None .String ())
244+ require .Equal ("09" , Permissions (9 ).String ())
245+ }
0 commit comments