summaryrefslogtreecommitdiffstats
path: root/server/sbus/tests/test_client.c
blob: b2dce42606eee2c2a7f79b5d34017e7f806b47ba (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include "events.h"
#include "util/util.h"
#include "dbus/dbus.h"
#include "sbus/sssd_dbus.h"
#include "sbus_interfaces.h"

/* Identity */
#define TEST_CLIENT_NAME "testclient"
#define TEST_CLIENT_VERSION 1

struct test_cli_ctx {
    struct sbus_method_ctx *sd_ctx;
    /*DBusConnection *conn;*/
    struct event_context *ev;
    struct sbus_conn_ctx *dct_ctx;
};

static int provide_identity(DBusMessage *message, void *data, DBusMessage **r);

struct sbus_method monitor_service_methods [] = {
        {SERVICE_METHOD_IDENTITY, provide_identity},
        {NULL, NULL}
};

static void request_version_timed(struct test_cli_ctx *ctx);

static void print_version(DBusPendingCall *pending, void *data)
{
    DBusMessage *reply;
    DBusError error;
    const char *version_string;
    int type;

    dbus_error_init(&error);

    reply = dbus_pending_call_steal_reply(pending);
    if (!reply) {
        /* reply should never be null. This function shouldn't be called
         * until reply is valid. If reply is NULL here, something is seriously
         * wrong and we should bail out.
         */
        DEBUG(0, ("Serious error. A reply callback was called but no reply was received"));
        exit(3);
    }

    type = dbus_message_get_type(reply);

    switch (type) {
    case DBUS_MESSAGE_TYPE_METHOD_RETURN:
        if (dbus_message_get_args(reply, &error,
                                  DBUS_TYPE_STRING,
                                  &version_string,
                                  DBUS_TYPE_INVALID)) {
            fprintf(stdout, "Version: %s\n", version_string);
            fflush(stdout);
        } else {
            DEBUG(0, ("Error getting arguments in print_version"));
        }
        break;
    case DBUS_MESSAGE_TYPE_ERROR:
        
        if (strcmp(DBUS_ERROR_NO_REPLY, dbus_message_get_error_name(reply))==0) {
            DEBUG(0, ("Received error. Timeout"));
        }
        else {
            DEBUG(0, ("Received error. Not a timeout: %s", dbus_message_get_error_name(reply)));
        }
        break;
    default:
        DEBUG(0, ("Received unexpected message\n"));
        exit(4);
    }
}

static void test_timed_handler(struct event_context *ev,
                               struct timed_event *te,
                               struct timeval tv, void *data)
{
    struct test_cli_ctx *test_ctx;
    struct sbus_method_ctx *ctx;
    DBusPendingCall *pending_reply;
    DBusMessage *vmsg;
    DBusError error;
    dbus_bool_t dbret;

    test_ctx = talloc_get_type(data, struct test_cli_ctx);
    ctx = test_ctx->sd_ctx;

    fprintf(stdout, ".");
    fflush(stdout);

    dbus_error_init(&error);
    vmsg = dbus_message_new_method_call(NULL,
                                        ctx->path, ctx->interface,
                                        MONITOR_METHOD_VERSION);

    dbret = dbus_connection_send_with_reply(sbus_get_connection(test_ctx->dct_ctx), vmsg,
                                            &pending_reply, -1);
    if (!dbret) {
        /* Critical failure */
        DEBUG(0,("Failed to send version_request"));
        exit(2);
    }

    dbus_pending_call_set_notify(pending_reply, print_version, NULL, NULL);

    dbus_message_unref(vmsg);

    request_version_timed(test_ctx);
}

static void request_version_timed(struct test_cli_ctx *ctx)
{
    struct timed_event *te = NULL;
    struct timeval tv;

    gettimeofday(&tv, NULL);
    tv.tv_sec += 5;
    tv.tv_usec = 0;
    te = event_add_timed(ctx->ev, ctx, tv, test_timed_handler, ctx);
    if (te == NULL) {
        DEBUG(0, ("failed to add event!\n"));
        exit(1);
    }
}

int main (int argc, const char *argv[])
{
    struct event_context *event_ctx;
    struct sbus_method_ctx *ctx;
    struct test_cli_ctx *test_ctx;
    struct sbus_method_ctx *service_methods;
    int ret;

    event_ctx = event_context_init(talloc_autofree_context());
    if (!event_ctx) {
        printf("Out of memory!?\n");
        exit(1);
    }

    ctx = talloc_zero(event_ctx, struct sbus_method_ctx);
    if (!ctx) {
        printf("Out of memory!?\n");
        exit(1);
    }

    test_ctx = talloc(event_ctx, struct test_cli_ctx);
    if (!test_ctx) {
        printf("Out of memory!?\n");
        exit(1);
    }

    test_ctx->ev = event_ctx;
    ctx->interface = talloc_strdup(ctx, MONITOR_DBUS_INTERFACE);
    ctx->path = talloc_strdup(ctx, MONITOR_DBUS_PATH);
    if (!ctx->interface || !ctx->path) {
        printf("Out of memory!?\n");
        exit(1);
    }

    ret = sbus_new_connection(test_ctx, test_ctx->ev,
                              DEFAULT_SBUS_ADDRESS,
                              &test_ctx->dct_ctx, NULL);
    if (ret != EOK) {
        exit(1);
    }

    test_ctx->sd_ctx = ctx;

    dbus_connection_set_exit_on_disconnect(sbus_get_connection(test_ctx->dct_ctx), TRUE);

    /* Set up a timed event to request the server version every
     * five seconds and print it to the screen.
     */
    request_version_timed(test_ctx);
    
    /* Set up handler for service methods */
    service_methods = talloc_zero(test_ctx, struct sbus_method_ctx);
    service_methods->interface = talloc_strdup(service_methods, SERVICE_INTERFACE);
    service_methods->path = talloc_strdup(service_methods, SERVICE_PATH);
    service_methods->methods = monitor_service_methods;
    service_methods->message_handler = sbus_message_handler;
    sbus_conn_add_method_ctx(test_ctx->dct_ctx, service_methods);

    /* Enter the main loop (and hopefully never return) */
    event_loop_wait(event_ctx);

    talloc_free(event_ctx);
    return EXIT_SUCCESS;
}

static int provide_identity(DBusMessage *message, void *data, DBusMessage **r) {
    const char *name = TEST_CLIENT_NAME;
    dbus_uint16_t version = TEST_CLIENT_VERSION;
    
    DBusMessage *reply;
    dbus_bool_t ret;

    reply = dbus_message_new_method_return(message);
    ret = dbus_message_append_args(reply, 
            DBUS_TYPE_STRING, &name,
            DBUS_TYPE_UINT16, &version,
            DBUS_TYPE_INVALID);

    if (!ret) {
        return EIO;
    }

    *r = reply;
    return EOK;
}