github.com/gogf/gf/v2@v2.7.4/net/gtcp/gtcp_z_unit_conn_pkg_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gtcp_test 8 9 import ( 10 "fmt" 11 "testing" 12 "time" 13 14 "github.com/gogf/gf/v2/debug/gdebug" 15 "github.com/gogf/gf/v2/net/gtcp" 16 "github.com/gogf/gf/v2/os/gfile" 17 "github.com/gogf/gf/v2/test/gtest" 18 "github.com/gogf/gf/v2/util/gconv" 19 ) 20 21 func Test_Package_Basic(t *testing.T) { 22 s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) { 23 defer conn.Close() 24 for { 25 data, err := conn.RecvPkg() 26 if err != nil { 27 break 28 } 29 conn.SendPkg(data) 30 } 31 }) 32 go s.Run() 33 defer s.Close() 34 time.Sleep(100 * time.Millisecond) 35 // SendPkg 36 gtest.C(t, func(t *gtest.T) { 37 conn, err := gtcp.NewConn(s.GetListenedAddress()) 38 t.AssertNil(err) 39 defer conn.Close() 40 for i := 0; i < 100; i++ { 41 err := conn.SendPkg([]byte(gconv.String(i))) 42 t.AssertNil(err) 43 } 44 for i := 0; i < 100; i++ { 45 err := conn.SendPkgWithTimeout([]byte(gconv.String(i)), time.Second) 46 t.AssertNil(err) 47 } 48 }) 49 // SendPkg with big data - failure. 50 gtest.C(t, func(t *gtest.T) { 51 conn, err := gtcp.NewConn(s.GetListenedAddress()) 52 t.AssertNil(err) 53 defer conn.Close() 54 data := make([]byte, 65536) 55 err = conn.SendPkg(data) 56 t.AssertNE(err, nil) 57 }) 58 // SendRecvPkg 59 gtest.C(t, func(t *gtest.T) { 60 conn, err := gtcp.NewConn(s.GetListenedAddress()) 61 t.AssertNil(err) 62 defer conn.Close() 63 for i := 100; i < 200; i++ { 64 data := []byte(gconv.String(i)) 65 result, err := conn.SendRecvPkg(data) 66 t.AssertNil(err) 67 t.Assert(result, data) 68 } 69 for i := 100; i < 200; i++ { 70 data := []byte(gconv.String(i)) 71 result, err := conn.SendRecvPkgWithTimeout(data, time.Second) 72 t.AssertNil(err) 73 t.Assert(result, data) 74 } 75 }) 76 // SendRecvPkg with big data - failure. 77 gtest.C(t, func(t *gtest.T) { 78 conn, err := gtcp.NewConn(s.GetListenedAddress()) 79 t.AssertNil(err) 80 defer conn.Close() 81 data := make([]byte, 65536) 82 result, err := conn.SendRecvPkg(data) 83 t.AssertNE(err, nil) 84 t.Assert(result, nil) 85 }) 86 // SendRecvPkg with big data - success. 87 gtest.C(t, func(t *gtest.T) { 88 conn, err := gtcp.NewConn(s.GetListenedAddress()) 89 t.AssertNil(err) 90 defer conn.Close() 91 data := make([]byte, 65500) 92 data[100] = byte(65) 93 data[65400] = byte(85) 94 result, err := conn.SendRecvPkg(data) 95 t.AssertNil(err) 96 t.Assert(result, data) 97 }) 98 } 99 100 func Test_Package_Basic_HeaderSize1(t *testing.T) { 101 s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) { 102 defer conn.Close() 103 for { 104 data, err := conn.RecvPkg(gtcp.PkgOption{HeaderSize: 1}) 105 if err != nil { 106 break 107 } 108 conn.SendPkg(data) 109 } 110 }) 111 go s.Run() 112 defer s.Close() 113 time.Sleep(100 * time.Millisecond) 114 115 // SendRecvPkg with empty data. 116 gtest.C(t, func(t *gtest.T) { 117 conn, err := gtcp.NewConn(s.GetListenedAddress()) 118 t.AssertNil(err) 119 defer conn.Close() 120 data := make([]byte, 0) 121 result, err := conn.SendRecvPkg(data) 122 t.AssertNil(err) 123 t.AssertNil(result) 124 }) 125 } 126 127 func Test_Package_Timeout(t *testing.T) { 128 s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) { 129 defer conn.Close() 130 for { 131 data, err := conn.RecvPkg() 132 if err != nil { 133 break 134 } 135 time.Sleep(time.Second) 136 gtest.Assert(conn.SendPkg(data), nil) 137 } 138 }) 139 go s.Run() 140 defer s.Close() 141 time.Sleep(100 * time.Millisecond) 142 gtest.C(t, func(t *gtest.T) { 143 conn, err := gtcp.NewConn(s.GetListenedAddress()) 144 t.AssertNil(err) 145 defer conn.Close() 146 data := []byte("10000") 147 result, err := conn.SendRecvPkgWithTimeout(data, time.Millisecond*500) 148 t.AssertNE(err, nil) 149 t.Assert(result, nil) 150 }) 151 gtest.C(t, func(t *gtest.T) { 152 conn, err := gtcp.NewConn(s.GetListenedAddress()) 153 t.AssertNil(err) 154 defer conn.Close() 155 data := []byte("10000") 156 result, err := conn.SendRecvPkgWithTimeout(data, time.Second*2) 157 t.AssertNil(err) 158 t.Assert(result, data) 159 }) 160 gtest.C(t, func(t *gtest.T) { 161 conn, err := gtcp.NewConn(s.GetListenedAddress()) 162 t.AssertNil(err) 163 defer conn.Close() 164 data := []byte("10000") 165 result, err := conn.SendRecvPkgWithTimeout(data, time.Second*2, gtcp.PkgOption{HeaderSize: 5}) 166 t.AssertNE(err, nil) 167 t.AssertNil(result) 168 }) 169 } 170 171 func Test_Package_Option(t *testing.T) { 172 s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) { 173 defer conn.Close() 174 option := gtcp.PkgOption{HeaderSize: 1} 175 for { 176 data, err := conn.RecvPkg(option) 177 if err != nil { 178 break 179 } 180 gtest.Assert(conn.SendPkg(data, option), nil) 181 } 182 }) 183 go s.Run() 184 defer s.Close() 185 time.Sleep(100 * time.Millisecond) 186 // SendRecvPkg with big data - failure. 187 gtest.C(t, func(t *gtest.T) { 188 conn, err := gtcp.NewConn(s.GetListenedAddress()) 189 t.AssertNil(err) 190 defer conn.Close() 191 data := make([]byte, 0xFF+1) 192 result, err := conn.SendRecvPkg(data, gtcp.PkgOption{HeaderSize: 1}) 193 t.AssertNE(err, nil) 194 t.Assert(result, nil) 195 }) 196 // SendRecvPkg with big data - success. 197 gtest.C(t, func(t *gtest.T) { 198 conn, err := gtcp.NewConn(s.GetListenedAddress()) 199 t.AssertNil(err) 200 defer conn.Close() 201 data := make([]byte, 0xFF) 202 data[100] = byte(65) 203 data[200] = byte(85) 204 result, err := conn.SendRecvPkg(data, gtcp.PkgOption{HeaderSize: 1}) 205 t.AssertNil(err) 206 t.Assert(result, data) 207 }) 208 } 209 210 func Test_Package_Option_HeadSize3(t *testing.T) { 211 s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) { 212 defer conn.Close() 213 option := gtcp.PkgOption{HeaderSize: 3} 214 for { 215 data, err := conn.RecvPkg(option) 216 if err != nil { 217 break 218 } 219 gtest.Assert(conn.SendPkg(data, option), nil) 220 } 221 }) 222 go s.Run() 223 defer s.Close() 224 time.Sleep(100 * time.Millisecond) 225 226 gtest.C(t, func(t *gtest.T) { 227 conn, err := gtcp.NewConn(s.GetListenedAddress()) 228 t.AssertNil(err) 229 defer conn.Close() 230 data := make([]byte, 0xFF) 231 data[100] = byte(65) 232 data[200] = byte(85) 233 result, err := conn.SendRecvPkg(data, gtcp.PkgOption{HeaderSize: 3}) 234 t.AssertNil(err) 235 t.Assert(result, data) 236 }) 237 } 238 239 func Test_Package_Option_HeadSize4(t *testing.T) { 240 s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) { 241 defer conn.Close() 242 option := gtcp.PkgOption{HeaderSize: 4} 243 for { 244 data, err := conn.RecvPkg(option) 245 if err != nil { 246 break 247 } 248 gtest.Assert(conn.SendPkg(data, option), nil) 249 } 250 }) 251 go s.Run() 252 defer s.Close() 253 time.Sleep(100 * time.Millisecond) 254 // SendRecvPkg with big data - failure. 255 gtest.C(t, func(t *gtest.T) { 256 conn, err := gtcp.NewConn(s.GetListenedAddress()) 257 t.AssertNil(err) 258 defer conn.Close() 259 data := make([]byte, 0xFFFF+1) 260 _, err = conn.SendRecvPkg(data, gtcp.PkgOption{HeaderSize: 4}) 261 t.AssertNil(err) 262 }) 263 // SendRecvPkg with big data - success. 264 gtest.C(t, func(t *gtest.T) { 265 conn, err := gtcp.NewConn(s.GetListenedAddress()) 266 t.AssertNil(err) 267 defer conn.Close() 268 data := make([]byte, 0xFF) 269 data[100] = byte(65) 270 data[200] = byte(85) 271 result, err := conn.SendRecvPkg(data, gtcp.PkgOption{HeaderSize: 4}) 272 t.AssertNil(err) 273 t.Assert(result, data) 274 }) 275 // pkgOption.HeaderSize oversize 276 gtest.C(t, func(t *gtest.T) { 277 conn, err := gtcp.NewConn(s.GetListenedAddress()) 278 t.AssertNil(err) 279 defer conn.Close() 280 data := make([]byte, 0xFF) 281 data[100] = byte(65) 282 data[200] = byte(85) 283 _, err = conn.SendRecvPkg(data, gtcp.PkgOption{HeaderSize: 5}) 284 t.AssertNE(err, nil) 285 }) 286 } 287 288 func Test_Server_NewServerKeyCrt(t *testing.T) { 289 var ( 290 noCrtFile = "noCrtFile" 291 noKeyFile = "noKeyFile" 292 crtFile = gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/crtFile" 293 keyFile = gfile.Dir(gdebug.CallerFilePath()) + gfile.Separator + "testdata/keyFile" 294 ) 295 gtest.C(t, func(t *gtest.T) { 296 addr := "127.0.0.1:%d" 297 freePort, _ := gtcp.GetFreePort() 298 addr = fmt.Sprintf(addr, freePort) 299 s, err := gtcp.NewServerKeyCrt(addr, noCrtFile, noKeyFile, func(conn *gtcp.Conn) { 300 }) 301 if err != nil { 302 t.AssertNil(s) 303 } 304 }) 305 gtest.C(t, func(t *gtest.T) { 306 addr := "127.0.0.1:%d" 307 freePort, _ := gtcp.GetFreePort() 308 addr = fmt.Sprintf(addr, freePort) 309 s, err := gtcp.NewServerKeyCrt(addr, crtFile, noKeyFile, func(conn *gtcp.Conn) { 310 }) 311 if err != nil { 312 t.AssertNil(s) 313 } 314 }) 315 gtest.C(t, func(t *gtest.T) { 316 addr := "127.0.0.1:%d" 317 freePort, _ := gtcp.GetFreePort() 318 addr = fmt.Sprintf(addr, freePort) 319 s, err := gtcp.NewServerKeyCrt(addr, crtFile, keyFile, func(conn *gtcp.Conn) { 320 }) 321 if err != nil { 322 t.AssertNil(s) 323 } 324 }) 325 } 326 327 func Test_Conn_RecvPkgError(t *testing.T) { 328 329 s := gtcp.NewServer(gtcp.FreePortAddress, func(conn *gtcp.Conn) { 330 defer conn.Close() 331 option := gtcp.PkgOption{HeaderSize: 5} 332 for { 333 _, err := conn.RecvPkg(option) 334 if err != nil { 335 break 336 } 337 } 338 }) 339 go s.Run() 340 defer s.Close() 341 342 time.Sleep(100 * time.Millisecond) 343 344 gtest.C(t, func(t *gtest.T) { 345 conn, err := gtcp.NewConn(s.GetListenedAddress()) 346 t.AssertNil(err) 347 defer conn.Close() 348 data := make([]byte, 65536) 349 result, err := conn.SendRecvPkg(data) 350 t.AssertNE(err, nil) 351 t.Assert(result, nil) 352 }) 353 }