github.com/pawelgaczynski/gain@v0.4.0-alpha.0.20230821120126-41f1e60a18da/conn_test.go (about) 1 package gain 2 3 import ( 4 "bytes" 5 "context" 6 "crypto/rand" 7 "testing" 8 "time" 9 10 . "github.com/stretchr/testify/require" 11 ) 12 13 func TestConnectionStateString(t *testing.T) { 14 Equal(t, "invalid", connInvalid.String()) 15 Equal(t, "accept", connAccept.String()) 16 Equal(t, "read", connRead.String()) 17 Equal(t, "write", connWrite.String()) 18 Equal(t, "close", connClose.String()) 19 } 20 21 func TestConnModeString(t *testing.T) { 22 Equal(t, "kernelSpace", connModeString(kernelSpace)) 23 Equal(t, "userSpace", connModeString(userSpace)) 24 Equal(t, "invalid", connModeString(2)) 25 } 26 27 func TestUserOpAllowed(t *testing.T) { 28 conn := newConnection() 29 30 NotNil(t, conn.userOpAllowed("userOp")) 31 32 conn.setKernelSpace() 33 NotNil(t, conn.userOpAllowed("userOp")) 34 35 conn.setUserSpace() 36 Nil(t, conn.userOpAllowed("userOp")) 37 38 conn.Close() 39 NotNil(t, conn.userOpAllowed("userOp")) 40 } 41 42 func TestMethodsUserOpNotAllowed(t *testing.T) { 43 conn := newConnection() 44 45 Equal(t, "op is not available in mode, op: setReadBuffer, mode: kernelSpace", conn.SetReadBuffer(1024).Error()) 46 47 Equal(t, "op is not available in mode, op: setWriteBuffer, mode: kernelSpace", conn.SetWriteBuffer(1024).Error()) 48 49 Equal(t, "op is not available in mode, op: setLinger, mode: kernelSpace", conn.SetLinger(0).Error()) 50 51 Equal(t, "op is not available in mode, op: setNoDelay, mode: kernelSpace", conn.SetNoDelay(true).Error()) 52 53 Equal(t, "op is not available in mode, op: setKeepAlivePeriod, mode: kernelSpace", 54 conn.SetKeepAlivePeriod(time.Second).Error()) 55 56 _, err := conn.Next(-1) 57 Equal(t, "op is not available in mode, op: next, mode: kernelSpace", err.Error()) 58 59 _, err = conn.Discard(1024) 60 Equal(t, "op is not available in mode, op: discard, mode: kernelSpace", err.Error()) 61 62 _, err = conn.Peek(1024) 63 Equal(t, "op is not available in mode, op: peek, mode: kernelSpace", err.Error()) 64 65 data := make([]byte, 1024) 66 _, err = rand.Read(data) 67 NoError(t, err) 68 reader := bytes.NewReader(data) 69 _, err = conn.ReadFrom(reader) 70 Equal(t, "op is not available in mode, op: readFrom, mode: kernelSpace", err.Error()) 71 72 buf := bytes.NewBuffer(nil) 73 _, err = conn.WriteTo(buf) 74 Equal(t, "op is not available in mode, op: writeTo, mode: kernelSpace", err.Error()) 75 76 _, err = conn.Read(make([]byte, 1024)) 77 Equal(t, "op is not available in mode, op: read, mode: kernelSpace", err.Error()) 78 79 _, err = conn.Write(make([]byte, 1024)) 80 Equal(t, "op is not available in mode, op: write, mode: kernelSpace", err.Error()) 81 } 82 83 func TestConnectionCtx(t *testing.T) { 84 conn := newConnection() 85 86 var ctx context.Context 87 88 conn.SetContext(ctx) 89 Equal(t, ctx, conn.Context()) 90 } 91 92 func TestConnectionReadFrom(t *testing.T) { 93 conn := newConnection() 94 conn.setUserSpace() 95 96 data := make([]byte, 1024) 97 _, err := rand.Read(data) 98 NoError(t, err) 99 reader := bytes.NewReader(data) 100 n, err := conn.ReadFrom(reader) 101 Nil(t, err) 102 Equal(t, int64(1024), n) 103 Equal(t, 1024, conn.OutboundBuffered()) 104 } 105 106 func TestConnectionWriteTo(t *testing.T) { 107 conn := newConnection() 108 conn.setUserSpace() 109 110 data := make([]byte, 1024) 111 n, err := rand.Read(data) 112 NoError(t, err) 113 Equal(t, 1024, n) 114 115 n, err = conn.inboundBuffer.Write(data) 116 NoError(t, err) 117 Equal(t, 1024, n) 118 119 buffer := make([]byte, 1024) 120 buf := bytes.NewBuffer(buffer) 121 nBytes, err := conn.WriteTo(buf) 122 NoError(t, err) 123 Equal(t, int64(1024), nBytes) 124 } 125 126 func TestConnectionPeek(t *testing.T) { 127 conn := newConnection() 128 conn.setUserSpace() 129 130 data := make([]byte, 1024) 131 n, err := rand.Read(data) 132 NoError(t, err) 133 Equal(t, 1024, n) 134 135 n, err = conn.inboundBuffer.Write(data) 136 NoError(t, err) 137 Equal(t, 1024, n) 138 139 buffer, err := conn.Peek(-1) 140 NoError(t, err) 141 Equal(t, 1024, len(buffer)) 142 Equal(t, data, buffer) 143 } 144 145 func TestConnectionDiscard(t *testing.T) { 146 conn := newConnection() 147 conn.setUserSpace() 148 149 data := make([]byte, 1024) 150 n, err := rand.Read(data) 151 NoError(t, err) 152 Equal(t, 1024, n) 153 154 n, err = conn.inboundBuffer.Write(data) 155 NoError(t, err) 156 Equal(t, 1024, n) 157 158 n, err = conn.Discard(512) 159 NoError(t, err) 160 Equal(t, 512, n) 161 162 buffer, err := conn.Peek(-1) 163 NoError(t, err) 164 Equal(t, 512, len(buffer)) 165 Equal(t, data[512:], buffer) 166 }