github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/kbfscrypto/pad_test.go (about) 1 // Copyright 2016 Keybase Inc. All rights reserved. 2 // Use of this source code is governed by a BSD 3 // license that can be found in the LICENSE file. 4 5 package kbfscrypto 6 7 import ( 8 "bytes" 9 "testing" 10 "testing/quick" 11 12 "github.com/stretchr/testify/require" 13 ) 14 15 // Test padding of blocks results in a larger block, with length 16 // equal to power of 2 + 4. 17 func TestBlockPadding(t *testing.T) { 18 f := func(b []byte) bool { 19 padded, err := PadBlock(b) 20 if err != nil { 21 t.Logf("padBlock err: %s", err) 22 return false 23 } 24 n := len(padded) 25 if n <= len(b) { 26 t.Logf("padBlock padded block len %d <= input block len %d", n, len(b)) 27 return false 28 } 29 // len of slice without uint32 prefix: 30 h := n - 4 31 if h&(h-1) != 0 { 32 t.Logf("padBlock padded block len %d not a power of 2", h) 33 return false 34 } 35 return true 36 } 37 38 err := quick.Check(f, nil) 39 require.NoError(t, err) 40 } 41 42 // Tests padding -> depadding results in same block data. 43 func TestBlockDepadding(t *testing.T) { 44 f := func(b []byte) bool { 45 padded, err := PadBlock(b) 46 if err != nil { 47 t.Logf("padBlock err: %s", err) 48 return false 49 } 50 depadded, err := DepadBlock(padded) 51 if err != nil { 52 t.Logf("depadBlock err: %s", err) 53 return false 54 } 55 if !bytes.Equal(b, depadded) { 56 return false 57 } 58 return true 59 } 60 61 err := quick.Check(f, nil) 62 require.NoError(t, err) 63 } 64 65 // Test padding of blocks results in blocks at least 2^8. 66 func TestBlockPadMinimum(t *testing.T) { 67 for i := 0; i < 256; i++ { 68 b := make([]byte, i) 69 err := RandRead(b) 70 require.NoError(t, err) 71 padded, err := PadBlock(b) 72 require.NoError(t, err) 73 require.Equal(t, 260, len(padded)) 74 } 75 }