Best K6 code snippet using data.Len
handshake_messages.go
Source:handshake_messages.go
...44 return m.raw45 }46 length := 2 + 32 + 1 + len(m.sessionId) + 2 + len(m.cipherSuites)*2 + 1 + len(m.compressionMethods)47 numExtensions := 048 extensionsLength := 049 if m.nextProtoNeg {50 numExtensions++51 }52 if m.ocspStapling {53 extensionsLength += 1 + 2 + 254 numExtensions++55 }56 if len(m.serverName) > 0 {57 extensionsLength += 5 + len(m.serverName)58 numExtensions++59 }60 if len(m.supportedCurves) > 0 {61 extensionsLength += 2 + 2*len(m.supportedCurves)62 numExtensions++63 }64 if len(m.supportedPoints) > 0 {65 extensionsLength += 1 + len(m.supportedPoints)66 numExtensions++67 }68 if m.ticketSupported {69 extensionsLength += len(m.sessionTicket)70 numExtensions++71 }72 if len(m.signatureAndHashes) > 0 {73 extensionsLength += 2 + 2*len(m.signatureAndHashes)74 numExtensions++75 }76 if numExtensions > 0 {77 extensionsLength += 4 * numExtensions78 length += 2 + extensionsLength79 }80 x := make([]byte, 4+length)81 x[0] = typeClientHello82 x[1] = uint8(length >> 16)83 x[2] = uint8(length >> 8)84 x[3] = uint8(length)85 x[4] = uint8(m.vers >> 8)86 x[5] = uint8(m.vers)87 copy(x[6:38], m.random)88 x[38] = uint8(len(m.sessionId))89 copy(x[39:39+len(m.sessionId)], m.sessionId)90 y := x[39+len(m.sessionId):]91 y[0] = uint8(len(m.cipherSuites) >> 7)92 y[1] = uint8(len(m.cipherSuites) << 1)93 for i, suite := range m.cipherSuites {94 y[2+i*2] = uint8(suite >> 8)95 y[3+i*2] = uint8(suite)96 }97 z := y[2+len(m.cipherSuites)*2:]98 z[0] = uint8(len(m.compressionMethods))99 copy(z[1:], m.compressionMethods)100 z = z[1+len(m.compressionMethods):]101 if numExtensions > 0 {102 z[0] = byte(extensionsLength >> 8)103 z[1] = byte(extensionsLength)104 z = z[2:]105 }106 if m.nextProtoNeg {107 z[0] = byte(extensionNextProtoNeg >> 8)108 z[1] = byte(extensionNextProtoNeg)109 // The length is always 0110 z = z[4:]111 }112 if len(m.serverName) > 0 {113 z[0] = byte(extensionServerName >> 8)114 z[1] = byte(extensionServerName)115 l := len(m.serverName) + 5116 z[2] = byte(l >> 8)117 z[3] = byte(l)118 z = z[4:]119 // RFC 3546, section 3.1120 //121 // struct {122 // NameType name_type;123 // select (name_type) {124 // case host_name: HostName;125 // } name;126 // } ServerName;127 //128 // enum {129 // host_name(0), (255)130 // } NameType;131 //132 // opaque HostName<1..2^16-1>;133 //134 // struct {135 // ServerName server_name_list<1..2^16-1>136 // } ServerNameList;137 z[0] = byte((len(m.serverName) + 3) >> 8)138 z[1] = byte(len(m.serverName) + 3)139 z[3] = byte(len(m.serverName) >> 8)140 z[4] = byte(len(m.serverName))141 copy(z[5:], []byte(m.serverName))142 z = z[l:]143 }144 if m.ocspStapling {145 // RFC 4366, section 3.6146 z[0] = byte(extensionStatusRequest >> 8)147 z[1] = byte(extensionStatusRequest)148 z[2] = 0149 z[3] = 5150 z[4] = 1 // OCSP type151 // Two zero valued uint16s for the two lengths.152 z = z[9:]153 }154 if len(m.supportedCurves) > 0 {155 // http://tools.ietf.org/html/rfc4492#section-5.5.1156 z[0] = byte(extensionSupportedCurves >> 8)157 z[1] = byte(extensionSupportedCurves)158 l := 2 + 2*len(m.supportedCurves)159 z[2] = byte(l >> 8)160 z[3] = byte(l)161 l -= 2162 z[4] = byte(l >> 8)163 z[5] = byte(l)164 z = z[6:]165 for _, curve := range m.supportedCurves {166 z[0] = byte(curve >> 8)167 z[1] = byte(curve)168 z = z[2:]169 }170 }171 if len(m.supportedPoints) > 0 {172 // http://tools.ietf.org/html/rfc4492#section-5.5.2173 z[0] = byte(extensionSupportedPoints >> 8)174 z[1] = byte(extensionSupportedPoints)175 l := 1 + len(m.supportedPoints)176 z[2] = byte(l >> 8)177 z[3] = byte(l)178 l--179 z[4] = byte(l)180 z = z[5:]181 for _, pointFormat := range m.supportedPoints {182 z[0] = byte(pointFormat)183 z = z[1:]184 }185 }186 if m.ticketSupported {187 // http://tools.ietf.org/html/rfc5077#section-3.2188 z[0] = byte(extensionSessionTicket >> 8)189 z[1] = byte(extensionSessionTicket)190 l := len(m.sessionTicket)191 z[2] = byte(l >> 8)192 z[3] = byte(l)193 z = z[4:]194 copy(z, m.sessionTicket)195 z = z[len(m.sessionTicket):]196 }197 if len(m.signatureAndHashes) > 0 {198 // https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1199 z[0] = byte(extensionSignatureAlgorithms >> 8)200 z[1] = byte(extensionSignatureAlgorithms)201 l := 2 + 2*len(m.signatureAndHashes)202 z[2] = byte(l >> 8)203 z[3] = byte(l)204 z = z[4:]205 l -= 2206 z[0] = byte(l >> 8)207 z[1] = byte(l)208 z = z[2:]209 for _, sigAndHash := range m.signatureAndHashes {210 z[0] = sigAndHash.hash211 z[1] = sigAndHash.signature212 z = z[2:]213 }214 }215 m.raw = x216 return x217}218func (m *clientHelloMsg) unmarshal(data []byte) bool {219 if len(data) < 42 {220 return false221 }222 m.raw = data223 m.vers = uint16(data[4])<<8 | uint16(data[5])224 m.random = data[6:38]225 sessionIdLen := int(data[38])226 if sessionIdLen > 32 || len(data) < 39+sessionIdLen {227 return false228 }229 m.sessionId = data[39 : 39+sessionIdLen]230 data = data[39+sessionIdLen:]231 if len(data) < 2 {232 return false233 }234 // cipherSuiteLen is the number of bytes of cipher suite numbers. Since235 // they are uint16s, the number must be even.236 cipherSuiteLen := int(data[0])<<8 | int(data[1])237 if cipherSuiteLen%2 == 1 || len(data) < 2+cipherSuiteLen {238 return false239 }240 numCipherSuites := cipherSuiteLen / 2241 m.cipherSuites = make([]uint16, numCipherSuites)242 for i := 0; i < numCipherSuites; i++ {243 m.cipherSuites[i] = uint16(data[2+2*i])<<8 | uint16(data[3+2*i])244 }245 data = data[2+cipherSuiteLen:]246 if len(data) < 1 {247 return false248 }249 compressionMethodsLen := int(data[0])250 if len(data) < 1+compressionMethodsLen {251 return false252 }253 m.compressionMethods = data[1 : 1+compressionMethodsLen]254 data = data[1+compressionMethodsLen:]255 m.nextProtoNeg = false256 m.serverName = ""257 m.ocspStapling = false258 m.ticketSupported = false259 m.sessionTicket = nil260 m.signatureAndHashes = nil261 if len(data) == 0 {262 // ClientHello is optionally followed by extension data263 return true264 }265 if len(data) < 2 {266 return false267 }268 extensionsLength := int(data[0])<<8 | int(data[1])269 data = data[2:]270 if extensionsLength != len(data) {271 return false272 }273 for len(data) != 0 {274 if len(data) < 4 {275 return false276 }277 extension := uint16(data[0])<<8 | uint16(data[1])278 length := int(data[2])<<8 | int(data[3])279 data = data[4:]280 if len(data) < length {281 return false282 }283 switch extension {284 case extensionServerName:285 if length < 2 {286 return false287 }288 numNames := int(data[0])<<8 | int(data[1])289 d := data[2:]290 for i := 0; i < numNames; i++ {291 if len(d) < 3 {292 return false293 }294 nameType := d[0]295 nameLen := int(d[1])<<8 | int(d[2])296 d = d[3:]297 if len(d) < nameLen {298 return false299 }300 if nameType == 0 {301 m.serverName = string(d[0:nameLen])302 break303 }304 d = d[nameLen:]305 }306 case extensionNextProtoNeg:307 if length > 0 {308 return false309 }310 m.nextProtoNeg = true311 case extensionStatusRequest:312 m.ocspStapling = length > 0 && data[0] == statusTypeOCSP313 case extensionSupportedCurves:314 // http://tools.ietf.org/html/rfc4492#section-5.5.1315 if length < 2 {316 return false317 }318 l := int(data[0])<<8 | int(data[1])319 if l%2 == 1 || length != l+2 {320 return false321 }322 numCurves := l / 2323 m.supportedCurves = make([]uint16, numCurves)324 d := data[2:]325 for i := 0; i < numCurves; i++ {326 m.supportedCurves[i] = uint16(d[0])<<8 | uint16(d[1])327 d = d[2:]328 }329 case extensionSupportedPoints:330 // http://tools.ietf.org/html/rfc4492#section-5.5.2331 if length < 1 {332 return false333 }334 l := int(data[0])335 if length != l+1 {336 return false337 }338 m.supportedPoints = make([]uint8, l)339 copy(m.supportedPoints, data[1:])340 case extensionSessionTicket:341 // http://tools.ietf.org/html/rfc5077#section-3.2342 m.ticketSupported = true343 m.sessionTicket = data[:length]344 case extensionSignatureAlgorithms:345 // https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1346 if length < 2 || length&1 != 0 {347 return false348 }349 l := int(data[0])<<8 | int(data[1])350 if l != length-2 {351 return false352 }353 n := l / 2354 d := data[2:]355 m.signatureAndHashes = make([]signatureAndHash, n)356 for i := range m.signatureAndHashes {357 m.signatureAndHashes[i].hash = d[0]358 m.signatureAndHashes[i].signature = d[1]359 d = d[2:]360 }361 }362 data = data[length:]363 }364 return true365}366type serverHelloMsg struct {367 raw []byte368 vers uint16369 random []byte370 sessionId []byte371 cipherSuite uint16372 compressionMethod uint8373 nextProtoNeg bool374 nextProtos []string375 ocspStapling bool376 ticketSupported bool377}378func (m *serverHelloMsg) equal(i interface{}) bool {379 m1, ok := i.(*serverHelloMsg)380 if !ok {381 return false382 }383 return bytes.Equal(m.raw, m1.raw) &&384 m.vers == m1.vers &&385 bytes.Equal(m.random, m1.random) &&386 bytes.Equal(m.sessionId, m1.sessionId) &&387 m.cipherSuite == m1.cipherSuite &&388 m.compressionMethod == m1.compressionMethod &&389 m.nextProtoNeg == m1.nextProtoNeg &&390 eqStrings(m.nextProtos, m1.nextProtos) &&391 m.ocspStapling == m1.ocspStapling &&392 m.ticketSupported == m1.ticketSupported393}394func (m *serverHelloMsg) marshal() []byte {395 if m.raw != nil {396 return m.raw397 }398 length := 38 + len(m.sessionId)399 numExtensions := 0400 extensionsLength := 0401 nextProtoLen := 0402 if m.nextProtoNeg {403 numExtensions++404 for _, v := range m.nextProtos {405 nextProtoLen += len(v)406 }407 nextProtoLen += len(m.nextProtos)408 extensionsLength += nextProtoLen409 }410 if m.ocspStapling {411 numExtensions++412 }413 if m.ticketSupported {414 numExtensions++415 }416 if numExtensions > 0 {417 extensionsLength += 4 * numExtensions418 length += 2 + extensionsLength419 }420 x := make([]byte, 4+length)421 x[0] = typeServerHello422 x[1] = uint8(length >> 16)423 x[2] = uint8(length >> 8)424 x[3] = uint8(length)425 x[4] = uint8(m.vers >> 8)426 x[5] = uint8(m.vers)427 copy(x[6:38], m.random)428 x[38] = uint8(len(m.sessionId))429 copy(x[39:39+len(m.sessionId)], m.sessionId)430 z := x[39+len(m.sessionId):]431 z[0] = uint8(m.cipherSuite >> 8)432 z[1] = uint8(m.cipherSuite)433 z[2] = uint8(m.compressionMethod)434 z = z[3:]435 if numExtensions > 0 {436 z[0] = byte(extensionsLength >> 8)437 z[1] = byte(extensionsLength)438 z = z[2:]439 }440 if m.nextProtoNeg {441 z[0] = byte(extensionNextProtoNeg >> 8)442 z[1] = byte(extensionNextProtoNeg)443 z[2] = byte(nextProtoLen >> 8)444 z[3] = byte(nextProtoLen)445 z = z[4:]446 for _, v := range m.nextProtos {447 l := len(v)448 if l > 255 {449 l = 255450 }451 z[0] = byte(l)452 copy(z[1:], []byte(v[0:l]))453 z = z[1+l:]454 }455 }456 if m.ocspStapling {457 z[0] = byte(extensionStatusRequest >> 8)458 z[1] = byte(extensionStatusRequest)459 z = z[4:]460 }461 if m.ticketSupported {462 z[0] = byte(extensionSessionTicket >> 8)463 z[1] = byte(extensionSessionTicket)464 z = z[4:]465 }466 m.raw = x467 return x468}469func (m *serverHelloMsg) unmarshal(data []byte) bool {470 if len(data) < 42 {471 return false472 }473 m.raw = data474 m.vers = uint16(data[4])<<8 | uint16(data[5])475 m.random = data[6:38]476 sessionIdLen := int(data[38])477 if sessionIdLen > 32 || len(data) < 39+sessionIdLen {478 return false479 }480 m.sessionId = data[39 : 39+sessionIdLen]481 data = data[39+sessionIdLen:]482 if len(data) < 3 {483 return false484 }485 m.cipherSuite = uint16(data[0])<<8 | uint16(data[1])486 m.compressionMethod = data[2]487 data = data[3:]488 m.nextProtoNeg = false489 m.nextProtos = nil490 m.ocspStapling = false491 m.ticketSupported = false492 if len(data) == 0 {493 // ServerHello is optionally followed by extension data494 return true495 }496 if len(data) < 2 {497 return false498 }499 extensionsLength := int(data[0])<<8 | int(data[1])500 data = data[2:]501 if len(data) != extensionsLength {502 return false503 }504 for len(data) != 0 {505 if len(data) < 4 {506 return false507 }508 extension := uint16(data[0])<<8 | uint16(data[1])509 length := int(data[2])<<8 | int(data[3])510 data = data[4:]511 if len(data) < length {512 return false513 }514 switch extension {515 case extensionNextProtoNeg:516 m.nextProtoNeg = true517 d := data[:length]518 for len(d) > 0 {519 l := int(d[0])520 d = d[1:]521 if l == 0 || l > len(d) {522 return false523 }524 m.nextProtos = append(m.nextProtos, string(d[:l]))525 d = d[l:]526 }527 case extensionStatusRequest:528 if length > 0 {529 return false530 }531 m.ocspStapling = true532 case extensionSessionTicket:533 if length > 0 {534 return false535 }536 m.ticketSupported = true537 }538 data = data[length:]539 }540 return true541}542type certificateMsg struct {543 raw []byte544 certificates [][]byte545}546func (m *certificateMsg) equal(i interface{}) bool {547 m1, ok := i.(*certificateMsg)548 if !ok {549 return false550 }551 return bytes.Equal(m.raw, m1.raw) &&552 eqByteSlices(m.certificates, m1.certificates)553}554func (m *certificateMsg) marshal() (x []byte) {555 if m.raw != nil {556 return m.raw557 }558 var i int559 for _, slice := range m.certificates {560 i += len(slice)561 }562 length := 3 + 3*len(m.certificates) + i563 x = make([]byte, 4+length)564 x[0] = typeCertificate565 x[1] = uint8(length >> 16)566 x[2] = uint8(length >> 8)567 x[3] = uint8(length)568 certificateOctets := length - 3569 x[4] = uint8(certificateOctets >> 16)570 x[5] = uint8(certificateOctets >> 8)571 x[6] = uint8(certificateOctets)572 y := x[7:]573 for _, slice := range m.certificates {574 y[0] = uint8(len(slice) >> 16)575 y[1] = uint8(len(slice) >> 8)576 y[2] = uint8(len(slice))577 copy(y[3:], slice)578 y = y[3+len(slice):]579 }580 m.raw = x581 return582}583func (m *certificateMsg) unmarshal(data []byte) bool {584 if len(data) < 7 {585 return false586 }587 m.raw = data588 certsLen := uint32(data[4])<<16 | uint32(data[5])<<8 | uint32(data[6])589 if uint32(len(data)) != certsLen+7 {590 return false591 }592 numCerts := 0593 d := data[7:]594 for certsLen > 0 {595 if len(d) < 4 {596 return false597 }598 certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])599 if uint32(len(d)) < 3+certLen {600 return false601 }602 d = d[3+certLen:]603 certsLen -= 3 + certLen604 numCerts++605 }606 m.certificates = make([][]byte, numCerts)607 d = data[7:]608 for i := 0; i < numCerts; i++ {609 certLen := uint32(d[0])<<16 | uint32(d[1])<<8 | uint32(d[2])610 m.certificates[i] = d[3 : 3+certLen]611 d = d[3+certLen:]612 }613 return true614}615type serverKeyExchangeMsg struct {616 raw []byte617 key []byte618}619func (m *serverKeyExchangeMsg) equal(i interface{}) bool {620 m1, ok := i.(*serverKeyExchangeMsg)621 if !ok {622 return false623 }624 return bytes.Equal(m.raw, m1.raw) &&625 bytes.Equal(m.key, m1.key)626}627func (m *serverKeyExchangeMsg) marshal() []byte {628 if m.raw != nil {629 return m.raw630 }631 length := len(m.key)632 x := make([]byte, length+4)633 x[0] = typeServerKeyExchange634 x[1] = uint8(length >> 16)635 x[2] = uint8(length >> 8)636 x[3] = uint8(length)637 copy(x[4:], m.key)638 m.raw = x639 return x640}641func (m *serverKeyExchangeMsg) unmarshal(data []byte) bool {642 m.raw = data643 if len(data) < 4 {644 return false645 }646 m.key = data[4:]647 return true648}649type certificateStatusMsg struct {650 raw []byte651 statusType uint8652 response []byte653}654func (m *certificateStatusMsg) equal(i interface{}) bool {655 m1, ok := i.(*certificateStatusMsg)656 if !ok {657 return false658 }659 return bytes.Equal(m.raw, m1.raw) &&660 m.statusType == m1.statusType &&661 bytes.Equal(m.response, m1.response)662}663func (m *certificateStatusMsg) marshal() []byte {664 if m.raw != nil {665 return m.raw666 }667 var x []byte668 if m.statusType == statusTypeOCSP {669 x = make([]byte, 4+4+len(m.response))670 x[0] = typeCertificateStatus671 l := len(m.response) + 4672 x[1] = byte(l >> 16)673 x[2] = byte(l >> 8)674 x[3] = byte(l)675 x[4] = statusTypeOCSP676 l -= 4677 x[5] = byte(l >> 16)678 x[6] = byte(l >> 8)679 x[7] = byte(l)680 copy(x[8:], m.response)681 } else {682 x = []byte{typeCertificateStatus, 0, 0, 1, m.statusType}683 }684 m.raw = x685 return x686}687func (m *certificateStatusMsg) unmarshal(data []byte) bool {688 m.raw = data689 if len(data) < 5 {690 return false691 }692 m.statusType = data[4]693 m.response = nil694 if m.statusType == statusTypeOCSP {695 if len(data) < 8 {696 return false697 }698 respLen := uint32(data[5])<<16 | uint32(data[6])<<8 | uint32(data[7])699 if uint32(len(data)) != 4+4+respLen {700 return false701 }702 m.response = data[8:]703 }704 return true705}706type serverHelloDoneMsg struct{}707func (m *serverHelloDoneMsg) equal(i interface{}) bool {708 _, ok := i.(*serverHelloDoneMsg)709 return ok710}711func (m *serverHelloDoneMsg) marshal() []byte {712 x := make([]byte, 4)713 x[0] = typeServerHelloDone714 return x715}716func (m *serverHelloDoneMsg) unmarshal(data []byte) bool {717 return len(data) == 4718}719type clientKeyExchangeMsg struct {720 raw []byte721 ciphertext []byte722}723func (m *clientKeyExchangeMsg) equal(i interface{}) bool {724 m1, ok := i.(*clientKeyExchangeMsg)725 if !ok {726 return false727 }728 return bytes.Equal(m.raw, m1.raw) &&729 bytes.Equal(m.ciphertext, m1.ciphertext)730}731func (m *clientKeyExchangeMsg) marshal() []byte {732 if m.raw != nil {733 return m.raw734 }735 length := len(m.ciphertext)736 x := make([]byte, length+4)737 x[0] = typeClientKeyExchange738 x[1] = uint8(length >> 16)739 x[2] = uint8(length >> 8)740 x[3] = uint8(length)741 copy(x[4:], m.ciphertext)742 m.raw = x743 return x744}745func (m *clientKeyExchangeMsg) unmarshal(data []byte) bool {746 m.raw = data747 if len(data) < 4 {748 return false749 }750 l := int(data[1])<<16 | int(data[2])<<8 | int(data[3])751 if l != len(data)-4 {752 return false753 }754 m.ciphertext = data[4:]755 return true756}757type finishedMsg struct {758 raw []byte759 verifyData []byte760}761func (m *finishedMsg) equal(i interface{}) bool {762 m1, ok := i.(*finishedMsg)763 if !ok {764 return false765 }766 return bytes.Equal(m.raw, m1.raw) &&767 bytes.Equal(m.verifyData, m1.verifyData)768}769func (m *finishedMsg) marshal() (x []byte) {770 if m.raw != nil {771 return m.raw772 }773 x = make([]byte, 4+len(m.verifyData))774 x[0] = typeFinished775 x[3] = byte(len(m.verifyData))776 copy(x[4:], m.verifyData)777 m.raw = x778 return779}780func (m *finishedMsg) unmarshal(data []byte) bool {781 m.raw = data782 if len(data) < 4 {783 return false784 }785 m.verifyData = data[4:]786 return true787}788type nextProtoMsg struct {789 raw []byte790 proto string791}792func (m *nextProtoMsg) equal(i interface{}) bool {793 m1, ok := i.(*nextProtoMsg)794 if !ok {795 return false796 }797 return bytes.Equal(m.raw, m1.raw) &&798 m.proto == m1.proto799}800func (m *nextProtoMsg) marshal() []byte {801 if m.raw != nil {802 return m.raw803 }804 l := len(m.proto)805 if l > 255 {806 l = 255807 }808 padding := 32 - (l+2)%32809 length := l + padding + 2810 x := make([]byte, length+4)811 x[0] = typeNextProtocol812 x[1] = uint8(length >> 16)813 x[2] = uint8(length >> 8)814 x[3] = uint8(length)815 y := x[4:]816 y[0] = byte(l)817 copy(y[1:], []byte(m.proto[0:l]))818 y = y[1+l:]819 y[0] = byte(padding)820 m.raw = x821 return x822}823func (m *nextProtoMsg) unmarshal(data []byte) bool {824 m.raw = data825 if len(data) < 5 {826 return false827 }828 data = data[4:]829 protoLen := int(data[0])830 data = data[1:]831 if len(data) < protoLen {832 return false833 }834 m.proto = string(data[0:protoLen])835 data = data[protoLen:]836 if len(data) < 1 {837 return false838 }839 paddingLen := int(data[0])840 data = data[1:]841 if len(data) != paddingLen {842 return false843 }844 return true845}846type certificateRequestMsg struct {847 raw []byte848 // hasSignatureAndHash indicates whether this message includes a list849 // of signature and hash functions. This change was introduced with TLS850 // 1.2.851 hasSignatureAndHash bool852 certificateTypes []byte853 signatureAndHashes []signatureAndHash854 certificateAuthorities [][]byte855}856func (m *certificateRequestMsg) equal(i interface{}) bool {857 m1, ok := i.(*certificateRequestMsg)858 if !ok {859 return false860 }861 return bytes.Equal(m.raw, m1.raw) &&862 bytes.Equal(m.certificateTypes, m1.certificateTypes) &&863 eqByteSlices(m.certificateAuthorities, m1.certificateAuthorities) &&864 eqSignatureAndHashes(m.signatureAndHashes, m1.signatureAndHashes)865}866func (m *certificateRequestMsg) marshal() (x []byte) {867 if m.raw != nil {868 return m.raw869 }870 // See http://tools.ietf.org/html/rfc4346#section-7.4.4871 length := 1 + len(m.certificateTypes) + 2872 casLength := 0873 for _, ca := range m.certificateAuthorities {874 casLength += 2 + len(ca)875 }876 length += casLength877 if m.hasSignatureAndHash {878 length += 2 + 2*len(m.signatureAndHashes)879 }880 x = make([]byte, 4+length)881 x[0] = typeCertificateRequest882 x[1] = uint8(length >> 16)883 x[2] = uint8(length >> 8)884 x[3] = uint8(length)885 x[4] = uint8(len(m.certificateTypes))886 copy(x[5:], m.certificateTypes)887 y := x[5+len(m.certificateTypes):]888 if m.hasSignatureAndHash {889 n := len(m.signatureAndHashes) * 2890 y[0] = uint8(n >> 8)891 y[1] = uint8(n)892 y = y[2:]893 for _, sigAndHash := range m.signatureAndHashes {894 y[0] = sigAndHash.hash895 y[1] = sigAndHash.signature896 y = y[2:]897 }898 }899 y[0] = uint8(casLength >> 8)900 y[1] = uint8(casLength)901 y = y[2:]902 for _, ca := range m.certificateAuthorities {903 y[0] = uint8(len(ca) >> 8)904 y[1] = uint8(len(ca))905 y = y[2:]906 copy(y, ca)907 y = y[len(ca):]908 }909 m.raw = x910 return911}912func (m *certificateRequestMsg) unmarshal(data []byte) bool {913 m.raw = data914 if len(data) < 5 {915 return false916 }917 length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])918 if uint32(len(data))-4 != length {919 return false920 }921 numCertTypes := int(data[4])922 data = data[5:]923 if numCertTypes == 0 || len(data) <= numCertTypes {924 return false925 }926 m.certificateTypes = make([]byte, numCertTypes)927 if copy(m.certificateTypes, data) != numCertTypes {928 return false929 }930 data = data[numCertTypes:]931 if m.hasSignatureAndHash {932 if len(data) < 2 {933 return false934 }935 sigAndHashLen := uint16(data[0])<<8 | uint16(data[1])936 data = data[2:]937 if sigAndHashLen&1 != 0 {938 return false939 }940 if len(data) < int(sigAndHashLen) {941 return false942 }943 numSigAndHash := sigAndHashLen / 2944 m.signatureAndHashes = make([]signatureAndHash, numSigAndHash)945 for i := range m.signatureAndHashes {946 m.signatureAndHashes[i].hash = data[0]947 m.signatureAndHashes[i].signature = data[1]948 data = data[2:]949 }950 }951 if len(data) < 2 {952 return false953 }954 casLength := uint16(data[0])<<8 | uint16(data[1])955 data = data[2:]956 if len(data) < int(casLength) {957 return false958 }959 cas := make([]byte, casLength)960 copy(cas, data)961 data = data[casLength:]962 m.certificateAuthorities = nil963 for len(cas) > 0 {964 if len(cas) < 2 {965 return false966 }967 caLen := uint16(cas[0])<<8 | uint16(cas[1])968 cas = cas[2:]969 if len(cas) < int(caLen) {970 return false971 }972 m.certificateAuthorities = append(m.certificateAuthorities, cas[:caLen])973 cas = cas[caLen:]974 }975 if len(data) > 0 {976 return false977 }978 return true979}980type certificateVerifyMsg struct {981 raw []byte982 hasSignatureAndHash bool983 signatureAndHash signatureAndHash984 signature []byte985}986func (m *certificateVerifyMsg) equal(i interface{}) bool {987 m1, ok := i.(*certificateVerifyMsg)988 if !ok {989 return false990 }991 return bytes.Equal(m.raw, m1.raw) &&992 m.hasSignatureAndHash == m1.hasSignatureAndHash &&993 m.signatureAndHash.hash == m1.signatureAndHash.hash &&994 m.signatureAndHash.signature == m1.signatureAndHash.signature &&995 bytes.Equal(m.signature, m1.signature)996}997func (m *certificateVerifyMsg) marshal() (x []byte) {998 if m.raw != nil {999 return m.raw1000 }1001 // See http://tools.ietf.org/html/rfc4346#section-7.4.81002 siglength := len(m.signature)1003 length := 2 + siglength1004 if m.hasSignatureAndHash {1005 length += 21006 }1007 x = make([]byte, 4+length)1008 x[0] = typeCertificateVerify1009 x[1] = uint8(length >> 16)1010 x[2] = uint8(length >> 8)1011 x[3] = uint8(length)1012 y := x[4:]1013 if m.hasSignatureAndHash {1014 y[0] = m.signatureAndHash.hash1015 y[1] = m.signatureAndHash.signature1016 y = y[2:]1017 }1018 y[0] = uint8(siglength >> 8)1019 y[1] = uint8(siglength)1020 copy(y[2:], m.signature)1021 m.raw = x1022 return1023}1024func (m *certificateVerifyMsg) unmarshal(data []byte) bool {1025 m.raw = data1026 if len(data) < 6 {1027 return false1028 }1029 length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])1030 if uint32(len(data))-4 != length {1031 return false1032 }1033 data = data[4:]1034 if m.hasSignatureAndHash {1035 m.signatureAndHash.hash = data[0]1036 m.signatureAndHash.signature = data[1]1037 data = data[2:]1038 }1039 if len(data) < 2 {1040 return false1041 }1042 siglength := int(data[0])<<8 + int(data[1])1043 data = data[2:]1044 if len(data) != siglength {1045 return false1046 }1047 m.signature = data1048 return true1049}1050type newSessionTicketMsg struct {1051 raw []byte1052 ticket []byte1053}1054func (m *newSessionTicketMsg) equal(i interface{}) bool {1055 m1, ok := i.(*newSessionTicketMsg)1056 if !ok {1057 return false1058 }1059 return bytes.Equal(m.raw, m1.raw) &&1060 bytes.Equal(m.ticket, m1.ticket)1061}1062func (m *newSessionTicketMsg) marshal() (x []byte) {1063 if m.raw != nil {1064 return m.raw1065 }1066 // See http://tools.ietf.org/html/rfc5077#section-3.31067 ticketLen := len(m.ticket)1068 length := 2 + 4 + ticketLen1069 x = make([]byte, 4+length)1070 x[0] = typeNewSessionTicket1071 x[1] = uint8(length >> 16)1072 x[2] = uint8(length >> 8)1073 x[3] = uint8(length)1074 x[8] = uint8(ticketLen >> 8)1075 x[9] = uint8(ticketLen)1076 copy(x[10:], m.ticket)1077 m.raw = x1078 return1079}1080func (m *newSessionTicketMsg) unmarshal(data []byte) bool {1081 m.raw = data1082 if len(data) < 10 {1083 return false1084 }1085 length := uint32(data[1])<<16 | uint32(data[2])<<8 | uint32(data[3])1086 if uint32(len(data))-4 != length {1087 return false1088 }1089 ticketLen := int(data[8])<<8 + int(data[9])1090 if len(data)-10 != ticketLen {1091 return false1092 }1093 m.ticket = data[10:]1094 return true1095}1096func eqUint16s(x, y []uint16) bool {1097 if len(x) != len(y) {1098 return false1099 }1100 for i, v := range x {1101 if y[i] != v {1102 return false1103 }1104 }...
https.go
Source:https.go
...63 err = fmt.Errorf("readHandshake: type[%d] is not clientHello", uint16(data[5]))64 return65 }66 // session67 sessionIdLen := int(data[43])68 if sessionIdLen > 32 || len(data) < 44+sessionIdLen {69 err = fmt.Errorf("readHandshake: sessionIdLen[%d] is long", sessionIdLen)70 return71 }72 data = data[44+sessionIdLen:]73 if len(data) < 2 {74 err = fmt.Errorf("readHandshake: dataLen[%d] after session is short", len(data))75 return76 }77 // cipher suite numbers78 cipherSuiteLen := int(data[0])<<8 | int(data[1])79 if cipherSuiteLen%2 == 1 || len(data) < 2+cipherSuiteLen {80 err = fmt.Errorf("readHandshake: dataLen[%d] after cipher suite is short", len(data))81 return82 }83 data = data[2+cipherSuiteLen:]84 if len(data) < 1 {85 err = fmt.Errorf("readHandshake: cipherSuiteLen[%d] is long", cipherSuiteLen)86 return87 }88 // compression method89 compressionMethodsLen := int(data[0])90 if len(data) < 1+compressionMethodsLen {91 err = fmt.Errorf("readHandshake: compressionMethodsLen[%d] is long", compressionMethodsLen)92 return93 }94 data = data[1+compressionMethodsLen:]95 if len(data) == 0 {96 // ClientHello is optionally followed by extension data97 err = fmt.Errorf("readHandshake: there is no extension data to get servername")98 return99 }100 if len(data) < 2 {101 err = fmt.Errorf("readHandshake: extension dataLen[%d] is too short", len(data))102 return103 }104 extensionsLength := int(data[0])<<8 | int(data[1])105 data = data[2:]106 if extensionsLength != len(data) {107 err = fmt.Errorf("readHandshake: extensionsLen[%d] is not equal to dataLen[%d]", extensionsLength, len(data))108 return109 }110 for len(data) != 0 {111 if len(data) < 4 {112 err = fmt.Errorf("readHandshake: extensionsDataLen[%d] is too short", len(data))113 return114 }115 extension := uint16(data[0])<<8 | uint16(data[1])116 length := int(data[2])<<8 | int(data[3])117 data = data[4:]118 if len(data) < length {119 err = fmt.Errorf("readHandshake: extensionLen[%d] is long", length)120 return121 }122 switch extension {123 case extensionRenegotiationInfo:124 if length != 1 || data[0] != 0 {125 err = fmt.Errorf("readHandshake: extension reNegotiationInfoLen[%d] is short", length)126 return127 }128 case extensionNextProtoNeg:129 case extensionStatusRequest:130 case extensionServerName:131 d := data[:length]132 if len(d) < 2 {133 err = fmt.Errorf("readHandshake: remiaining dataLen[%d] is short", len(d))134 return135 }136 namesLen := int(d[0])<<8 | int(d[1])137 d = d[2:]138 if len(d) != namesLen {139 err = fmt.Errorf("readHandshake: nameListLen[%d] is not equal to dataLen[%d]", namesLen, len(d))140 return141 }142 for len(d) > 0 {143 if len(d) < 3 {144 err = fmt.Errorf("readHandshake: extension serverNameLen[%d] is short", len(d))145 return146 }147 nameType := d[0]148 nameLen := int(d[1])<<8 | int(d[2])149 d = d[3:]150 if len(d) < nameLen {151 err = fmt.Errorf("readHandshake: nameLen[%d] is not equal to dataLen[%d]", nameLen, len(d))152 return153 }154 if nameType == 0 {155 serverName := string(d[:nameLen])156 host = strings.TrimSpace(serverName)157 return host, nil158 }159 d = d[nameLen:]160 }161 }162 data = data[length:]163 }164 err = fmt.Errorf("Unknow error")165 return166}167func GetHttpsHostname(c net.Conn) (_ net.Conn, _ map[string]string, err error) {168 reqInfoMap := make(map[string]string, 0)169 sc, rd := gnet.NewSharedConn(c)170 host, err := readHandshake(rd)171 if err != nil {172 return nil, reqInfoMap, err173 }...
Len
Using AI Code Generation
1import "fmt"2type data struct {3}4func (d *data) Len() int {5 return len(d.name)6}7func main() {8 d := data{"John", 20}9 fmt.Println(d.Len())10}11import "fmt"12type data struct {13}14func (d *data) Len() int {15 return len(d.name)16}17func main() {18 d := data{"John", 20}19 fmt.Println(d.Len())20}21import "fmt"22type data struct {23}24func (d *data) Len() int {25 return len(d.name)26}27func main() {28 d := data{"John", 20}29 fmt.Println(d.Len())30}31import "fmt"32type data struct {33}34func (d *data) Len() int {35 return len(d.name)36}37func main() {38 d := data{"John", 20}39 fmt.Println(d.Len())40}41import "fmt"42type data struct {43}44func (d *data) Len() int {45 return len(d.name)46}47func main() {48 d := data{"John", 20}49 fmt.Println(d.Len())50}51import "fmt"52type data struct {53}54func (d *data) Len() int {55 return len(d.name)56}57func main() {58 d := data{"John", 20}59 fmt.Println(d.Len())60}
Len
Using AI Code Generation
1import (2func main() {3 d := data{1, 2}4 fmt.Println(d.Len())5}6import (7func main() {8 d := data{1, 2}9 fmt.Println(d.Len())10}11import (12func main() {13 d := data{1, 2}14 fmt.Println(d.Len())15}16import (17func main() {18 d := data{1, 2}19 fmt.Println(d.Len())20}21import (22func main() {23 d := data{1, 2}24 fmt.Println(d.Len())25}26import (27func main() {28 d := data{1, 2}29 fmt.Println(d.Len())30}31import (32func main() {33 d := data{1, 2}34 fmt.Println(d.Len())35}36import (37func main() {38 d := data{1, 2}39 fmt.Println(d.Len())40}41import (42func main() {43 d := data{1, 2}44 fmt.Println(d.Len())45}46import (47func main() {48 d := data{1, 2}49 fmt.Println(d.Len())50}51import (52func main() {
Len
Using AI Code Generation
1data := Data{"test"}2fmt.Println(data.Len())3data := Data{"test"}4fmt.Println(data.Len())5data := Data{"test"}6fmt.Println(data.Len())7data := Data{"test"}8fmt.Println(data.Len())9data := Data{"test"}10fmt.Println(data.Len())11data := Data{"test"}12fmt.Println(data.Len())13data := Data{"test"}14fmt.Println(data.Len())15data := Data{"test"}16fmt.Println(data.Len())17data := Data{"test"}18fmt.Println(data.Len())19data := Data{"test"}20fmt.Println(data.Len())21data := Data{"test"}22fmt.Println(data.Len())23data := Data{"test"}24fmt.Println(data.Len())25data := Data{"test"}26fmt.Println(data.Len())27data := Data{"test"}28fmt.Println(data.Len())29data := Data{"test"}30fmt.Println(data.Len())31data := Data{"test"}32fmt.Println(data.Len())33data := Data{"test"}34fmt.Println(data.Len())35data := Data{"test"}36fmt.Println(data.Len())
Len
Using AI Code Generation
1import (2func main() {3 fruits := []string{"peach", "banana", "kiwi"}4 sort.Strings(fruits)5 fmt.Println("Sorted:", fruits)6 i := sort.SearchStrings(fruits, "banana")7 fmt.Println("Index:", i)8 nums := []int{7, 2, 8}9 sort.Ints(nums)10 fmt.Println("Sorted:", nums)11 i = sort.SearchInts(nums, 8)12 fmt.Println("Index:", i)13}
Len
Using AI Code Generation
1func main() {2 data := Data{1, 2, 3, 4, 5}3 fmt.Println(data.Len())4}5func (d Data) Len() int {6 return len(d)7}8import "testing"9func TestLen(t *testing.T) {10 data := Data{1, 2, 3, 4, 5}11 if data.Len() != 5 {12 t.Error("Expected 5 but got ", data.Len())13 }14}
Len
Using AI Code Generation
1import "fmt"2func main() {3 data = data{1, 2, 3, 4}4 fmt.Println(data.Len())5}6import "fmt"7type data struct {8}9func (data) Len() int {10}11func main() {12 data = data{1, 2, 3, 4}13 fmt.Println(data.Len())14}15import "fmt"16func main() {17 data = data{1, 2, 3, 4}18 fmt.Println(data.Len())19}20import "fmt"21type data struct {22}23func (data) Len() int {24}25func main() {26 data = data{1, 2, 3, 4}27 fmt.Println(data.Len())28}29./1.go:8:6: data.Len undefined (type data has no field or method Len)30import "fmt"31func main() {32 data = data{1, 2, 3, 4}33 fmt.Println(data.Len())34}
Len
Using AI Code Generation
1import "fmt"2func main() {3 d.Set(5)4 fmt.Println(d.Len())5}6import "fmt"7func main() {8 d.Set(5)9 fmt.Println(d.Len())10}11import "fmt"12func main() {13 d.Set(5)14 fmt.Println(d.Len())15}16import "fmt"17func main() {18 d.Set(5)19 fmt.Println(d.Len())20}21import "fmt"22func main() {23 d.Set(5)24 fmt.Println(d.Len())25}26import "fmt"27func main() {28 d.Set(5)29 fmt.Println(d.Len())30}31import "fmt"32func main() {33 d.Set(5)34 fmt.Println(d.Len())35}36import "fmt"37func main() {38 d.Set(5)39 fmt.Println(d.Len())40}41import "fmt"42func main() {43 d.Set(5)44 fmt.Println(d.Len())45}46import "fmt"47func main() {48 d.Set(5)49 fmt.Println(d.Len())50}51import "fmt"52func main() {53 d.Set(5)54 fmt.Println(d.Len())55}
Len
Using AI Code Generation
1import ( "fmt" "math" )2func main() {3}4import ( "fmt" "math" )5func main() {6}7import ( "fmt" "math" )8func main() {9}10import ( "fmt" "math" )11func main() {12}13import ( "fmt" "math" )14func main() {15}16import ( "fmt" "math" )17func main() {18}19import ( "fmt" "math" )20func main() {21}22import ( "fmt" "math" )23func main() {24}25import ( "fmt" "math" )26func main() {27}28import ( "fmt" "math" )29func main() {30}31import ( "fmt" "math" )32func main() {33}34import ( "fmt" "math" )35func main() {36}37import ( "
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!