github.com/cloudwego/kitex@v0.9.0/pkg/protocol/bthrift/binary_test.go (about) 1 /* 2 * Copyright 2022 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package bthrift 18 19 import ( 20 "fmt" 21 "testing" 22 23 "github.com/apache/thrift/lib/go/thrift" 24 "github.com/cloudwego/netpoll" 25 26 "github.com/cloudwego/kitex/internal/test" 27 "github.com/cloudwego/kitex/pkg/remote" 28 internalnetpoll "github.com/cloudwego/kitex/pkg/remote/trans/netpoll" 29 ) 30 31 // TestWriteMessageEnd test binary WriteMessageEnd function 32 func TestWriteMessageEnd(t *testing.T) { 33 buf := make([]byte, 0) 34 test.Assert(t, Binary.WriteMessageEnd(buf) == 0) 35 } 36 37 // TestWriteStructBegin test binary WriteStructBegin and ReadStructBegin func 38 func TestWriteStructBegin(t *testing.T) { 39 buf := make([]byte, 0) 40 test.Assert(t, Binary.WriteStructBegin(buf, "") == 0) 41 42 name, length, err := Binary.ReadStructBegin(nil) 43 test.Assert(t, name == "") 44 test.Assert(t, length == 0) 45 test.Assert(t, nil == err) 46 } 47 48 // TestWriteStructEnd test binary WriteStructEnd and ReadStructEnd func 49 func TestWriteStructEnd(t *testing.T) { 50 buf := make([]byte, 0) 51 test.Assert(t, Binary.WriteStructEnd(buf) == 0) 52 53 length, err := Binary.ReadStructEnd(nil) 54 test.Assert(t, length == 0) 55 test.Assert(t, nil == err) 56 } 57 58 // TestWriteAndReadFieldBegin test binary WriteFieldBegin and ReadFieldBegin func 59 func TestWriteAndReadFieldBegin(t *testing.T) { 60 buf := make([]byte, 64) 61 exceptWs := "080020" 62 exceptSize := 3 63 wn := Binary.WriteFieldBegin(buf, "", thrift.I32, 32) 64 ws := fmt.Sprintf("%x", buf[:wn]) 65 test.Assert(t, wn == exceptSize, wn, exceptSize) 66 test.Assert(t, ws == exceptWs, ws, exceptWs) 67 68 name, tID, id, length, err := Binary.ReadFieldBegin(buf) 69 test.Assert(t, nil == err) 70 test.Assert(t, thrift.I32 == tID) 71 test.Assert(t, id == 32) 72 test.Assert(t, length == exceptSize) 73 test.Assert(t, name == "") 74 75 length, err = Binary.ReadFieldEnd(nil) 76 test.Assert(t, length == 0) 77 test.Assert(t, nil == err) 78 } 79 80 // TestWriteFieldEnd test binary WriteFieldEnd func 81 func TestWriteFieldEnd(t *testing.T) { 82 buf := make([]byte, 0) 83 test.Assert(t, Binary.WriteFieldEnd(buf) == 0) 84 } 85 86 // TestWriteFieldStop test binary WriteFieldStop func 87 func TestWriteFieldStop(t *testing.T) { 88 buf := make([]byte, 64) 89 test.Assert(t, Binary.WriteFieldStop(buf) == 1) 90 } 91 92 // TestWriteAndReadMapBegin test binary WriteMapBegin and ReadMapBegin 93 func TestWriteAndReadMapBegin(t *testing.T) { 94 buf := make([]byte, 64) 95 exceptWs := "0d2000000001" 96 exceptSize := 6 97 wn := Binary.WriteMapBegin(buf, thrift.MAP, 32, 1) 98 ws := fmt.Sprintf("%x", buf[:wn]) 99 test.Assert(t, wn == exceptSize, wn, exceptSize) 100 test.Assert(t, ws == exceptWs, ws, exceptWs) 101 102 keyTp, valTp, size, length, err := Binary.ReadMapBegin(buf) 103 test.Assert(t, nil == err) 104 test.Assert(t, thrift.MAP == keyTp) 105 test.Assert(t, valTp == 32) 106 test.Assert(t, size == 1) 107 test.Assert(t, length == exceptSize) 108 109 length, err = Binary.ReadMapEnd(nil) 110 test.Assert(t, length == 0) 111 test.Assert(t, nil == err) 112 } 113 114 // TestWriteMapEnd test binary WriteMapEnd 115 func TestWriteMapEnd(t *testing.T) { 116 buf := make([]byte, 64) 117 test.Assert(t, Binary.WriteMapEnd(buf) == 0) 118 } 119 120 // TestWriteAndReadListBegin test binary WriteListBegin and ReadListBegin 121 func TestWriteAndReadListBegin(t *testing.T) { 122 buf := make([]byte, 128) 123 exceptWs := "0f00000020" 124 exceptSize := 5 125 wn := Binary.WriteListBegin(buf, thrift.LIST, 32) 126 ws := fmt.Sprintf("%x", buf[:wn]) 127 test.Assert(t, wn == exceptSize, wn, exceptSize) 128 test.Assert(t, ws == exceptWs, ws, exceptWs) 129 130 tp, size, length, err := Binary.ReadListBegin(buf) 131 test.Assert(t, thrift.LIST == tp) 132 test.Assert(t, nil == err) 133 test.Assert(t, size == 32) 134 test.Assert(t, length == exceptSize) 135 136 length, err = Binary.ReadListEnd(nil) 137 test.Assert(t, length == 0) 138 test.Assert(t, nil == err) 139 } 140 141 // TestWriteListEnd test binary WriteListEnd 142 func TestWriteListEnd(t *testing.T) { 143 buf := make([]byte, 64) 144 test.Assert(t, Binary.WriteListEnd(buf) == 0) 145 } 146 147 // TestWriteAndReadSetBegin test binary WriteSetBegin and ReadSetBegin 148 func TestWriteAndReadSetBegin(t *testing.T) { 149 buf := make([]byte, 128) 150 exceptWs := "0e00000020" 151 exceptSize := 5 152 wn := Binary.WriteSetBegin(buf, thrift.SET, 32) 153 ws := fmt.Sprintf("%x", buf[:wn]) 154 test.Assert(t, wn == exceptSize, wn, exceptSize) 155 test.Assert(t, ws == exceptWs, ws, exceptWs) 156 157 tp, size, length, err := Binary.ReadSetBegin(buf) 158 test.Assert(t, nil == err) 159 test.Assert(t, size == 32) 160 test.Assert(t, thrift.SET == tp) 161 test.Assert(t, length == exceptSize) 162 163 length, err = Binary.ReadSetEnd(nil) 164 test.Assert(t, length == 0) 165 test.Assert(t, nil == err) 166 } 167 168 // TestWriteSetEnd test binary WriteSetEnd 169 func TestWriteSetEnd(t *testing.T) { 170 buf := make([]byte, 64) 171 test.Assert(t, Binary.WriteSetEnd(buf) == 0) 172 } 173 174 // TestWriteAndReadBool test binary WriteBool and ReadBool 175 func TestWriteAndReadBool(t *testing.T) { 176 buf := make([]byte, 8) 177 exceptWs := "01" 178 exceptSize := 1 179 wn := Binary.WriteBool(buf, true) 180 ws := fmt.Sprintf("%x", buf[:wn]) 181 test.Assert(t, wn == exceptSize, wn, exceptSize) 182 test.Assert(t, ws == exceptWs, ws, exceptWs) 183 184 v, length, err := Binary.ReadBool(buf) 185 test.Assert(t, nil == err) 186 test.Assert(t, exceptSize == length) 187 test.Assert(t, v) 188 189 exceptWs = "00" 190 wn = Binary.WriteBool(buf, false) 191 ws = fmt.Sprintf("%x", buf[:wn]) 192 test.Assert(t, wn == exceptSize, wn, exceptSize) 193 test.Assert(t, ws == exceptWs, ws, exceptWs) 194 195 v, length, err = Binary.ReadBool(buf) 196 test.Assert(t, nil == err) 197 test.Assert(t, exceptSize == length) 198 test.Assert(t, !v) 199 } 200 201 // TestWriteAndReadByte test binary WriteByte and ReadByte 202 func TestWriteAndReadByte(t *testing.T) { 203 buf := make([]byte, 1) 204 exceptWs := "31" 205 exceptSize := 1 206 wn := Binary.WriteByte(buf, '1') 207 ws := fmt.Sprintf("%x", buf[:wn]) 208 test.Assert(t, wn == exceptSize, wn, exceptSize) 209 test.Assert(t, ws == exceptWs, ws, exceptWs) 210 211 v, length, err := Binary.ReadByte(buf) 212 test.Assert(t, nil == err) 213 test.Assert(t, exceptSize == length) 214 test.Assert(t, v == '1') 215 } 216 217 // TestWriteAndReadI16 test binary WriteI16 and ReadI16 218 func TestWriteAndReadI16(t *testing.T) { 219 buf := make([]byte, 4) 220 exceptWs := "0001" 221 exceptSize := 2 222 wn := Binary.WriteI16(buf, 1) 223 ws := fmt.Sprintf("%x", buf[:wn]) 224 test.Assert(t, wn == exceptSize, wn, exceptSize) 225 test.Assert(t, ws == exceptWs, ws, exceptWs) 226 227 v, length, err := Binary.ReadI16(buf) 228 test.Assert(t, nil == err) 229 test.Assert(t, exceptSize == length) 230 test.Assert(t, v == 1) 231 } 232 233 // TestWriteAndReadI32 test binary WriteI32 and ReadI32 234 func TestWriteAndReadI32(t *testing.T) { 235 buf := make([]byte, 4) 236 exceptWs := "00000001" 237 exceptSize := 4 238 wn := Binary.WriteI32(buf, 1) 239 ws := fmt.Sprintf("%x", buf[:wn]) 240 test.Assert(t, wn == exceptSize, wn, exceptSize) 241 test.Assert(t, ws == exceptWs, ws, exceptWs) 242 243 v, length, err := Binary.ReadI32(buf) 244 test.Assert(t, nil == err) 245 test.Assert(t, exceptSize == length) 246 test.Assert(t, v == 1) 247 } 248 249 // TestWriteAndReadI64 test binary WriteI64 and ReadI64 250 func TestWriteAndReadI64(t *testing.T) { 251 buf := make([]byte, 8) 252 exceptWs := "0000000000000001" 253 exceptSize := 8 254 wn := Binary.WriteI64(buf, 1) 255 ws := fmt.Sprintf("%x", buf[:wn]) 256 test.Assert(t, wn == exceptSize, wn, exceptSize) 257 test.Assert(t, ws == exceptWs, ws, exceptWs) 258 259 v, length, err := Binary.ReadI64(buf) 260 test.Assert(t, nil == err) 261 test.Assert(t, exceptSize == length) 262 test.Assert(t, v == 1) 263 } 264 265 // TestWriteAndReadDouble test binary WriteDouble and ReadDouble 266 func TestWriteAndReadDouble(t *testing.T) { 267 buf := make([]byte, 8) 268 exceptWs := "3ff0000000000000" 269 exceptSize := 8 270 wn := Binary.WriteDouble(buf, 1) 271 ws := fmt.Sprintf("%x", buf[:wn]) 272 test.Assert(t, wn == exceptSize, wn, exceptSize) 273 test.Assert(t, ws == exceptWs, ws, exceptWs) 274 275 v, length, err := Binary.ReadDouble(buf) 276 test.Assert(t, nil == err) 277 test.Assert(t, exceptSize == length) 278 test.Assert(t, v == 1) 279 } 280 281 // TestWriteAndReadString test binary WriteString and ReadString 282 func TestWriteAndReadString(t *testing.T) { 283 buf := make([]byte, 128) 284 exceptWs := "000000056b69746578" 285 exceptSize := 9 286 wn := Binary.WriteString(buf, "kitex") 287 ws := fmt.Sprintf("%x", buf[:wn]) 288 test.Assert(t, wn == exceptSize, wn, exceptSize) 289 test.Assert(t, ws == exceptWs, ws, exceptWs) 290 291 v, length, err := Binary.ReadString(buf) 292 test.Assert(t, nil == err) 293 test.Assert(t, exceptSize == length) 294 test.Assert(t, v == "kitex") 295 } 296 297 // TestWriteAndReadBinary test binary WriteBinary and ReadBinary 298 func TestWriteAndReadBinary(t *testing.T) { 299 buf := make([]byte, 128) 300 exceptWs := "000000056b69746578" 301 exceptSize := 9 302 val := []byte("kitex") 303 wn := Binary.WriteBinary(buf, val) 304 ws := fmt.Sprintf("%x", buf[:wn]) 305 test.Assert(t, wn == exceptSize, wn, exceptSize) 306 test.Assert(t, ws == exceptWs, ws, exceptWs) 307 308 v, length, err := Binary.ReadBinary(buf) 309 test.Assert(t, nil == err) 310 test.Assert(t, exceptSize == length) 311 for i := 0; i < len(v); i++ { 312 test.Assert(t, val[i] == v[i]) 313 } 314 } 315 316 // TestWriteStringNocopy test binary WriteStringNocopy with small content 317 func TestWriteStringNocopy(t *testing.T) { 318 buf := make([]byte, 128) 319 exceptWs := "0000000c6d657373616765426567696e" 320 exceptSize := 16 321 out := internalnetpoll.NewReaderWriterByteBuffer(netpoll.NewLinkBuffer(0)) 322 nw, _ := out.(remote.NocopyWrite) 323 wn := Binary.WriteStringNocopy(buf, nw, "messageBegin") 324 ws := fmt.Sprintf("%x", buf[:wn]) 325 test.Assert(t, wn == exceptSize, wn, exceptSize) 326 test.Assert(t, ws == exceptWs, ws, exceptWs) 327 } 328 329 // TestWriteBinaryNocopy test binary NewReaderWriterByteBuffer 330 func TestWriteBinaryNocopy(t *testing.T) { 331 buf := make([]byte, 128) 332 exceptWs := "0000000c6d657373616765426567696e" 333 exceptSize := 16 334 out := internalnetpoll.NewReaderWriterByteBuffer(netpoll.NewLinkBuffer(0)) 335 nw, _ := out.(remote.NocopyWrite) 336 wn := Binary.WriteBinaryNocopy(buf, nw, []byte("messageBegin")) 337 ws := fmt.Sprintf("%x", buf[:wn]) 338 test.Assert(t, wn == exceptSize, wn, exceptSize) 339 test.Assert(t, ws == exceptWs, ws, exceptWs) 340 } 341 342 // TestLength test binary length functions 343 func TestLength(t *testing.T) { 344 // Message Length 345 test.Assert(t, Binary.MessageBeginLength("kitex", thrift.CALL, 1) == 17) 346 test.Assert(t, Binary.MessageEndLength() == 0) 347 348 // Struct Length 349 test.Assert(t, Binary.StructBeginLength("kitex") == 0) 350 test.Assert(t, Binary.StructEndLength() == 0) 351 352 // Field Length 353 test.Assert(t, Binary.FieldBeginLength("kitex", thrift.I32, 1) == 3) 354 test.Assert(t, Binary.FieldEndLength() == 0) 355 test.Assert(t, Binary.FieldStopLength() == 1) 356 357 // Map Length 358 test.Assert(t, Binary.MapBeginLength(thrift.MAP, thrift.MAP, 4) == 6) 359 test.Assert(t, Binary.MapEndLength() == 0) 360 361 // List Length 362 test.Assert(t, Binary.ListBeginLength(thrift.LIST, 4) == 5) 363 test.Assert(t, Binary.ListEndLength() == 0) 364 365 // Set Length 366 test.Assert(t, Binary.SetBeginLength(thrift.SET, 4) == 5) 367 test.Assert(t, Binary.SetEndLength() == 0) 368 369 // Bool Length 370 test.Assert(t, Binary.BoolLength(true) == 1) 371 test.Assert(t, Binary.BoolLength(false) == 1) 372 373 // Byte Length 374 test.Assert(t, Binary.ByteLength(1) == 1) 375 376 // Int Length 377 test.Assert(t, Binary.I16Length(1) == 2) 378 test.Assert(t, Binary.I32Length(1) == 4) 379 test.Assert(t, Binary.I64Length(1) == 8) 380 381 // Double Length 382 test.Assert(t, Binary.DoubleLength(1) == 8) 383 384 // String Length 385 test.Assert(t, Binary.StringLength("1") == 5) 386 test.Assert(t, Binary.StringLengthNocopy("1") == 5) 387 388 // Bytes Length 389 test.Assert(t, Binary.BinaryLength([]byte("1")) == 5) 390 test.Assert(t, Binary.BinaryLengthNocopy([]byte("1")) == 5) 391 } 392 393 // TestWriteMessageBegin test binary WriteMessageBegin and ReadMessageBegin with four thrift TMessageType 394 func TestWriteMessageBegin(t *testing.T) { 395 buf := make([]byte, 128) 396 exceptWs := "800100010000000c6d657373616765426567696e00000001" 397 exceptSize := 24 398 wn := Binary.WriteMessageBegin(buf, "messageBegin", thrift.CALL, 1) 399 ws := fmt.Sprintf("%x", buf[:wn]) 400 test.Assert(t, wn == exceptSize, wn, exceptSize) 401 test.Assert(t, ws == exceptWs, ws, exceptWs) 402 403 name, id, seqid, length, err := Binary.ReadMessageBegin(buf) 404 test.Assert(t, nil == err) 405 test.Assert(t, seqid == 1) 406 test.Assert(t, thrift.CALL == id) 407 test.Assert(t, name == "messageBegin") 408 test.Assert(t, length == exceptSize) 409 410 length, err = Binary.ReadMessageEnd(nil) 411 test.Assert(t, length == 0) 412 test.Assert(t, nil == err) 413 414 exceptWs = "800100020000000c6d657373616765426567696e00000001" 415 wn = Binary.WriteMessageBegin(buf, "messageBegin", thrift.REPLY, 1) 416 ws = fmt.Sprintf("%x", buf[:wn]) 417 test.Assert(t, wn == exceptSize, wn, exceptSize) 418 test.Assert(t, ws == exceptWs, ws, exceptWs) 419 420 name, id, seqid, length, err = Binary.ReadMessageBegin(buf) 421 test.Assert(t, nil == err) 422 test.Assert(t, seqid == 1) 423 test.Assert(t, thrift.REPLY == id) 424 test.Assert(t, name == "messageBegin") 425 test.Assert(t, length == exceptSize) 426 427 length, err = Binary.ReadMessageEnd(nil) 428 test.Assert(t, length == 0) 429 test.Assert(t, nil == err) 430 431 exceptWs = "800100030000000c6d657373616765426567696e00000001" 432 wn = Binary.WriteMessageBegin(buf, "messageBegin", thrift.EXCEPTION, 1) 433 ws = fmt.Sprintf("%x", buf[:wn]) 434 test.Assert(t, wn == exceptSize, wn, exceptSize) 435 test.Assert(t, ws == exceptWs, ws, exceptWs) 436 437 name, id, seqid, length, err = Binary.ReadMessageBegin(buf) 438 test.Assert(t, nil == err) 439 test.Assert(t, seqid == 1) 440 test.Assert(t, thrift.EXCEPTION == id) 441 test.Assert(t, name == "messageBegin") 442 test.Assert(t, length == exceptSize) 443 444 length, err = Binary.ReadMessageEnd(nil) 445 test.Assert(t, length == 0) 446 test.Assert(t, nil == err) 447 448 exceptWs = "800100040000000c6d657373616765426567696e00000001" 449 wn = Binary.WriteMessageBegin(buf, "messageBegin", thrift.ONEWAY, 1) 450 ws = fmt.Sprintf("%x", buf[:wn]) 451 test.Assert(t, wn == exceptSize, wn, exceptSize) 452 test.Assert(t, ws == exceptWs, ws, exceptWs) 453 454 name, id, seqid, length, err = Binary.ReadMessageBegin(buf) 455 test.Assert(t, nil == err) 456 test.Assert(t, seqid == 1) 457 test.Assert(t, thrift.ONEWAY == id) 458 test.Assert(t, name == "messageBegin") 459 test.Assert(t, length == exceptSize) 460 461 length, err = Binary.ReadMessageEnd(nil) 462 test.Assert(t, length == 0) 463 test.Assert(t, nil == err) 464 } 465 466 // TestSkip test binary Skip with write functions 467 func TestSkip(t *testing.T) { 468 buf := make([]byte, 1024) 469 offset, length := 0, 0 470 length += Binary.WriteByte(buf[length:], '1') 471 length += Binary.WriteString(buf[length:], "2") 472 length += Binary.WriteDouble(buf[length:], 3) 473 length += Binary.WriteByte(buf[length:], '4') 474 length += Binary.WriteBool(buf[length:], true) 475 length += Binary.WriteI16(buf[length:], 6) 476 length += Binary.WriteI32(buf[length:], 7) 477 length += Binary.WriteI64(buf[length:], 8) 478 length += Binary.WriteListBegin(buf[length:], thrift.LIST, 9) 479 length += Binary.WriteListEnd(buf[length:]) 480 length += Binary.WriteMapBegin(buf[length:], thrift.MAP, thrift.STRING, 10) 481 length += Binary.WriteMapEnd(buf[length:]) 482 length += Binary.WriteSetBegin(buf[length:], thrift.STRING, 11) 483 length += Binary.WriteSetEnd(buf[length:]) 484 485 test.Assert(t, length == 46) 486 487 size, _ := Binary.Skip(buf, thrift.BYTE) 488 offset += size 489 valStr, _, _ := Binary.ReadString(buf[offset:]) 490 test.Assert(t, valStr == "2") 491 492 size, _ = Binary.Skip(buf[offset:], thrift.STRING) 493 offset += size 494 valDouble, _, _ := Binary.ReadDouble(buf[offset:]) 495 test.Assert(t, valDouble == 3) 496 497 size, _ = Binary.Skip(buf[offset:], thrift.DOUBLE) 498 offset += size 499 valInt8, _, _ := Binary.ReadByte(buf[offset:]) 500 test.Assert(t, valInt8 == '4') 501 502 size, _ = Binary.Skip(buf[offset:], thrift.BYTE) 503 offset += size 504 valBool, _, _ := Binary.ReadBool(buf[offset:]) 505 test.Assert(t, valBool) 506 507 size, _ = Binary.Skip(buf[offset:], thrift.BOOL) 508 offset += size 509 valI16, _, _ := Binary.ReadI16(buf[offset:]) 510 test.Assert(t, valI16 == 6) 511 512 size, _ = Binary.Skip(buf[offset:], thrift.I16) 513 offset += size 514 valI32, _, _ := Binary.ReadI32(buf[offset:]) 515 test.Assert(t, valI32 == 7) 516 517 size, _ = Binary.Skip(buf[offset:], thrift.I32) 518 offset += size 519 valI64, _, _ := Binary.ReadI64(buf[offset:]) 520 test.Assert(t, valI64 == 8) 521 522 size, _ = Binary.Skip(buf[offset:], thrift.I64) 523 offset += size 524 _, valList, _, _ := Binary.ReadListBegin(buf[offset:]) 525 test.Assert(t, valList == 9) 526 527 size, _ = Binary.Skip(buf[offset+valList:], thrift.LIST) 528 offset += size 529 _, _, valMap, _, _ := Binary.ReadMapBegin(buf[offset:]) 530 test.Assert(t, valMap == 10) 531 532 size, _ = Binary.Skip(buf[offset+valMap+valList:], thrift.MAP) 533 offset += size 534 _, valSet, _, _ := Binary.ReadSetBegin(buf[offset:]) 535 test.Assert(t, valSet == 11) 536 }