func TestRepeatedExtensionsFieldsIssue161(t *testing.T) { r := rand.New(rand.NewSource(time.Now().UnixNano())) rep := 10 ints := make([]int64, rep) for i := range ints { ints[i] = r.Int63() } input := &MyExtendable{} if err := proto.SetExtension(input, E_FieldD, ints); err != nil { t.Fatal(err) } data, err := proto.Marshal(input) if err != nil { t.Fatal(err) } output := &MyExtendable{} if err := proto.Unmarshal(data, output); err != nil { t.Fatal(err) } if !input.Equal(output) { t.Fatal("expected equal") } data2, err2 := proto.Marshal(output) if err2 != nil { t.Fatal(err2) } if len(data) != len(data2) { t.Fatal("expected equal length buffers") } }
func TestNoMergeExtensionMerge(t *testing.T) { bigm := prototests.AContainer m := &prototests.Small{SmallField: proto.Int64(1)} data, err := proto.Marshal(bigm) if err != nil { panic(err) } mdata, err := proto.Marshal(m) if err != nil { panic(err) } key := uint32(101)<<3 | uint32(2) datakey := make([]byte, 10) n := binary.PutUvarint(datakey, uint64(key)) datakey = datakey[:n] datalen := make([]byte, 10) n = binary.PutUvarint(datalen, uint64(len(mdata))) datalen = datalen[:n] data = append(data, append(datakey, append(datalen, mdata...)...)...) err = noMerge(data, bigm.Description(), "prototests", "Container") if err == nil || !strings.Contains(err.Error(), "FieldB requires merging") { t.Fatalf("FieldB should require merging, but error is %v", err) } t.Log(err) }
func TestNoMergeExtensionMerge(t *testing.T) { r := rand.New(rand.NewSource(time.Now().UnixNano())) bigm := test.NewPopulatedMyExtendable(r, true) m := test.NewPopulatedNinOptNative(r, true) err := proto.SetExtension(bigm, test.E_FieldB, m) if err != nil { panic(err) } data, err := proto.Marshal(bigm) if err != nil { panic(err) } key := uint32(101)<<3 | uint32(2) data2 := make([]byte, 10) n := binary.PutUvarint(data2, uint64(key)) data2 = data2[:n] data = append(data, data2...) data4, err := proto.Marshal(test.NewPopulatedNinOptNative(r, true)) if err != nil { panic(err) } data3 := make([]byte, 10) n = binary.PutUvarint(data3, uint64(len(data4))) data3 = data3[:n] data = append(data, data3...) data = append(data, data4...) err = fieldpath.NoMerge(data, test.ThetestDescription(), "test", "MyExtendable") if err == nil || !strings.Contains(err.Error(), "requires merging") { t.Fatalf("should require merging") } }
func (this *uint32Writer) WriteMsg(msg proto.Message) (err error) { var data []byte if m, ok := msg.(marshaler); ok { n, ok := getSize(m) if !ok { data, err = proto.Marshal(msg) if err != nil { return err } } if n >= len(this.buffer) { this.buffer = make([]byte, n) } _, err = m.MarshalTo(this.buffer) if err != nil { return err } data = this.buffer[:n] } else { data, err = proto.Marshal(msg) if err != nil { return err } } length := uint32(len(data)) if err := binary.Write(this.w, this.byteOrder, &length); err != nil { return err } _, err = this.w.Write(data) return err }
func TestOldUnoM(t *testing.T) { popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) older := NewPopulatedOldUnoM(popr, true) // need optional field to be always initialized, to check it's lost in this test older.Field1 = proto.String(randStringUnrecognized(popr)) data1, err := proto.Marshal(older) if err != nil { panic(err) } newer := &UnoM{} if err = proto.Unmarshal(data1, newer); err != nil { panic(err) } data2, err := proto.Marshal(newer) if err != nil { panic(err) } older2 := &OldUnoM{} if err := proto.Unmarshal(data2, older2); err != nil { panic(err) } // check that Field1 is lost if older2.Field1 != nil { t.Fatalf("field must be lost, but it's not, older: %#v, older2: %#v", older, older2) } // now restore Field1 and messages should be equal now older2.Field1 = older.Field1 if err := older.VerboseEqual(older2); err != nil { t.Fatalf("%#v !VerboseProto %#v, since %v", older, older2, err) } }
// makeCompoundMessage takes a list of messages and generates // a single compound message containing all of them func makeCompoundMessage(msgs [][]byte) []byte { cMsg := CompoundMessage{} cMsg.Messages = make([]*CompoundMessage_SimpleMessage, 0, len(msgs)) for _, m := range msgs { cMsg.Messages = append(cMsg.Messages, &CompoundMessage_SimpleMessage{ Payload: m, }) } buf, err := proto.Marshal(&cMsg) if err != nil { return nil } gMsg := GossipMessage{ Type: MessageTypeCompound, Data: buf, } buf, err = proto.Marshal(&gMsg) if err != nil { return nil } return buf }
func (e *LoggregatorEmitter) EmitLogMessage(logMessage *logmessage.LogMessage) { messages := splitMessage(string(logMessage.GetMessage())) for _, message := range messages { if isEmpty(message) { continue } if len(message) > MAX_MESSAGE_BYTE_SIZE { logMessage.Message = append([]byte(message)[0:TRUNCATED_OFFSET], TRUNCATED_BYTES...) } else { logMessage.Message = []byte(message) } if e.sharedSecret == "" { marshalledLogMessage, err := proto.Marshal(logMessage) if err != nil { e.logger.Errorf("Error marshalling message: %s", err) return } e.LoggregatorClient.Send(marshalledLogMessage) } else { logEnvelope, err := e.newLogEnvelope(*logMessage.AppId, logMessage) if err != nil { e.logger.Errorf("Error creating envelope: %s", err) return } marshalledLogEnvelope, err := proto.Marshal(logEnvelope) if err != nil { e.logger.Errorf("Error marshalling envelope: %s", err) return } e.LoggregatorClient.Send(marshalledLogEnvelope) } } }
func (this *fullWriter) WriteMsg(msg proto.Message) (err error) { var data []byte if m, ok := msg.(marshaler); ok { n, ok := getSize(m) if !ok { data, err = proto.Marshal(msg) if err != nil { return err } } if n >= len(this.buffer) { this.buffer = make([]byte, n) } _, err = m.MarshalTo(this.buffer) if err != nil { return err } data = this.buffer[:n] } else { data, err = proto.Marshal(msg) if err != nil { return err } } _, err = this.w.Write(data) return err }
func (this *varintWriter) WriteMsg(msg proto.Message) (err error) { var data []byte if m, ok := msg.(marshaler); ok { n, ok := getSize(m) if !ok { data, err = proto.Marshal(msg) if err != nil { return err } } if n >= len(this.buffer) { this.buffer = make([]byte, n) } _, err = m.MarshalTo(this.buffer) if err != nil { return err } data = this.buffer[:n] } else { data, err = proto.Marshal(msg) if err != nil { return err } } length := uint64(len(data)) n := binary.PutUvarint(this.lenBuf, length) _, err = this.w.Write(this.lenBuf[:n]) if err != nil { return err } _, err = this.w.Write(data) return err }
func TestRepeatedExtensionsMsgsIssue161(t *testing.T) { r := rand.New(rand.NewSource(time.Now().UnixNano())) rep := 10 nins := make([]*NinOptNative, rep) for i := range nins { nins[i] = NewPopulatedNinOptNative(r, true) } input := &MyExtendable{} if err := proto.SetExtension(input, E_FieldE, nins); err != nil { t.Fatal(err) } data, err := proto.Marshal(input) if err != nil { t.Fatal(err) } output := &MyExtendable{} if err := proto.Unmarshal(data, output); err != nil { t.Fatal(err) } if !input.Equal(output) { t.Fatal("expected equal") } data2, err2 := proto.Marshal(output) if err2 != nil { t.Fatal(err2) } if len(data) != len(data2) { t.Fatal("expected equal length buffers") } }
func (o *OortFS) Create(ctx context.Context, parent, id []byte, inode uint64, name string, attr *pb.Attr, isdir bool) (string, *pb.Attr, error) { // Check to see if the name already exists b, err := o.comms.ReadGroupItem(ctx, parent, []byte(name)) if err != nil && !store.IsNotFound(err) { // TODO: Needs beter error handling return "", &pb.Attr{}, err } p := &pb.DirEntry{} err = proto.Unmarshal(b, p) if err != nil { return "", &pb.Attr{}, err } // Return an error if entry already exists and is not a tombstone if len(b) > 0 && p.Tombstone == nil { return "", &pb.Attr{}, nil } var direntType fuse.DirentType if isdir { direntType = fuse.DT_Dir } else { direntType = fuse.DT_File } // Add the name to the group d := &pb.DirEntry{ Version: DirEntryVersion, Name: name, Id: id, Type: uint32(direntType), } b, err = proto.Marshal(d) if err != nil { return "", &pb.Attr{}, err } err = o.comms.WriteGroup(ctx, parent, []byte(name), b) if err != nil { return "", &pb.Attr{}, err } // Add the inode entry n := &pb.InodeEntry{ Version: InodeEntryVersion, Inode: inode, IsDir: isdir, Attr: attr, Blocks: 0, } b, err = proto.Marshal(n) if err != nil { return "", &pb.Attr{}, err } err = o.WriteChunk(ctx, id, b) if err != nil { return "", &pb.Attr{}, err } return name, attr, nil }
func (o *OortFS) Create(ctx context.Context, parent, id []byte, inode uint64, name string, attr *pb.Attr, isdir bool) (string, *pb.Attr, error) { v, err := o.validateIP(ctx) if err != nil { return "", nil, err } if !v { return "", nil, errors.New("Unknown or unauthorized FS use") } // Check to see if the name already exists b, err := o.comms.ReadGroupItem(ctx, parent, []byte(name)) if err != nil && !store.IsNotFound(err) { // TODO: Needs beter error handling return "", &pb.Attr{}, err } if len(b) > 0 { return "", &pb.Attr{}, nil } p := &pb.DirEntry{} err = proto.Unmarshal(b, p) if err != nil { return "", &pb.Attr{}, err } // Add the name to the group d := &pb.DirEntry{ Version: DirEntryVersion, Name: name, Id: id, } b, err = proto.Marshal(d) if err != nil { return "", &pb.Attr{}, err } err = o.comms.WriteGroup(ctx, parent, []byte(name), b) if err != nil { return "", &pb.Attr{}, err } // Add the inode entry n := &pb.InodeEntry{ Version: InodeEntryVersion, Inode: inode, IsDir: isdir, Attr: attr, Blocks: 0, } b, err = proto.Marshal(n) if err != nil { return "", &pb.Attr{}, err } err = o.WriteChunk(ctx, id, b) if err != nil { return "", &pb.Attr{}, err } return name, attr, nil }
func writeResponse(w io.Writer, id uint64, serr string, response proto.Message) (err error) { // clean response if error if serr != "" { response = nil } // marshal response pbResponse := []byte{} if response != nil { pbResponse, err = proto.Marshal(response) if err != nil { return err } } // compress serialized proto data compressedPbResponse, err := snappy.Encode(nil, pbResponse) if err != nil { return err } // generate header header := &wire.ResponseHeader{ Id: id, Error: serr, RawResponseLen: uint32(len(pbResponse)), SnappyCompressedResponseLen: uint32(len(compressedPbResponse)), Checksum: crc32.ChecksumIEEE(compressedPbResponse), } // check header size pbHeader, err := proto.Marshal(header) if err != err { return } if uint32(len(pbHeader)) > wire.Default_Const_MaxHeaderLen { return fmt.Errorf("protorpc.writeResponse: header larger than max_header_len: %d.", len(pbHeader), ) } // send header (more) if err = sendFrame(w, pbHeader); err != nil { return } // send body (end) if err = sendFrame(w, compressedPbResponse); err != nil { return } return nil }
func (o *OortFS) Symlink(ctx context.Context, parent, id []byte, name string, target string, attr *pb.Attr, inode uint64) (*pb.SymlinkResponse, error) { v, err := o.validateIP(ctx) if err != nil { return nil, err } if !v { return nil, errors.New("Unknown or unauthorized FS use") } // Check to see if the name exists val, err := o.comms.ReadGroupItem(ctx, parent, []byte(name)) if err != nil && !store.IsNotFound(err) { // TODO: Needs beter error handling return &pb.SymlinkResponse{}, err } if len(val) > 1 { // Exists already return &pb.SymlinkResponse{}, nil } n := &pb.InodeEntry{ Version: InodeEntryVersion, Inode: inode, IsDir: false, IsLink: true, Target: target, Attr: attr, } b, err := proto.Marshal(n) if err != nil { return &pb.SymlinkResponse{}, err } err = o.WriteChunk(ctx, id, b) if err != nil { return &pb.SymlinkResponse{}, err } // Add the name to the group d := &pb.DirEntry{ Version: DirEntryVersion, Name: name, Id: id, } b, err = proto.Marshal(d) if err != nil { return &pb.SymlinkResponse{}, err } err = o.comms.WriteGroup(ctx, parent, []byte(name), b) if err != nil { return &pb.SymlinkResponse{}, err } return &pb.SymlinkResponse{Name: name, Attr: attr}, nil }
// updateRangeDescriptor adds a ConditionalPut on the range descriptor. The // conditional put verifies that changes to the range descriptor are made in a // well-defined order, preventing a scenario where a wayward replica which is // no longer part of the original Raft group comes back online to form a // splinter group with a node which was also a former replica, and hijacks the // range descriptor. This is a last line of defense; other mechanisms should // prevent rogue replicas from getting this far (see #768). func updateRangeDescriptor(b *client.Batch, descKey proto.Key, oldDesc, newDesc *proto.RangeDescriptor) error { var oldValue []byte if oldDesc != nil { var err error if oldValue, err = gogoproto.Marshal(oldDesc); err != nil { return err } } newValue, err := gogoproto.Marshal(newDesc) if err != nil { return err } b.CPut(descKey, newValue, oldValue) return nil }
// AddInfoProto adds or updates an info object. Returns an error if info // couldn't be added. func (g *Gossip) AddInfoProto(key string, proto gogoproto.Message, ttl time.Duration) error { bytes, err := gogoproto.Marshal(proto) if err != nil { return err } return g.AddInfo(key, bytes, ttl) }
// MarshalBinary encodes r to a binary format. func (r *CreateIteratorResponse) MarshalBinary() ([]byte, error) { var pb internal.CreateIteratorResponse if r.Err != nil { pb.Err = proto.String(r.Err.Error()) } return proto.Marshal(&pb) }
func (sink *WebsocketSink) Run(inputChan <-chan *events.Envelope) { stopChan := make(chan struct{}) sink.logger.Debugf("Websocket Sink %s: Running for streamId [%s]", sink.clientAddress, sink.streamId) context := truncatingbuffer.NewDefaultContext(sink.dropsondeOrigin, sink.Identifier()) buffer := sinks.RunTruncatingBuffer(inputChan, sink.messageDrainBufferSize, context, sink.logger, stopChan) for { sink.logger.Debugf("Websocket Sink %s: Waiting for activity", sink.clientAddress) messageEnvelope, ok := <-buffer.GetOutputChannel() if !ok { sink.logger.Debugf("Websocket Sink %s: Closed listener channel detected. Closing websocket", sink.clientAddress) close(stopChan) return } messageBytes, err := proto.Marshal(messageEnvelope) if err != nil { sink.logger.Errorf("Websocket Sink %s: Error marshalling %s envelope from origin %s: %s", sink.clientAddress, messageEnvelope.GetEventType().String(), messageEnvelope.GetOrigin(), err.Error()) continue } sink.logger.Debugf("Websocket Sink %s: Received %s message from %s at %d. Sending data.", sink.clientAddress, messageEnvelope.GetEventType().String(), messageEnvelope.GetOrigin(), messageEnvelope.Timestamp) if sink.writeTimeout != 0 { sink.ws.SetWriteDeadline(time.Now().Add(sink.writeTimeout)) } err = sink.ws.WriteMessage(gorilla.BinaryMessage, messageBytes) if err != nil { sink.logger.Debugf("Websocket Sink %s: Error when trying to send data to sink %s. Requesting close. Err: %v", sink.clientAddress, err) close(stopChan) return } sink.logger.Debugf("Websocket Sink %s: Successfully sent data", sink.clientAddress) } }
// post posts the call using the HTTP client. The call's method is // appended to KVDBEndpoint and set as the URL path. The call's arguments // are protobuf-serialized and written as the POST body. The content // type is set to application/x-protobuf. // // On success, the response body is unmarshalled into call.Reply. func (s *HTTPSender) post(call *Call) (*http.Response, error) { // Marshal the args into a request body. body, err := gogoproto.Marshal(call.Args) if err != nil { return nil, err } url := fmt.Sprintf("%s://%s%s%s", KVDBScheme, s.server, KVDBEndpoint, call.Method) req, err := http.NewRequest("POST", url, bytes.NewReader(body)) if err != nil { return nil, util.Errorf("unable to create request: %s", err) } req.Header.Add("Content-Type", "application/x-protobuf") req.Header.Add("Accept", "application/x-protobuf") resp, err := s.client.Do(req) if resp == nil { return nil, &httpSendError{util.Errorf("http client was closed: %s", err)} } defer resp.Body.Close() if err != nil { return nil, &httpSendError{err} } b, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, &httpSendError{err} } if resp.StatusCode != 200 { return resp, errors.New(resp.Status) } if err := gogoproto.Unmarshal(b, call.Reply); err != nil { log.Errorf("request completed, but unable to unmarshal response from server: %s; body=%q", err, b) return nil, &httpSendError{err} } return resp, nil }
// JoinRaft sends a configuration change to nodes to // add a new member to the raft cluster func (n *Node) JoinRaft(ctx context.Context, info *NodeInfo) (*JoinRaftResponse, error) { meta, err := proto.Marshal(info) if err != nil { log.Fatal("Can't marshal node: ", info.ID) } confChange := raftpb.ConfChange{ ID: info.ID, Type: raftpb.ConfChangeAddNode, NodeID: info.ID, Context: meta, } err = n.ProposeConfChange(n.Ctx, confChange) if err != nil { return &JoinRaftResponse{ Success: false, Error: ErrConfChangeRefused.Error(), }, nil } var nodes []*NodeInfo for _, node := range n.Cluster.Peers() { nodes = append(nodes, &NodeInfo{ ID: node.ID, Addr: node.Addr, }) } return &JoinRaftResponse{ Success: true, Nodes: nodes, Error: "", }, nil }
func Generate(req *plugin.CodeGeneratorRequest) *plugin.CodeGeneratorResponse { // Begin by allocating a generator. The request and response structures are stored there // so we can do error handling easily - the response structure contains the field to // report failure. g := generator.New() g.Request = req g.CommandLineParameters(g.Request.GetParameter()) // Create a wrapped version of the Descriptors and EnumDescriptors that // point to the file that defines them. g.WrapTypes() g.SetPackageNames() g.BuildTypeNameMap() g.GenerateAllFiles() gtest := generator.New() data, err := proto.Marshal(req) if err != nil { g.Error(err, "failed to marshal modified proto") } if err := proto.Unmarshal(data, gtest.Request); err != nil { g.Error(err, "parsing modified proto") } if len(gtest.Request.FileToGenerate) == 0 { gtest.Fail("no files to generate") } gtest.CommandLineParameters(gtest.Request.GetParameter()) // Create a wrapped version of the Descriptors and EnumDescriptors that // point to the file that defines them. gtest.WrapTypes() gtest.SetPackageNames() gtest.BuildTypeNameMap() gtest.GeneratePlugin(testgen.NewPlugin()) for i := 0; i < len(gtest.Response.File); i++ { if strings.Contains(*gtest.Response.File[i].Content, `//These tests are generated by github.com/gogo/protobuf/plugin/testgen`) { gtest.Response.File[i].Name = proto.String(strings.Replace(*gtest.Response.File[i].Name, ".pb.go", "pb_test.go", -1)) g.Response.File = append(g.Response.File, gtest.Response.File[i]) } } for i := 0; i < len(g.Response.File); i++ { formatted, err := format.Source([]byte(g.Response.File[i].GetContent())) if err != nil { g.Error(err, "go format error") } fmts := string(formatted) g.Response.File[i].Content = &fmts } return g.Response }
// Ensure shards with deprecated "OwnerIDs" can be decoded. func TestShardInfo_UnmarshalBinary_OwnerIDs(t *testing.T) { // Encode deprecated form to bytes. buf, err := proto.Marshal(&internal.ShardInfo{ ID: proto.Uint64(1), OwnerIDs: []uint64{10, 20, 30}, }) if err != nil { t.Fatal(err) } // Decode deprecated form. var si meta.ShardInfo if err := si.UnmarshalBinary(buf); err != nil { t.Fatal(err) } // Verify data is migrated correctly. if !reflect.DeepEqual(si, meta.ShardInfo{ ID: 1, Owners: []meta.ShardOwner{ {NodeID: 10}, {NodeID: 20}, {NodeID: 30}, }, }) { t.Fatalf("unexpected shard info: %s", spew.Sdump(si)) } }
func TestUNcompressedConfigMapGet(t *testing.T) { vers := int32(1) name := "smug-pigeon" key := testKey(name, vers) rel := releaseStub(name, vers, rspb.Status_DEPLOYED) // Create a test fixture which contains an uncompressed release cfgmap, err := newConfigMapsObject(key, rel, nil) if err != nil { t.Fatalf("Failed to create configmap: %s", err) } b, err := proto.Marshal(rel) if err != nil { t.Fatalf("Failed to marshal release: %s", err) } cfgmap.Data["release"] = base64.StdEncoding.EncodeToString(b) var mock MockConfigMapsInterface mock.objects = map[string]*api.ConfigMap{key: cfgmap} cfgmaps := NewConfigMaps(&mock) // get release with key got, err := cfgmaps.Get(key) if err != nil { t.Fatalf("Failed to get release: %s", err) } // compare fetched release with original if !reflect.DeepEqual(rel, got) { t.Errorf("Expected {%q}, got {%q}", rel, got) } }
func mustMarshal(m proto.Message) []byte { b, err := proto.Marshal(m) if err != nil { panic(err) } return b }
func TestMessageProto(t *testing.T) { popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) p := NewPopulatedMessage(popr, false) data, err := github_com_gogo_protobuf_proto.Marshal(p) if err != nil { panic(err) } msg := &Message{} if err := github_com_gogo_protobuf_proto.Unmarshal(data, msg); err != nil { panic(err) } littlefuzz := make([]byte, len(data)) copy(littlefuzz, data) for i := range data { data[i] = byte(popr.Intn(256)) } if err := p.VerboseEqual(msg); err != nil { t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err) } if !p.Equal(msg) { t.Fatalf("%#v !Proto %#v", msg, p) } if len(littlefuzz) > 0 { fuzzamount := 100 for i := 0; i < fuzzamount; i++ { littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256)) littlefuzz = append(littlefuzz, byte(popr.Intn(256))) } // shouldn't panic _ = github_com_gogo_protobuf_proto.Unmarshal(littlefuzz, msg) } }
// Build creates the defRefsIndex. func (x *defRefsIndex) Build(refs []*graph.Ref, fbr fileByteRanges, ofs byteOffsets) error { x.Lock() defer x.Unlock() vlog.Printf("defRefsIndex: building inverted def->ref index (%d refs)...", len(refs)) defToRefOfs := map[graph.RefDefKey]byteOffsets{} for i, ref := range refs { defToRefOfs[ref.RefDefKey()] = append(defToRefOfs[ref.RefDefKey()], ofs[i]) } vlog.Printf("defRefsIndex: adding %d index phtable keys...", len(defToRefOfs)) b := phtable.Builder(len(fbr)) for def, refOfs := range defToRefOfs { v, err := binary.Marshal(refOfs) if err != nil { return err } k, err := proto.Marshal(&def) if err != nil { return err } b.Add([]byte(k), v) } vlog.Printf("defRefsIndex: building index phtable...") h, err := b.Build() if err != nil { return err } h.StoreKeys = true // so defRefUnitsIndex can enumerate defs pointed to by this unit's refs x.phtable = h x.ready = true vlog.Printf("defRefsIndex: done building index.") return nil }
func (m *MesosMessenger) encodeLoop() { for { select { case <-m.stop: return case msg := <-m.encodingQueue: e := func() error { //TODO(jdef) implement timeout for context ctx, cancel := context.WithCancel(context.TODO()) defer cancel() b, err := proto.Marshal(msg.ProtoMessage) if err != nil { return err } msg.Bytes = b select { case <-ctx.Done(): return ctx.Err() case m.sendingQueue <- msg: return nil } }() if e != nil { m.reportError(fmt.Errorf("Failed to enqueue message %v: %v", msg, e)) } } } }
func TestGetExtensionStability(t *testing.T) { check := func(m *pb.MyMessage) bool { ext1, err := proto.GetExtension(m, pb.E_Ext_More) if err != nil { t.Fatalf("GetExtension() failed: %s", err) } ext2, err := proto.GetExtension(m, pb.E_Ext_More) if err != nil { t.Fatalf("GetExtension() failed: %s", err) } return ext1 == ext2 } msg := &pb.MyMessage{Count: proto.Int32(4)} ext0 := &pb.Ext{} if err := proto.SetExtension(msg, pb.E_Ext_More, ext0); err != nil { t.Fatalf("Could not set ext1: %s", ext0) } if !check(msg) { t.Errorf("GetExtension() not stable before marshaling") } bb, err := proto.Marshal(msg) if err != nil { t.Fatalf("Marshal() failed: %s", err) } msg1 := &pb.MyMessage{} err = proto.Unmarshal(bb, msg1) if err != nil { t.Fatalf("Unmarshal() failed: %s", err) } if !check(msg1) { t.Errorf("GetExtension() not stable after unmarshaling") } }
func TestFloatingPointProto(t *testing.T) { seed := time.Now().UnixNano() popr := math_rand.New(math_rand.NewSource(seed)) p := NewPopulatedFloatingPoint(popr, false) dAtA, err := github_com_gogo_protobuf_proto.Marshal(p) if err != nil { t.Fatalf("seed = %d, err = %v", seed, err) } msg := &FloatingPoint{} if err := github_com_gogo_protobuf_proto.Unmarshal(dAtA, msg); err != nil { t.Fatalf("seed = %d, err = %v", seed, err) } littlefuzz := make([]byte, len(dAtA)) copy(littlefuzz, dAtA) for i := range dAtA { dAtA[i] = byte(popr.Intn(256)) } if err := p.VerboseEqual(msg); err != nil { t.Fatalf("seed = %d, %#v !VerboseProto %#v, since %v", seed, msg, p, err) } if !p.Equal(msg) { t.Fatalf("seed = %d, %#v !Proto %#v", seed, msg, p) } if len(littlefuzz) > 0 { fuzzamount := 100 for i := 0; i < fuzzamount; i++ { littlefuzz[popr.Intn(len(littlefuzz))] = byte(popr.Intn(256)) littlefuzz = append(littlefuzz, byte(popr.Intn(256))) } // shouldn't panic _ = github_com_gogo_protobuf_proto.Unmarshal(littlefuzz, msg) } }
func TestMarshalRace(t *testing.T) { // unregistered extension desc := &proto.ExtensionDesc{ ExtendedType: (*pb.MyMessage)(nil), ExtensionType: (*bool)(nil), Field: 101010100, Name: "emptyextension", Tag: "varint,0,opt", } m := &pb.MyMessage{Count: proto.Int32(4)} if err := proto.SetExtension(m, desc, proto.Bool(true)); err != nil { t.Errorf("proto.SetExtension(m, desc, true): got error %q, want nil", err) } errChan := make(chan error, 3) for n := 3; n > 0; n-- { go func() { _, err := proto.Marshal(m) errChan <- err }() } for i := 0; i < 3; i++ { err := <-errChan if err != nil { t.Fatal(err) } } }