summaryrefslogtreecommitdiff
path: root/usr/src/lib/fm/libfmd_msg/common/fmd_msg_test.c
blob: baa90276f0d009a30447088abaac8984c34f1e1c (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <sys/types.h>
#include <sys/wait.h>

#include <sys/fm/protocol.h>
#include <fm/fmd_msg.h>

#include <unistd.h>
#include <signal.h>
#include <strings.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

#define	TEST_ARR_SZ	2

int
main(int argc, char *argv[])
{
	fmd_msg_hdl_t *h;
	pid_t pid;
	int i, err = 0;
	char *s;

	nvlist_t *auth, *fmri, *list, *test_arr[TEST_ARR_SZ];
	const char *code = "TEST-8000-08";
	int64_t tod[] = { 0x9400000, 0 };

	if (argc > 1) {
		(void) fprintf(stderr, "Usage: %s\n", argv[0]);
		return (2);
	}

	/*
	 * Build up a valid list.suspect event for a fictional diagnosis
	 * using a diagnosis code from our test dictionary so we can format
	 * messages.
	 */
	if (nvlist_alloc(&auth, NV_UNIQUE_NAME, 0) != 0 ||
	    nvlist_alloc(&fmri, NV_UNIQUE_NAME, 0) != 0 ||
	    nvlist_alloc(&list, NV_UNIQUE_NAME, 0) != 0) {
		(void) fprintf(stderr, "%s: nvlist_alloc failed\n", argv[0]);
		return (1);
	}

	err |= nvlist_add_uint8(auth, FM_VERSION, FM_FMRI_AUTH_VERSION);
	err |= nvlist_add_string(auth, FM_FMRI_AUTH_PRODUCT, "product");
	err |= nvlist_add_string(auth, FM_FMRI_AUTH_PRODUCT_SN, "product_sn");
	err |= nvlist_add_string(auth, FM_FMRI_AUTH_CHASSIS, "chassis");
	err |= nvlist_add_string(auth, FM_FMRI_AUTH_DOMAIN, "domain");
	err |= nvlist_add_string(auth, FM_FMRI_AUTH_SERVER, "server");

	if (err != 0) {
		(void) fprintf(stderr, "%s: failed to build auth nvlist: %s\n",
		    argv[0], strerror(err));
		return (1);
	}

	err |= nvlist_add_uint8(fmri, FM_VERSION, FM_FMD_SCHEME_VERSION);
	err |= nvlist_add_string(fmri, FM_FMRI_SCHEME, FM_FMRI_SCHEME_FMD);
	err |= nvlist_add_nvlist(fmri, FM_FMRI_AUTHORITY, auth);
	err |= nvlist_add_string(fmri, FM_FMRI_FMD_NAME, "fmd_msg_test");
	err |= nvlist_add_string(fmri, FM_FMRI_FMD_VERSION, "1.0");

	if (err != 0) {
		(void) fprintf(stderr, "%s: failed to build fmri nvlist: %s\n",
		    argv[0], strerror(err));
		return (1);
	}

	err |= nvlist_add_uint8(list, FM_VERSION, FM_SUSPECT_VERSION);
	err |= nvlist_add_string(list, FM_CLASS, FM_LIST_SUSPECT_CLASS);
	err |= nvlist_add_string(list, FM_SUSPECT_UUID, "12345678");
	err |= nvlist_add_string(list, FM_SUSPECT_DIAG_CODE, code);
	err |= nvlist_add_int64_array(list, FM_SUSPECT_DIAG_TIME, tod, 2);
	err |= nvlist_add_nvlist(list, FM_SUSPECT_DE, fmri);
	err |= nvlist_add_uint32(list, FM_SUSPECT_FAULT_SZ, 0);

	/*
	 * Add a contrived nvlist array to our list.suspect so that we can
	 * exercise the expansion syntax for dereferencing nvlist array members
	 */
	for (i = 0; i < TEST_ARR_SZ; i++) {
		if (nvlist_alloc(&test_arr[i], NV_UNIQUE_NAME, 0) != 0) {
			(void) fprintf(stderr, "%s: failed to alloc nvlist "
			    "array: %s\n", argv[0], strerror(err));
			return (1);
		}
		err |= nvlist_add_uint8(test_arr[i], "index", i);
	}
	err |= nvlist_add_nvlist_array(list, "test_arr", test_arr, TEST_ARR_SZ);

	if (err != 0) {
		(void) fprintf(stderr, "%s: failed to build list nvlist: %s\n",
		    argv[0], strerror(err));
		return (1);
	}

	/*
	 * Now initialize the libfmd_msg library for testing, using the message
	 * catalogs found in the proto area of the current workspace.
	 */
	if ((h = fmd_msg_init(getenv("ROOT"), FMD_MSG_VERSION)) == NULL) {
		(void) fprintf(stderr, "%s: fmd_msg_init failed: %s\n",
		    argv[0], strerror(errno));
		return (1);
	}

	/*
	 * Test 0: Verify that both fmd_msg_getitem_id and fmd_msg_gettext_id
	 * return NULL and EINVAL for an illegal message code, and NULL
	 * and ENOENT for a valid but not defined message code.
	 */
	s = fmd_msg_getitem_id(h, NULL, "I_AM_NOT_VALID", 0);
	if (s != NULL || errno != EINVAL) {
		(void) fprintf(stderr, "%s: test0 FAIL: illegal code returned "
		    "s = %p, errno = %d\n", argv[0], (void *)s, errno);
		return (1);
	}

	s = fmd_msg_gettext_id(h, NULL, "I_AM_NOT_VALID");
	if (s != NULL || errno != EINVAL) {
		(void) fprintf(stderr, "%s: test0 FAIL: illegal code returned "
		    "s = %p, errno = %d\n", argv[0], (void *)s, errno);
		return (1);
	}

	s = fmd_msg_getitem_id(h, NULL, "I_AM_NOT_HERE-0000-0000", 0);
	if (s != NULL || errno != ENOENT) {
		(void) fprintf(stderr, "%s: test0 FAIL: missing code returned "
		    "s = %p, errno = %d\n", argv[0], (void *)s, errno);
		return (1);
	}

	s = fmd_msg_gettext_id(h, NULL, "I_AM_NOT_HERE-0000-0000");
	if (s != NULL || errno != ENOENT) {
		(void) fprintf(stderr, "%s: test0 FAIL: missing code returned "
		    "s = %p, errno = %d\n", argv[0], (void *)s, errno);
		return (1);
	}

	/*
	 * Test 1: Use fmd_msg_getitem_id to retrieve the item strings for
	 * a known message code without having any actual event handle.
	 */
	for (i = 0; i < FMD_MSG_ITEM_MAX; i++) {
		if ((s = fmd_msg_getitem_id(h, NULL, code, i)) == NULL) {
			(void) fprintf(stderr, "%s: fmd_msg_getitem_id failed "
			    "for %s, item %d: %s\n",
			    argv[0], code, i, strerror(errno));
		}

		(void) printf("code %s item %d = <<%s>>\n", code, i, s);
		free(s);
	}

	/*
	 * Test 2: Use fmd_msg_gettext_id to retrieve the complete message for
	 * a known message code without having any actual event handle.
	 */
	if ((s = fmd_msg_gettext_id(h, NULL, code)) == NULL) {
		(void) fprintf(stderr, "%s: fmd_msg_gettext_id failed for %s: "
		    "%s\n", argv[0], code, strerror(errno));
		return (1);
	}

	(void) printf("%s\n", s);
	free(s);

	/*
	 * Test 3: Use fmd_msg_getitem_nv to retrieve the item strings for
	 * our list.suspect event handle.
	 */
	for (i = 0; i < FMD_MSG_ITEM_MAX; i++) {
		if ((s = fmd_msg_getitem_nv(h, NULL, list, i)) == NULL) {
			(void) fprintf(stderr, "%s: fmd_msg_getitem_nv failed "
			    "for %s, item %d: %s\n",
			    argv[0], code, i, strerror(errno));
		}

		(void) printf("code %s item %d = <<%s>>\n", code, i, s);
		free(s);
	}

	/*
	 * Test 4: Use fmd_msg_getitem_nv to retrieve the complete message for
	 * a known message code using our list.suspect event handle.
	 */
	if ((s = fmd_msg_gettext_nv(h, NULL, list)) == NULL) {
		(void) fprintf(stderr, "%s: fmd_msg_gettext_nv failed for %s: "
		    "%s\n", argv[0], code, strerror(errno));
		return (1);
	}

	(void) printf("%s\n", s);
	free(s);

	/*
	 * Test 5: Use fmd_msg_getitem_nv to retrieve the complete message for
	 * a known message code using our list.suspect event handle, but this
	 * time set the URL to our own customized URL.  Our contrived message
	 * has been designed to exercise the key aspects of the variable
	 * expansion syntax.
	 */
	if (fmd_msg_url_set(h, "http://foo.bar.com/") != 0) {
		(void) fprintf(stderr, "%s: fmd_msg_url_set failed: %s\n",
		    argv[0], strerror(errno));
	}

	if ((s = fmd_msg_gettext_nv(h, NULL, list)) == NULL) {
		(void) fprintf(stderr, "%s: fmd_msg_gettext_nv failed for %s: "
		    "%s\n", argv[0], code, strerror(errno));
		return (1);
	}

	(void) printf("%s\n", s);
	free(s);

	for (i = 0; i < TEST_ARR_SZ; i++)
		nvlist_free(test_arr[i]);
	nvlist_free(fmri);
	nvlist_free(auth);
	nvlist_free(list);

	fmd_msg_fini(h);	/* free library state before dumping core */
	pid = fork();		/* fork into background to not bother make(1) */

	switch (pid) {
	case -1:
		(void) fprintf(stderr, "FAIL (failed to fork)\n");
		return (1);
	case 0:
		abort();
		return (1);
	}

	if (waitpid(pid, &err, 0) == -1) {
		(void) fprintf(stderr, "FAIL (failed to wait for %d: %s)\n",
		    (int)pid, strerror(errno));
		return (1);
	}

	if (WIFSIGNALED(err) == 0 || WTERMSIG(err) != SIGABRT) {
		(void) fprintf(stderr, "FAIL (child did not SIGABRT)\n");
		return (1);
	}

	if (!WCOREDUMP(err)) {
		(void) fprintf(stderr, "FAIL (no core generated)\n");
		return (1);
	}

	(void) fprintf(stderr, "done\n");
	return (0);
}