Esempio n. 1
0
func (c *serverCodec) WriteResponse(r *rpc.Response, x interface{}) error {
	var pb proto.Message
	if x != nil {
		var ok bool
		if pb, ok = x.(proto.Message); !ok {
			if _, ok = x.(struct{}); !ok {
				c.mutex.Lock()
				delete(c.pending, r.Seq)
				c.mutex.Unlock()
				return fmt.Errorf("ServerCodec.WriteResponse: %T does not implement proto.Message", x)
			}
		}
	}

	c.mutex.Lock()
	id, ok := c.pending[r.Seq]
	if !ok {
		c.mutex.Unlock()
		return errors.New("ServerCodec.WriteResponse: invalid sequence number in response")
	}
	delete(c.pending, r.Seq)
	c.mutex.Unlock()

	var header wire.Header
	header.Id = proto.Uint64(id)
	if r.Error != "" {
		header.Error = proto.String(r.Error)
	}

	// Write the Header and Result
	if err := writeProto(c.w, &header); err != nil {
		return err
	}
	return writeProto(c.w, pb)
}
Esempio n. 2
0
func TestStringEscaping(t *testing.T) {
	testCases := []struct {
		in  *pb.Strings
		out string
	}{
		{
			// Test data from C++ test (TextFormatTest.StringEscape).
			// Single divergence: we don't escape apostrophes.
			&pb.Strings{StringField: proto.String("\"A string with ' characters \n and \r newlines and \t tabs and \001 slashes \\ and  multiple   spaces")},
			"string_field: \"\\\"A string with ' characters \\n and \\r newlines and \\t tabs and \\001 slashes \\\\ and  multiple   spaces\"\n",
		},
		{
			// Test data from the same C++ test.
			&pb.Strings{StringField: proto.String("\350\260\267\346\255\214")},
			"string_field: \"\\350\\260\\267\\346\\255\\214\"\n",
		},
		{
			// Some UTF-8.
			&pb.Strings{StringField: proto.String("\x00\x01\xff\x81")},
			`string_field: "\000\001\377\201"` + "\n",
		},
	}

	for i, tc := range testCases {
		var buf bytes.Buffer
		if err := proto.MarshalText(&buf, tc.in); err != nil {
			t.Errorf("proto.MarsalText: %v", err)
			continue
		}
		s := buf.String()
		if s != tc.out {
			t.Errorf("#%d: Got:\n%s\nExpected:\n%s\n", i, s, tc.out)
			continue
		}

		// Check round-trip.
		pb := new(pb.Strings)
		if err := proto.UnmarshalText(s, pb); err != nil {
			t.Errorf("#%d: UnmarshalText: %v", i, err)
			continue
		}
		if !proto.Equal(pb, tc.in) {
			t.Errorf("#%d: Round-trip failed:\nstart: %v\n  end: %v", i, tc.in, pb)
		}
	}
}
Esempio n. 3
0
func init() {
	ext := &pb.Ext{
		Data: proto.String("extension"),
	}
	if err := proto.SetExtension(cloneTestMessage, pb.E_Ext_More, ext); err != nil {
		panic("SetExtension: " + err.Error())
	}
}
Esempio n. 4
0
func testEchoStub(t *testing.T, stub EchoService) {
	var args EchoRequest
	var reply EchoResponse
	var err error

	// EchoService.Echo
	args.Msg = proto.String("Hello, Protobuf-RPC")
	if err = stub.Echo(&args, &reply); err != nil {
		t.Fatalf(`stub.Echo: %v`, err)
	}
	if reply.GetMsg() != args.GetMsg() {
		t.Fatalf(`stub.Echo: expected = "%s", got = "%s"`, args.GetMsg(), reply.GetMsg())
	}
}
Esempio n. 5
0
func testEchoClient(t *testing.T, client *rpc.Client) {
	var args EchoRequest
	var reply EchoResponse
	var err error

	// EchoService.Echo
	args.Msg = proto.String("Hello, Protobuf-RPC")
	if err = client.Call("EchoService.Echo", &args, &reply); err != nil {
		t.Fatalf(`echo.Echo: %v`, err)
	}
	if reply.GetMsg() != args.GetMsg() {
		t.Fatalf(`echo.Echo: expected = "%s", got = "%s"`, args.GetMsg(), reply.GetMsg())
	}
}
Esempio n. 6
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
}
Esempio n. 7
0
func TestEcho(t *testing.T) {
	onceEcho.Do(func() {
		go ListenAndServeEchoService("tcp", ":2015", new(Echo))
	})
	client, stub, err := DialEchoService("tcp", "127.0.0.1:2015")
	if err != nil {
		t.Fatalf(`DialEchoService("tcp", "127.0.0.1:2015"): %v`, err)
	}
	defer client.Close()

	var args EchoRequest
	var reply EchoResponse

	// EchoService.Echo
	args.Msg = proto.String("Hello, Protobuf-RPC")
	if err = stub.Echo(&args, &reply); err != nil {
		t.Fatalf(`stub.Echo: %v`, err)
	}
	if reply.GetMsg() != args.GetMsg() {
		t.Fatalf(`stub.Echo: expected = "%s", got = "%s"`, args.GetMsg(), reply.GetMsg())
	}
}
Esempio n. 8
0
func (c *clientCodec) WriteRequest(r *rpc.Request, param interface{}) error {
	var pb proto.Message
	if param != nil {
		var ok bool
		if pb, ok = param.(proto.Message); !ok {
			return fmt.Errorf("ClientCodec.WriteRequest: %T does not implement proto.Message", param)
		}
	}

	c.mutex.Lock()
	c.pending[r.Seq] = r.ServiceMethod
	c.mutex.Unlock()

	var header wire.Header
	header.Method = proto.String(r.ServiceMethod)
	header.Id = proto.Uint64(r.Seq)

	// Write the Header and Param
	if err := writeProto(c.w, &header); err != nil {
		return err
	}
	return writeProto(c.w, pb)
}
Esempio n. 9
0
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package proto_test

import (
	"testing"

	"encoding/protobuf/proto"

	pb "./testdata"
)

var cloneTestMessage = &pb.MyMessage{
	Count: proto.Int32(42),
	Name:  proto.String("Dave"),
	Pet:   []string{"bunny", "kitty", "horsey"},
	Inner: &pb.InnerMessage{
		Host:      proto.String("niles"),
		Port:      proto.Int32(9099),
		Connected: proto.Bool(true),
	},
	Others: []*pb.OtherMessage{
		{
			Value: []byte("some bytes"),
		},
	},
	Somegroup: &pb.MyMessage_SomeGroup{
		GroupField: proto.Int32(6),
	},
	RepBytes: [][]byte{[]byte("sham"), []byte("wow")},
Esempio n. 10
0
func main() {
	var client *rpc.Client
	var err error

	client, err = protorpc.Dial("tcp", "127.0.0.1:1234")
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	arithStub := service.NewArithServiceStub(client)
	echoStub := service.NewEchoServiceStub(client)

	var arithArgs service.ArithRequest
	var arithReply service.ArithResponse
	var echoArgs service.EchoRequest
	var echoReply service.EchoResponse

	// Add
	arithArgs.A = proto.Int32(1)
	arithArgs.B = proto.Int32(2)
	if err = arithStub.Add(&arithArgs, &arithReply); err != nil {
		log.Fatalf(`arith.Add: %v`, err)
	}
	if arithReply.GetC() != 3 {
		log.Fatalf(`arith.Add: expected = %d, got = %d`, 3, arithReply.GetC())
	}

	// Mul
	arithArgs.A = proto.Int32(2)
	arithArgs.B = proto.Int32(3)
	if err = arithStub.Mul(&arithArgs, &arithReply); err != nil {
		log.Fatalf(`arith.Mul: %v`, err)
	}
	if arithReply.GetC() != 6 {
		log.Fatalf(`arith.Mul: expected = %d, got = %d`, 6, arithReply.GetC())
	}

	// Div
	arithArgs.A = proto.Int32(13)
	arithArgs.B = proto.Int32(5)
	if err = arithStub.Div(&arithArgs, &arithReply); err != nil {
		log.Fatalf(`arith.Div: %v`, err)
	}
	if arithReply.GetC() != 2 {
		log.Fatalf(`arith.Div: expected = %d, got = %d`, 2, arithReply.GetC())
	}

	// Div zero
	arithArgs.A = proto.Int32(1)
	arithArgs.B = proto.Int32(0)
	if err = arithStub.Div(&arithArgs, &arithReply); err.Error() != "divide by zero" {
		log.Fatalf(`arith.Div: expected = "%s", got = "%s"`, "divide by zero", err.Error())
	}

	// Error
	arithArgs.A = proto.Int32(1)
	arithArgs.B = proto.Int32(2)
	if err = arithStub.Error(&arithArgs, &arithReply); err.Error() != "ArithError" {
		log.Fatalf(`arith.Error: expected = "%s", got = "%s"`, "ArithError", err.Error())
	}

	// EchoService.Echo
	echoArgs.Msg = proto.String("Hello, Protobuf-RPC")
	if err = echoStub.Echo(&echoArgs, &echoReply); err != nil {
		log.Fatalf(`echoStub.Echo: %v`, err)
	}
	if echoArgs.GetMsg() != echoReply.GetMsg() {
		log.Fatalf(`echoStub.Echo: expected = "%s", got = "%s"`, echoArgs.GetMsg(), echoReply.GetMsg())
	}

	fmt.Printf("Done\n")
}