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/>.
21 #include "testutils.h"
23 #include "rpc/virnetserverclient.h"
25 #define VIR_FROM_THIS VIR_FROM_RPC
27 #ifdef HAVE_SOCKETPAIR
30 testClientNew(virNetServerClientPtr client ATTRIBUTE_UNUSED
,
31 void *opaque ATTRIBUTE_UNUSED
)
35 if (VIR_ALLOC(dummy
) < 0)
43 testClientFree(void *opaque
)
48 static int testIdentity(const void *opaque ATTRIBUTE_UNUSED
)
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");
67 if (virNetSocketNewConnectSockFD(sv
[0], &sock
) < 0) {
68 virDispatchError(NULL
);
73 if (!(client
= virNetServerClientNew(1, sock
, 0, false, 1,
79 virDispatchError(NULL
);
83 if (!(ident
= virNetServerClientGetIdentity(client
))) {
84 fprintf(stderr
, "Failed to create identity\n");
88 if (virIdentityGetAttr(ident
,
89 VIR_IDENTITY_ATTR_UNIX_USER_NAME
,
91 fprintf(stderr
, "Missing username in identity\n");
94 if (STRNEQ_NULLABLE("astrochicken", gotUsername
)) {
95 fprintf(stderr
, "Want username 'astrochicken' got '%s'\n",
96 NULLSTR(gotUsername
));
100 if (virIdentityGetAttr(ident
,
101 VIR_IDENTITY_ATTR_UNIX_USER_ID
,
103 fprintf(stderr
, "Missing user ID in identity\n");
106 if (STRNEQ_NULLABLE("666", gotUserID
)) {
107 fprintf(stderr
, "Want username '666' got '%s'\n",
112 if (virIdentityGetAttr(ident
,
113 VIR_IDENTITY_ATTR_UNIX_GROUP_NAME
,
114 &gotGroupname
) < 0) {
115 fprintf(stderr
, "Missing groupname in identity\n");
118 if (STRNEQ_NULLABLE("fictionalusers", gotGroupname
)) {
119 fprintf(stderr
, "Want groupname 'fictionalusers' got '%s'\n",
120 NULLSTR(gotGroupname
));
124 if (virIdentityGetAttr(ident
,
125 VIR_IDENTITY_ATTR_UNIX_GROUP_ID
,
127 fprintf(stderr
, "Missing group ID in identity\n");
130 if (STRNEQ_NULLABLE("7337", gotGroupID
)) {
131 fprintf(stderr
, "Want groupname '7337' got '%s'\n",
132 NULLSTR(gotGroupID
));
136 if (virIdentityGetAttr(ident
,
137 VIR_IDENTITY_ATTR_SELINUX_CONTEXT
,
138 &gotSELinuxContext
) < 0) {
139 fprintf(stderr
, "Missing SELinux context in identity\n");
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
));
150 virObjectUnref(sock
);
152 virNetServerClientClose(client
);
153 virObjectUnref(client
);
154 virObjectUnref(ident
);
155 VIR_FORCE_CLOSE(sv
[0]);
156 VIR_FORCE_CLOSE(sv
[1]);
167 if (virTestRun("Identity",
168 testIdentity
, NULL
) < 0)
171 return ret
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
173 VIR_TEST_MAIN_PRELOAD(mymain
, abs_builddir
"/.libs/virnetserverclientmock.so")
180 VIR_TEST_MAIN(mymain
);