File: message_test.go

package info (click to toggle)
golang-go-dbus 1~bzr20150122-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 328 kB
  • sloc: makefile: 13
file content (143 lines) | stat: -rw-r--r-- 4,257 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package dbus

import (
	"bytes"
	"io"
	. "gopkg.in/check.v1"
)

var testMessage = []byte{
	'l',        // Byte order
	1,          // Message type
	0,          // Flags
	1,          // Protocol
	8, 0, 0, 0, // Body length
	1, 0, 0, 0, // Serial
	127, 0, 0, 0, // Header fields array length
	1, 1, 'o', 0, // Path, type OBJECT_PATH
	21, 0, 0, 0, '/', 'o', 'r', 'g', '/', 'f', 'r', 'e', 'e', 'd', 'e', 's', 'k', 't', 'o', 'p', '/', 'D', 'B', 'u', 's', 0,
	0, 0,
	2, 1, 's', 0, // Interface, type STRING
	20, 0, 0, 0, 'o', 'r', 'g', '.', 'f', 'r', 'e', 'e', 'd', 'e', 's', 'k', 't', 'o', 'p', '.', 'D', 'B', 'u', 's', 0,
	0, 0, 0,
	3, 1, 's', 0, // Member, type STRING
	12, 0, 0, 0, 'N', 'a', 'm', 'e', 'H', 'a', 's', 'O', 'w', 'n', 'e', 'r', 0,
	0, 0, 0,
	6, 1, 's', 0, // Destination, type STRING
	20, 0, 0, 0, 'o', 'r', 'g', '.', 'f', 'r', 'e', 'e', 'd', 'e', 's', 'k', 't', 'o', 'p', '.', 'D', 'B', 'u', 's', 0,
	0, 0, 0,
	8, 1, 'g', 0, // Signature, type SIGNATURE
	1, 's', 0,
	0,
	// Message body
	3, 0, 0, 0,
	'x', 'y', 'z', 0}

func (s *S) TestReadMessage(c *C) {
	r := bytes.NewReader(testMessage)

	msg, err := readMessage(r)
	if nil != err {
		c.Error(err)
	}
	c.Check(msg.Type, Equals, TypeMethodCall)
	c.Check(msg.Path, Equals, ObjectPath("/org/freedesktop/DBus"))
	c.Check(msg.Dest, Equals, "org.freedesktop.DBus")
	c.Check(msg.Interface, Equals, "org.freedesktop.DBus")
	c.Check(msg.Member, Equals, "NameHasOwner")
	c.Check(msg.sig, Equals, Signature("s"))
	var arg string
	if err := msg.Args(&arg); err != nil {
		c.Error(err)
	}
	c.Check(arg, Equals, "xyz")

	// Try reading a second message from the reader
	msg, err = readMessage(r)
	if err == nil {
		c.Error("Should not have been able to read a second message.")
	} else if err != io.EOF {
		c.Error(err)
	}
}

func (s *S) TestWriteMessage(c *C) {
	msg := newMessage()
	msg.Type = TypeMethodCall
	msg.Flags = MessageFlag(0)
	msg.serial = 1
	msg.Path = "/org/freedesktop/DBus"
	msg.Dest = "org.freedesktop.DBus"
	msg.Interface = "org.freedesktop.DBus"
	msg.Member = "NameHasOwner"
	if err := msg.AppendArgs("xyz"); err != nil {
		c.Error(err)
	}

	buff := new(bytes.Buffer)
	n, err := msg.WriteTo(buff)
	c.Check(err, Equals, nil)
	c.Check(n, Equals, int64(len(testMessage)))
	c.Check(buff.Bytes(), DeepEquals, testMessage)
}

func (s *S) TestNewMethodCallMessage(c *C) {
	msg := NewMethodCallMessage("com.destination", "/path", "com.interface", "method")
	c.Check(msg.Type, Equals, TypeMethodCall)
	c.Check(msg.Dest, Equals, "com.destination")
	c.Check(msg.Path, Equals, ObjectPath("/path"))
	c.Check(msg.Interface, Equals, "com.interface")
	c.Check(msg.Member, Equals, "method")

	// No signature or data
	c.Check(msg.sig, Equals, Signature(""))
	c.Check(msg.body, DeepEquals, []byte{})
}

func (s *S) TestNewMethodReturnMessage(c *C) {
	call := NewMethodCallMessage("com.destination", "/path", "com.interface", "method")
	call.serial = 42
	call.Sender = ":1.2"

	reply := NewMethodReturnMessage(call)
	c.Check(reply.Type, Equals, TypeMethodReturn)
	c.Check(reply.Dest, Equals, ":1.2")
	c.Check(reply.replySerial, Equals, uint32(42))

	// No signature or data
	c.Check(reply.sig, Equals, Signature(""))
	c.Check(reply.body, DeepEquals, []byte{})
}

func (s *S) TestNewSignalMessage(c *C) {
	msg := NewSignalMessage("/path", "com.interface", "signal")
	c.Check(msg.Type, Equals, TypeSignal)
	c.Check(msg.Dest, Equals, "")
	c.Check(msg.Path, Equals, ObjectPath("/path"))
	c.Check(msg.Interface, Equals, "com.interface")
	c.Check(msg.Member, Equals, "signal")

	// No signature or data
	c.Check(msg.sig, Equals, Signature(""))
	c.Check(msg.body, DeepEquals, []byte{})
}

func (s *S) TestNewErrorMessage(c *C) {
	call := NewMethodCallMessage("com.destination", "/path", "com.interface", "method")
	call.serial = 42
	call.Sender = ":1.2"

	reply := NewErrorMessage(call, "com.interface.Error", "message")
	c.Check(reply.Type, Equals, TypeError)
	c.Check(reply.Dest, Equals, ":1.2")
	c.Check(reply.replySerial, Equals, uint32(42))
	c.Check(reply.ErrorName, Equals, "com.interface.Error")

	// No signature or data
	c.Check(reply.sig, Equals, Signature("s"))
	var errorMessage string
	if err := reply.Args(&errorMessage); err != nil {
		c.Error(err)
	}
	c.Check(errorMessage, Equals, "message")
}