func randomMessage() *rpc.Message { msg := new(rpc.Message) msg.Body = make([]byte, 10) io.ReadFull(rand.Reader, msg.Body) msg.Header = make(map[string]string, 2) msg.Header["aaa"] = "hello" msg.Header["aa"] = "hell" return msg }
func UnmarshalCommand(data []byte) (cmd *Command, err error) { if len(data) < 4 { return } cmd = new(Command) cmd.Type = data[0] nrParams := int(data[1] >> 4) nrHeaders := int((uint16(data[2]) << 8) | (uint16(data[3]))) data = data[4:] if nrParams > 0 { cmd.Params = make([]string, nrParams) var str []byte for i := 0; i < nrParams; i++ { str, data, err = cutString(data) if err != nil { return } cmd.Params[i] = string(str) } } var msg *rpc.Message msg = nil if nrHeaders > 0 { msg = new(rpc.Message) msg.Header = make(map[string]string, nrHeaders) var key []byte var value []byte for i := 0; i < nrHeaders; i++ { key, data, err = cutString(data) if err != nil { return } value, data, err = cutString(data) if err != nil { return } msg.Header[string(key)] = string(value) } } if len(data) > 0 { if msg == nil { msg = new(rpc.Message) } msg.Body = data } if msg != nil { cmd.Message = msg } return }
func randomMessage(maxNrHeader, maxBodyLen, maxStrLen int) *rpc.Message { nrHeaders := weakrand.Intn(maxNrHeaders) bodyLen := weakrand.Intn(maxBodyLen) if nrHeaders <= 0 && bodyLen <= 0 { return nil } msg := new(rpc.Message) if bodyLen > 0 { msg.Body = randomBytes(maxBodyLen) } if nrHeaders <= 0 { return msg } msg.Header = make(map[string]string, nrHeaders) for i := 0; i < nrHeaders; i++ { msg.Header[string(randomBytes(maxStrLen))] = string(randomBytes(maxStrLen)) } return msg }