Пример #1
0
func TestAMarshalTo(t *testing.T) {
	popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano()))
	p := NewPopulatedA(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 := &A{}
	if err := github_com_dropbox_goprotoc_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)
	}
}
Пример #2
0
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)
}
Пример #3
0
func TestOldNewOldNew(t *testing.T) {
	popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano()))
	older := NewPopulatedOldA(popr, true)
	data1, err := dropbox_gogoprotobuf_proto.Marshal(older)
	if err != nil {
		panic(err)
	}
	newer := &A{}
	if err := dropbox_gogoprotobuf_proto.Unmarshal(data1, newer); err != nil {
		panic(err)
	}
	data2, err := dropbox_gogoprotobuf_proto.Marshal(newer)
	if err != nil {
		panic(err)
	}
	bluer := &OldA{}
	if err := dropbox_gogoprotobuf_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 := dropbox_gogoprotobuf_proto.Marshal(bluer)
	if err != nil {
		panic(err)
	}
	purple := &A{}
	if err := dropbox_gogoprotobuf_proto.Unmarshal(data3, purple); err != nil {
		panic(err)
	}
	data4, err := dropbox_gogoprotobuf_proto.Marshal(purple)
	if err != nil {
		panic(err)
	}
	magenta := &OldA{}
	if err := dropbox_gogoprotobuf_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)
	}
}
Пример #4
0
func TestNewOld(t *testing.T) {
	popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano()))
	newer := NewPopulatedA(popr, true)
	data1, err := dropbox_gogoprotobuf_proto.Marshal(newer)
	if err != nil {
		panic(err)
	}
	older := &OldA{}
	if err := dropbox_gogoprotobuf_proto.Unmarshal(data1, older); err != nil {
		panic(err)
	}
	data2, err := dropbox_gogoprotobuf_proto.Marshal(older)
	if err != nil {
		panic(err)
	}
	bluer := &A{}
	if err := dropbox_gogoprotobuf_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)
	}
}
Пример #5
0
func TestGroups2_GVerboseEqual(t *testing5.T) {
	popr := math_rand5.New(math_rand5.NewSource(time5.Now().UnixNano()))
	p := NewPopulatedGroups2_G(popr, false)
	data, err := dropbox_gogoprotobuf_proto2.Marshal(p)
	if err != nil {
		panic(err)
	}
	msg := &Groups2_G{}
	if err := dropbox_gogoprotobuf_proto2.Unmarshal(data, msg); err != nil {
		panic(err)
	}
	if err := p.VerboseEqual(msg); err != nil {
		t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)
	}
}
Пример #6
0
func TestEVerboseEqual(t *testing4.T) {
	popr := math_rand4.New(math_rand4.NewSource(time4.Now().UnixNano()))
	p := NewPopulatedE(popr, false)
	data, err := github_com_dropbox_goprotoc_proto1.Marshal(p)
	if err != nil {
		panic(err)
	}
	msg := &E{}
	if err := github_com_dropbox_goprotoc_proto1.Unmarshal(data, msg); err != nil {
		panic(err)
	}
	if err := p.VerboseEqual(msg); err != nil {
		t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)
	}
}
Пример #7
0
func TestOldWithGroup_Group2VerboseEqual(t *testing6.T) {
	popr := math_rand6.New(math_rand6.NewSource(time6.Now().UnixNano()))
	p := NewPopulatedOldWithGroup_Group2(popr, false)
	data, err := dropbox_gogoprotobuf_proto3.Marshal(p)
	if err != nil {
		panic(err)
	}
	msg := &OldWithGroup_Group2{}
	if err := dropbox_gogoprotobuf_proto3.Unmarshal(data, msg); err != nil {
		panic(err)
	}
	if err := p.VerboseEqual(msg); err != nil {
		t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)
	}
}
Пример #8
0
func TestNinRepEnumVerboseEqual(t *testing3.T) {
	popr := math_rand3.New(math_rand3.NewSource(time3.Now().UnixNano()))
	p := NewPopulatedNinRepEnum(popr, false)
	data, err := dropbox_gogoprotobuf_proto2.Marshal(p)
	if err != nil {
		panic(err)
	}
	msg := &NinRepEnum{}
	if err := dropbox_gogoprotobuf_proto2.Unmarshal(data, msg); err != nil {
		panic(err)
	}
	if err := p.VerboseEqual(msg); err != nil {
		t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err)
	}
}
Пример #9
0
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)
}
Пример #10
0
func TestFooProto(t *testing.T) {
	popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano()))
	p := NewPopulatedFoo(popr, false)
	data, err := dropbox_gogoprotobuf_proto.Marshal(p)
	if err != nil {
		panic(err)
	}
	msg := &Foo{}
	if err := dropbox_gogoprotobuf_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)
	}
}
Пример #11
0
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)
	}
}
Пример #12
0
func BenchmarkEProtoUnmarshal(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_dropbox_goprotoc_proto.Marshal(NewPopulatedE(popr, false))
		if err != nil {
			panic(err)
		}
		datas[i] = data
	}
	msg := &E{}
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		total += len(datas[i%10000])
		if err := github_com_dropbox_goprotoc_proto.Unmarshal(datas[i%10000], msg); err != nil {
			panic(err)
		}
	}
	b.SetBytes(int64(total / b.N))
}
Пример #13
0
func TestOldWithGroup_Group2Proto(t *testing.T) {
	popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano()))
	p := NewPopulatedOldWithGroup_Group2(popr, false)
	data, err := dropbox_gogoprotobuf_proto.Marshal(p)
	if err != nil {
		panic(err)
	}
	msg := &OldWithGroup_Group2{}
	if err := dropbox_gogoprotobuf_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)
	}
}
Пример #14
0
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...)
	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
}
Пример #15
0
func (this *varintReader) ReadMsg(msg proto.Message) error {
	firstLen, err := this.r.Read(this.lenBuf)
	if err != nil {
		return err
	}
	length64, lenLen := binary.Uvarint(this.lenBuf)
	if lenLen <= 0 {
		if lenLen == 0 {
			return errSmallBuffer
		}
		return errLargeValue
	}
	msgLen := int(length64)
	if len(this.buf) < msgLen {
		this.buf = make([]byte, msgLen)
	}
	prefixN := copy(this.buf, this.lenBuf[lenLen:firstLen])
	if _, err := io.ReadFull(this.r, this.buf[prefixN:msgLen]); err != nil {
		return err
	}
	return proto.Unmarshal(this.buf[:msgLen], msg)
}
Пример #16
0
func main() {
	// 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()

	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")
	}

	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()

	if err := proto.Unmarshal(data, gtest.Request); err != nil {
		gtest.Error(err, "parsing input 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/dropbox/goprotoc/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])
		}
	}

	// Send back the results.
	data, err = proto.Marshal(g.Response)
	if err != nil {
		g.Error(err, "failed to marshal output proto")
	}
	_, err = os.Stdout.Write(data)
	if err != nil {
		g.Error(err, "failed to write output proto")
	}
}