1 |
/* |
2 |
* Unit test functions for ASCEND: utilities/ascDynaLoad.c |
3 |
* |
4 |
* Copyright (C) 2005 Jerry St.Clair |
5 |
* |
6 |
* This file is part of the Ascend Environment. |
7 |
* |
8 |
* The Ascend Environment is free software; you can redistribute it |
9 |
* and/or modify it under the terms of the GNU General Public License as |
10 |
* published by the Free Software Foundation; either version 2 of the |
11 |
* License, or (at your option) any later version. |
12 |
* |
13 |
* The Ascend Environment is distributed in hope that it will be useful, |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 |
* General Public License for more details. |
17 |
* |
18 |
* You should have received a copy of the GNU General Public License |
19 |
* along with the program; if not, write to the Free Software Foundation, |
20 |
* Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named |
21 |
* COPYING. |
22 |
*/ |
23 |
|
24 |
#include <stdio.h> |
25 |
#ifdef __WIN32__ |
26 |
#include <io.h> |
27 |
#endif |
28 |
#include "utilities/ascConfig.h" |
29 |
#include "utilities/ascMalloc.h" |
30 |
#include "utilities/ascDynaLoad.h" |
31 |
#include "CUnit/CUnit.h" |
32 |
#include "test_ascDynaLoad.h" |
33 |
#include "test_ascDynaLoad_shlib.h" |
34 |
|
35 |
/* |
36 |
* ascDynaLoad.[ch] has several different platform-dependent |
37 |
* implementations. This test function only tests the platform it |
38 |
* is being run on. At present, testing is not implemented for all |
39 |
* ASCEND platforms. |
40 |
* |
41 |
* This test function attempts to open and execute a shared library |
42 |
* whose path is defined in variable shlib_name. Modify this variable |
43 |
* for the shared library you want to use. Minimally, the shared |
44 |
* library must provide the following public symbols: |
45 |
* |
46 |
* - init function, [int (*)(void)], returns -5. |
47 |
* - isInitialized function, [int (*)(void)], returns TRUE if init() |
48 |
* has been called, FALSE if cleanup or nothing has |
49 |
* been called. |
50 |
* - cleanup function, [void (*)(void)], no return. |
51 |
* - value data [int] == FALSE initially, TRUE after init() has |
52 |
* been called, FALSE after cleanup() has been called. |
53 |
*/ |
54 |
static void test_ascDynaLoad(void) |
55 |
{ |
56 |
FILE *file; |
57 |
initFunc init_func; |
58 |
isInitializedFunc isInitialized_func; |
59 |
cleanupFunc cleanup_func; |
60 |
valuetype *value; |
61 |
unsigned long prior_meminuse; |
62 |
|
63 |
#ifdef __WIN32__ |
64 |
const char *shlib_name = "..\\..\\..\\generic\\utilities\\test\\test_ascDynaLoad_shlib.dll"; |
65 |
#else |
66 |
const char *shlib_name = "../../../generic/utilities/test/test_ascDynaLoad_shlib.so"; |
67 |
#endif /* __WIN32__ */ |
68 |
|
69 |
|
70 |
prior_meminuse = ascmeminuse(); /* save meminuse() at start of test function */ |
71 |
|
72 |
/* test Asc_DynamicLoad(), Asc_DynamicUnLoad() */ |
73 |
CU_TEST(1 == Asc_DynamicLoad(NULL, NULL)); /* NULL path */ |
74 |
CU_TEST(-3 == Asc_DynamicUnLoad(NULL)); |
75 |
|
76 |
CU_TEST(1 == Asc_DynamicLoad("dummy", NULL)); /* nonexistent shared lib */ |
77 |
CU_TEST(-3 == Asc_DynamicUnLoad("dummy")); |
78 |
|
79 |
if (NULL == (file = fopen(shlib_name, "r"))) { /* make sure we can open the test shared library */ |
80 |
CU_FAIL("Could not find test shared library. Aborting test."); |
81 |
} else { |
82 |
|
83 |
fclose(file); |
84 |
|
85 |
if (0 == Asc_DynamicLoad(shlib_name, NULL)) { /* shared lib with no init func */ |
86 |
CU_PASS("Opening of shared library succeeded."); |
87 |
|
88 |
CU_TEST(NULL != (init_func = (initFunc)Asc_DynamicSymbol(shlib_name, "init"))); |
89 |
CU_TEST(NULL != (isInitialized_func = (isInitializedFunc)Asc_DynamicSymbol(shlib_name, "isInitialized"))); |
90 |
CU_TEST(NULL != (cleanup_func = (cleanupFunc)Asc_DynamicSymbol(shlib_name, "cleanup"))); |
91 |
CU_TEST(NULL != (value = (valuetype*)Asc_DynamicSymbol(shlib_name, "value"))); |
92 |
if ((NULL != init_func) && |
93 |
(NULL != isInitialized_func) && |
94 |
(NULL != cleanup_func) && |
95 |
(NULL != value)) { |
96 |
CU_TEST(FALSE == (*isInitialized_func)()); |
97 |
CU_TEST(FALSE == (*value)); |
98 |
CU_TEST(-5 == (*init_func)()); |
99 |
CU_TEST(TRUE == (*isInitialized_func)()); |
100 |
CU_TEST(TRUE == (*value)); |
101 |
(*cleanup_func)(); |
102 |
CU_TEST(FALSE == (*isInitialized_func)()); |
103 |
CU_TEST(FALSE == (*value)); |
104 |
} |
105 |
CU_TEST(0 == Asc_DynamicUnLoad(shlib_name)); |
106 |
} |
107 |
else { |
108 |
CU_FAIL("Opening of shared library failed."); |
109 |
} |
110 |
|
111 |
if (-5 == Asc_DynamicLoad(shlib_name, "init")) { /* shared lib with init func */ |
112 |
CU_PASS("Opening of shared library succeeded."); |
113 |
|
114 |
CU_TEST(NULL != (init_func = (initFunc)Asc_DynamicSymbol(shlib_name, "init"))); |
115 |
CU_TEST(NULL != (isInitialized_func = (isInitializedFunc)Asc_DynamicSymbol(shlib_name, "isInitialized"))); |
116 |
CU_TEST(NULL != (cleanup_func = (cleanupFunc)Asc_DynamicSymbol(shlib_name, "cleanup"))); |
117 |
CU_TEST(NULL != (value = (valuetype*)Asc_DynamicSymbol(shlib_name, "value"))); |
118 |
if ((NULL != init_func) && |
119 |
(NULL != isInitialized_func) && |
120 |
(NULL != cleanup_func) && |
121 |
(NULL != value)) { |
122 |
CU_TEST(TRUE == (*isInitialized_func)()); |
123 |
CU_TEST(TRUE == (*value)); |
124 |
(*cleanup_func)(); |
125 |
CU_TEST(FALSE == (*isInitialized_func)()); |
126 |
CU_TEST(FALSE == (*value)); |
127 |
} |
128 |
CU_TEST(0 == Asc_DynamicUnLoad(shlib_name)); |
129 |
} |
130 |
else { |
131 |
CU_FAIL("Opening of shared library failed."); |
132 |
} |
133 |
} |
134 |
|
135 |
/* test Asc_DynamicSymbol() - normal operation tested in previous tests */ |
136 |
|
137 |
CU_TEST(NULL == Asc_DynamicSymbol(NULL, "init")); /* NULL libname */ |
138 |
CU_TEST(NULL == Asc_DynamicSymbol(shlib_name, NULL)); /* NULL symbol */ |
139 |
CU_TEST(NULL == Asc_DynamicSymbol(shlib_name, "init")); /* library not open */ |
140 |
|
141 |
CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */ |
142 |
} |
143 |
|
144 |
/*===========================================================================*/ |
145 |
/* Registration information */ |
146 |
|
147 |
static CU_TestInfo ascDynaLoad_test_list[] = { |
148 |
{"test_ascDynaLoad", test_ascDynaLoad}, |
149 |
CU_TEST_INFO_NULL |
150 |
}; |
151 |
|
152 |
static CU_SuiteInfo suites[] = { |
153 |
{"test_utilities_ascDynaLoad", NULL, NULL, ascDynaLoad_test_list}, |
154 |
CU_SUITE_INFO_NULL |
155 |
}; |
156 |
|
157 |
/*-------------------------------------------------------------------*/ |
158 |
CU_ErrorCode test_register_utilities_ascDynaLoad(void) |
159 |
{ |
160 |
return CU_register_suites(suites); |
161 |
} |