~ubuntu-branches/debian/sid/upstart/sid

« back to all changes in this revision

Viewing changes to upstart/tests/test_control.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant
  • Date: 2006-12-13 17:27:37 UTC
  • mto: (16.1.6 feisty)
  • mto: This revision was merged to the branch mainline in revision 17.
  • Revision ID: james.westby@ubuntu.com-20061213172737-xtzw97b0cl4q7oc5
Tags: upstream-0.3.1
ImportĀ upstreamĀ versionĀ 0.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
21
21
 */
22
22
 
23
 
#include <config.h>
 
23
#include <nih/test.h>
24
24
 
25
25
#include <sys/types.h>
26
26
#include <sys/socket.h>
27
27
#include <sys/un.h>
28
28
 
29
 
#include <stdio.h>
30
 
#include <assert.h>
31
 
#include <stdlib.h>
 
29
#include <string.h>
32
30
#include <unistd.h>
33
31
 
34
32
#include <nih/macros.h>
39
37
#include <upstart/control.h>
40
38
 
41
39
 
42
 
int
 
40
void
43
41
test_open (void)
44
42
{
45
43
        struct sockaddr_un addr;
46
 
        int                ret = 0, sock, val;
47
 
        char               name[26];
 
44
        int                sock, val;
 
45
        char               name[108];
48
46
        socklen_t          len;
49
47
 
50
 
        printf ("Testing upstart_open()\n");
 
48
        /* Check that the socket opened is a datagram socket in the
 
49
         * AF_UNIX abstract namespace with a path that includes the PID
 
50
         * of the current process.  The SO_PASSCRED option should be set
 
51
         * so that we can get the credentials of any sender.
 
52
         */
 
53
        TEST_FUNCTION ("upstart_open");
51
54
        sock = upstart_open ();
52
55
 
53
 
        /* Socket should be in AF_UNIX space */
54
56
        len = sizeof (addr);
55
 
        assert (getsockname (sock, (struct sockaddr *)&addr, &len) == 0);
56
 
        if (addr.sun_family != AF_UNIX) {
57
 
                printf ("BAD: address family wasn't what we expected.\n");
58
 
                ret = 1;
59
 
        }
60
 
 
61
 
        /* Socket should be in abstract namespace */
62
 
        if (addr.sun_path[0] != '\0') {
63
 
                printf ("BAD: address type wasn't what we expected.\n");
64
 
                ret = 1;
65
 
        }
66
 
 
67
 
        /* Name should be /com/ubuntu/upstart/$PID */
 
57
        getsockname (sock, (struct sockaddr *)&addr, &len);
 
58
 
 
59
        TEST_EQ (addr.sun_family, AF_UNIX);
 
60
        TEST_EQ (addr.sun_path[0], '\0');
 
61
 
68
62
        sprintf (name, "/com/ubuntu/upstart/%d", getpid ());
69
 
        if (strncmp (addr.sun_path + 1, name, strlen (name))) {
70
 
                printf ("BAD: address wasn't what we expected.\n");
71
 
                ret = 1;
72
 
        }
73
 
 
74
 
        /* Should work on datagrams */
75
 
        val = 0;
76
 
        len = sizeof (val);
77
 
        assert (getsockopt (sock, SOL_SOCKET, SO_TYPE, &val, &len) == 0);
78
 
        if (val != SOCK_DGRAM) {
79
 
                printf ("BAD: socket type wasn't what we expected.\n");
80
 
                ret = 1;
81
 
        }
82
 
 
83
 
        /* Credentials should be passed with any received message */
84
 
        val = 0;
85
 
        len = sizeof (val);
86
 
        assert (getsockopt (sock, SOL_SOCKET, SO_PASSCRED, &val, &len) == 0);
87
 
        if (val == 0) {
88
 
                printf ("BAD: socket will not receive credentials.\n");
89
 
                ret = 1;
90
 
        }
 
63
        TEST_EQ_STRN (addr.sun_path + 1, name);
 
64
 
 
65
        val = 0;
 
66
        len = sizeof (val);
 
67
        getsockopt (sock, SOL_SOCKET, SO_TYPE, &val, &len);
 
68
 
 
69
        TEST_EQ (val, SOCK_DGRAM);
 
70
 
 
71
        val = 0;
 
72
        len = sizeof (val);
 
73
        getsockopt (sock, SOL_SOCKET, SO_PASSCRED, &val, &len);
 
74
 
 
75
        TEST_NE (val, 0);
 
76
 
91
77
 
92
78
        close (sock);
93
 
 
94
 
        return ret;
95
79
}
96
80
 
97
81
 
98
 
int
 
82
void
99
83
test_send_msg_to (void)
100
84
{
101
85
        UpstartMsg *msg;
102
86
        NihError   *err;
103
 
        int         ret = 0, sock, retval;
 
87
        int         sock, ret;
104
88
 
105
 
        printf ("Testing upstart_send_msg_to()\n");
 
89
        TEST_FUNCTION ("upstart_send_msg_to");
106
90
        sock = upstart_open ();
107
91
        msg = nih_new (NULL, UpstartMsg);
108
92
 
109
 
        printf ("...with unknown message type\n");
 
93
        /* Check that sending an unknown message type results in the
 
94
         * UPSTART_INVALID_MESSAGE error being raised.
 
95
         */
 
96
        TEST_FEATURE ("with unknown message type");
110
97
        msg->type = 90210;
111
 
        retval = upstart_send_msg_to (getpid (), sock, msg);
112
 
 
113
 
        /* Return value should be negative */
114
 
        if (retval >= 0) {
115
 
                printf ("BAD: return value wasn't what we expected.\n");
116
 
                ret = 1;
117
 
        }
118
 
 
119
 
        /* UPSTART_INVALID_MESSAGE should be raised */
 
98
        ret = upstart_send_msg_to (getpid (), sock, msg);
 
99
 
 
100
        TEST_LT (ret, 0);
 
101
 
120
102
        err = nih_error_get ();
121
 
        if (err->number != UPSTART_INVALID_MESSAGE) {
122
 
                printf ("BAD: raised error wasn't what we expected.\n");
123
 
                ret = 1;
124
 
        }
 
103
 
 
104
        TEST_EQ (err->number, UPSTART_INVALID_MESSAGE);
 
105
 
125
106
        nih_free (err);
126
107
 
127
108
 
128
 
        printf ("...with overly long message\n");
 
109
        /* Check that sending a message that's too long also results in the
 
110
         * UPSTART_INVALID_MESSAGE error being raised.
 
111
         */
 
112
        TEST_FEATURE ("with overly long message");
129
113
        msg->type = UPSTART_JOB_QUERY;
130
114
        msg->job_query.name = nih_alloc (msg, 8192);
131
115
        memset (msg->job_query.name, 'a', 8191);
132
116
        msg->job_query.name[8191] = '\0';
133
117
 
134
 
        retval = upstart_send_msg_to (getpid (), sock, msg);
135
 
 
136
 
        /* Return value should be negative */
137
 
        if (retval >= 0) {
138
 
                printf ("BAD: return value wasn't what we expected.\n");
139
 
                ret = 1;
140
 
        }
141
 
 
142
 
        /* UPSTART_INVALID_MESSAGE should be raised */
 
118
        ret = upstart_send_msg_to (getpid (), sock, msg);
 
119
 
 
120
        TEST_LT (ret, 0);
 
121
 
143
122
        err = nih_error_get ();
144
 
        if (err->number != UPSTART_INVALID_MESSAGE) {
145
 
                printf ("BAD: raised error wasn't what we expected.\n");
146
 
                ret = 1;
147
 
        }
 
123
 
 
124
        TEST_EQ (err->number, UPSTART_INVALID_MESSAGE);
 
125
 
148
126
        nih_free (err);
149
127
 
150
128
 
151
129
        nih_free (msg);
152
130
        close (sock);
153
 
 
154
 
        return ret;
155
131
}
156
132
 
157
 
int
 
133
void
158
134
test_recv_msg (void)
159
135
{
160
136
        struct sockaddr_un  addr;
163
139
        NihError           *err;
164
140
        pid_t               pid;
165
141
        char                buf[80];
166
 
        int                 ret = 0, s_sock, r_sock;
 
142
        int                 s_sock, r_sock;
167
143
 
168
 
        printf ("Testing upstart_recv_msg()\n");
 
144
        TEST_FUNCTION ("upstart_recv_msg");
169
145
        s_sock = socket (PF_UNIX, SOCK_DGRAM, 0);
170
146
        r_sock = upstart_open ();
171
147
 
176
152
        addrlen += snprintf (addr.sun_path + 1, sizeof (addr.sun_path) -1,
177
153
                             "/com/ubuntu/upstart/%d", getpid ());
178
154
 
179
 
        printf ("...without magic marker\n");
 
155
        /* Check that receiving a message without the usual magic marker
 
156
         * results in only UPSTART_INVALID_MESSAGE being raised.
 
157
         */
 
158
        TEST_FEATURE ("without magic marker");
180
159
        memset (buf, 0, 16);
181
160
        memcpy (buf, "wibblefart", 10);
182
161
        memcpy (buf + 16, "\0\0\0\0", 4);
183
162
        sendto (s_sock, buf, 20, 0, (struct sockaddr *)&addr, addrlen);
184
163
        msg = upstart_recv_msg (NULL, r_sock, NULL);
185
164
 
186
 
        /* Return value should be NULL */
187
 
        if (msg != NULL) {
188
 
                printf ("BAD: return value wasn't what we expected.\n");
189
 
                ret = 1;
190
 
        }
 
165
        TEST_EQ_P (msg, NULL);
191
166
 
192
 
        /* UPSTART_INVALID_MESSAGE should be raised */
193
167
        err = nih_error_get ();
194
 
        if (err->number != UPSTART_INVALID_MESSAGE) {
195
 
                printf ("BAD: raised error wasn't what we expected.\n");
196
 
                ret = 1;
197
 
        }
 
168
 
 
169
        TEST_EQ (err->number, UPSTART_INVALID_MESSAGE);
 
170
 
198
171
        nih_free (err);
199
172
 
200
173
 
201
 
        printf ("...with unknown message type\n");
 
174
        /* Check that receiving a message with an unknown type results in
 
175
         * the UPSTART_INVALID_MESSAGE error being raised.
 
176
         */
 
177
        TEST_FEATURE ("with unknown message type");
202
178
        memset (buf, 0, 16);
203
179
        strncpy (buf, PACKAGE_STRING, 16);
204
180
        memcpy (buf + 16, "\0\0\0\001", 4);
205
181
        sendto (s_sock, buf, 20, 0, (struct sockaddr *)&addr, addrlen);
206
182
        msg = upstart_recv_msg (NULL, r_sock, NULL);
207
183
 
208
 
        /* Return value should be NULL */
209
 
        if (msg != NULL) {
210
 
                printf ("BAD: return value wasn't what we expected.\n");
211
 
                ret = 1;
212
 
        }
 
184
        TEST_EQ_P (msg, NULL);
213
185
 
214
 
        /* UPSTART_INVALID_MESSAGE should be raised */
215
186
        err = nih_error_get ();
216
 
        if (err->number != UPSTART_INVALID_MESSAGE) {
217
 
                printf ("BAD: raised error wasn't what we expected.\n");
218
 
                ret = 1;
219
 
        }
 
187
 
 
188
        TEST_EQ (err->number, UPSTART_INVALID_MESSAGE);
 
189
 
220
190
        nih_free (err);
221
191
 
222
192
 
223
 
        printf ("...with short message\n");
 
193
        /* Check that receiving a message which has been truncated (or is
 
194
         * just too short) results in the UPSTART_INVALID_MESSAGE error
 
195
         * being raised.
 
196
         */
 
197
        TEST_FEATURE ("with short message");
224
198
        memset (buf, 0, 16);
225
199
        strncpy (buf, PACKAGE_STRING, 16);
226
200
        memcpy (buf + 16, "\001\0\0\0", 4);
228
202
        sendto (s_sock, buf, 28, 0, (struct sockaddr *)&addr, addrlen);
229
203
        msg = upstart_recv_msg (NULL, r_sock, NULL);
230
204
 
231
 
        /* Return value should be NULL */
232
 
        if (msg != NULL) {
233
 
                printf ("BAD: return value wasn't what we expected.\n");
234
 
                ret = 1;
235
 
        }
 
205
        TEST_EQ_P (msg, NULL);
236
206
 
237
 
        /* UPSTART_INVALID_MESSAGE should be raised */
238
207
        err = nih_error_get ();
239
 
        if (err->number != UPSTART_INVALID_MESSAGE) {
240
 
                printf ("BAD: raised error wasn't what we expected.\n");
241
 
                ret = 1;
242
 
        }
 
208
 
 
209
        TEST_EQ (err->number, UPSTART_INVALID_MESSAGE);
 
210
 
243
211
        nih_free (err);
244
212
 
245
213
 
246
 
        printf ("...with valid message\n");
 
214
        /* Check that a valid message being received returns that message,
 
215
         * allocated with nih_alloc and with the values filled in.  Also
 
216
         * check that the pid of the sender is stored in the argument.
 
217
         */
 
218
        TEST_FEATURE ("with valid message");
247
219
        memset (buf, 0, 16);
248
220
        strncpy (buf, PACKAGE_STRING, 16);
249
221
        memcpy (buf + 16, "\0\0\0\0", 4);
250
222
        sendto (s_sock, buf, 20, 0, (struct sockaddr *)&addr, addrlen);
251
223
        msg = upstart_recv_msg (NULL, r_sock, &pid);
252
224
 
253
 
        /* Message type should be UPSTART_NO_OP */
254
 
        if (msg->type != UPSTART_NO_OP) {
255
 
                printf ("BAD: message type wasn't what we expected.\n");
256
 
                ret = 1;
257
 
        }
258
 
 
259
 
        /* Process id should be stored in pid */
260
 
        if (pid != getpid ()) {
261
 
                printf ("BAD: process id wasn't what we expected.\n");
262
 
                ret = 1;
263
 
        }
 
225
        TEST_ALLOC_SIZE (msg, sizeof (UpstartMsg));
 
226
        TEST_EQ (msg->type, UPSTART_NO_OP);
 
227
        TEST_EQ (pid, getpid ());
264
228
 
265
229
        nih_free (msg);
266
230
 
267
231
 
268
232
        close (s_sock);
269
233
        close (r_sock);
270
 
 
271
 
        return ret;
272
234
}
273
235
 
274
 
int
 
236
void
275
237
test_messages (void)
276
238
{
277
239
        UpstartMsg *s_msg, *r_msg;
278
240
        int         s_sock, r_sock;
279
 
        int         ret = 0;
280
241
 
281
242
        /* Rather than test the sending and receiving separately,
282
243
         * check whether messages poked in one end come out the other
283
244
         * the same way
284
245
         */
285
246
 
286
 
        printf ("Testing upstart_send/recv_msg()\n");
 
247
        TEST_FUNCTION ("upstart_send/recv_msg");
287
248
        s_sock = socket (PF_UNIX, SOCK_DGRAM, 0);
288
249
        r_sock = upstart_open ();
289
250
        s_msg = nih_new (NULL, UpstartMsg);
290
251
 
291
252
 
292
 
        printf ("...with UPSTART_NO_OP\n");
 
253
        /* Check that an UPSTART_NO_OP message can be sent and received
 
254
         * correctly, with all fields transmitted properly.
 
255
         */
 
256
        TEST_FEATURE ("with UPSTART_NO_OP");
293
257
        s_msg->type = UPSTART_NO_OP;
294
258
 
295
259
        upstart_send_msg_to (getpid (), s_sock, s_msg);
296
260
        r_msg = upstart_recv_msg (NULL, r_sock, NULL);
297
261
 
298
 
        /* Type should be UPSTART_NO_OP */
299
 
        if (r_msg->type != UPSTART_NO_OP) {
300
 
                printf ("BAD: message type wasn't what we expected.\n");
301
 
                ret = 1;
302
 
        }
 
262
        TEST_ALLOC_SIZE (r_msg, sizeof (UpstartMsg));
 
263
        TEST_EQ (r_msg->type, UPSTART_NO_OP);
303
264
 
304
265
        nih_free (r_msg);
305
266
 
306
267
 
307
 
        printf ("...with UPSTART_JOB_START\n");
 
268
        /* Check that an UPSTART_JOB_START message can be sent and received
 
269
         * correctly, with all fields transmitted properly.
 
270
         */
 
271
        TEST_FEATURE ("with UPSTART_JOB_START");
308
272
        s_msg->type = UPSTART_JOB_START;
309
273
        s_msg->job_start.name = "wibble";
310
274
 
311
275
        upstart_send_msg_to (getpid (), s_sock, s_msg);
312
276
        r_msg = upstart_recv_msg (NULL, r_sock, NULL);
313
277
 
314
 
        /* Type should be UPSTART_JOB_START */
315
 
        if (r_msg->type != UPSTART_JOB_START) {
316
 
                printf ("BAD: message type wasn't what we expected.\n");
317
 
                ret = 1;
318
 
        }
319
 
 
320
 
        /* Name should be what we sent */
321
 
        if (strcmp (r_msg->job_start.name, "wibble")) {
322
 
                printf ("BAD: job name wasn't what we expected.\n");
323
 
                ret = 1;
324
 
        }
325
 
 
326
 
        /* Name should be nih_alloc child of message */
327
 
        if (nih_alloc_parent (r_msg->job_start.name) != r_msg) {
328
 
                printf ("BAD: name wasn't nih_alloc child of message.\n");
329
 
                ret = 1;
330
 
        }
 
278
        TEST_ALLOC_SIZE (r_msg, sizeof (UpstartMsg));
 
279
        TEST_EQ (r_msg->type, UPSTART_JOB_START);
 
280
        TEST_EQ_STR (r_msg->job_start.name, "wibble");
 
281
        TEST_ALLOC_PARENT (r_msg->job_start.name, r_msg);
331
282
 
332
283
        nih_free (r_msg);
333
284
 
334
285
 
335
 
        printf ("...with UPSTART_JOB_STOP\n");
 
286
        /* Check that an UPSTART_JOB_STOP message can be sent and received
 
287
         * correctly, with all fields transmitted properly.
 
288
         */
 
289
        TEST_FEATURE ("with UPSTART_JOB_STOP");
336
290
        s_msg->type = UPSTART_JOB_STOP;
337
291
        s_msg->job_stop.name = "wibble";
338
292
 
339
293
        upstart_send_msg_to (getpid (), s_sock, s_msg);
340
294
        r_msg = upstart_recv_msg (NULL, r_sock, NULL);
341
295
 
342
 
        /* Type should be UPSTART_JOB_STOP */
343
 
        if (r_msg->type != UPSTART_JOB_STOP) {
344
 
                printf ("BAD: message type wasn't what we expected.\n");
345
 
                ret = 1;
346
 
        }
347
 
 
348
 
        /* Name should be what we sent */
349
 
        if (strcmp (r_msg->job_stop.name, "wibble")) {
350
 
                printf ("BAD: job name wasn't what we expected.\n");
351
 
                ret = 1;
352
 
        }
353
 
 
354
 
        /* Name should be nih_alloc child of message */
355
 
        if (nih_alloc_parent (r_msg->job_stop.name) != r_msg) {
356
 
                printf ("BAD: name wasn't nih_alloc child of message.\n");
357
 
                ret = 1;
358
 
        }
 
296
        TEST_ALLOC_SIZE (r_msg, sizeof (UpstartMsg));
 
297
        TEST_EQ (r_msg->type, UPSTART_JOB_STOP);
 
298
        TEST_EQ_STR (r_msg->job_stop.name, "wibble");
 
299
        TEST_ALLOC_PARENT (r_msg->job_stop.name, r_msg);
359
300
 
360
301
        nih_free (r_msg);
361
302
 
362
303
 
363
 
        printf ("...with UPSTART_JOB_QUERY\n");
 
304
        /* Check that an UPSTART_JOB_QUERY message can be sent and received
 
305
         * correctly, with all fields transmitted properly.
 
306
         */
 
307
        TEST_FEATURE ("with UPSTART_JOB_QUERY");
364
308
        s_msg->type = UPSTART_JOB_QUERY;
365
309
        s_msg->job_query.name = "wibble";
366
310
 
367
311
        upstart_send_msg_to (getpid (), s_sock, s_msg);
368
312
        r_msg = upstart_recv_msg (NULL, r_sock, NULL);
369
313
 
370
 
        /* Type should be UPSTART_JOB_QUERY */
371
 
        if (r_msg->type != UPSTART_JOB_QUERY) {
372
 
                printf ("BAD: message type wasn't what we expected.\n");
373
 
                ret = 1;
374
 
        }
375
 
 
376
 
        /* Name should be what we sent */
377
 
        if (strcmp (r_msg->job_query.name, "wibble")) {
378
 
                printf ("BAD: job name wasn't what we expected.\n");
379
 
                ret = 1;
380
 
        }
381
 
 
382
 
        /* Name should be nih_alloc child of message */
383
 
        if (nih_alloc_parent (r_msg->job_query.name) != r_msg) {
384
 
                printf ("BAD: name wasn't nih_alloc child of message.\n");
385
 
                ret = 1;
386
 
        }
 
314
        TEST_ALLOC_SIZE (r_msg, sizeof (UpstartMsg));
 
315
        TEST_EQ (r_msg->type, UPSTART_JOB_QUERY);
 
316
        TEST_EQ_STR (r_msg->job_query.name, "wibble");
 
317
        TEST_ALLOC_PARENT (r_msg->job_query.name, r_msg);
387
318
 
388
319
        nih_free (r_msg);
389
320
 
390
321
 
391
 
        printf ("...with UPSTART_JOB_STATUS\n");
 
322
        /* Check that an UPSTART_JOB_STATUS message can be sent and received
 
323
         * correctly, with all fields transmitted properly.
 
324
         */
 
325
        TEST_FEATURE ("with UPSTART_JOB_STATUS");
392
326
        s_msg->type = UPSTART_JOB_STATUS;
393
327
        s_msg->job_status.name = "wibble";
394
328
        s_msg->job_status.description = "foo bar";
400
334
        upstart_send_msg_to (getpid (), s_sock, s_msg);
401
335
        r_msg = upstart_recv_msg (NULL, r_sock, NULL);
402
336
 
403
 
        /* Type should be UPSTART_JOB_STATUS */
404
 
        if (r_msg->type != UPSTART_JOB_STATUS) {
405
 
                printf ("BAD: message type wasn't what we expected.\n");
406
 
                ret = 1;
407
 
        }
408
 
 
409
 
        /* Name should be what we sent */
410
 
        if (strcmp (r_msg->job_status.name, "wibble")) {
411
 
                printf ("BAD: job name wasn't what we expected.\n");
412
 
                ret = 1;
413
 
        }
414
 
 
415
 
        /* Name should be nih_alloc child of message */
416
 
        if (nih_alloc_parent (r_msg->job_status.name) != r_msg) {
417
 
                printf ("BAD: name wasn't nih_alloc child of message.\n");
418
 
                ret = 1;
419
 
        }
420
 
 
421
 
        /* Description should be what we sent */
422
 
        if (strcmp (r_msg->job_status.description, "foo bar")) {
423
 
                printf ("BAD: description wasn't what we expected.\n");
424
 
                ret = 1;
425
 
        }
426
 
 
427
 
        /* Description should be nih_alloc child of message */
428
 
        if (nih_alloc_parent (r_msg->job_status.description) != r_msg) {
429
 
                printf ("BAD: desc wasn't nih_alloc child of message.\n");
430
 
                ret = 1;
431
 
        }
432
 
 
433
 
        /* Job goal should be what we sent */
434
 
        if (r_msg->job_status.goal != JOB_START) {
435
 
                printf ("BAD: job goal wasn't what we expected.\n");
436
 
                ret = 1;
437
 
        }
438
 
 
439
 
        /* Job state should be what we sent */
440
 
        if (r_msg->job_status.state != JOB_STARTING) {
441
 
                printf ("BAD: job state wasn't what we expected.\n");
442
 
                ret = 1;
443
 
        }
444
 
 
445
 
        /* Process state should be what we sent */
446
 
        if (r_msg->job_status.process_state != PROCESS_ACTIVE) {
447
 
                printf ("BAD: process state wasn't what we expected.\n");
448
 
                ret = 1;
449
 
        }
450
 
 
451
 
        /* Process id should be what we sent */
452
 
        if (r_msg->job_status.pid != 123) {
453
 
                printf ("BAD: process id wasn't what we expected.\n");
454
 
                ret = 1;
455
 
        }
 
337
        TEST_ALLOC_SIZE (r_msg, sizeof (UpstartMsg));
 
338
        TEST_EQ (r_msg->type, UPSTART_JOB_STATUS);
 
339
        TEST_EQ_STR (r_msg->job_status.name, "wibble");
 
340
        TEST_ALLOC_PARENT (r_msg->job_status.name, r_msg);
 
341
        TEST_EQ_STR (r_msg->job_status.description, "foo bar");
 
342
        TEST_ALLOC_PARENT (r_msg->job_status.description, r_msg);
 
343
        TEST_EQ (r_msg->job_status.goal, JOB_START);
 
344
        TEST_EQ (r_msg->job_status.state, JOB_STARTING);
 
345
        TEST_EQ (r_msg->job_status.process_state, PROCESS_ACTIVE);
 
346
        TEST_EQ (r_msg->job_status.pid, 123);
456
347
 
457
348
        nih_free (r_msg);
458
349
 
459
350
 
460
 
        printf ("...with UPSTART_JOB_STATUS without description\n");
 
351
        /* Check that an UPSTART_JOB_STATUS message without a job
 
352
         * description can be sent and received correctly, with all fields
 
353
         * transmitted properly.
 
354
         */
 
355
        TEST_FEATURE ("with UPSTART_JOB_STATUS without description");
461
356
        s_msg->job_status.description = NULL;
462
357
 
463
358
        upstart_send_msg_to (getpid (), s_sock, s_msg);
464
359
        r_msg = upstart_recv_msg (NULL, r_sock, NULL);
465
360
 
466
 
        /* Description should be NULL */
467
 
        if (r_msg->job_status.description != NULL) {
468
 
                printf ("BAD: description wasn't what we expected.\n");
469
 
                ret = 1;
470
 
        }
 
361
        TEST_ALLOC_SIZE (r_msg, sizeof (UpstartMsg));
 
362
        TEST_EQ (r_msg->type, UPSTART_JOB_STATUS);
 
363
        TEST_EQ_STR (r_msg->job_status.name, "wibble");
 
364
        TEST_ALLOC_PARENT (r_msg->job_status.name, r_msg);
 
365
        TEST_EQ_P (r_msg->job_status.description, NULL);
 
366
        TEST_EQ (r_msg->job_status.goal, JOB_START);
 
367
        TEST_EQ (r_msg->job_status.state, JOB_STARTING);
 
368
        TEST_EQ (r_msg->job_status.process_state, PROCESS_ACTIVE);
 
369
        TEST_EQ (r_msg->job_status.pid, 123);
471
370
 
472
371
        nih_free (r_msg);
473
372
 
474
373
 
475
 
        printf ("...with UPSTART_JOB_UNKNOWN\n");
 
374
        /* Check that an UPSTART_JOB_UNKNOWN message can be sent and received
 
375
         * correctly, with all fields transmitted properly.
 
376
         */
 
377
        TEST_FEATURE ("with UPSTART_JOB_UNKNOWN");
476
378
        s_msg->type = UPSTART_JOB_UNKNOWN;
477
379
        s_msg->job_unknown.name = "wibble";
478
380
 
479
381
        upstart_send_msg_to (getpid (), s_sock, s_msg);
480
382
        r_msg = upstart_recv_msg (NULL, r_sock, NULL);
481
383
 
482
 
        /* Type should be UPSTART_JOB_UNKNOWN */
483
 
        if (r_msg->type != UPSTART_JOB_UNKNOWN) {
484
 
                printf ("BAD: message type wasn't what we expected.\n");
485
 
                ret = 1;
486
 
        }
487
 
 
488
 
        /* Name should be what we sent */
489
 
        if (strcmp (r_msg->job_unknown.name, "wibble")) {
490
 
                printf ("BAD: job name wasn't what we expected.\n");
491
 
                ret = 1;
492
 
        }
493
 
 
494
 
        /* Name should be nih_alloc child of message */
495
 
        if (nih_alloc_parent (r_msg->job_unknown.name) != r_msg) {
496
 
                printf ("BAD: name wasn't nih_alloc child of message.\n");
497
 
                ret = 1;
498
 
        }
 
384
        TEST_ALLOC_SIZE (r_msg, sizeof (UpstartMsg));
 
385
        TEST_EQ (r_msg->type, UPSTART_JOB_UNKNOWN);
 
386
        TEST_EQ_STR (r_msg->job_unknown.name, "wibble");
 
387
        TEST_ALLOC_PARENT (r_msg->job_unknown.name, r_msg);
499
388
 
500
389
        nih_free (r_msg);
501
390
 
502
391
 
503
 
        printf ("...with UPSTART_EVENT_QUEUE\n");
 
392
        /* Check that an UPSTART_EVENT_QUEUE message can be sent and received
 
393
         * correctly, with all fields transmitted properly.
 
394
         */
 
395
        TEST_FEATURE ("with UPSTART_EVENT_QUEUE");
504
396
        s_msg->type = UPSTART_EVENT_QUEUE;
505
397
        s_msg->event_queue.name = "frodo";
506
398
 
507
399
        upstart_send_msg_to (getpid (), s_sock, s_msg);
508
400
        r_msg = upstart_recv_msg (NULL, r_sock, NULL);
509
401
 
510
 
        /* Type should be UPSTART_EVENT_QUEUE */
511
 
        if (r_msg->type != UPSTART_EVENT_QUEUE) {
512
 
                printf ("BAD: message type wasn't what we expected.\n");
513
 
                ret = 1;
514
 
        }
515
 
 
516
 
        /* Name should be what we sent */
517
 
        if (strcmp (r_msg->event_queue.name, "frodo")) {
518
 
                printf ("BAD: event name wasn't what we expected.\n");
519
 
                ret = 1;
520
 
        }
521
 
 
522
 
        /* Name should be nih_alloc child of message */
523
 
        if (nih_alloc_parent (r_msg->event_queue.name) != r_msg) {
524
 
                printf ("BAD: name wasn't nih_alloc child of message.\n");
525
 
                ret = 1;
526
 
        }
 
402
        TEST_ALLOC_SIZE (r_msg, sizeof (UpstartMsg));
 
403
        TEST_EQ (r_msg->type, UPSTART_EVENT_QUEUE);
 
404
        TEST_EQ_STR (r_msg->event_queue.name, "frodo");
 
405
        TEST_ALLOC_PARENT (r_msg->event_queue.name, r_msg);
527
406
 
528
407
        nih_free (r_msg);
529
408
 
530
409
 
531
 
        printf ("...with UPSTART_EVENT\n");
 
410
        /* Check that an UPSTART_EVENT message can be sent and received
 
411
         * correctly, with all fields transmitted properly.
 
412
         */
 
413
        TEST_FEATURE ("with UPSTART_EVENT");
532
414
        s_msg->type = UPSTART_EVENT;
533
415
        s_msg->event.name = "foo";
534
416
 
535
417
        upstart_send_msg_to (getpid (), s_sock, s_msg);
536
418
        r_msg = upstart_recv_msg (NULL, r_sock, NULL);
537
419
 
538
 
        /* Type should be UPSTART_EVENT */
539
 
        if (r_msg->type != UPSTART_EVENT) {
540
 
                printf ("BAD: message type wasn't what we expected.\n");
541
 
                ret = 1;
542
 
        }
543
 
 
544
 
        /* Name should be what we sent */
545
 
        if (strcmp (r_msg->event.name, "foo")) {
546
 
                printf ("BAD: event name wasn't what we expected.\n");
547
 
                ret = 1;
548
 
        }
549
 
 
550
 
        /* Name should be nih_alloc child of message */
551
 
        if (nih_alloc_parent (r_msg->event.name) != r_msg) {
552
 
                printf ("BAD: name wasn't nih_alloc child of message.\n");
553
 
                ret = 1;
554
 
        }
 
420
        TEST_ALLOC_SIZE (r_msg, sizeof (UpstartMsg));
 
421
        TEST_EQ (r_msg->type, UPSTART_EVENT);
 
422
        TEST_EQ_STR (r_msg->event.name, "foo");
 
423
        TEST_ALLOC_PARENT (r_msg->event.name, r_msg);
555
424
 
556
425
        nih_free (r_msg);
557
426
 
558
427
 
559
 
        printf ("...with UPSTART_JOB_LIST\n");
 
428
        /* Check that an UPSTART_JOB_LIST message can be sent and received
 
429
         * correctly, with all fields transmitted properly.
 
430
         */
 
431
        TEST_FEATURE ("with UPSTART_JOB_LIST");
560
432
        s_msg->type = UPSTART_JOB_LIST;
561
433
 
562
434
        upstart_send_msg_to (getpid (), s_sock, s_msg);
563
435
        r_msg = upstart_recv_msg (NULL, r_sock, NULL);
564
436
 
565
 
        /* Type should be UPSTART_JOB_LIST */
566
 
        if (r_msg->type != UPSTART_JOB_LIST) {
567
 
                printf ("BAD: message type wasn't what we expected.\n");
568
 
                ret = 1;
569
 
        }
 
437
        TEST_ALLOC_SIZE (r_msg, sizeof (UpstartMsg));
 
438
        TEST_EQ (r_msg->type, UPSTART_JOB_LIST);
570
439
 
571
440
        nih_free (r_msg);
572
441
 
573
442
 
574
 
        printf ("...with UPSTART_JOB_LIST_END\n");
 
443
        /* Check that an UPSTART_JOB_LIST_END message can be sent and received
 
444
         * correctly, with all fields transmitted properly.
 
445
         */
 
446
        TEST_FEATURE ("with UPSTART_JOB_LIST_END");
575
447
        s_msg->type = UPSTART_JOB_LIST_END;
576
448
 
577
449
        upstart_send_msg_to (getpid (), s_sock, s_msg);
578
450
        r_msg = upstart_recv_msg (NULL, r_sock, NULL);
579
451
 
580
 
        /* Type should be UPSTART_JOB_LIST_END */
581
 
        if (r_msg->type != UPSTART_JOB_LIST_END) {
582
 
                printf ("BAD: message type wasn't what we expected.\n");
583
 
                ret = 1;
584
 
        }
 
452
        TEST_ALLOC_SIZE (r_msg, sizeof (UpstartMsg));
 
453
        TEST_EQ (r_msg->type, UPSTART_JOB_LIST_END);
585
454
 
586
455
        nih_free (r_msg);
587
456
 
588
457
 
589
 
        printf ("...with UPSTART_WATCH_JOBS\n");
 
458
        /* Check that an UPSTART_WATCH_JOBS message can be sent and received
 
459
         * correctly, with all fields transmitted properly.
 
460
         */
 
461
        TEST_FEATURE ("with UPSTART_WATCH_JOBS");
590
462
        s_msg->type = UPSTART_WATCH_JOBS;
591
463
 
592
464
        upstart_send_msg_to (getpid (), s_sock, s_msg);
593
465
        r_msg = upstart_recv_msg (NULL, r_sock, NULL);
594
466
 
595
 
        /* Type should be UPSTART_WATCH_JOBS */
596
 
        if (r_msg->type != UPSTART_WATCH_JOBS) {
597
 
                printf ("BAD: message type wasn't what we expected.\n");
598
 
                ret = 1;
599
 
        }
 
467
        TEST_ALLOC_SIZE (r_msg, sizeof (UpstartMsg));
 
468
        TEST_EQ (r_msg->type, UPSTART_WATCH_JOBS);
600
469
 
601
470
        nih_free (r_msg);
602
471
 
603
472
 
604
 
        printf ("...with UPSTART_UNWATCH_JOBS\n");
 
473
        /* Check that an UPSTART_UNWATCH_JOBS message can be sent and received
 
474
         * correctly, with all fields transmitted properly.
 
475
         */
 
476
        TEST_FEATURE ("with UPSTART_UNWATCH_JOBS");
605
477
        s_msg->type = UPSTART_UNWATCH_JOBS;
606
478
 
607
479
        upstart_send_msg_to (getpid (), s_sock, s_msg);
608
480
        r_msg = upstart_recv_msg (NULL, r_sock, NULL);
609
481
 
610
 
        /* Type should be UPSTART_UNWATCH_JOBS */
611
 
        if (r_msg->type != UPSTART_UNWATCH_JOBS) {
612
 
                printf ("BAD: message type wasn't what we expected.\n");
613
 
                ret = 1;
614
 
        }
 
482
        TEST_ALLOC_SIZE (r_msg, sizeof (UpstartMsg));
 
483
        TEST_EQ (r_msg->type, UPSTART_UNWATCH_JOBS);
615
484
 
616
485
        nih_free (r_msg);
617
486
 
618
487
 
619
 
        printf ("...with UPSTART_WATCH_EVENTS\n");
 
488
        /* Check that an UPSTART_WATCH_EVENTS message can be sent and received
 
489
         * correctly, with all fields transmitted properly.
 
490
         */
 
491
        TEST_FEATURE ("with UPSTART_WATCH_EVENTS");
620
492
        s_msg->type = UPSTART_WATCH_EVENTS;
621
493
 
622
494
        upstart_send_msg_to (getpid (), s_sock, s_msg);
623
495
        r_msg = upstart_recv_msg (NULL, r_sock, NULL);
624
496
 
625
 
        /* Type should be UPSTART_WATCH_EVENTS */
626
 
        if (r_msg->type != UPSTART_WATCH_EVENTS) {
627
 
                printf ("BAD: message type wasn't what we expected.\n");
628
 
                ret = 1;
629
 
        }
 
497
        TEST_ALLOC_SIZE (r_msg, sizeof (UpstartMsg));
 
498
        TEST_EQ (r_msg->type, UPSTART_WATCH_EVENTS);
630
499
 
631
500
        nih_free (r_msg);
632
501
 
633
502
 
634
 
        printf ("...with UPSTART_UNWATCH_EVENTS\n");
 
503
        /* Check that an UPSTART_UNWATCH_EVENTS message can be sent and
 
504
         * received correctly, with all fields transmitted properly.
 
505
         */
 
506
        TEST_FEATURE ("with UPSTART_UNWATCH_EVENTS");
635
507
        s_msg->type = UPSTART_UNWATCH_EVENTS;
636
508
 
637
509
        upstart_send_msg_to (getpid (), s_sock, s_msg);
638
510
        r_msg = upstart_recv_msg (NULL, r_sock, NULL);
639
511
 
640
 
        /* Type should be UPSTART_UNWATCH_EVENTS */
641
 
        if (r_msg->type != UPSTART_UNWATCH_EVENTS) {
642
 
                printf ("BAD: message type wasn't what we expected.\n");
643
 
                ret = 1;
644
 
        }
 
512
        TEST_ALLOC_SIZE (r_msg, sizeof (UpstartMsg));
 
513
        TEST_EQ (r_msg->type, UPSTART_UNWATCH_EVENTS);
645
514
 
646
515
        nih_free (r_msg);
647
516
 
648
517
 
649
 
        printf ("...with UPSTART_SHUTDOWN\n");
 
518
        /* Check that an UPSTART_SHUTDOWN message can be sent and received
 
519
         * correctly, with all fields transmitted properly.
 
520
         */
 
521
        TEST_FEATURE ("with UPSTART_SHUTDOWN");
650
522
        s_msg->type = UPSTART_SHUTDOWN;
651
523
        s_msg->shutdown.name = "reboot";
652
524
 
653
525
        upstart_send_msg_to (getpid (), s_sock, s_msg);
654
526
        r_msg = upstart_recv_msg (NULL, r_sock, NULL);
655
527
 
656
 
        /* Type should be UPSTART_SHUTDOWN */
657
 
        if (r_msg->type != UPSTART_SHUTDOWN) {
658
 
                printf ("BAD: message type wasn't what we expected.\n");
659
 
                ret = 1;
660
 
        }
661
 
 
662
 
        /* Name should be what we sent */
663
 
        if (strcmp (r_msg->shutdown.name, "reboot")) {
664
 
                printf ("BAD: event name wasn't what we expected.\n");
665
 
                ret = 1;
666
 
        }
667
 
 
668
 
        /* Name should be nih_alloc child of message */
669
 
        if (nih_alloc_parent (r_msg->event_queue.name) != r_msg) {
670
 
                printf ("BAD: name wasn't nih_alloc child of message.\n");
671
 
                ret = 1;
672
 
        }
 
528
        TEST_ALLOC_SIZE (r_msg, sizeof (UpstartMsg));
 
529
        TEST_EQ (r_msg->type, UPSTART_SHUTDOWN);
 
530
        TEST_EQ_STR (r_msg->shutdown.name, "reboot");
 
531
        TEST_ALLOC_PARENT (r_msg->shutdown.name, r_msg);
673
532
 
674
533
        nih_free (r_msg);
675
534
 
 
535
 
676
536
        nih_free (s_msg);
677
537
 
678
538
        close (r_sock);
679
539
        close (s_sock);
680
 
 
681
 
        return ret;
682
 
}
683
 
 
684
 
int
685
 
test_free (void)
686
 
{
687
 
        void *ptr;
688
 
        int   ret = 0;
689
 
 
690
 
        printf ("Testing upstart_free()\n");
691
 
        ptr = nih_alloc (NULL, 1024);
692
 
        upstart_free (ptr);
693
 
 
694
 
        /* didn't crash, so it worked */
695
 
 
696
 
        return ret;
697
540
}
698
541
 
699
542
 
701
544
main (int   argc,
702
545
      char *argv[])
703
546
{
704
 
        int ret = 0;
705
 
 
706
 
        ret |= test_open ();
707
 
        ret |= test_send_msg_to ();
708
 
        ret |= test_recv_msg ();
709
 
        ret |= test_messages ();
710
 
        ret |= test_free ();
711
 
 
712
 
        return ret;
 
547
        test_open ();
 
548
        test_send_msg_to ();
 
549
        test_recv_msg ();
 
550
        test_messages ();
 
551
 
 
552
        return 0;
713
553
}