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) } }
//See another version of this test in proto/extensions_test.go func TestGetExtensionStability(t *testing.T) { check := func(m *NoExtensionsMap) bool { ext1, err := proto.GetExtension(m, E_FieldB1) if err != nil { t.Fatalf("GetExtension() failed: %s", err) } ext2, err := proto.GetExtension(m, E_FieldB1) if err != nil { t.Fatalf("GetExtension() failed: %s", err) } return ext1.(*NinOptNative).Equal(ext2) } msg := &NoExtensionsMap{Field1: proto.Int64(2)} ext0 := &NinOptNative{Field1: proto.Float64(1)} if err := proto.SetExtension(msg, E_FieldB1, 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 := &NoExtensionsMap{} 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 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/pixty/gogoprotobuf/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 }
func TestZeroLengthOptionalBytes(t *testing.T) { roundtrip := func(f *Foo) *Foo { data, err := proto.Marshal(f) if err != nil { panic(err) } newF := &Foo{} err = proto.Unmarshal(data, newF) if err != nil { panic(err) } return newF } f := &Foo{} roundtrippedF := roundtrip(f) if roundtrippedF.Bar != nil { t.Fatalf("should be nil") } f.Bar = []byte{} roundtrippedF = roundtrip(f) if roundtrippedF.Bar == nil { t.Fatalf("should not be nil") } if len(roundtrippedF.Bar) != 0 { t.Fatalf("should be empty") } }
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 TestFloatingPointMarshalTo(t *testing.T) { popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) p := NewPopulatedFloatingPoint(popr, false) size := p.Size() data := make([]byte, size) for i := range data { data[i] = byte(popr.Intn(256)) } _, err := p.MarshalTo(data) if err != nil { panic(err) } msg := &FloatingPoint{} if err := github_com_gogo_protobuf_proto.Unmarshal(data, msg); err != nil { panic(err) } 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) } }
func TestRoundTripProto3(t *testing.T) { m := &pb.Message{ Name: "David", // (2 | 1<<3): 0x0a 0x05 "David" Hilarity: pb.Message_PUNS, // (0 | 2<<3): 0x10 0x01 HeightInCm: 178, // (0 | 3<<3): 0x18 0xb2 0x01 Data: []byte("roboto"), // (2 | 4<<3): 0x20 0x06 "roboto" ResultCount: 47, // (0 | 7<<3): 0x38 0x2f TrueScotsman: true, // (0 | 8<<3): 0x40 0x01 Score: 8.1, // (5 | 9<<3): 0x4d <8.1> Key: []uint64{1, 0xdeadbeef}, Nested: &pb.Nested{ Bunny: "Monty", }, } t.Logf(" m: %v", m) b, err := proto.Marshal(m) if err != nil { t.Fatalf("proto.Marshal: %v", err) } t.Logf(" b: %q", b) m2 := new(pb.Message) if err := proto.Unmarshal(b, m2); err != nil { t.Fatalf("proto.Unmarshal: %v", err) } t.Logf("m2: %v", m2) if !proto.Equal(m, m2) { t.Errorf("proto.Equal returned false:\n m: %v\nm2: %v", m, m2) } }
func (this *fullReader) ReadMsg(msg proto.Message) error { length, err := this.r.Read(this.buf) if err != nil { return err } return proto.Unmarshal(this.buf[:length], msg) }
func TestOldNewOldNew(t *testing.T) { popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) older := NewPopulatedOldA(popr, true) data1, err := proto.Marshal(older) if err != nil { panic(err) } newer := &A{} if err := proto.Unmarshal(data1, newer); err != nil { panic(err) } data2, err := proto.Marshal(newer) if err != nil { panic(err) } bluer := &OldA{} if err := proto.Unmarshal(data2, bluer); err != nil { panic(err) } if err := older.VerboseEqual(bluer); err != nil { t.Fatalf("%#v !VerboseProto %#v, since %v", older, bluer, err) } data3, err := proto.Marshal(bluer) if err != nil { panic(err) } purple := &A{} if err := proto.Unmarshal(data3, purple); err != nil { panic(err) } data4, err := proto.Marshal(purple) if err != nil { panic(err) } magenta := &OldA{} if err := proto.Unmarshal(data4, magenta); err != nil { panic(err) } if err := older.VerboseEqual(magenta); err != nil { t.Fatalf("%#v !VerboseProto %#v, since %v", older, magenta, err) } }
func TestOldNew(t *testing.T) { popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) older := NewPopulatedOldWithGroup(popr, true) data1, err := proto.Marshal(older) if err != nil { panic(err) } newer := &NewNoGroup{} if err := proto.Unmarshal(data1, newer); err != nil { panic(err) } data2, err := proto.Marshal(newer) if err != nil { panic(err) } bluer := &OldWithGroup{} if err := proto.Unmarshal(data2, bluer); err != nil { panic(err) } if err := older.VerboseEqual(bluer); err != nil { t.Fatalf("%#v !VerboseProto %#v, since %v", older, bluer, err) } }
func TestNewOld(t *testing.T) { popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) newer := NewPopulatedA(popr, true) data1, err := proto.Marshal(newer) if err != nil { panic(err) } older := &OldA{} if err := proto.Unmarshal(data1, older); err != nil { panic(err) } data2, err := proto.Marshal(older) if err != nil { panic(err) } bluer := &A{} if err := proto.Unmarshal(data2, bluer); err != nil { panic(err) } if err := newer.VerboseEqual(bluer); err != nil { t.Fatalf("%#v !VerboseProto %#v, since %v", newer, bluer, err) } }
func BenchmarkUnmarshalNinOptStructWhole(b *testing.B) { r := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) for i := 0; i < b.N; i++ { b.StopTimer() p := test.NewPopulatedNinOptStruct(r, false) data, err := proto.Marshal(p) if err != nil { panic(err) } b.StartTimer() pp := &test.NinOptStruct{} proto.Unmarshal(data, pp) } }
func TestAllMapsVerboseEqual(t *testing.T) { popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) p := NewPopulatedAllMaps(popr, false) data, err := github_com_gogo_protobuf_proto.Marshal(p) if err != nil { panic(err) } msg := &AllMaps{} if err := github_com_gogo_protobuf_proto.Unmarshal(data, msg); err != nil { panic(err) } if err := p.VerboseEqual(msg); err != nil { t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err) } }
func TestRepeatedOptional(t *testing.T) { repeated := &FooWithRepeated{Bar: [][]byte{[]byte("a"), []byte("b")}} data, err := proto.Marshal(repeated) if err != nil { panic(err) } optional := &Foo{} err = proto.Unmarshal(data, optional) if err != nil { panic(err) } if !bytes.Equal(optional.Bar, []byte("b")) { t.Fatalf("should return the last entry") } }
func TestBugUuid(t *testing.T) { u := &CustomContainer{CustomStruct: NidOptCustom{Id: Uuid{}}} data, err := proto.Marshal(u) if err != nil { panic(err) } u2 := &CustomContainer{} err = proto.Unmarshal(data, u2) if err != nil { panic(err) } t.Logf("%+v", u2) if u2.CustomStruct.Id != nil { t.Fatalf("should be nil") } }
func Read() *plugin.CodeGeneratorRequest { g := generator.New() data, err := ioutil.ReadAll(os.Stdin) if err != nil { g.Error(err, "reading input") } if err := proto.Unmarshal(data, g.Request); err != nil { g.Error(err, "parsing input proto") } if len(g.Request.FileToGenerate) == 0 { g.Fail("no files to generate") } return g.Request }
func TestMoreDefaultsAProto(t *testing.T) { popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) p := NewPopulatedMoreDefaultsA(popr, false) data, err := github_com_gogo_protobuf_proto.Marshal(p) if err != nil { panic(err) } msg := &MoreDefaultsA{} if err := github_com_gogo_protobuf_proto.Unmarshal(data, msg); err != nil { panic(err) } for i := range data { data[i] = byte(popr.Intn(256)) } if !p.Equal(msg) { t.Fatalf("%#v !Proto %#v", msg, p) } }
func (this *varintReader) ReadMsg(msg proto.Message) error { length64, err := binary.ReadUvarint(this.r) if err != nil { return err } length := int(length64) if length < 0 || length > this.maxSize { return io.ErrShortBuffer } if len(this.buf) < length { this.buf = make([]byte, length) } buf := this.buf[:length] if _, err := io.ReadFull(this.r, buf); err != nil { return err } return proto.Unmarshal(buf, msg) }
func (this *uint32Reader) ReadMsg(msg proto.Message) error { if _, err := io.ReadFull(this.r, this.lenBuf); err != nil { return err } length32 := this.byteOrder.Uint32(this.lenBuf) length := int(length32) if length < 0 || length > this.maxSize { return io.ErrShortBuffer } if length >= len(this.buf) { this.buf = make([]byte, length) } _, err := io.ReadFull(this.r, this.buf[:length]) if err != nil { return err } return proto.Unmarshal(this.buf[:length], msg) }
func TestIssue42Order(t *testing.T) { unordered := NewPopulatedUnorderedFields(math_rand.New(math_rand.NewSource(time.Now().UnixNano())), false) udata, err := proto.Marshal(unordered) if err != nil { t.Fatal(err) } ordered := &OrderedFields{} if err := proto.Unmarshal(udata, ordered); err != nil { t.Fatal(err) } data, err := proto.Marshal(ordered) if err != nil { t.Fatal(err) } if !bytes.Equal(udata, data) { t.Fatalf("expected data to be marshaled in the same order, please sort fields before marshaling") } }
func TestUnsafeIssue21(t *testing.T) { popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) msg1 := NewPopulatedNinRepNativeUnsafe(popr, true) data1, err := proto.Marshal(msg1) if err != nil { panic(err) } packedmsg := &NinRepPackedNativeUnsafe{} err = proto.Unmarshal(data1, packedmsg) if err != nil { panic(err) } if len(packedmsg.XXX_unrecognized) != 0 { t.Fatalf("packed msg unmarshaled unrecognized fields, even though there aren't any") } if err := VerboseEqualUnsafe(msg1, packedmsg); err != nil { t.Fatalf("%v", err) } }
func BenchmarkFloatingPointProtoUnmarshal(b *testing.B) { popr := math_rand.New(math_rand.NewSource(616)) total := 0 datas := make([][]byte, 10000) for i := 0; i < 10000; i++ { data, err := github_com_gogo_protobuf_proto.Marshal(NewPopulatedFloatingPoint(popr, false)) if err != nil { panic(err) } datas[i] = data } msg := &FloatingPoint{} b.ResetTimer() for i := 0; i < b.N; i++ { total += len(datas[i%10000]) if err := github_com_gogo_protobuf_proto.Unmarshal(datas[i%10000], msg); err != nil { panic(err) } } b.SetBytes(int64(total / b.N)) }
func TestAllMapsProto(t *testing.T) { popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) p := NewPopulatedAllMaps(popr, false) data, err := github_com_gogo_protobuf_proto.Marshal(p) if err != nil { panic(err) } msg := &AllMaps{} if err := github_com_gogo_protobuf_proto.Unmarshal(data, msg); err != nil { panic(err) } 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) } }
func parseFile(filename string, includeSourceInfo bool, includeImports bool, paths ...string) (*descriptor.FileDescriptorSet, error) { args := []string{"--proto_path=" + strings.Join(paths, ":")} if includeSourceInfo { args = append(args, "--include_source_info") } if includeImports { args = append(args, "--include_imports") } args = append(args, "--descriptor_set_out=/dev/stdout") args = append(args, filename) cmd := exec.Command("protoc", args...) cmd.Env = []string{} data, err := cmd.CombinedOutput() if err != nil { return nil, &errCmd{data, err} } fileDesc := &descriptor.FileDescriptorSet{} if err := proto.Unmarshal(data, fileDesc); err != nil { return nil, err } return fileDesc, nil }
func TestInt32Int64Compatibility(t *testing.T) { //test nullable int32 and int64 data1, err := testSize(&NinOptNative{ Field3: proto.Int32(-1), }, "nullable", 11) if err != nil { t.Error(err) } //change marshaled data1 to unmarshal into 4th field which is an int64 data1[0] = uint8(uint32(4 /*fieldNumber*/)<<3 | uint32(0 /*wireType*/)) u1 := &NinOptNative{} err = proto.Unmarshal(data1, u1) if err != nil { t.Error(err) } if !u1.Equal(&NinOptNative{ Field4: proto.Int64(-1), }) { t.Error("nullable unmarshaled int32 is not the same int64") } //test non-nullable int32 and int64 data2, err := testSize(&NidOptNative{ Field3: -1, }, "non nullable", 67) if err != nil { t.Error(err) } //change marshaled data2 to unmarshal into 4th field which is an int64 field3 := uint8(uint32(3 /*fieldNumber*/)<<3 | uint32(0 /*wireType*/)) field4 := uint8(uint32(4 /*fieldNumber*/)<<3 | uint32(0 /*wireType*/)) for i, c := range data2 { if c == field4 { data2[i] = field3 } else if c == field3 { data2[i] = field4 } } u2 := &NidOptNative{} err = proto.Unmarshal(data2, u2) if err != nil { t.Error(err) } if !u2.Equal(&NidOptNative{ Field4: -1, }) { t.Error("non nullable unmarshaled int32 is not the same int64") } //test repeated int32 and int64 if _, err := testSize(&NinRepNative{ Field3: []int32{-1}, }, "repeated", 11); err != nil { t.Error(err) } //test packed repeated int32 and int64 m4 := &NinRepPackedNative{ Field3: []int32{-1}, } data4, err := testSize(m4, "packed", 12) if err != nil { t.Error(err) } u4 := &NinRepPackedNative{} err = proto.Unmarshal(data4, u4) if err != nil { t.Error(err) } if err := u4.VerboseEqual(m4); err != nil { t.Fatalf("%#v", u4) } t.Logf("tested all") }
func main() { if len(flag.Args()) == 0 { er("fieldpath needs to be provided") } var desc = &descriptor.FileDescriptorSet{} if len(descFilename) > 0 { data, err := ioutil.ReadFile(descFilename) if err != nil { er(err.Error()) } if err := proto.Unmarshal(data, desc); err != nil { er(fmt.Sprintf("Reading descriptor_set_in filename (%v) : %v", descFilename, err)) } } if len(protoFilename) > 0 { var err error desc, err = parser.ParseFile(protoFilename, strings.Split(protoPath, ":")...) if err != nil { er(fmt.Sprintf("Parsing proto filename (%v) with proto path (%v) : %v", protoFilename, protoPath, err)) } } if desc == nil { er("either descriptor_set_in or proto_filename flag needs to be provided") } us := []*fieldpath.Unmarshaler{} printers := []func(v []byte){} for _, a := range flag.Args() { fieldpaths := strings.Split(a, ".") if len(fieldpaths) < 2 { er("fieldpath flag needs at least a package.message structure") } rootPkg := fieldpaths[0] rootMsg := fieldpaths[1] if len(fieldpaths) >= 3 { fpath := strings.Join(fieldpaths[2:], ".") if expandFlag { var err error fpath, err = fieldpath.Expand(rootPkg, rootMsg, fpath, desc) if err != nil { er(err.Error()) } } field, err := fieldpath.NewGenericPath(rootPkg, rootMsg, desc, fpath) if err != nil { er(err.Error()) } stdout := &ToStdout{} stdout.BytesFunc = func(v []byte) { os.Stdout.Write([]byte(fmt.Sprintf("%v\n", v))) } if field.IsMessage() { stdout.BytesFunc = func(v []byte) { err := fieldpath.ToString(rootPkg, rootMsg, desc, fpath, v, 0, os.Stdout) if err != nil { er(err.Error()) } } } u := field.NewUnmarshaler(stdout) us = append(us, u) } else { printers = append(printers, func(v []byte) { err := fieldpath.ToString(rootPkg, rootMsg, desc, "", v, 0, os.Stdout) if err != nil { er(err.Error()) } }) } } var r reader if inputDelim == "varint" { r = newVarintReader(os.Stdin) } else if inputDelim == "big32" { r = newBig32Reader(os.Stdin) } else { r = newAllReader(os.Stdin) } c := fieldpath.Compile(us...) for { data := r.Read() if data == nil { return } for _, p := range printers { p(data) } err := c.Unmarshal(data) if err != nil { er(err.Error()) } if inputDelim == "none" { return } } }
func (this *codec) Unmarshal(data []byte, v interface{}) error { return proto.Unmarshal(data, v.(proto.Message)) }