From e3c59ea9c64879ae4711fe67cce75b5280823e71 Mon Sep 17 00:00:00 2001 From: Patrick Boyd Date: Mon, 30 Sep 2024 09:57:27 -0500 Subject: [PATCH] Increase test and benchmark coverage --- conn_test.go | 4 + decoder_test.go | 115 +++++++ encoder.go | 7 + encoder_test.go | 654 +++++++++++++++++++++++++++++++------- exec_command_test.go | 6 +- export_test.go | 41 ++- proto_test.go | 49 +++ server_interfaces_test.go | 10 +- 8 files changed, 737 insertions(+), 149 deletions(-) diff --git a/conn_test.go b/conn_test.go index f94dc96..e140ef4 100644 --- a/conn_test.go +++ b/conn_test.go @@ -169,6 +169,7 @@ func TestCloseBeforeSignal(t *testing.T) { reader, pipewriter := io.Pipe() defer pipewriter.Close() defer reader.Close() + wg := sync.WaitGroup{} bus, err := NewConn(rwc{Reader: reader, Writer: io.Discard}) if err != nil { @@ -178,11 +179,13 @@ func TestCloseBeforeSignal(t *testing.T) { ch := make(chan *Signal, 1) bus.Signal(ch) + wg.Add(1) go func() { _, err := pipewriter.Write([]byte("REJECTED name\r\nOK myuuid\r\n")) if err != nil { t.Errorf("error writing to pipe: %v", err) } + wg.Done() }() err = bus.Auth([]Auth{fakeAuth{}}) @@ -207,6 +210,7 @@ func TestCloseBeforeSignal(t *testing.T) { if err != nil { t.Fatal(err) } + wg.Wait() } func TestCloseChannelAfterRemoveSignal(t *testing.T) { diff --git a/decoder_test.go b/decoder_test.go index f81b5d4..1488f23 100644 --- a/decoder_test.go +++ b/decoder_test.go @@ -86,3 +86,118 @@ func TestSigByteSize(t *testing.T) { } } } + +func BenchmarkDecodeArrayEmptyStruct(b *testing.B) { + buf := bytes.NewBuffer(nil) + msg := &Message{ + Type: 0x02, + Flags: 0x00, + Headers: map[HeaderField]Variant{ + 0x06: { + sig: Signature{ + str: "s", + }, + value: ":1.391", + }, + 0x05: { + sig: Signature{ + str: "u", + }, + value: uint32(2), + }, + 0x08: { + sig: Signature{ + str: "g", + }, + value: Signature{ + str: "v", + }, + }, + }, + Body: []interface{}{ + Variant{ + sig: Signature{ + str: "(sa(iiay)ss)", + }, + value: property{ + IconName: "iconname", + Pixmaps: []pixmap{}, + Title: "title", + Description: "description", + }, + }, + }, + serial: 0x00000003, + } + err := msg.EncodeTo(buf, binary.LittleEndian) + if err != nil { + b.Fatal(err) + } + data := buf.Bytes() + for i := 0; i < b.N; i++ { + buf.Reset() + buf.Write(data) + _, err = DecodeMessage(buf) + if err != nil { + b.Fatal(err) + } + } +} + +func BenchmarkDecodePropertyChanged(b *testing.B) { + intVal := int32(1) + buf := bytes.NewBuffer(nil) + msg := &Message{ + Type: TypeSignal, + Flags: 0x00, + Headers: map[HeaderField]Variant{ + FieldSignature: { + sig: Signature{ + str: "g", + }, + value: Signature{ + str: "sa{sv}as", + }, + }, + FieldInterface: { + sig: Signature{ + str: "s", + }, + value: "org.freedesktop.DBus.Properties", + }, + FieldMember: { + sig: Signature{ + str: "s", + }, + value: "PropertiesChanged", + }, + FieldPath: { + sig: Signature{ + str: "o", + }, + value: ObjectPath("/com/github/pboyd/Stress"), + }, + }, + Body: []interface{}{ + "com.github.pboyd.Stress", + map[string]Variant{ + "SomeInt": {sig: Signature{str: "i"}, value: &intVal}, + }, + []string{}, + }, + serial: 0x000029f5, + } + err := msg.EncodeTo(buf, binary.LittleEndian) + if err != nil { + b.Fatal(err) + } + data := buf.Bytes() + for n := 0; n < b.N; n++ { + buf.Reset() + buf.Write(data) + _, err = DecodeMessage(buf) + if err != nil { + b.Fatal(err) + } + } +} diff --git a/encoder.go b/encoder.go index 015b26c..0d7be09 100644 --- a/encoder.go +++ b/encoder.go @@ -35,6 +35,13 @@ func newEncoderAtOffset(out io.Writer, offset int, order binary.ByteOrder, fds [ return enc } +func (enc *encoder) Reset(out io.Writer, order binary.ByteOrder, fds []int) { + enc.out = out + enc.order = order + enc.pos = 0 + enc.fds = fds +} + // Aligns the next output to be on a multiple of n. Panics on write errors. func (enc *encoder) align(n int) { pad := enc.padding(0, n) diff --git a/encoder_test.go b/encoder_test.go index 9c1628a..368bbbb 100644 --- a/encoder_test.go +++ b/encoder_test.go @@ -7,61 +7,52 @@ import ( "testing" ) -func TestEncodeArrayOfMaps(t *testing.T) { - tests := []struct { - name string - vs []interface{} - }{ - { - "aligned at 8 at start of array", - []interface{}{ - "12345", - []map[string]Variant{ - { - "abcdefg": MakeVariant("foo"), - "cdef": MakeVariant(uint32(2)), - }, - }, - }, - }, - { - "not aligned at 8 for start of array", - []interface{}{ - "1234567890", - []map[string]Variant{ - { - "abcdefg": MakeVariant("foo"), - "cdef": MakeVariant(uint32(2)), - }, - }, - }, - }, +func TestEncodeByte(t *testing.T) { + val := byte(10) + buf := new(bytes.Buffer) + fds := make([]int, 0) + order := binary.LittleEndian + enc := newEncoder(buf, binary.LittleEndian, fds) + err := enc.Encode(val) + if err != nil { + t.Fatal(err) } - for _, order := range []binary.ByteOrder{binary.LittleEndian, binary.BigEndian} { - for _, tt := range tests { - buf := new(bytes.Buffer) - fds := make([]int, 0) - enc := newEncoder(buf, order, fds) - if err := enc.Encode(tt.vs...); err != nil { - t.Fatal(err) - } - dec := newDecoder(buf, order, enc.fds) - v, err := dec.Decode(SignatureOf(tt.vs...)) - if err != nil { - t.Errorf("%q: decode (%v) failed: %v", tt.name, order, err) - continue - } - if !reflect.DeepEqual(v, tt.vs) { - t.Errorf("%q: (%v) not equal: got '%v', want '%v'", tt.name, order, v, tt.vs) - continue - } - } + expected := []byte{0xa} + if !bytes.Equal(buf.Bytes(), expected) { + t.Errorf("not equal: got '%#v', want '%#v'", + buf.Bytes(), expected) + } + + dec := newDecoder(buf, order, enc.fds) + v, err := dec.Decode(SignatureOf(val)) + if err != nil { + t.Fatal(err) + } + var out byte + if err := Store(v, &out); err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(out, val) { + t.Errorf("not equal: got '%v', want '%v'", + out, val) } } -func TestEncodeMapStringInterface(t *testing.T) { - val := map[string]interface{}{"foo": "bar"} +func BenchmarkEncodeByte(b *testing.B) { + val := byte(10) + buf := new(bytes.Buffer) + fds := make([]int, 0) + enc := newEncoder(buf, binary.LittleEndian, fds) + for n := 0; n < b.N; n++ { + buf.Reset() + enc.Reset(buf, nativeEndian, fds) + enc.Encode(val) + } +} + +func TestEncodeBool(t *testing.T) { + val := true buf := new(bytes.Buffer) fds := make([]int, 0) order := binary.LittleEndian @@ -71,12 +62,18 @@ func TestEncodeMapStringInterface(t *testing.T) { t.Fatal(err) } + expected := []byte{0x1, 0x0, 0x0, 0x0} + if !bytes.Equal(buf.Bytes(), expected) { + t.Errorf("not equal: got '%#v', want '%#v'", + buf.Bytes(), expected) + } + dec := newDecoder(buf, order, enc.fds) v, err := dec.Decode(SignatureOf(val)) if err != nil { t.Fatal(err) } - out := map[string]interface{}{} + var out bool if err := Store(v, &out); err != nil { t.Fatal(err) } @@ -86,10 +83,20 @@ func TestEncodeMapStringInterface(t *testing.T) { } } -type empty interface{} +func BenchmarkEncodeBool(b *testing.B) { + val := true + buf := new(bytes.Buffer) + fds := make([]int, 0) + enc := newEncoder(buf, binary.LittleEndian, fds) + for n := 0; n < b.N; n++ { + buf.Reset() + enc.Reset(buf, nativeEndian, fds) + enc.Encode(val) + } +} -func TestEncodeMapStringNamedInterface(t *testing.T) { - val := map[string]empty{"foo": "bar"} +func TestEncodeInt(t *testing.T) { + val := 10 buf := new(bytes.Buffer) fds := make([]int, 0) order := binary.LittleEndian @@ -99,12 +106,18 @@ func TestEncodeMapStringNamedInterface(t *testing.T) { t.Fatal(err) } + expected := []byte{0xa, 0x0, 0x0, 0x0} + if !bytes.Equal(buf.Bytes(), expected) { + t.Errorf("not equal: got '%#v', want '%#v'", + buf.Bytes(), expected) + } + dec := newDecoder(buf, order, enc.fds) v, err := dec.Decode(SignatureOf(val)) if err != nil { t.Fatal(err) } - out := map[string]empty{} + var out int if err := Store(v, &out); err != nil { t.Fatal(err) } @@ -114,16 +127,20 @@ func TestEncodeMapStringNamedInterface(t *testing.T) { } } -type fooer interface { - Foo() +func BenchmarkEncodeIntToInt(b *testing.B) { + val := 10 + buf := bytes.NewBuffer(make([]byte, 0, 4)) + fds := make([]int, 0) + enc := newEncoder(buf, binary.LittleEndian, fds) + for n := 0; n < b.N; n++ { + buf.Reset() + enc.Reset(buf, nativeEndian, fds) + enc.Encode(val) + } } -type fooimpl string - -func (fooimpl) Foo() {} - -func TestEncodeMapStringNonEmptyInterface(t *testing.T) { - val := map[string]fooer{"foo": fooimpl("bar")} +func TestEncodeIntToNonCovertible(t *testing.T) { + val := 150 buf := new(bytes.Buffer) fds := make([]int, 0) order := binary.LittleEndian @@ -133,20 +150,27 @@ func TestEncodeMapStringNonEmptyInterface(t *testing.T) { t.Fatal(err) } + expected := []byte{0x96, 0x0, 0x0, 0x0} + if !bytes.Equal(buf.Bytes(), expected) { + t.Errorf("not equal: got '%#v', want '%#v'", + buf.Bytes(), expected) + } + dec := newDecoder(buf, order, enc.fds) v, err := dec.Decode(SignatureOf(val)) if err != nil { t.Fatal(err) } - out := map[string]fooer{} + var out bool err = Store(v, &out) if err == nil { - t.Fatal("Shouldn't be able to convert to non empty interfaces") + t.Logf("%t\n", out) + t.Fatal("Type mismatch should have occurred") } } -func TestEncodeSliceInterface(t *testing.T) { - val := []interface{}{"foo", "bar"} +func TestEncodeUint(t *testing.T) { + val := uint(10) buf := new(bytes.Buffer) fds := make([]int, 0) order := binary.LittleEndian @@ -156,12 +180,18 @@ func TestEncodeSliceInterface(t *testing.T) { t.Fatal(err) } + expected := []byte{0xa, 0x0, 0x0, 0x0} + if !bytes.Equal(buf.Bytes(), expected) { + t.Errorf("not equal: got '%#v', want '%#v'", + buf.Bytes(), expected) + } + dec := newDecoder(buf, order, enc.fds) v, err := dec.Decode(SignatureOf(val)) if err != nil { t.Fatal(err) } - out := []interface{}{} + var out uint if err := Store(v, &out); err != nil { t.Fatal(err) } @@ -171,33 +201,45 @@ func TestEncodeSliceInterface(t *testing.T) { } } -func BenchmarkEncodeSliceInterface(b *testing.B) { - val := []interface{}{"foo", "bar"} - sig := SignatureOf(val) - buf := &bytes.Buffer{} +func BenchmarkEncodeUInt(b *testing.B) { + val := uint(10) + buf := new(bytes.Buffer) fds := make([]int, 0) - - b.ReportAllocs() - b.ResetTimer() + enc := newEncoder(buf, binary.LittleEndian, fds) for n := 0; n < b.N; n++ { buf.Reset() + enc.Reset(buf, nativeEndian, fds) + enc.Encode(val) + } +} - enc := newEncoder(buf, binary.LittleEndian, fds) - err := enc.Encode(val) - if err != nil { - b.Fatal(err) - } +func TestEncodeUintToNonCovertible(t *testing.T) { + val := uint(10) + buf := new(bytes.Buffer) + fds := make([]int, 0) + order := binary.LittleEndian + enc := newEncoder(buf, binary.LittleEndian, fds) + err := enc.Encode(val) + if err != nil { + t.Fatal(err) + } - dec := newDecoder(buf, binary.LittleEndian, enc.fds) - _, err = dec.Decode(sig) - if err != nil { - b.Fatal(err) - } + dec := newDecoder(buf, order, enc.fds) + v, err := dec.Decode(SignatureOf(val)) + if err != nil { + t.Fatal(err) + } + var out bool + err = Store(v, &out) + if err == nil { + t.Fatal("Type mismatch should have occurred") } } -func TestEncodeSliceNamedInterface(t *testing.T) { - val := []empty{"foo", "bar"} +type boolean bool + +func TestEncodeOfAssignableType(t *testing.T) { + val := boolean(true) buf := new(bytes.Buffer) fds := make([]int, 0) order := binary.LittleEndian @@ -207,13 +249,20 @@ func TestEncodeSliceNamedInterface(t *testing.T) { t.Fatal(err) } + expected := []byte{0x1, 0x0, 0x0, 0x0} + if !bytes.Equal(buf.Bytes(), expected) { + t.Errorf("not equal: got '%#v', want '%#v'", + buf.Bytes(), expected) + } + dec := newDecoder(buf, order, enc.fds) v, err := dec.Decode(SignatureOf(val)) if err != nil { t.Fatal(err) } - out := []empty{} - if err := Store(v, &out); err != nil { + var out boolean + err = Store(v, &out) + if err != nil { t.Fatal(err) } if !reflect.DeepEqual(out, val) { @@ -222,19 +271,130 @@ func TestEncodeSliceNamedInterface(t *testing.T) { } } -func TestEncodeNestedInterface(t *testing.T) { - val := map[string]interface{}{ - "foo": []interface{}{ - "1", "2", "3", "5", - map[string]interface{}{ - "bar": "baz", +func BenchmarkEncodeOfAssignableType(b *testing.B) { + val := bool(true) + buf := new(bytes.Buffer) + fds := make([]int, 0) + enc := newEncoder(buf, binary.LittleEndian, fds) + for n := 0; n < b.N; n++ { + buf.Reset() + enc.Reset(buf, nativeEndian, fds) + enc.Encode(val) + } +} + +func TestEncodeArrayOfMaps(t *testing.T) { + tests := []struct { + name string + vs []interface{} + bigEndian [][]byte + littleEndian [][]byte // Three are a few ways that the underlying code may read the map + }{ + { + name: "aligned at 8 at start of array", + vs: []interface{}{ + "12345", + []map[string]Variant{ + { + "abcdefg": MakeVariant("foo"), + "cdef": MakeVariant(uint32(2)), + }, + }, + }, + bigEndian: [][]byte{ + {0x0, 0x0, 0x0, 0x5, 0x31, 0x32, 0x33, 0x34, 0x35, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x28, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x0, 0x1, 0x73, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x66, 0x6f, 0x6f, 0x0, 0x0, 0x0, 0x0, 0x4, 0x63, 0x64, 0x65, 0x66, 0x0, 0x1, 0x75, 0x0, 0x0, 0x0, 0x0, 0x2}, + {0x0, 0x0, 0x0, 0x5, 0x31, 0x32, 0x33, 0x34, 0x35, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x28, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x63, 0x64, 0x65, 0x66, 0x0, 0x1, 0x75, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x7, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x0, 0x1, 0x73, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x66, 0x6f, 0x6f, 0x0}, + }, + littleEndian: [][]byte{ + {0x5, 0x0, 0x0, 0x0, 0x31, 0x32, 0x33, 0x34, 0x35, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x28, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x0, 0x1, 0x73, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x66, 0x6f, 0x6f, 0x0, 0x4, 0x0, 0x0, 0x0, 0x63, 0x64, 0x65, 0x66, 0x0, 0x1, 0x75, 0x0, 0x2, 0x0, 0x0, 0x0}, + {0x5, 0x0, 0x0, 0x0, 0x31, 0x32, 0x33, 0x34, 0x35, 0x0, 0x0, 0x0, 0x30, 0x0, 0x0, 0x0, 0x28, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4, 0x0, 0x0, 0x0, 0x63, 0x64, 0x65, 0x66, 0x0, 0x1, 0x75, 0x0, 0x2, 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x0, 0x1, 0x73, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x66, 0x6f, 0x6f, 0x0}, }, }, - "bar": map[string]interface{}{ - "baz": "quux", - "quux": "quuz", + { + name: "not aligned at 8 for start of array", + vs: []interface{}{ + "1234567890", + []map[string]Variant{ + { + "abcdefg": MakeVariant("foo"), + "cdef": MakeVariant(uint32(2)), + }, + }, + }, + bigEndian: [][]byte{ + {0x0, 0x0, 0x0, 0xa, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2c, 0x0, 0x0, 0x0, 0x28, 0x0, 0x0, 0x0, 0x7, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x0, 0x1, 0x73, 0x0, 0x0, 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x6f, 0x0, 0x4, 0x0, 0x0, 0x0, 0x63, 0x64, 0x65, 0x66, 0x0, 0x1, 0x75, 0x0, 0x2, 0x0, 0x0, 0x0}, + {0xa, 0x0, 0x0, 0x0, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x0, 0x0, 0x2c, 0x0, 0x0, 0x0, 0x28, 0x0, 0x0, 0x0, 0x4, 0x0, 0x0, 0x0, 0x63, 0x64, 0x65, 0x66, 0x0, 0x1, 0x75, 0x0, 0x2, 0x0, 0x0, 0x0, 0x7, 0x0, 0x0, 0x0, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x0, 0x1, 0x73, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x66, 0x6f, 0x6f, 0x0}, + }, + }, + } + for _, order := range []binary.ByteOrder{binary.LittleEndian, binary.BigEndian} { + for _, tt := range tests { + buf := new(bytes.Buffer) + fds := make([]int, 0) + enc := newEncoder(buf, order, fds) + if err := enc.Encode(tt.vs...); err != nil { + t.Fatal(err) + } + data := buf.Bytes() + expected := tt.littleEndian + if order == binary.BigEndian { + expected = tt.bigEndian + } + + found := false + for _, e := range expected { + if bytes.Equal(data, e) { + found = true + break + } + } + + if !found { + t.Errorf("%q: (%v) not equal: got '%#v', want one of '%#v'", tt.name, order, data, expected) + continue + } + + dec := newDecoder(buf, order, enc.fds) + v, err := dec.Decode(SignatureOf(tt.vs...)) + if err != nil { + t.Errorf("%q: decode (%v) failed: %v", tt.name, order, err) + continue + } + if !reflect.DeepEqual(v, tt.vs) { + t.Errorf("%q: (%v) not equal: got '%v', want '%v'", tt.name, order, v, tt.vs) + continue + } + } + } +} + +func BenchmarkEncodeArrayOfMaps(b *testing.B) { + buf := new(bytes.Buffer) + fds := make([]int, 0) + enc := newEncoder(buf, nativeEndian, fds) + vs := []interface{}{ + "12345", + []map[string]Variant{ + { + "abcdefg": MakeVariant("foo"), + "cdef": MakeVariant(uint32(2)), + }, }, } + for n := 0; n < b.N; n++ { + buf.Reset() + enc.Reset(buf, nativeEndian, fds) + enc.Encode(vs...) + } +} + +func TestEncodeMapStringInterface(t *testing.T) { + val := map[string]interface{}{"foo": "bar"} buf := new(bytes.Buffer) fds := make([]int, 0) order := binary.LittleEndian @@ -243,6 +403,11 @@ func TestEncodeNestedInterface(t *testing.T) { if err != nil { t.Fatal(err) } + expected := []byte{0x14, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x66, 0x6f, 0x6f, 0x0, 0x1, 0x73, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x62, 0x61, 0x72, 0x0} + if !bytes.Equal(buf.Bytes(), expected) { + t.Errorf("not equal: got '%#v', want '%#v'", + buf.Bytes(), expected) + } dec := newDecoder(buf, order, enc.fds) v, err := dec.Decode(SignatureOf(val)) @@ -254,13 +419,30 @@ func TestEncodeNestedInterface(t *testing.T) { t.Fatal(err) } if !reflect.DeepEqual(out, val) { - t.Errorf("not equal: got '%#v', want '%#v'", + t.Errorf("not equal: got '%v', want '%v'", out, val) } } -func TestEncodeInt(t *testing.T) { - val := 10 +func BenchmarkEncodeMapStringInterface(b *testing.B) { + val := map[string]interface{}{"foo": "bar"} + buf := &bytes.Buffer{} + fds := make([]int, 0) + + b.ReportAllocs() + b.ResetTimer() + enc := newEncoder(buf, binary.LittleEndian, fds) + for n := 0; n < b.N; n++ { + buf.Reset() + enc.Reset(buf, nativeEndian, fds) + enc.Encode(val) + } +} + +type empty interface{} + +func TestEncodeMapStringNamedInterface(t *testing.T) { + val := map[string]empty{"foo": "bar"} buf := new(bytes.Buffer) fds := make([]int, 0) order := binary.LittleEndian @@ -269,13 +451,18 @@ func TestEncodeInt(t *testing.T) { if err != nil { t.Fatal(err) } + expected := []byte{0x14, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x66, 0x6f, 0x6f, 0x0, 0x1, 0x73, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x62, 0x61, 0x72, 0x0} + if !bytes.Equal(buf.Bytes(), expected) { + t.Errorf("not equal: got '%#v', want '%#v'", + buf.Bytes(), expected) + } dec := newDecoder(buf, order, enc.fds) v, err := dec.Decode(SignatureOf(val)) if err != nil { t.Fatal(err) } - var out int + out := map[string]empty{} if err := Store(v, &out); err != nil { t.Fatal(err) } @@ -285,8 +472,31 @@ func TestEncodeInt(t *testing.T) { } } -func TestEncodeIntToNonCovertible(t *testing.T) { - val := 150 +func BenchmarkEncodeMapStringNamedInterface(b *testing.B) { + val := map[string]empty{"foo": "bar"} + buf := &bytes.Buffer{} + fds := make([]int, 0) + + b.ReportAllocs() + b.ResetTimer() + enc := newEncoder(buf, binary.LittleEndian, fds) + for n := 0; n < b.N; n++ { + buf.Reset() + enc.Reset(buf, nativeEndian, fds) + enc.Encode(val) + } +} + +type fooer interface { + Foo() +} + +type fooimpl string + +func (fooimpl) Foo() {} + +func TestEncodeMapStringNonEmptyInterface(t *testing.T) { + val := map[string]fooer{"foo": fooimpl("bar")} buf := new(bytes.Buffer) fds := make([]int, 0) order := binary.LittleEndian @@ -295,22 +505,41 @@ func TestEncodeIntToNonCovertible(t *testing.T) { if err != nil { t.Fatal(err) } + expected := []byte{0x14, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x66, 0x6f, 0x6f, 0x0, 0x1, 0x73, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x62, 0x61, 0x72, 0x0} + if !bytes.Equal(buf.Bytes(), expected) { + t.Errorf("not equal: got '%#v', want '%#v'", + buf.Bytes(), expected) + } dec := newDecoder(buf, order, enc.fds) v, err := dec.Decode(SignatureOf(val)) if err != nil { t.Fatal(err) } - var out bool + out := map[string]fooer{} err = Store(v, &out) if err == nil { - t.Logf("%t\n", out) - t.Fatal("Type mismatch should have occurred") + t.Fatal("Shouldn't be able to convert to non empty interfaces") } } -func TestEncodeUint(t *testing.T) { - val := uint(10) +func BenchmarkEncodeMapStringNonEmptyInterface(b *testing.B) { + val := map[string]fooer{"foo": fooimpl("bar")} + buf := &bytes.Buffer{} + fds := make([]int, 0) + + b.ReportAllocs() + b.ResetTimer() + enc := newEncoder(buf, binary.LittleEndian, fds) + for n := 0; n < b.N; n++ { + buf.Reset() + enc.Reset(buf, nativeEndian, fds) + enc.Encode(val) + } +} + +func TestEncodeSliceInterface(t *testing.T) { + val := []interface{}{"foo", "bar"} buf := new(bytes.Buffer) fds := make([]int, 0) order := binary.LittleEndian @@ -319,13 +548,18 @@ func TestEncodeUint(t *testing.T) { if err != nil { t.Fatal(err) } + expected := []byte{0x18, 0x0, 0x0, 0x0, 0x1, 0x73, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x66, 0x6f, 0x6f, 0x0, 0x1, 0x73, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x62, 0x61, 0x72, 0x0} + if !bytes.Equal(buf.Bytes(), expected) { + t.Errorf("not equal: got '%#v', want '%#v'", + buf.Bytes(), expected) + } dec := newDecoder(buf, order, enc.fds) v, err := dec.Decode(SignatureOf(val)) if err != nil { t.Fatal(err) } - var out uint + out := []interface{}{} if err := Store(v, &out); err != nil { t.Fatal(err) } @@ -335,8 +569,23 @@ func TestEncodeUint(t *testing.T) { } } -func TestEncodeUintToNonCovertible(t *testing.T) { - val := uint(10) +func BenchmarkEncodeSliceInterface(b *testing.B) { + val := []interface{}{"foo", "bar"} + buf := &bytes.Buffer{} + fds := make([]int, 0) + + b.ReportAllocs() + b.ResetTimer() + enc := newEncoder(buf, binary.LittleEndian, fds) + for n := 0; n < b.N; n++ { + buf.Reset() + enc.Reset(buf, nativeEndian, fds) + enc.Encode(val) + } +} + +func TestEncodeSliceNamedInterface(t *testing.T) { + val := []empty{"foo", "bar"} buf := new(bytes.Buffer) fds := make([]int, 0) order := binary.LittleEndian @@ -345,23 +594,55 @@ func TestEncodeUintToNonCovertible(t *testing.T) { if err != nil { t.Fatal(err) } + expected := []byte{0x18, 0x0, 0x0, 0x0, 0x1, 0x73, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x66, 0x6f, 0x6f, 0x0, 0x1, 0x73, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x62, 0x61, 0x72, 0x0} + if !bytes.Equal(buf.Bytes(), expected) { + t.Errorf("not equal: got '%#v', want '%#v'", + buf.Bytes(), expected) + } dec := newDecoder(buf, order, enc.fds) v, err := dec.Decode(SignatureOf(val)) if err != nil { t.Fatal(err) } - var out bool - err = Store(v, &out) - if err == nil { - t.Fatal("Type mismatch should have occurred") + out := []empty{} + if err := Store(v, &out); err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(out, val) { + t.Errorf("not equal: got '%v', want '%v'", + out, val) } } -type boolean bool +func BenchmarkEncodeSliceNamedInterface(b *testing.B) { + val := []empty{"foo", "bar"} + buf := &bytes.Buffer{} + fds := make([]int, 0) -func TestEncodeOfAssignableType(t *testing.T) { - val := boolean(true) + b.ReportAllocs() + b.ResetTimer() + enc := newEncoder(buf, binary.LittleEndian, fds) + for n := 0; n < b.N; n++ { + buf.Reset() + enc.Reset(buf, nativeEndian, fds) + enc.Encode(val) + } +} + +func TestEncodeNestedInterface(t *testing.T) { + val := map[string]interface{}{ + "foo": []interface{}{ + "1", "2", "3", "5", + map[string]interface{}{ + "bar": "baz", + }, + }, + "bar": map[string]interface{}{ + "baz": "quux", + "quux": "quuz", + }, + } buf := new(bytes.Buffer) fds := make([]int, 0) order := binary.LittleEndian @@ -371,22 +652,65 @@ func TestEncodeOfAssignableType(t *testing.T) { t.Fatal(err) } + expected := [][]byte{ + {0xad, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x66, 0x6f, 0x6f, 0x0, 0x2, 0x61, 0x76, 0x0, 0x54, 0x0, 0x0, 0x0, 0x1, 0x73, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,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x75, 0x7a, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x66, 0x6f, 0x6f, 0x0, 0x2, 0x61, 0x76, 0x0, 0x54, 0x0, 0x0, 0x0, 0x1, 0x73, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x31, 0x0, 0x1, 0x73, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x32, 0x0, 0x1, 0x73, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x33, 0x0, 0x1, 0x73, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x35, 0x0, 0x5, 0x61, 0x7b, 0x73, 0x76, 0x7d, 0x0, 0x0, 0x0, 0x0, 0x14, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x62, 0x61, 0x72, 0x0, 0x1, 0x73, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x62, 0x61, 0x7a, 0x0}, + } + found := false + for _, e := range expected { + if bytes.Equal(buf.Bytes(), e) { + found = true + break + } + } + + if !found { + t.Errorf("not equal: got '%#v', want one of '%#v'", + buf.Bytes(), expected) + } + dec := newDecoder(buf, order, enc.fds) v, err := dec.Decode(SignatureOf(val)) if err != nil { t.Fatal(err) } - var out boolean - err = Store(v, &out) - if err != nil { + out := map[string]interface{}{} + if err := Store(v, &out); err != nil { t.Fatal(err) } if !reflect.DeepEqual(out, val) { - t.Errorf("not equal: got '%v', want '%v'", + t.Errorf("not equal: got '%#v', want '%#v'", out, val) } } +func BenchmarkEncodeNestedInterface(b *testing.B) { + val := map[string]interface{}{ + "foo": []interface{}{ + "1", "2", "3", "5", + map[string]interface{}{ + "bar": "baz", + }, + }, + "bar": map[string]interface{}{ + "baz": "quux", + "quux": "quuz", + }, + } + buf := &bytes.Buffer{} + fds := make([]int, 0) + + b.ReportAllocs() + b.ResetTimer() + enc := newEncoder(buf, binary.LittleEndian, fds) + for n := 0; n < b.N; n++ { + buf.Reset() + enc.Reset(buf, nativeEndian, fds) + enc.Encode(val) + } +} + func TestEncodeVariant(t *testing.T) { var res map[ObjectPath]map[string]map[string]Variant src := map[ObjectPath]map[string]map[string]Variant{ @@ -406,6 +730,23 @@ func TestEncodeVariant(t *testing.T) { t.Fatal(err) } + expected := [][]byte{ + {0x4b, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x8, 0x0, 0x0, 0x0, 0x2f, 0x66, 0x6f, 0x6f, 0x2f, 0x62, 0x61, 0x72, 0x0, 0x0, 0x0, 0x0, 0x33, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x66, 0x6f, 0x6f, 0x0, 0x23, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x62, 0x61, 0x72, 0x0, 0x1, 0x69, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x62, 0x61, 0x7a, 0x0, 0x1, 0x73, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x32, 0x30, 0x0}, + {0x50, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x8, 0x0, 0x0, 0x0, 0x2f, 0x66, 0x6f, 0x6f, 0x2f, 0x62, 0x61, 0x72, 0x0, 0x0, 0x0, 0x0, 0x38, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x66, 0x6f, 0x6f, 0x0, 0x28, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x62, 0x61, 0x7a, 0x0, 0x1, 0x73, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x32, 0x30, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x62, 0x61, 0x72, 0x0, 0x1, 0x69, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0}, + } + found := false + for _, e := range expected { + if bytes.Equal(buf.Bytes(), e) { + found = true + break + } + } + + if !found { + t.Errorf("not equal: got '%#v', want one of '%#v'", + buf.Bytes(), expected) + } + dec := newDecoder(buf, order, enc.fds) v, err := dec.Decode(SignatureOf(src)) if err != nil { @@ -418,6 +759,28 @@ func TestEncodeVariant(t *testing.T) { _ = res[ObjectPath("/foo/bar")]["foo"]["baz"].Value().(string) } +func BenchmarkEncodeVariant(b *testing.B) { + src := map[ObjectPath]map[string]map[string]Variant{ + ObjectPath("/foo/bar"): { + "foo": { + "bar": MakeVariant(10), + "baz": MakeVariant("20"), + }, + }, + } + buf := &bytes.Buffer{} + fds := make([]int, 0) + + b.ReportAllocs() + b.ResetTimer() + enc := newEncoder(buf, binary.LittleEndian, fds) + for n := 0; n < b.N; n++ { + buf.Reset() + enc.Reset(buf, nativeEndian, fds) + enc.Encode(src) + } +} + func TestEncodeVariantToList(t *testing.T) { var res map[string]Variant src := map[string]interface{}{ @@ -431,6 +794,11 @@ func TestEncodeVariantToList(t *testing.T) { if err != nil { t.Fatal(err) } + expected := []byte{0x32, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x66, 0x6f, 0x6f, 0x0, 0x2, 0x61, 0x76, 0x0, 0x22, 0x0, 0x0, 0x0, 0x1, 0x73, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x61, 0x0, 0x1, 0x73, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x62, 0x0, 0x1, 0x73, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x63, 0x0} + if !bytes.Equal(buf.Bytes(), expected) { + t.Errorf("not equal: got '%#v', want '%#v'", + buf.Bytes(), expected) + } dec := newDecoder(buf, order, enc.fds) v, err := dec.Decode(SignatureOf(src)) @@ -444,6 +812,23 @@ func TestEncodeVariantToList(t *testing.T) { _ = res["foo"].Value().([]Variant) } +func BenchmarkEncodeVariantToList(b *testing.B) { + src := map[string]interface{}{ + "foo": []interface{}{"a", "b", "c"}, + } + buf := &bytes.Buffer{} + fds := make([]int, 0) + + b.ReportAllocs() + b.ResetTimer() + enc := newEncoder(buf, binary.LittleEndian, fds) + for n := 0; n < b.N; n++ { + buf.Reset() + enc.Reset(buf, nativeEndian, fds) + enc.Encode(src) + } +} + func TestEncodeVariantToUint64(t *testing.T) { var res map[string]Variant src := map[string]interface{}{ @@ -458,6 +843,12 @@ func TestEncodeVariantToUint64(t *testing.T) { t.Fatal(err) } + expected := []byte{0x18, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x66, 0x6f, 0x6f, 0x0, 0x1, 0x74, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0} + if !bytes.Equal(buf.Bytes(), expected) { + t.Errorf("not equal: got '%#v', want '%#v'", + buf.Bytes(), expected) + } + dec := newDecoder(buf, order, enc.fds) v, err := dec.Decode(SignatureOf(src)) if err != nil { @@ -469,3 +860,20 @@ func TestEncodeVariantToUint64(t *testing.T) { } _ = res["foo"].Value().(uint64) } + +func BenchmarkEncodeVariantToUint64(b *testing.B) { + src := map[string]interface{}{ + "foo": uint64(10), + } + buf := &bytes.Buffer{} + fds := make([]int, 0) + + b.ReportAllocs() + b.ResetTimer() + enc := newEncoder(buf, binary.LittleEndian, fds) + for n := 0; n < b.N; n++ { + buf.Reset() + enc.Reset(buf, nativeEndian, fds) + enc.Encode(src) + } +} diff --git a/exec_command_test.go b/exec_command_test.go index a05036b..11d538e 100755 --- a/exec_command_test.go +++ b/exec_command_test.go @@ -56,9 +56,7 @@ DBUS_SESSION_BUS_WINDOWID=16777217` } if err == nil { t.Error("Excepted error, got none") - } else { - if err.Error() != expErr { - t.Errorf("Expected error to be %q, got %q", expErr, err.Error()) - } + } else if err.Error() != expErr { + t.Errorf("Expected error to be %q, got %q", expErr, err.Error()) } } diff --git a/export_test.go b/export_test.go index 5d04575..d33d584 100644 --- a/export_test.go +++ b/export_test.go @@ -7,6 +7,11 @@ import ( "testing" ) +const ( + barString = "bar" + fooString = "foo" +) + type lowerCaseExport struct{} type fooExport struct { @@ -15,19 +20,19 @@ type fooExport struct { func (export *fooExport) Foo(message Message, param string) (string, *Error) { export.message = message - return "foo", nil + return fooString, nil } type barExport struct{} func (export barExport) Foo(param string) (string, *Error) { - return "bar", nil + return barString, nil } type badExport struct{} func (export badExport) Foo(param string) string { - return "bar" + return barString } type invalidMessageExport struct{} @@ -170,7 +175,7 @@ func TestExport_noerror(t *testing.T) { } if response != "cool" { - t.Errorf(`Response was %s, expected "foo"`, response) + t.Errorf(`Response was %s, expected "cool"`, response) } if export.message.serial == 0 { @@ -201,7 +206,7 @@ func TestExport_message(t *testing.T) { t.Errorf("Unexpected error calling Foo: %s", err) } - if response != "foo" { + if response != fooString { t.Errorf(`Response was %s, expected "foo"`, response) } @@ -218,7 +223,7 @@ func TestExport_invalidPath(t *testing.T) { } defer connection.Close() - err = connection.Export(nil, "foo", "bar") + err = connection.Export(nil, fooString, barString) if err == nil { t.Error("Expected an error due to exporting with an invalid path") } @@ -382,7 +387,7 @@ func TestExportSubtree(t *testing.T) { t.Errorf("Unexpected error calling Foo: %s", err) } - if response != "foo" { + if response != fooString { t.Errorf(`Response was %s, expected "foo"`, response) } @@ -475,7 +480,7 @@ func TestExportSubtree_exportPrecedence(t *testing.T) { t.Errorf("Unexpected error calling Foo: %s", err) } - if response != "bar" { + if response != barString { t.Errorf(`Response was %s, expected "bar"`, response) } @@ -493,7 +498,7 @@ func TestExportSubtree_exportPrecedence(t *testing.T) { } // Now the subtree export should handle the call - if response != "foo" { + if response != fooString { t.Errorf(`Response was %s, expected "foo"`, response) } } @@ -509,7 +514,7 @@ func TestExportSubtreeWithMap(t *testing.T) { name := connection.Names()[0] mapping := make(map[string]string) - mapping["Foo"] = "foo" // Export this method as lower-case + mapping["Foo"] = fooString // Export this method as lower-case err = connection.ExportSubtreeWithMap(&fooExport{}, mapping, "/org/guelfey/DBus/Test", "org.guelfey.DBus.Test") if err != nil { @@ -526,7 +531,7 @@ func TestExportSubtreeWithMap(t *testing.T) { t.Errorf("Unexpected error calling Foo: %s", err) } - if response != "foo" { + if response != fooString { t.Errorf(`Response was %s, expected "foo"`, response) } @@ -553,7 +558,7 @@ func TestExportSubtreeWithMap_bypassAlias(t *testing.T) { name := connection.Names()[0] mapping := make(map[string]string) - mapping["Foo"] = "foo" // Export this method as lower-case + mapping["Foo"] = fooString // Export this method as lower-case err = connection.ExportSubtreeWithMap(&fooExport{}, mapping, "/org/guelfey/DBus/Test", "org.guelfey.DBus.Test") if err != nil { @@ -570,6 +575,7 @@ func TestExportSubtreeWithMap_bypassAlias(t *testing.T) { } } +//nolint:dupl func TestExportMethodTable(t *testing.T) { connection, err := ConnectSessionBus() if err != nil { @@ -597,7 +603,7 @@ func TestExportMethodTable(t *testing.T) { t.Errorf("Unexpected error calling Foo: %s", err) } - if response != "foo" { + if response != fooString { t.Errorf(`Response was %s, expected "foo"`, response) } @@ -610,7 +616,7 @@ func TestExportMethodTable(t *testing.T) { t.Errorf("Unexpected error calling Foo: %s", err) } - if response != "foo" { + if response != fooString { t.Errorf(`Response was %s, expected "foo"`, response) } @@ -630,6 +636,7 @@ func TestExportMethodTable(t *testing.T) { } } +//nolint:dupl func TestExportSubtreeMethodTable(t *testing.T) { connection, err := ConnectSessionBus() if err != nil { @@ -659,7 +666,7 @@ func TestExportSubtreeMethodTable(t *testing.T) { t.Errorf("Unexpected error calling Foo: %s", err) } - if response != "foo" { + if response != fooString { t.Errorf(`Response was %s, expected "foo"`, response) } @@ -672,7 +679,7 @@ func TestExportSubtreeMethodTable(t *testing.T) { t.Errorf("Unexpected error calling Foo: %s", err) } - if response != "foo" { + if response != fooString { t.Errorf(`Response was %s, expected "foo"`, response) } @@ -720,7 +727,7 @@ func TestExportMethodTable_NotFunc(t *testing.T) { t.Errorf("Unexpected error calling Foo: %s", err) } - if response != "foo" { + if response != fooString { t.Errorf(`Response was %s, expected "foo"`, response) } diff --git a/proto_test.go b/proto_test.go index 534a9ee..2d1ef97 100644 --- a/proto_test.go +++ b/proto_test.go @@ -368,6 +368,55 @@ func BenchmarkDecodeMessageBig(b *testing.B) { } } +func TestEncodeSmallMessage(t *testing.T) { + buf := new(bytes.Buffer) + err := smallMessage.EncodeTo(buf, binary.LittleEndian) + if err != nil { + t.Fatal(err) + } + expected := [][]byte{ + {0x6c, 0x1, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x6e, 0x0, 0x0, 0x0, 0x6, 0x1, 0x73, 0x0, 0x14, 0x0, 0x0, 0x0, 0x6f, 0x72, 0x67, 0x2e, 0x66, 0x72, 0x65, 0x65, 0x64, 0x65, 0x73, 0x6b, 0x74, 0x6f, 0x70, 0x2e, 0x44, 0x42, 0x75, 0x73, 0x0, 0x0, 0x0, 0x0, 0x1, 0x1, 0x6f, 0x0, 0x15, 0x0, 0x0, 0x0, 0x2f, 0x6f, 0x72, 0x67, 0x2f, 0x66, 0x72, 0x65, 0x65, 0x64, 0x65, 0x73, 0x6b, 0x74, 0x6f, 0x70, 0x2f, 0x44, 0x42, 0x75, 0x73, 0x0, 0x0, 0x0, 0x2, 0x1, 0x73, 0x0, 0x14, 0x0, 0x0, 0x0, 0x6f, 0x72, 0x67, 0x2e, 0x66, 0x72, 0x65, 0x65, 0x64, 0x65, 0x73, 0x6b, 0x74, 0x6f, 0x70, 0x2e, 0x44, 0x42, 0x75, 0x73, 0x0, 0x0, 0x0, 0x0, 0x3, 0x1, 0x73, 0x0, 0x5, 0x0, 0x0, 0x0, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x0, 0x0, 0x0}, + {0x6c, 0x1, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0,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x1, 0x73, 0x0, 0x5, 0x0, 0x0, 0x0, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x0, 0x0, 0x0, 0x6, 0x1, 0x73, 0x0, 0x14, 0x0, 0x0, 0x0, 0x6f, 0x72, 0x67, 0x2e, 0x66, 0x72, 0x65, 0x65, 0x64, 0x65, 0x73, 0x6b, 0x74, 0x6f, 0x70, 0x2e, 0x44, 0x42, 0x75, 0x73, 0x0, 0x0, 0x0, 0x0}, + {0x6c, 0x1, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x6d, 0x0, 0x0, 0x0, 0x3, 0x1, 0x73, 0x0, 0x5, 0x0, 0x0, 0x0, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x0, 0x0, 0x0, 0x6, 0x1, 0x73, 0x0, 0x14, 0x0, 0x0, 0x0, 0x6f, 0x72, 0x67, 0x2e, 0x66, 0x72, 0x65, 0x65, 0x64, 0x65, 0x73, 0x6b, 0x74, 0x6f, 0x70, 0x2e, 0x44, 0x42, 0x75, 0x73, 0x0, 0x0, 0x0, 0x0, 0x1, 0x1, 0x6f, 0x0, 0x15, 0x0, 0x0, 0x0, 0x2f, 0x6f, 0x72, 0x67, 0x2f, 0x66, 0x72, 0x65, 0x65, 0x64, 0x65, 0x73, 0x6b, 0x74, 0x6f, 0x70, 0x2f, 0x44, 0x42, 0x75, 0x73, 0x0, 0x0, 0x0, 0x2, 0x1, 0x73, 0x0, 0x14, 0x0, 0x0, 0x0, 0x6f, 0x72, 0x67, 0x2e, 0x66, 0x72, 0x65, 0x65, 0x64, 0x65, 0x73, 0x6b, 0x74, 0x6f, 0x70, 0x2e, 0x44, 0x42, 0x75, 0x73, 0x0, 0x0, 0x0, 0x0}, + } + found := false + for _, e := range expected { + if bytes.Equal(buf.Bytes(), e) { + found = true + break + } + } + if !found { + t.Errorf("got %#v, but expected one of %#v", buf.Bytes(), expected) + } +} + +func TestEncodeBigMessage(t *testing.T) { + buf := new(bytes.Buffer) + err := bigMessage.EncodeTo(buf, binary.LittleEndian) + if err != nil { + t.Fatal(err) + } + expected := [][]byte{ + {0x6c, 0x1, 0x0, 0x1, 0xb0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x9b, 0x0, 0x0, 0x0, 0x6, 0x1, 0x73, 0x0, 0x1d, 0x0, 0x0, 0x0, 0x6f, 0x72, 0x67, 0x2e, 0x66, 0x72, 0x65, 0x65, 0x64, 0x65, 0x73, 0x6b, 0x74, 0x6f, 0x70, 0x2e, 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x0, 0x0, 0x0, 0x1, 0x1, 0x6f, 0x0, 0x1e, 0x0, 0x0, 0x0, 0x2f, 0x6f, 0x72, 0x67, 0x2f, 0x66, 0x72, 0x65, 0x65, 0x64, 0x65, 0x73, 0x6b, 0x74, 0x6f, 0x70, 0x2f, 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x0, 0x0, 0x2, 0x1, 0x73, 0x0, 0x1d, 0x0, 0x0, 0x0, 0x6f, 0x72, 0x67, 0x2e, 0x66, 0x72, 0x65, 0x65, 0x64, 0x65, 0x73, 0x6b, 0x74, 0x6f, 0x70, 0x2e, 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x0, 0x0, 0x0, 0x3, 0x1, 0x73, 0x0, 0x6, 0x0, 0x0, 0x0, 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x0, 0x0, 0x8, 0x1, 0x67, 0x0, 0xd, 0x73, 0x75, 0x73, 0x73, 0x73, 0x61, 0x73, 0x61, 0x7b, 0x73, 0x76, 0x7d, 0x69, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x8, 0x0, 0x0, 0x0, 0x61, 0x70, 0x70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, 0x74, 0x69, 0x6f, 0x6e, 0x0, 0x0, 0xff, 0xff, 0xff, 0xff}, + } + found := false + for _, e := range expected { + if bytes.Equal(buf.Bytes(), e) { + found = true + break + } + } + if !found { + t.Errorf("got %#v, but expected one of %#v", buf.Bytes(), expected) + } +} + func BenchmarkEncodeMessageSmall(b *testing.B) { var err error for i := 0; i < b.N; i++ { diff --git a/server_interfaces_test.go b/server_interfaces_test.go index 23d5802..3e867e2 100644 --- a/server_interfaces_test.go +++ b/server_interfaces_test.go @@ -243,7 +243,7 @@ func TestHandlerCall(t *testing.T) { defer conn.Close() obj := conn.Object(tester.Name(), "/com/github/godbus/tester") var out string - in := "foo" + in := fooString err = obj.Call("com.github.godbus.dbus.Tester.Test", 0, in).Store(&out) if err != nil { t.Errorf("Unexpected error: %s", err) @@ -266,7 +266,7 @@ func TestHandlerCallGenericError(t *testing.T) { defer conn.Close() obj := conn.Object(tester.Name(), "/com/github/godbus/tester") var out string - in := "foo" + in := fooString err = obj.Call("com.github.godbus.dbus.Tester.Error", 0, in).Store(&out) if err != nil && err.(Error).Body[0].(string) != "foo" { t.Errorf("Unexpected error: %s", err) @@ -287,7 +287,7 @@ func TestHandlerCallNonExistent(t *testing.T) { defer conn.Close() obj := conn.Object(tester.Name(), "/com/github/godbus/tester/nonexist") var out string - in := "foo" + in := fooString err = obj.Call("com.github.godbus.dbus.Tester.Test", 0, in).Store(&out) if err != nil { if err.Error() != "Object does not implement the interface 'com.github.godbus.dbus.Tester'" { @@ -309,7 +309,7 @@ func TestHandlerInvalidFunc(t *testing.T) { defer conn.Close() obj := conn.Object(tester.Name(), "/com/github/godbus/tester") var out string - in := "foo" + in := fooString err = obj.Call("com.github.godbus.dbus.Tester.Notexist", 0, in).Store(&out) if err == nil { t.Errorf("didn't get expected error") @@ -438,7 +438,7 @@ func TestHandlerSignal(t *testing.T) { } select { case sig := <-tester.sigs: - if sig.Body[0] != "foo" { + if sig.Body[0] != fooString { t.Errorf("Unexpected signal got %s, expected %s", sig.Body[0], "foo") } case <-time.After(time.Second * 10): // overly generous timeout