func TestUnmarshalPartiallyPopulatedOptionalFieldsFails(t *testing.T) {
	// Fill in all fields, then randomly remove one.
	dataOut := &test.NinOptNative{
		Field1:  proto.Float64(0),
		Field2:  proto.Float32(0),
		Field3:  proto.Int32(0),
		Field4:  proto.Int64(0),
		Field5:  proto.Uint32(0),
		Field6:  proto.Uint64(0),
		Field7:  proto.Int32(0),
		Field8:  proto.Int64(0),
		Field9:  proto.Uint32(0),
		Field10: proto.Int32(0),
		Field11: proto.Uint64(0),
		Field12: proto.Int64(0),
		Field13: proto.Bool(false),
		Field14: proto.String("0"),
		Field15: []byte("0"),
	}
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	fieldName := "Field" + strconv.Itoa(r.Intn(15)+1)
	field := reflect.ValueOf(dataOut).Elem().FieldByName(fieldName)
	fieldType := field.Type()
	field.Set(reflect.Zero(fieldType))
	encodedMessage, err := proto.Marshal(dataOut)
	if err != nil {
		t.Fatalf("Unexpected error when marshalling dataOut: %v", err)
	}
	dataIn := NidOptNative{}
	err = proto.Unmarshal(encodedMessage, &dataIn)
	if err.Error() != `proto: required field "`+fieldName+`" not set` {
		t.Fatalf(`err.Error() != "proto: required field "`+fieldName+`" not set"; was "%s" instead`, err.Error())
	}
}
Example #2
0
func TestProto3SetDefaults(t *testing.T) {
	in := &pb.Message{
		Terrain: map[string]*pb.Nested{
			"meadow": new(pb.Nested),
		},
		Proto2Field: new(tpb.SubDefaults),
		Proto2Value: map[string]*tpb.SubDefaults{
			"badlands": new(tpb.SubDefaults),
		},
	}

	got := proto.Clone(in).(*pb.Message)
	proto.SetDefaults(got)

	// There are no defaults in proto3.  Everything should be the zero value, but
	// we need to remember to set defaults for nested proto2 messages.
	want := &pb.Message{
		Terrain: map[string]*pb.Nested{
			"meadow": new(pb.Nested),
		},
		Proto2Field: &tpb.SubDefaults{N: proto.Int64(7)},
		Proto2Value: map[string]*tpb.SubDefaults{
			"badlands": {N: proto.Int64(7)},
		},
	}

	if !proto.Equal(got, want) {
		t.Errorf("with in = %v\nproto.SetDefaults(in) =>\ngot %v\nwant %v", in, got, want)
	}
}
//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")
	}
}
Example #4
0
func newTestMessage() *pb.MyMessage {
	msg := &pb.MyMessage{
		Count: proto.Int32(42),
		Name:  proto.String("Dave"),
		Quote: proto.String(`"I didn't want to go."`),
		Pet:   []string{"bunny", "kitty", "horsey"},
		Inner: &pb.InnerMessage{
			Host:      proto.String("footrest.syd"),
			Port:      proto.Int32(7001),
			Connected: proto.Bool(true),
		},
		Others: []*pb.OtherMessage{
			{
				Key:   proto.Int64(0xdeadbeef),
				Value: []byte{1, 65, 7, 12},
			},
			{
				Weight: proto.Float32(6.022),
				Inner: &pb.InnerMessage{
					Host: proto.String("lesha.mtv"),
					Port: proto.Int32(8002),
				},
			},
		},
		Bikeshed: pb.MyMessage_BLUE.Enum(),
		Somegroup: &pb.MyMessage_SomeGroup{
			GroupField: proto.Int32(8),
		},
		// One normally wouldn't do this.
		// This is an undeclared tag 13, as a varint (wire type 0) with value 4.
		XXX_unrecognized: []byte{13<<3 | 0, 4},
	}
	ext := &pb.Ext{
		Data: proto.String("Big gobs for big rats"),
	}
	if err := proto.SetExtension(msg, pb.E_Ext_More, ext); err != nil {
		panic(err)
	}
	greetings := []string{"adg", "easy", "cow"}
	if err := proto.SetExtension(msg, pb.E_Greeting, greetings); err != nil {
		panic(err)
	}

	// Add an unknown extension. We marshal a pb.Ext, and fake the ID.
	b, err := proto.Marshal(&pb.Ext{Data: proto.String("3G skiing")})
	if err != nil {
		panic(err)
	}
	b = append(proto.EncodeVarint(201<<3|proto.WireBytes), b...)
	proto.SetRawExtension(msg, 201, b)

	// Extensions can be plain fields, too, so let's test that.
	b = append(proto.EncodeVarint(202<<3|proto.WireVarint), 19)
	proto.SetRawExtension(msg, 202, b)

	return msg
}
func TestUnmarshalMerge(t *testing.T) {
	popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano()))
	p := NewPopulatedBig(popr, true)
	if p.GetSub() == nil {
		p.Sub = &Sub{SubNumber: proto.Int64(12345)}
	}
	data, err := proto.Marshal(p)
	if err != nil {
		t.Fatal(err)
	}
	s := &Sub{}
	b := &Big{
		Sub: s,
	}
	err = proto.UnmarshalMerge(data, b)
	if err != nil {
		t.Fatal(err)
	}
	if s.GetSubNumber() != p.GetSub().GetSubNumber() {
		t.Fatalf("should have unmarshaled subnumber into sub")
	}
}
Example #6
0
	pb "limbo.services/protobuf/jsonpb/jsonpb_test_proto"
	"limbo.services/protobuf/proto"
	proto3pb "limbo.services/protobuf/proto/proto3_proto"
)

var (
	marshaler = Marshaler{}

	marshalerAllOptions = Marshaler{
		Indent: "  ",
	}

	simpleObject = &pb.Simple{
		OInt32:     proto.Int32(-32),
		OInt64:     proto.Int64(-6400000000),
		OUint32:    proto.Uint32(32),
		OUint64:    proto.Uint64(6400000000),
		OSint32:    proto.Int32(-13),
		OSint64:    proto.Int64(-2600000000),
		OFloat:     proto.Float32(3.14),
		ODouble:    proto.Float64(6.02214179e23),
		OBool:      proto.Bool(true),
		OString:    proto.String("hello \"there\""),
		OBytes:     []byte("beep boop"),
		OCastBytes: pb.Bytes("wow"),
	}

	simpleObjectJSON = `{` +
		`"o_bool":true,` +
		`"o_int32":-32,` +
Example #7
0
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{}
	if err = proto.Unmarshal(data1, u1); 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{}
	if err = proto.Unmarshal(data2, u2); err != nil {
		t.Error(err)
	}
	if !u2.Equal(&NidOptNative{
		Field4: -1,
	}) {
		t.Error("non nullable unmarshaled int32 is not the same int64")
	}

	//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{}
	if err := proto.Unmarshal(data4, u4); err != nil {
		t.Error(err)
	}
	if err := u4.VerboseEqual(m4); err != nil {
		t.Fatalf("%#v", u4)
	}

	//test repeated int32 and int64

	if _, err := testSize(&NinRepNative{
		Field3: []int32{-1},
	}, "repeated", 11); err != nil {
		t.Error(err)
	}

	t.Logf("tested all")
}
Example #8
0
			Pet: []string{"horsey"},
			Others: []*pb.OtherMessage{
				{
					Value: []byte("some bytes"),
				},
			},
		},
		dst: &pb.MyMessage{
			Inner: &pb.InnerMessage{
				Host: proto.String("niles"),
				Port: proto.Int32(9099),
			},
			Pet: []string{"bunny", "kitty"},
			Others: []*pb.OtherMessage{
				{
					Key: proto.Int64(31415926535),
				},
				{
					// Explicitly test a src=nil field
					Inner: nil,
				},
			},
		},
		want: &pb.MyMessage{
			Inner: &pb.InnerMessage{
				Host:      proto.String("hey"),
				Connected: proto.Bool(true),
				Port:      proto.Int32(9099),
			},
			Pet: []string{"bunny", "kitty", "horsey"},
			Others: []*pb.OtherMessage{