backup: Wire up qemu full pull backup commands over QMP
[libvirt/ericb.git] / tests / virnetserverclienttest.c
blobcb5071b2d987daaa554daba624cbc0b63b745b57
1 /*
2 * Copyright (C) 2013 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
19 #include <config.h>
21 #include "testutils.h"
22 #include "virerror.h"
23 #include "rpc/virnetserverclient.h"
25 #define VIR_FROM_THIS VIR_FROM_RPC
27 #ifdef HAVE_SOCKETPAIR
29 static void *
30 testClientNew(virNetServerClientPtr client ATTRIBUTE_UNUSED,
31 void *opaque ATTRIBUTE_UNUSED)
33 char *dummy;
35 if (VIR_ALLOC(dummy) < 0)
36 return NULL;
38 return dummy;
42 static void
43 testClientFree(void *opaque)
45 VIR_FREE(opaque);
48 static int testIdentity(const void *opaque ATTRIBUTE_UNUSED)
50 int sv[2];
51 int ret = -1;
52 virNetSocketPtr sock = NULL;
53 virNetServerClientPtr client = NULL;
54 virIdentityPtr ident = NULL;
55 const char *gotUsername = NULL;
56 const char *gotUserID = NULL;
57 const char *gotGroupname = NULL;
58 const char *gotGroupID = NULL;
59 const char *gotSELinuxContext = NULL;
61 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) < 0) {
62 virReportSystemError(errno, "%s",
63 "Cannot create socket pair");
64 return -1;
67 if (virNetSocketNewConnectSockFD(sv[0], &sock) < 0) {
68 virDispatchError(NULL);
69 goto cleanup;
71 sv[0] = -1;
73 if (!(client = virNetServerClientNew(1, sock, 0, false, 1,
74 NULL,
75 testClientNew,
76 NULL,
77 testClientFree,
78 NULL))) {
79 virDispatchError(NULL);
80 goto cleanup;
83 if (!(ident = virNetServerClientGetIdentity(client))) {
84 fprintf(stderr, "Failed to create identity\n");
85 goto cleanup;
88 if (virIdentityGetAttr(ident,
89 VIR_IDENTITY_ATTR_UNIX_USER_NAME,
90 &gotUsername) < 0) {
91 fprintf(stderr, "Missing username in identity\n");
92 goto cleanup;
94 if (STRNEQ_NULLABLE("astrochicken", gotUsername)) {
95 fprintf(stderr, "Want username 'astrochicken' got '%s'\n",
96 NULLSTR(gotUsername));
97 goto cleanup;
100 if (virIdentityGetAttr(ident,
101 VIR_IDENTITY_ATTR_UNIX_USER_ID,
102 &gotUserID) < 0) {
103 fprintf(stderr, "Missing user ID in identity\n");
104 goto cleanup;
106 if (STRNEQ_NULLABLE("666", gotUserID)) {
107 fprintf(stderr, "Want username '666' got '%s'\n",
108 NULLSTR(gotUserID));
109 goto cleanup;
112 if (virIdentityGetAttr(ident,
113 VIR_IDENTITY_ATTR_UNIX_GROUP_NAME,
114 &gotGroupname) < 0) {
115 fprintf(stderr, "Missing groupname in identity\n");
116 goto cleanup;
118 if (STRNEQ_NULLABLE("fictionalusers", gotGroupname)) {
119 fprintf(stderr, "Want groupname 'fictionalusers' got '%s'\n",
120 NULLSTR(gotGroupname));
121 goto cleanup;
124 if (virIdentityGetAttr(ident,
125 VIR_IDENTITY_ATTR_UNIX_GROUP_ID,
126 &gotGroupID) < 0) {
127 fprintf(stderr, "Missing group ID in identity\n");
128 goto cleanup;
130 if (STRNEQ_NULLABLE("7337", gotGroupID)) {
131 fprintf(stderr, "Want groupname '7337' got '%s'\n",
132 NULLSTR(gotGroupID));
133 goto cleanup;
136 if (virIdentityGetAttr(ident,
137 VIR_IDENTITY_ATTR_SELINUX_CONTEXT,
138 &gotSELinuxContext) < 0) {
139 fprintf(stderr, "Missing SELinux context in identity\n");
140 goto cleanup;
142 if (STRNEQ_NULLABLE("foo_u:bar_r:wizz_t:s0-s0:c0.c1023", gotSELinuxContext)) {
143 fprintf(stderr, "Want groupname 'foo_u:bar_r:wizz_t:s0-s0:c0.c1023' got '%s'\n",
144 NULLSTR(gotGroupID));
145 goto cleanup;
148 ret = 0;
149 cleanup:
150 virObjectUnref(sock);
151 if (client)
152 virNetServerClientClose(client);
153 virObjectUnref(client);
154 virObjectUnref(ident);
155 VIR_FORCE_CLOSE(sv[0]);
156 VIR_FORCE_CLOSE(sv[1]);
157 return ret;
161 static int
162 mymain(void)
164 int ret = 0;
167 if (virTestRun("Identity",
168 testIdentity, NULL) < 0)
169 ret = -1;
171 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
173 VIR_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/virnetserverclientmock.so")
174 #else
175 static int
176 mymain(void)
178 return EXIT_AM_SKIP;
180 VIR_TEST_MAIN(mymain);
181 #endif