~jamesodhunt/upstart/event-prefixes-temp

« back to all changes in this revision

Viewing changes to util/tests/test_initctl.c

* Merge of lp:~stgraber/upstart/upstart-session-socket.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
#include <signal.h>
30
30
#include <unistd.h>
31
31
#include <regex.h>
32
 
#include <sys/types.h>        
 
32
#include <sys/types.h>
33
33
#include <sys/stat.h>
34
34
 
35
35
#include <nih-dbus/dbus_error.h>
379
379
}
380
380
 
381
381
extern int use_dbus;
 
382
extern int user_mode;
382
383
extern int dbus_bus_type;
383
384
extern char *dest_name;
384
385
extern const char *dest_address;
508
509
                dbus_shutdown ();
509
510
        }
510
511
 
 
512
        /* Check that we can create a proxy to Upstart's private internal
 
513
         * server in user mode, and that this is the default behaviour if we don't
 
514
         * fiddle with the other options.  The returned proxy should
 
515
         * hold the only reference to the connection.
 
516
         */
 
517
        TEST_FEATURE ("with user-mode");
 
518
        TEST_ALLOC_FAIL {
 
519
                use_dbus = -1;
 
520
                dbus_bus_type = -1;
 
521
                dest_name = NULL;
 
522
                dest_address = DBUS_ADDRESS_UPSTART;
 
523
                user_mode = TRUE;
 
524
 
 
525
                assert0 (setenv ("UPSTART_SESSION",
 
526
                                 "unix:abstract=/com/ubuntu/upstart/test-session",
 
527
                                 TRUE));
 
528
 
 
529
                TEST_ALLOC_SAFE {
 
530
                        server = nih_dbus_server (getenv ("UPSTART_SESSION"),
 
531
                                                  my_connect_handler,
 
532
                                                  NULL);
 
533
                        assert (server != NULL);
 
534
                }
 
535
 
 
536
                my_connect_handler_called = FALSE;
 
537
                last_connection = NULL;
 
538
 
 
539
                TEST_DIVERT_STDERR (output) {
 
540
                        proxy = upstart_open (NULL);
 
541
                }
 
542
                rewind (output);
 
543
 
 
544
                if (test_alloc_failed
 
545
                    && (proxy == NULL)) {
 
546
                        TEST_FILE_EQ (output, "test: Cannot allocate memory\n");
 
547
                        TEST_FILE_END (output);
 
548
                        TEST_FILE_RESET (output);
 
549
 
 
550
                        if (last_connection) {
 
551
                                dbus_connection_close (last_connection);
 
552
                                dbus_connection_unref (last_connection);
 
553
                        }
 
554
 
 
555
                        dbus_server_disconnect (server);
 
556
                        dbus_server_unref (server);
 
557
 
 
558
                        dbus_shutdown ();
 
559
                        continue;
 
560
                }
 
561
 
 
562
                nih_main_loop ();
 
563
 
 
564
                TEST_TRUE (my_connect_handler_called);
 
565
                TEST_NE_P (last_connection, NULL);
 
566
 
 
567
                TEST_NE_P (proxy, NULL);
 
568
                TEST_ALLOC_SIZE (proxy, sizeof (NihDBusProxy));
 
569
 
 
570
                TEST_NE_P (proxy->connection, NULL);
 
571
                TEST_EQ_P (proxy->name, NULL);
 
572
                TEST_EQ_P (proxy->owner, NULL);
 
573
                TEST_EQ_STR (proxy->path, DBUS_PATH_UPSTART);
 
574
                TEST_ALLOC_PARENT (proxy->path, proxy);
 
575
                TEST_FALSE (proxy->auto_start);
 
576
 
 
577
                TEST_EQ_P (proxy->lost_handler, NULL);
 
578
                TEST_EQ_P (proxy->data, NULL);
 
579
 
 
580
                nih_free (proxy);
 
581
 
 
582
                TEST_FILE_END (output);
 
583
                TEST_FILE_RESET (output);
 
584
 
 
585
                dbus_connection_close (last_connection);
 
586
                dbus_connection_unref (last_connection);
 
587
 
 
588
                dbus_server_disconnect (server);
 
589
                dbus_server_unref (server);
 
590
 
 
591
                dbus_shutdown ();
 
592
 
 
593
                unsetenv ("UPSTART_SESSION");
 
594
                user_mode = FALSE;
 
595
        }
 
596
 
511
597
 
512
598
        /* Check that we can create a connection to Upstart via the system
513
599
         * bus.  The returned proxy should use the default name on that
719
805
        }
720
806
 
721
807
 
 
808
        /* Check that when we attempt to connect to Upstart in user mode but
 
809
         * without UPSTART_SESSION set in the environment, an appropriate
 
810
         * error is output.
 
811
         */
 
812
        TEST_FEATURE ("with user-mode and no target");
 
813
        TEST_ALLOC_FAIL {
 
814
                use_dbus = -1;
 
815
                dbus_bus_type = -1;
 
816
                dest_name = NULL;
 
817
                dest_address = DBUS_ADDRESS_UPSTART;
 
818
                user_mode = TRUE;
 
819
 
 
820
                unsetenv ("UPSTART_SESSION");
 
821
 
 
822
                TEST_DIVERT_STDERR (output) {
 
823
                        proxy = upstart_open (NULL);
 
824
                }
 
825
                rewind (output);
 
826
 
 
827
                TEST_EQ_P (proxy, NULL);
 
828
 
 
829
                TEST_FILE_EQ (output, ("test: UPSTART_SESSION isn't set in the environment. "
 
830
                                       "Unable to locate the Upstart instance.\n"));
 
831
                TEST_FILE_END (output);
 
832
                TEST_FILE_RESET (output);
 
833
 
 
834
                dbus_shutdown ();
 
835
                user_mode = FALSE;
 
836
        }
 
837
 
722
838
        fclose (output);
723
839
}
724
840