xref: /titanic_41/usr/src/cmd/sendmail/libsm/t-exc.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
3*7c478bd9Sstevel@tonic-gate  *	All rights reserved.
4*7c478bd9Sstevel@tonic-gate  *
5*7c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
6*7c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
7*7c478bd9Sstevel@tonic-gate  * the sendmail distribution.
8*7c478bd9Sstevel@tonic-gate  */
9*7c478bd9Sstevel@tonic-gate 
10*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
11*7c478bd9Sstevel@tonic-gate 
12*7c478bd9Sstevel@tonic-gate #include <sm/gen.h>
13*7c478bd9Sstevel@tonic-gate SM_IDSTR(id, "@(#)$Id: t-exc.c,v 1.18 2001/07/05 22:46:35 gshapiro Exp $")
14*7c478bd9Sstevel@tonic-gate 
15*7c478bd9Sstevel@tonic-gate #include <string.h>
16*7c478bd9Sstevel@tonic-gate #include <sm/heap.h>
17*7c478bd9Sstevel@tonic-gate #include <sm/io.h>
18*7c478bd9Sstevel@tonic-gate #include <sm/test.h>
19*7c478bd9Sstevel@tonic-gate 
20*7c478bd9Sstevel@tonic-gate const SM_EXC_TYPE_T EtypeTest1 =
21*7c478bd9Sstevel@tonic-gate {
22*7c478bd9Sstevel@tonic-gate 	SmExcTypeMagic,
23*7c478bd9Sstevel@tonic-gate 	"E:test1",
24*7c478bd9Sstevel@tonic-gate 	"i",
25*7c478bd9Sstevel@tonic-gate 	sm_etype_printf,
26*7c478bd9Sstevel@tonic-gate 	"test1 exception argv[0]=%0",
27*7c478bd9Sstevel@tonic-gate };
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate const SM_EXC_TYPE_T EtypeTest2 =
30*7c478bd9Sstevel@tonic-gate {
31*7c478bd9Sstevel@tonic-gate 	SmExcTypeMagic,
32*7c478bd9Sstevel@tonic-gate 	"E:test2",
33*7c478bd9Sstevel@tonic-gate 	"i",
34*7c478bd9Sstevel@tonic-gate 	sm_etype_printf,
35*7c478bd9Sstevel@tonic-gate 	"test2 exception argv[0]=%0",
36*7c478bd9Sstevel@tonic-gate };
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate int
main(argc,argv)39*7c478bd9Sstevel@tonic-gate main(argc, argv)
40*7c478bd9Sstevel@tonic-gate 	int argc;
41*7c478bd9Sstevel@tonic-gate 	char **argv;
42*7c478bd9Sstevel@tonic-gate {
43*7c478bd9Sstevel@tonic-gate 	void *p;
44*7c478bd9Sstevel@tonic-gate 	int volatile x;
45*7c478bd9Sstevel@tonic-gate 	char *unknown, *cant;
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate 	sm_test_begin(argc, argv, "test exception handling");
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate 	/*
50*7c478bd9Sstevel@tonic-gate 	**  SM_TRY
51*7c478bd9Sstevel@tonic-gate 	*/
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate 	cant = "can't happen";
54*7c478bd9Sstevel@tonic-gate 	x = 0;
55*7c478bd9Sstevel@tonic-gate 	SM_TRY
56*7c478bd9Sstevel@tonic-gate 		x = 1;
57*7c478bd9Sstevel@tonic-gate 	SM_END_TRY
58*7c478bd9Sstevel@tonic-gate 	SM_TEST(x == 1);
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 	/*
61*7c478bd9Sstevel@tonic-gate 	**  SM_FINALLY-0
62*7c478bd9Sstevel@tonic-gate 	*/
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 	x = 0;
65*7c478bd9Sstevel@tonic-gate 	SM_TRY
66*7c478bd9Sstevel@tonic-gate 		x = 1;
67*7c478bd9Sstevel@tonic-gate 	SM_FINALLY
68*7c478bd9Sstevel@tonic-gate 		x = 2;
69*7c478bd9Sstevel@tonic-gate 	SM_END_TRY
70*7c478bd9Sstevel@tonic-gate 	SM_TEST(x == 2);
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	/*
73*7c478bd9Sstevel@tonic-gate 	**  SM_FINALLY-1
74*7c478bd9Sstevel@tonic-gate 	*/
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	x = 0;
77*7c478bd9Sstevel@tonic-gate 	SM_TRY
78*7c478bd9Sstevel@tonic-gate 		SM_TRY
79*7c478bd9Sstevel@tonic-gate 			x = 1;
80*7c478bd9Sstevel@tonic-gate 			sm_exc_raisenew_x(&EtypeTest1, 17);
81*7c478bd9Sstevel@tonic-gate 		SM_FINALLY
82*7c478bd9Sstevel@tonic-gate 			x = 2;
83*7c478bd9Sstevel@tonic-gate 			sm_exc_raisenew_x(&EtypeTest2, 42);
84*7c478bd9Sstevel@tonic-gate 		SM_END_TRY
85*7c478bd9Sstevel@tonic-gate 	SM_EXCEPT(exc, "E:test2")
86*7c478bd9Sstevel@tonic-gate 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
87*7c478bd9Sstevel@tonic-gate 			 "got exception test2: can't happen\n");
88*7c478bd9Sstevel@tonic-gate 	SM_EXCEPT(exc, "E:test1")
89*7c478bd9Sstevel@tonic-gate 		SM_TEST(x == 2 && exc->exc_argv[0].v_int == 17);
90*7c478bd9Sstevel@tonic-gate 		if (!(x == 2 && exc->exc_argv[0].v_int == 17))
91*7c478bd9Sstevel@tonic-gate 		{
92*7c478bd9Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
93*7c478bd9Sstevel@tonic-gate 				"can't happen: x=%d argv[0]=%d\n",
94*7c478bd9Sstevel@tonic-gate 				x, exc->exc_argv[0].v_int);
95*7c478bd9Sstevel@tonic-gate 		}
96*7c478bd9Sstevel@tonic-gate 	SM_EXCEPT(exc, "*")
97*7c478bd9Sstevel@tonic-gate 	{
98*7c478bd9Sstevel@tonic-gate 		unknown = "unknown exception: ";
99*7c478bd9Sstevel@tonic-gate 		SM_TEST(strcmp(unknown, cant) == 0);
100*7c478bd9Sstevel@tonic-gate 	}
101*7c478bd9Sstevel@tonic-gate 	SM_END_TRY
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	x = 3;
104*7c478bd9Sstevel@tonic-gate 	SM_TRY
105*7c478bd9Sstevel@tonic-gate 		x = 4;
106*7c478bd9Sstevel@tonic-gate 		sm_exc_raisenew_x(&EtypeTest1, 94);
107*7c478bd9Sstevel@tonic-gate 	SM_FINALLY
108*7c478bd9Sstevel@tonic-gate 		x = 5;
109*7c478bd9Sstevel@tonic-gate 		sm_exc_raisenew_x(&EtypeTest2, 95);
110*7c478bd9Sstevel@tonic-gate 	SM_EXCEPT(exc, "E:test2")
111*7c478bd9Sstevel@tonic-gate 	{
112*7c478bd9Sstevel@tonic-gate 		unknown = "got exception test2: ";
113*7c478bd9Sstevel@tonic-gate 		SM_TEST(strcmp(unknown, cant) == 0);
114*7c478bd9Sstevel@tonic-gate 	}
115*7c478bd9Sstevel@tonic-gate 	SM_EXCEPT(exc, "E:test1")
116*7c478bd9Sstevel@tonic-gate 		SM_TEST(x == 5 && exc->exc_argv[0].v_int == 94);
117*7c478bd9Sstevel@tonic-gate 		if (!(x == 5 && exc->exc_argv[0].v_int == 94))
118*7c478bd9Sstevel@tonic-gate 		{
119*7c478bd9Sstevel@tonic-gate 			(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
120*7c478bd9Sstevel@tonic-gate 				"can't happen: x=%d argv[0]=%d\n",
121*7c478bd9Sstevel@tonic-gate 				x, exc->exc_argv[0].v_int);
122*7c478bd9Sstevel@tonic-gate 		}
123*7c478bd9Sstevel@tonic-gate 	SM_EXCEPT(exc, "*")
124*7c478bd9Sstevel@tonic-gate 	{
125*7c478bd9Sstevel@tonic-gate 		unknown = "unknown exception: ";
126*7c478bd9Sstevel@tonic-gate 		SM_TEST(strcmp(unknown, cant) == 0);
127*7c478bd9Sstevel@tonic-gate 	}
128*7c478bd9Sstevel@tonic-gate 	SM_END_TRY
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	SM_TRY
131*7c478bd9Sstevel@tonic-gate 		sm_exc_raisenew_x(&SmEtypeErr, "test %d", 0);
132*7c478bd9Sstevel@tonic-gate 	SM_EXCEPT(exc, "*")
133*7c478bd9Sstevel@tonic-gate #if DEBUG
134*7c478bd9Sstevel@tonic-gate 		(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
135*7c478bd9Sstevel@tonic-gate 			"test 0 got an exception, as expected:\n");
136*7c478bd9Sstevel@tonic-gate 		sm_exc_print(exc, smioout);
137*7c478bd9Sstevel@tonic-gate #endif /* DEBUG */
138*7c478bd9Sstevel@tonic-gate 		return sm_test_end();
139*7c478bd9Sstevel@tonic-gate 	SM_END_TRY
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	p = sm_malloc_x((size_t)(-1));
142*7c478bd9Sstevel@tonic-gate 	(void) sm_io_fprintf(smioout, SM_TIME_DEFAULT,
143*7c478bd9Sstevel@tonic-gate 		"sm_malloc_x unexpectedly succeeded, returning %p\n", p);
144*7c478bd9Sstevel@tonic-gate 	unknown = "sm_malloc_x unexpectedly succeeded";
145*7c478bd9Sstevel@tonic-gate 	SM_TEST(strcmp(unknown, cant) == 0);
146*7c478bd9Sstevel@tonic-gate 	return sm_test_end();
147*7c478bd9Sstevel@tonic-gate }
148