1 : /*
2 : * This file is part of MIN Test Framework. Copyright © 2008 Nokia Corporation
3 : * and/or its subsidiary(-ies).
4 : * Contact: Konrad Marek Zapalowicz
5 : * Contact e-mail: DG.MIN-Support@nokia.com
6 : *
7 : * This program is free software: you can redistribute it and/or modify it
8 : * under the terms of the GNU General Public License as published by the Free
9 : * Software Foundation, version 2 of the License.
10 : *
11 : * This program is distributed in the hope that it will be useful, but WITHOUT
12 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 : * more details. You should have received a copy of the GNU General Public
15 : * License along with this program. If not, see
16 : * <http://www.gnu.org/licenses/>.
17 : */
18 :
19 :
20 : /**
21 : * @file tc.c
22 : * @version 0.1
23 : * @brief This file contains implementation of the tc
24 : * test module of MIN Test Framework.
25 : */
26 :
27 : /* ------------------------------------------------------------------------- */
28 : /* INCLUDE FILES */
29 : #include "tc.h"
30 :
31 : /* ------------------------------------------------------------------------- */
32 : /* EXTERNAL DATA STRUCTURES */
33 : /* None */
34 :
35 : /* ------------------------------------------------------------------------- */
36 : /* EXTERNAL GLOBAL VARIABLES */
37 : extern char *module_date;
38 : extern char *module_time;
39 : /* ------------------------------------------------------------------------- */
40 : /* EXTERNAL FUNCTION PROTOTYPES */
41 : /* None */
42 :
43 : /* ------------------------------------------------------------------------- */
44 : /* GLOBAL VARIABLES */
45 : TTestModuleType module_type = ETestClass;
46 : unsigned int module_version = 200907;
47 : /* ------------------------------------------------------------------------- */
48 : /* CONSTANTS */
49 : /* None */
50 :
51 : /* ------------------------------------------------------------------------- */
52 : /* MACROS */
53 : /* None */
54 :
55 : /* ------------------------------------------------------------------------- */
56 : /* LOCAL GLOBAL VARIABLES */
57 : DLList *variables;
58 : /* ------------------------------------------------------------------------- */
59 : /* LOCAL CONSTANTS AND MACROS */
60 : /* None */
61 :
62 : /* ------------------------------------------------------------------------- */
63 : /* MODULE DATA STRUCTURES */
64 : /* None */
65 :
66 : /* ------------------------------------------------------------------------- */
67 : /* LOCAL FUNCTION PROTOTYPES */
68 : /* ------------------------------------------------------------------------- */
69 : /** Used for finding callname. */
70 : int _look4callname (const void *a, const void *b);
71 : /** Initialize scripter local variables */
72 : int _init_vars ();
73 : /** Send the variables back to scripter */
74 : int _send_vars ();
75 : /** Find the variable from list */
76 : ScriptVariable *_find_var (const char *varname);
77 :
78 : /* ------------------------------------------------------------------------- */
79 : /* FORWARD DECLARATIONS */
80 : /* None */
81 :
82 : /* ==================== LOCAL FUNCTIONS ==================================== */
83 : /* ------------------------------------------------------------------------- */
84 : int _look4callname (const void *a, const void *b)
85 99 : {
86 99 : TestCaseInfoTC *tci = (TestCaseInfoTC *) a;
87 99 : return strcmp (tci->name_, (char *)b);
88 : }
89 : /* ------------------------------------------------------------------------- */
90 : int _init_vars ()
91 27 : {
92 : void *shmaddr;
93 : unsigned int size;
94 27 : char *var_buff, *variable_str = INITPTR, *p;
95 27 : int retval = ENOERR;
96 : int shmid;
97 : MinItemParser *mip;
98 : ScriptVariable *variable;
99 :
100 27 : variables = dl_list_create();
101 27 : shmid = shmget (getppid(), sizeof (unsigned int), 0660);
102 27 : if (shmid == -1) {
103 0 : MIN_WARN ("Could not create shared memory segment");
104 0 : return -1;
105 : }
106 :
107 27 : shmaddr = sm_attach (shmid);
108 27 : if (shmaddr == INITPTR) {
109 0 : MIN_WARN ("Could not attach to shared memory segment");
110 0 : return -1;
111 : }
112 :
113 27 : retval = sm_read (shmaddr, &size, sizeof (unsigned int));
114 27 : if (retval != ENOERR) {
115 0 : MIN_WARN ("Read from shared memory segment failed");
116 0 : retval = -1;
117 0 : goto exit;
118 27 : } else if (size == 0) {
119 0 : MIN_WARN ("No variables in shared memory segment");
120 0 : goto exit;
121 : }
122 : /*
123 : ** Read the variable buffer from shared memory
124 : */
125 27 : var_buff = NEW2 (char, size + 1 );
126 27 : sm_read (shmaddr + sizeof (unsigned int), var_buff, size);
127 27 : var_buff [size] = '\0';
128 :
129 : /*
130 : ** Create Item Parser for variable parsing
131 : */
132 27 : mip = mip_create (var_buff, 0, size);
133 27 : if (mip == INITPTR) {
134 0 : MIN_WARN ("Failed to create item parser "
135 : "from variables");
136 :
137 0 : retval = -1;
138 0 : goto exit;
139 : }
140 : /*
141 : ** Parse the variables and save them to variable list
142 : */
143 27 : mip_get_string (mip, INITPTR, &variable_str);
144 209 : while (variable_str != INITPTR) {
145 182 : if ((p = strrchr (variable_str, '='))) {
146 : /*
147 : ** initialized variable
148 : */
149 182 : *p = '\0';
150 182 : p++;
151 182 : if (_find_var (variable_str) != INITPTR) {
152 0 : MIN_WARN ("variable with name %s "
153 : "already exists", variable_str);
154 0 : goto loopend;
155 : }
156 182 : variable = NEW (ScriptVariable);
157 182 : variable->var_name_ = NEW2 (char,
158 : strlen (variable_str) +1);
159 182 : strcpy (variable->var_name_, variable_str);
160 182 : if (strlen (p) > 0) {
161 182 : variable->is_initialized_ = ESTrue;
162 182 : variable->var_value_ = NEW2 (char, strlen(p)
163 : + 1);
164 182 : strcpy (variable->var_value_, p);
165 : } else {
166 0 : variable->is_initialized_ = ESFalse;
167 :
168 0 : MIN_WARN ("missing variable value (%s)",
169 : variable_str);
170 : }
171 : } else {
172 : /*
173 : ** unitialized variable
174 : */
175 0 : if (_find_var (variable_str) != INITPTR) {
176 0 : MIN_WARN ("variable with name %s already "
177 : "exists", variable_str);
178 0 : goto loopend;
179 : }
180 0 : variable = NEW (ScriptVariable);
181 0 : variable->is_initialized_ = ESFalse;
182 0 : variable->var_name_ = NEW2 (char,
183 : strlen (variable_str) +1);
184 0 : strcpy (variable->var_name_, variable_str);
185 :
186 : }
187 182 : dl_list_add (variables, variable);
188 182 : loopend:
189 182 : if (mip_get_next_string (mip, &variable_str) != ENOERR)
190 27 : break;
191 : }
192 :
193 27 : DELETE (var_buff);
194 27 : DELETE (variable_str);
195 27 : mip_destroy (&mip);
196 27 : exit:
197 :
198 27 : sm_detach (shmaddr);
199 :
200 27 : return retval;
201 : }
202 : /* ------------------------------------------------------------------------- */
203 : int _send_vars ()
204 27 : {
205 : DLListIterator it;
206 : ScriptVariable *var;
207 : Text *vars;
208 : int shmid;
209 27 : void *sh_mem_handle = NULL, *tmp;
210 :
211 :
212 27 : shmid = shmget (getppid(), sizeof (unsigned int), 0660);
213 27 : if (shmid == -1) {
214 0 : MIN_WARN ("Could not create shared memory segment");
215 0 : goto exit;
216 : }
217 :
218 27 : vars = tx_create (INITPTR);
219 :
220 236 : for (it = dl_list_head(variables); it != DLListNULLIterator;
221 182 : it = dl_list_next(it)) {
222 182 : var = dl_list_data (it);
223 : /*
224 : ** We only send back variables with a value
225 : */
226 182 : if (var->is_initialized_ == ESFalse)
227 0 : continue;
228 182 : tx_c_append (vars, var->var_name_);
229 182 : tx_c_append (vars, "=");
230 182 : tx_c_append (vars, var->var_value_);
231 182 : tx_c_append (vars, " ");
232 : }
233 :
234 27 : sh_mem_handle = sm_attach (shmid);
235 27 : if ((vars->size_ + sizeof (unsigned int)) > sm_get_segsz (shmid)) {
236 0 : tx_destroy (&vars);
237 0 : return 1;
238 : }
239 :
240 :
241 27 : sm_write (sh_mem_handle,
242 : (void *)&vars->size_,
243 : sizeof (unsigned int));
244 27 : tmp = sh_mem_handle + sizeof (unsigned int);
245 27 : sm_write (tmp,
246 : (void *)(tx_share_buf (vars)),
247 : vars->size_);
248 :
249 27 : MIN_DEBUG ("SENT VARIABLES: %s", tx_share_buf(vars));
250 27 : sm_detach (sh_mem_handle);
251 27 : tx_destroy (&vars);
252 27 : exit:
253 236 : for (it = dl_list_head(variables); it != DLListNULLIterator;
254 182 : it = dl_list_next(it)) {
255 182 : var = dl_list_data (it);
256 182 : DELETE (var->var_name_);
257 182 : if (var->is_initialized_)
258 182 : DELETE (var->var_value_);
259 182 : DELETE (var);
260 : }
261 27 : dl_list_free (&variables);
262 27 : return 0;
263 : }
264 :
265 : /* ------------------------------------------------------------------------- */
266 :
267 : ScriptVariable *_find_var (const char *varname)
268 192 : {
269 : DLListIterator it;
270 : ScriptVariable *var;
271 :
272 192 : it = dl_list_head (variables);
273 909 : while (it != INITPTR) {
274 535 : var = dl_list_data (it);
275 535 : if (!strcmp (var->var_name_, varname)) {
276 10 : return var;
277 : }
278 525 : it = dl_list_next (it);
279 : }
280 182 : return INITPTR;
281 : }
282 :
283 : /* ------------------------------------------------------------------------- */
284 : /* ======================== FUNCTIONS ====================================== */
285 : /* ------------------------------------------------------------------------- */
286 : int ts_run_method (MinItemParser * item)
287 27 : {
288 : DLList *l;
289 : DLListIterator it;
290 : int retval;
291 : char *callname;
292 :
293 27 : l = dl_list_create ();
294 27 : callname = INITPTR;
295 :
296 : /*
297 : * Take callname
298 : */
299 27 : retval = mip_get_next_string (item, &callname);
300 27 : if (retval != ENOERR) {
301 0 : retval = -1;
302 0 : goto EXIT;
303 : }
304 :
305 : /*
306 : * Obtain list of test cases
307 : */
308 27 : ts_get_test_cases (&l);
309 :
310 : /*
311 : * Look for test with name set to callname
312 : */
313 27 : it = dl_list_find (dl_list_head (l)
314 : , dl_list_tail (l)
315 : , _look4callname, callname);
316 :
317 27 : if (it == DLListNULLIterator) {
318 0 : retval = -1;
319 0 : DELETE (callname);
320 0 : it = dl_list_head (l);
321 0 : while (it != DLListNULLIterator) {
322 0 : free (dl_list_data (it));
323 0 : dl_list_remove_it (it);
324 0 : it = dl_list_head (l);
325 : }
326 0 : dl_list_free (&l);
327 0 : goto EXIT;
328 : }
329 :
330 : /*
331 : * Intialize variables
332 : */
333 27 : _init_vars ();
334 : /*
335 : * Call function
336 : */
337 27 : retval = ((TestCaseInfoTC *) dl_list_data (it))->test_ (item);
338 :
339 : /*
340 : * Send variables back to scripter
341 : */
342 27 : if (_send_vars ()) {
343 0 : MIN_WARN ("Variable sending failed");
344 0 : retval = -1;
345 : }
346 :
347 : /*
348 : * Cleanup
349 : */
350 27 : DELETE (callname);
351 27 : it = dl_list_head (l);
352 216 : while (it != DLListNULLIterator) {
353 162 : free (dl_list_data (it));
354 162 : dl_list_remove_it (it);
355 162 : it = dl_list_head (l);
356 : }
357 27 : dl_list_free (&l);
358 27 : EXIT:
359 27 : return retval;
360 : }
361 : /* ------------------------------------------------------------------------- */
362 : unsigned int get_module_type()
363 234 : { return module_type; }
364 : /* ------------------------------------------------------------------------- */
365 : unsigned int get_module_version()
366 234 : { return module_version; }
367 : /* ------------------------------------------------------------------------- */
368 : char* get_module_date()
369 468 : { return module_date; }
370 : /* ------------------------------------------------------------------------- */
371 : char* get_module_time()
372 468 : { return module_time; }
373 : /* ------------------------------------------------------------------------- */
374 : int SetLocalValue (const char *varname, const char *varval)
375 0 : {
376 : ScriptVariable *var;
377 :
378 0 : if ((var = _find_var (varname)) == INITPTR) {
379 0 : MIN_WARN ("no variable by the name %s found", varname);
380 0 : return -1;
381 : }
382 :
383 0 : if (var->is_initialized_ && var->var_value_ != INITPTR &&
384 : var->var_value_ != NULL)
385 0 : DELETE (var->var_value_);
386 0 : var->is_initialized_ = ESTrue;
387 :
388 0 : if (varval != INITPTR && varval != NULL) {
389 0 : var->var_value_ = NEW2 (char, strlen (varval) + 1);
390 0 : strcpy (var->var_value_, varval);
391 : } else {
392 0 : var->var_value_ = NEW2 (char, strlen ("NULL") + 1);
393 0 : strcpy (var->var_value_, "NULL");
394 : }
395 :
396 0 : return 0;
397 : }
398 : /* ------------------------------------------------------------------------- */
399 : int SetLocalValueInt (const char *varname, const long value)
400 0 : {
401 : ScriptVariable *var;
402 :
403 0 : if ((var = _find_var (varname)) == INITPTR) {
404 0 : MIN_WARN ("no variable by the name %s found", varname);
405 0 : return -1;
406 : }
407 0 : if (var->is_initialized_ && var->var_value_ != INITPTR &&
408 : var->var_value_ != NULL)
409 0 : DELETE (var->var_value_);
410 0 : var->is_initialized_ = ESTrue;
411 :
412 0 : var->var_value_ = NEW2 (char, 32);
413 0 : sprintf (var->var_value_, "%ld", value);
414 :
415 0 : return 0;
416 : }
417 : /* ------------------------------------------------------------------------- */
418 : int GetLocalValueInt (const char *varname, long *value)
419 10 : {
420 : ScriptVariable *var;
421 :
422 10 : if ((var = _find_var (varname)) == INITPTR) {
423 0 : MIN_WARN ("no variable by the name %s found", varname);
424 0 : return -1;
425 : }
426 :
427 10 : if (var->is_initialized_ == ESFalse) {
428 0 : MIN_WARN ("variable %s is not initialized", varname);
429 0 : return -1;
430 : }
431 :
432 10 : *value = strtol (var->var_value_, NULL, 10);
433 :
434 10 : if (*value == 0 && errno) { /* The value is 0, or there was error */
435 0 : MIN_WARN ("conversion from string to integer failed %s",
436 : strerror (errno));
437 0 : return -1;
438 : }
439 10 : return 0;
440 : }
441 : /* ------------------------------------------------------------------------- */
442 : int GetLocalValue (const char *varname, char **value)
443 0 : {
444 : ScriptVariable *var;
445 :
446 0 : if ((var = _find_var (varname)) == INITPTR) {
447 0 : MIN_WARN ("no variable by the name %s found", varname);
448 0 : return -1;
449 : }
450 :
451 0 : if (var->is_initialized_ == ESFalse) {
452 0 : MIN_WARN ("variable %s is not initialized", varname);
453 0 : return -1;
454 : }
455 :
456 0 : *value = var->var_value_;
457 :
458 0 : return 0;
459 : }
460 : /* ------------------------------------------------------------------------- */
461 : /* ================= OTHER EXPORTED FUNCTIONS ============================== */
462 : /* None */
463 :
464 : /* ------------------------------------------------------------------------- */
465 : /* End of file */
|