Example #1
0
func TestJoinRawBytesAsStruct(t *testing.T) {
	expectedStruct := TestStructA{
		1,
		2,
		"3",
	}
	expectedRaw := vom.RawBytesOf(expectedStruct)
	fieldsRaw := []*vom.RawBytes{
		vom.RawBytesOf(int8(1)),
		vom.RawBytesOf(uint64(2)),
		vom.RawBytesOf("3"),
	}

	var out *vom.RawBytes
	target, err := vdl.ReflectTarget(reflect.ValueOf(&out))
	if err != nil {
		t.Fatalf("error in ReflectTarget: %v", err)
	}
	if err := util.JoinRawBytesAsStruct(target, expectedRaw.Type, fieldsRaw); err != nil {
		t.Fatalf("error joining raw bytes: %v", err)
	}

	if got, want := out, expectedRaw; !reflect.DeepEqual(got, want) {
		t.Errorf("got %v, want %v", got, want)
	}
}
Example #2
0
func encodeMessageFromVom(header bindings.MessageHeader, argptrs []interface{}, t *vdl.Type) (*bindings.Message, error) {
	// Convert argptrs into their true form: []*vom.RawBytes
	inargs := make([]*vom.RawBytes, len(argptrs))
	for i := range argptrs {
		inargs[i] = *argptrs[i].(**vom.RawBytes)
	}

	encoder := bindings.NewEncoder()
	if err := header.Encode(encoder); err != nil {
		return nil, err
	}
	if bytes, handles, err := encoder.Data(); err != nil {
		return nil, err
	} else {
		target := transcoder.ToMojomTarget()
		if err := util.JoinRawBytesAsStruct(target, t, inargs); err != nil {
			return nil, err
		}
		moreBytes := target.Bytes()
		// Append the encoded "payload" to the end of the slice.
		bytes = append(bytes, moreBytes...)

		return &bindings.Message{
			Header:  header,
			Bytes:   bytes,
			Handles: handles,
			Payload: moreBytes,
		}, nil
	}
}
Example #3
0
func (s *messageReceiver) call(name, method string, value []byte, inParamsType mojom_types.MojomStruct, outParamsType *mojom_types.MojomStruct) ([]byte, error) {
	s.ctx.Infof("server: %s.%s: %#v", name, method, inParamsType)
	inVType, err := transcoder.MojomStructToVDLType(inParamsType, s.header.desc)
	if err != nil {
		return nil, err
	}

	// Decode the vom.RawBytes from the mojom bytes and mojom type.
	target := util.StructSplitTarget()
	if err := transcoder.FromMojo(target, value, inVType); err != nil {
		return nil, fmt.Errorf("transcoder.FromMojo failed: %v", err)
	}

	// inVdlValue is a struct, but we need to send []interface.
	inargs := target.Fields()
	inargsIfc := make([]interface{}, len(inargs))
	for i := range inargs {
		inargsIfc[i] = inargs[i]
	}

	// We know that the v23serverproxy will give us back a bunch of
	// data in []interface{}. so we'll want to decode them into *vom.RawBytes.
	s.ctx.Infof("%s %v", method, outParamsType)
	var numParams int
	if outParamsType != nil {
		numParams = len(outParamsType.Fields)
	}
	outargs := make([]*vom.RawBytes, numParams)
	outptrs := make([]interface{}, len(outargs))
	for i := range outargs {
		outptrs[i] = &outargs[i]
	}

	// Now, run the call without any authorization.
	if err := v23.GetClient(s.ctx).Call(s.ctx, name, method, inargsIfc, outptrs, options.ServerAuthorizer{security.AllowEveryone()}); err != nil {
		return nil, err
	}

	if outParamsType == nil {
		return nil, nil
	}

	outVType, err := transcoder.MojomStructToVDLType(*outParamsType, s.header.desc)
	if err != nil {
		return nil, err
	}

	toMojoTarget := transcoder.ToMojomTarget()
	if err := util.JoinRawBytesAsStruct(toMojoTarget, outVType, outargs); err != nil {
		return nil, err
	}
	return toMojoTarget.Bytes(), nil
}