summaryrefslogtreecommitdiff
path: root/src/tests/evil/evil_test_dlfcn.c
blob: 56b4eaffb938faf7c9f02eadba1562522770b545 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* EVIL - EFL library for Windows port
 * Copyright (C) 2015 Vincent Torri
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library;
 * if not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <evil_private.h>

#include "evil_suite.h"


typedef int (*_evil_init)(void);
typedef int (*_evil_shutdwon)(void);


EFL_START_TEST(evil_dlfcn_dlopen_success)
{
   void *mod;
   int res;

   mod = dlopen("c:\\windows\\system32\\kernel32.dll", 0);
   fail_if(mod == NULL);

   res = dlclose(mod);
   fail_if(res != 0);
}
EFL_END_TEST

EFL_START_TEST(evil_dlfcn_dlopen_failure)
{
   void *mod;

   /* non existent DLL */
   mod = dlopen("c:\\windows\\system32\\kernel32.dl", 0);
   fail_if(mod != NULL);
}
EFL_END_TEST

EFL_START_TEST(evil_dlfcn_dlsym_success)
{
   _evil_init sym_init;
   _evil_shutdwon sym_shutdown;
   void *mod;
   int res;

   mod = dlopen("libevil-1.dll", 0);
   fail_if(mod == NULL);

   sym_init = dlsym(mod, "evil_init");
   fail_if(sym_init == NULL);
   fail_if(sym_init() != 2);

   sym_shutdown = dlsym(mod, "evil_shutdown");
   fail_if(sym_shutdown == NULL);
   fail_if(sym_shutdown() != 1);

   res = dlclose(mod);
   fail_if(res != 0);
}
EFL_END_TEST

EFL_START_TEST(evil_dlfcn_dlsym_failure)
{
   void *mod;
   void *sym;
   int res;

   mod = dlopen("libevil-1.dll", 0);
   fail_if(mod == NULL);

   /* non-existent symbol */
   sym = dlsym(mod, "evil_ini");
   fail_if(sym != NULL);

   res = dlclose(mod);
   fail_if(res != 0);
}
EFL_END_TEST

EFL_START_TEST(evil_dlfcn_dladdr)
{
   Dl_info info;
   void *mod;
   void *sym;
   char *dll;
   int res;

   mod = dlopen("libevil-1.dll", 0);
   fail_if(mod == NULL);

   sym = dlsym(mod, "evil_init");
   fail_if(sym == NULL);

   res = dladdr(sym, &info);
   fail_if(res == 0);

   fail_if(mod != info.dli_fbase);
   dll = strrchr(info.dli_fname, '\\') + 1;
   fail_if(strcmp("libevil-1.dll", dll) != 0);
   fail_if(sym != info.dli_saddr);
   fail_if(strcmp("evil_init", info.dli_sname) != 0);

   res = dlclose(mod);
   fail_if(res != 0);
}
EFL_END_TEST

void evil_test_dlfcn(TCase *tc)
{
   tcase_add_test(tc, evil_dlfcn_dlopen_success);
   tcase_add_test(tc, evil_dlfcn_dlopen_failure);
   tcase_add_test(tc, evil_dlfcn_dlsym_success);
   tcase_add_test(tc, evil_dlfcn_dlsym_failure);
   tcase_add_test(tc, evil_dlfcn_dladdr);
}