github.com/simonmittag/ws@v1.1.0-rc.5.0.20210419231947-82b846128245/rw_test.go (about)

     1  package ws
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  type RWTestCase struct {
     9  	Data   []byte
    10  	Header Header
    11  	Err    bool
    12  }
    13  
    14  type RWBenchCase struct {
    15  	label  string
    16  	header Header
    17  }
    18  
    19  var RWBenchCases = []RWBenchCase{
    20  	{
    21  		"no-mask",
    22  		Header{
    23  			OpCode: OpText,
    24  			Fin:    true,
    25  		},
    26  	},
    27  	{
    28  		"mask",
    29  		Header{
    30  			OpCode: OpText,
    31  			Fin:    true,
    32  			Masked: true,
    33  			Mask:   NewMask(),
    34  		},
    35  	},
    36  	{
    37  		"mask-u16",
    38  		Header{
    39  			OpCode: OpText,
    40  			Fin:    true,
    41  			Length: len16,
    42  			Masked: true,
    43  			Mask:   NewMask(),
    44  		},
    45  	},
    46  	{
    47  		"mask-u64",
    48  		Header{
    49  			OpCode: OpText,
    50  			Fin:    true,
    51  			Length: len64,
    52  			Masked: true,
    53  			Mask:   NewMask(),
    54  		},
    55  	},
    56  }
    57  
    58  var RWTestCases = []RWTestCase{
    59  	{
    60  		Data: bits("1 001 0001 0 1100100"),
    61  		//          _ ___ ____ _ _______
    62  		//          |  |   |   |    |
    63  		//         Fin |   |  Mask Length
    64  		//            Rsv  |
    65  		//             TextFrame
    66  		Header: Header{
    67  			Fin:    true,
    68  			Rsv:    Rsv(false, false, true),
    69  			OpCode: OpText,
    70  			Length: 100,
    71  		},
    72  	},
    73  	{
    74  		Data: bits("1 001 0001 1 1100100 00000001 10001000 00000000 11111111"),
    75  		//          _ ___ ____ _ _______ ___________________________________
    76  		//          |  |   |   |    |                     |
    77  		//         Fin |   |  Mask Length             Mask value
    78  		//            Rsv  |
    79  		//             TextFrame
    80  		Header: Header{
    81  			Fin:    true,
    82  			Rsv:    Rsv(false, false, true),
    83  			OpCode: OpText,
    84  			Length: 100,
    85  			Masked: true,
    86  			Mask:   [4]byte{0x01, 0x88, 0x00, 0xff},
    87  		},
    88  	},
    89  	{
    90  		Data: bits("0 110 0010 0 1111110 00001111 11111111"),
    91  		//          _ ___ ____ _ _______ _________________
    92  		//          |  |   |   |    |            |
    93  		//         Fin |   |  Mask Length   Length value
    94  		//            Rsv  |
    95  		//             BinaryFrame
    96  		Header: Header{
    97  			Fin:    false,
    98  			Rsv:    Rsv(true, true, false),
    99  			OpCode: OpBinary,
   100  			Length: 0x0fff,
   101  		},
   102  	},
   103  	{
   104  		Data: bits("1 000 1010 0 1111111 01111111 00000000 00000000 00000000 00000000 00000000 00000000 00000000"),
   105  		//          _ ___ ____ _ _______ _______________________________________________________________________
   106  		//          |  |   |   |    |                                       |
   107  		//         Fin |   |  Mask Length                              Length value
   108  		//            Rsv  |
   109  		//              PongFrame
   110  		Header: Header{
   111  			Fin:    true,
   112  			Rsv:    Rsv(false, false, false),
   113  			OpCode: OpPong,
   114  			Length: 0x7f00000000000000,
   115  		},
   116  	},
   117  }
   118  
   119  func bits(s string) []byte {
   120  	s = strings.Replace(s, " ", "", -1)
   121  	bts := make([]byte, len(s)/8)
   122  
   123  	for i, j := 0, 0; i < len(s); i, j = i+8, j+1 {
   124  		fmt.Sscanf(s[i:], "%08b", &bts[j])
   125  	}
   126  
   127  	return bts
   128  }