github.com/ezoic/ws@v1.0.4-0.20220713205711-5c1d69e074c5/wsutil/cipher_test.go (about) 1 package wsutil 2 3 import ( 4 "bytes" 5 "fmt" 6 "io/ioutil" 7 "reflect" 8 "testing" 9 10 "github.com/ezoic/ws" 11 ) 12 13 func TestCipherReader(t *testing.T) { 14 for i, test := range []struct { 15 label string 16 data []byte 17 chop int 18 }{ 19 { 20 label: "simple", 21 data: []byte("hello, websockets!"), 22 chop: 512, 23 }, 24 { 25 label: "chopped", 26 data: []byte("hello, websockets!"), 27 chop: 3, 28 }, 29 } { 30 t.Run(fmt.Sprintf("%s#%d", test.label, i), func(t *testing.T) { 31 mask := ws.NewMask() 32 masked := make([]byte, len(test.data)) 33 copy(masked, test.data) 34 ws.Cipher(masked, mask, 0) 35 36 src := &chopReader{bytes.NewReader(masked), test.chop} 37 rd := NewCipherReader(src, mask) 38 39 bts, err := ioutil.ReadAll(rd) 40 if err != nil { 41 t.Errorf("unexpected error: %s", err) 42 return 43 } 44 if !reflect.DeepEqual(bts, test.data) { 45 t.Errorf("read data is not equal:\n\tact:\t%#v\n\texp:\t%#x\n", bts, test.data) 46 return 47 } 48 }) 49 } 50 }