OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / gorilla / websocket / mask_test.go
1 // Copyright 2016 The Gorilla WebSocket Authors. All rights reserved.  Use of
2 // this source code is governed by a BSD-style license that can be found in the
3 // LICENSE file.
4
5 // !appengine
6
7 package websocket
8
9 import (
10         "fmt"
11         "testing"
12 )
13
14 func maskBytesByByte(key [4]byte, pos int, b []byte) int {
15         for i := range b {
16                 b[i] ^= key[pos&3]
17                 pos++
18         }
19         return pos & 3
20 }
21
22 func notzero(b []byte) int {
23         for i := range b {
24                 if b[i] != 0 {
25                         return i
26                 }
27         }
28         return -1
29 }
30
31 func TestMaskBytes(t *testing.T) {
32         key := [4]byte{1, 2, 3, 4}
33         for size := 1; size <= 1024; size++ {
34                 for align := 0; align < wordSize; align++ {
35                         for pos := 0; pos < 4; pos++ {
36                                 b := make([]byte, size+align)[align:]
37                                 maskBytes(key, pos, b)
38                                 maskBytesByByte(key, pos, b)
39                                 if i := notzero(b); i >= 0 {
40                                         t.Errorf("size:%d, align:%d, pos:%d, offset:%d", size, align, pos, i)
41                                 }
42                         }
43                 }
44         }
45 }
46
47 func BenchmarkMaskBytes(b *testing.B) {
48         for _, size := range []int{2, 4, 8, 16, 32, 512, 1024} {
49                 b.Run(fmt.Sprintf("size-%d", size), func(b *testing.B) {
50                         for _, align := range []int{wordSize / 2} {
51                                 b.Run(fmt.Sprintf("align-%d", align), func(b *testing.B) {
52                                         for _, fn := range []struct {
53                                                 name string
54                                                 fn   func(key [4]byte, pos int, b []byte) int
55                                         }{
56                                                 {"byte", maskBytesByByte},
57                                                 {"word", maskBytes},
58                                         } {
59                                                 b.Run(fn.name, func(b *testing.B) {
60                                                         key := newMaskKey()
61                                                         data := make([]byte, size+align)[align:]
62                                                         for i := 0; i < b.N; i++ {
63                                                                 fn.fn(key, 0, data)
64                                                         }
65                                                         b.SetBytes(int64(len(data)))
66                                                 })
67                                         }
68                                 })
69                         }
70                 })
71         }
72 }