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