1/*
2* Copyright (c) 2004, Bull S.A..  All rights reserved.
3* Created by: Sebastien Decugis
4
5* This program is free software; you can redistribute it and/or modify it
6* under the terms of version 2 of the GNU General Public License as
7* published by the Free Software Foundation.
8*
9* This program is distributed in the hope that it would be useful, but
10* WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12*
13* You should have received a copy of the GNU General Public License along
14* with this program; if not, write the Free Software Foundation, Inc., 59
15* Temple Place - Suite 330, Boston MA 02111-1307, USA.
16
17
18* This sample test aims to check the following assertion:
19*
20* The child process gets a copy of the parent message catalog descriptor.
21
22
23* The steps are:
24* -> Create a message catalog file from the "messcat_src.txt" file
25* -> Open this catalog
26* -> fork
27* -> Check that the child can read from the message catalog.
28
29* The test fails if the message catalog is read in the parent and not in the child.
30
31*/
32
33
34/* We are testing conformance to IEEE Std 1003.1, 2003 Edition */
35#define _POSIX_C_SOURCE 200112L
36
37/********************************************************************************************/
38/****************************** standard includes *****************************************/
39/********************************************************************************************/
40#include <pthread.h>
41 #include <stdarg.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46
47#include <sys/wait.h>
48 #include <errno.h>
49
50#include <nl_types.h>
51
52/********************************************************************************************/
53/******************************   Test framework   *****************************************/
54/********************************************************************************************/
55#include "testfrmw.h"
56 #include "testfrmw.c"
57/* This header is responsible for defining the following macros:
58 * UNRESOLVED(ret, descr);
59 *    where descr is a description of the error and ret is an int (error code for example)
60 * FAILED(descr);
61 *    where descr is a short text saying why the test has failed.
62 * PASSED();
63 *    No parameter.
64 *
65 * Both three macros shall terminate the calling process.
66 * The testcase shall not terminate in any other maneer.
67 *
68 * The other file defines the functions
69 * void output_init()
70 * void output(char * string, ...)
71 *
72 * Those may be used to output information.
73 */
74
75/********************************************************************************************/
76/********************************** Configuration ******************************************/
77/********************************************************************************************/
78#ifndef VERBOSE
79#define VERBOSE 1
80#endif
81
82#define PATH_OFFSET "conformance/interfaces/fork/"
83
84/********************************************************************************************/
85/***********************************    Test case   *****************************************/
86/********************************************************************************************/
87
88void read_catalog( nl_catd cat, char * who )
89{
90	char * msg = NULL;
91	int i, j;
92	errno = 0;
93
94#if VERBOSE > 0
95
96	output( "Reading the message catalog from %s...\n", who );
97#endif
98
99	for ( i = 1; i <= 2; i++ )
100	{
101		for ( j = 1; j <= 2; j++ )
102		{
103			msg = catgets( cat, i, j, "not found" );
104
105			if ( errno != 0 )
106			{
107				UNRESOLVED( errno, "catgets returned an error" );
108			}
109
110#if VERBOSE > 1
111			output( "set %i msg %i: %s\n", i, j, msg );
112
113#endif
114
115		}
116	}
117
118#if VERBOSE > 0
119	output( "Message catalog read successfully in %s\n", who );
120
121#endif
122}
123
124/* The main test function. */
125int main( int argc, char * argv[] )
126{
127	int ret, status;
128	pid_t child, ctl;
129
130	nl_catd messcat;
131
132	/* Initialize output */
133	output_init();
134
135
136	/* Generate the message catalog file from the text sourcefile */
137
138	if ( system( NULL ) )
139	{
140		ret = system( "gencat mess.cat " PATH_OFFSET "messcat_src.txt" );
141
142		if ( ret != 0 )
143		{
144			output( "Unable to find messcat_src.txt in standard directory %s\n", PATH_OFFSET );
145			output( "Trying local dir\n" );
146			ret = system( "gencat mess.cat messcat_src.txt" );
147
148			if ( ret != 0 )
149			{
150				output( "Could not find the source file for message catalog.\n" \
151				        "You may need to execute gencat yourself.\n" );
152			}
153		}
154	}
155
156	/* Try opening the message catalog file */
157	messcat = catopen( "./mess.cat", 0 );
158
159	if ( messcat == ( nl_catd ) - 1 )
160	{
161		UNRESOLVED( errno, "Could not open ./mess.cat. You may need to do a gencat before executing this testcase" );
162	}
163
164	/* Read the message catalog */
165	read_catalog( messcat, "parent" );
166
167
168	/* Create the child */
169	child = fork();
170
171	if ( child == ( pid_t ) - 1 )
172	{
173		UNRESOLVED( errno, "Failed to fork" );
174	}
175
176	/* child */
177	if ( child == ( pid_t ) 0 )
178	{
179		read_catalog( messcat, "child" );
180
181		/* We're done */
182		exit( PTS_PASS );
183	}
184
185	/* Parent joins the child */
186	ctl = waitpid( child, &status, 0 );
187
188	if ( ctl != child )
189	{
190		UNRESOLVED( errno, "Waitpid returned the wrong PID" );
191	}
192
193	if ( ( !WIFEXITED( status ) ) || ( WEXITSTATUS( status ) != PTS_PASS ) )
194	{
195		FAILED( "Child exited abnormally" );
196	}
197
198	/* We can now clean up the message catalog file */
199	ret = catclose( messcat );
200
201	if ( ret != 0 )
202	{
203		UNRESOLVED( errno, "Failed to close the message catalog" );
204	}
205
206	/* Try removing the message catalog file */
207	system( "rm -f mess.cat" );
208
209	/* Test passed */
210#if VERBOSE > 0
211
212	output( "Test passed\n" );
213
214#endif
215
216	PASSED;
217}
218
219
220