Skip to content

Commit 2da44d0

Browse files
committed
Run more tests with sample DLLs.
1 parent 36574a5 commit 2da44d0

9 files changed

+253
-2
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
*.o
22
*.obj
33
*.exe
4+
tests/*.dll
5+
tests/*.res

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ script:
1919
- cd example/DllLoader
2020
- WINEPREFIX=`pwd`/$WINE WINEPATH=/usr/lib/gcc/$PLATFORM-w64-mingw32/4.6/ $WINE ./DllLoader.exe
2121
- WINEPREFIX=`pwd`/$WINE WINEPATH=/usr/lib/gcc/$PLATFORM-w64-mingw32/4.6/ $WINE ./DllLoaderLoader.exe
22+
- make test PLATFORM=$PLATFORM UNICODE=$UNICODE

Makefile

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SUBDIRS = example
1+
SUBDIRS = example tests
22

33
.PHONY: subdirs $(SUBDIRS)
44

@@ -13,5 +13,8 @@ clean: $(CLEANDIRS)
1313
$(CLEANDIRS):
1414
$(MAKE) -C $(@:clean-%=%) clean
1515

16+
test:
17+
$(MAKE) -C tests test
18+
1619
.PHONY: subdirs $(INSTALLDIRS)
17-
.PHONY: clean
20+
.PHONY: clean test

tests/LoadDll.cpp

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#define WIN32_LEAN_AND_MEAN
2+
3+
#include <windows.h>
4+
#include <tchar.h>
5+
#include <stdio.h>
6+
#include <malloc.h>
7+
8+
#include "../MemoryModule.h"
9+
10+
typedef int (*addNumberProc)(int, int);
11+
12+
BOOL LoadFromMemory(char *filename)
13+
{
14+
FILE *fp;
15+
unsigned char *data=NULL;
16+
size_t size;
17+
HMEMORYMODULE handle;
18+
addNumberProc addNumber;
19+
HMEMORYRSRC resourceInfo;
20+
DWORD resourceSize;
21+
LPVOID resourceData;
22+
TCHAR buffer[100];
23+
BOOL result = TRUE;
24+
25+
fp = fopen(filename, "rb");
26+
if (fp == NULL)
27+
{
28+
printf("Can't open DLL file \"%s\".", filename);
29+
result = FALSE;
30+
goto exit;
31+
}
32+
33+
fseek(fp, 0, SEEK_END);
34+
size = ftell(fp);
35+
data = (unsigned char *)malloc(size);
36+
fseek(fp, 0, SEEK_SET);
37+
fread(data, 1, size, fp);
38+
fclose(fp);
39+
40+
handle = MemoryLoadLibrary(data);
41+
if (handle == NULL)
42+
{
43+
_tprintf(_T("Can't load library from memory.\n"));
44+
result = FALSE;
45+
goto exit;
46+
}
47+
48+
addNumber = (addNumberProc)MemoryGetProcAddress(handle, "addNumbers");
49+
_tprintf(_T("From memory: %d\n"), addNumber(1, 2));
50+
51+
resourceInfo = MemoryFindResource(handle, MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION);
52+
_tprintf(_T("MemoryFindResource returned 0x%p\n"), resourceInfo);
53+
54+
if (resourceInfo != NULL) {
55+
resourceSize = MemorySizeofResource(handle, resourceInfo);
56+
resourceData = MemoryLoadResource(handle, resourceInfo);
57+
_tprintf(_T("Memory resource data: %ld bytes at 0x%p\n"), resourceSize, resourceData);
58+
59+
MemoryLoadString(handle, 1, buffer, sizeof(buffer));
60+
_tprintf(_T("String1: %s\n"), buffer);
61+
62+
MemoryLoadString(handle, 20, buffer, sizeof(buffer));
63+
_tprintf(_T("String2: %s\n"), buffer);
64+
} else {
65+
result = FALSE;
66+
}
67+
68+
MemoryFreeLibrary(handle);
69+
70+
exit:
71+
if (data)
72+
free(data);
73+
return result;
74+
}
75+
76+
int main(int argc, char* argv[])
77+
{
78+
if (argc < 2) {
79+
fprintf(stderr, "USAGE: %s <filename.dll>\n", argv[0]);
80+
return 1;
81+
}
82+
83+
if (!LoadFromMemory(argv[1])) {
84+
return 2;
85+
}
86+
87+
return 0;
88+
}

tests/Makefile

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
UNAME := $(shell uname)
2+
3+
ifeq ($(UNAME), Linux)
4+
ifndef PLATFORM
5+
PLATFORM = i686
6+
endif
7+
CC = $(PLATFORM)-w64-mingw32-g++
8+
CXX = $(PLATFORM)-w64-mingw32-g++
9+
LD = $(PLATFORM)-w64-mingw32-ld
10+
RC = $(PLATFORM)-w64-mingw32-windres
11+
else
12+
CC = g++
13+
CXX = g++
14+
LD = ld
15+
RC = rc
16+
endif
17+
18+
RM = rm
19+
CFLAGS = -Wall -g
20+
LDFLAGS =
21+
RCFLAGS = -O coff
22+
23+
ifdef UNICODE
24+
CFLAGS += -DUNICODE -D_UNICODE
25+
endif
26+
27+
CFLAGS_DLL = -DSAMPLEDLL_EXPORTS
28+
CFLAGS_EXE =
29+
LDFLAGS_DLL = -shared
30+
LDFLAGS_EXE = -static
31+
32+
TEST_DLLS = \
33+
test-align-128.dll \
34+
test-align-256.dll \
35+
test-align-512.dll \
36+
test-align-768.dll \
37+
test-align-1024.dll \
38+
test-align-2048.dll \
39+
test-align-3072.dll \
40+
test-align-4096.dll \
41+
test-align-100.dll \
42+
test-align-200.dll \
43+
test-align-300.dll \
44+
test-align-400.dll \
45+
test-align-500.dll \
46+
test-align-600.dll \
47+
test-align-800.dll \
48+
test-align-900.dll \
49+
test-align-1000.dll \
50+
test-relocate.dll \
51+
52+
LOADDLL_OBJ = LoadDll.o ../MemoryModule.o
53+
DLL_OBJ = SampleDLL.o SampleDLL.res
54+
55+
all: LoadDll.exe $(TEST_DLLS)
56+
57+
LoadDll.exe: $(LOADDLL_OBJ)
58+
$(CC) $(LDFLAGS_EXE) $(LDFLAGS) -Wl,--image-base -Wl,0x20000000 -o LoadDll.exe $(LOADDLL_OBJ)
59+
60+
LoadDll.o: LoadDll.cpp
61+
$(CXX) $(CFLAGS) $(CFLAGS_EXE) -c $<
62+
63+
test-align-%.dll: $(DLL_OBJ)
64+
$(LD) $(LDFLAGS_DLL) $(LDFLAGS) --file-alignment $* --section-alignment $* -o $@ $(DLL_OBJ)
65+
66+
test-relocate.dll: $(DLL_OBJ)
67+
$(CXX) $(LDFLAGS_DLL) $(LDFLAGS) -Wl,--image-base -Wl,0x20000000 -o $@ $(DLL_OBJ)
68+
69+
%.o: %.cpp
70+
$(CXX) $(CFLAGS) $(CFLAGS_DLL) -c $<
71+
72+
%.o: %.cc
73+
$(CC) $(CFLAGS) $(CFLAGS_DLL) -c $<
74+
75+
%.res: %.rc
76+
$(RC) $(RCFLAGS) -o $*.res $<
77+
78+
clean:
79+
$(RM) -rf LoadDll.exe $(TEST_DLLS) $(LOADDLL_OBJ) $(DLL_OBJ)
80+
81+
test: all
82+
./runtests.sh $(PLATFORM) "$(TEST_DLLS)"

tests/SampleDLL.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "SampleDLL.h"
2+
3+
extern "C" {
4+
5+
SAMPLEDLL_API int addNumbers(int a, int b)
6+
{
7+
return a + b;
8+
}
9+
10+
}

tests/SampleDLL.h

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
extern "C" {
2+
3+
#ifdef SAMPLEDLL_EXPORTS
4+
#define SAMPLEDLL_API __declspec(dllexport)
5+
#else
6+
#define SAMPLEDLL_API __declspec(dllimport)
7+
#endif
8+
9+
SAMPLEDLL_API int addNumbers(int a, int b);
10+
11+
}

tests/SampleDLL.rc

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
1 VERSIONINFO
2+
FILEVERSION 1,0,0,0
3+
PRODUCTVERSION 1,0,0,0
4+
BEGIN
5+
BLOCK "StringFileInfo"
6+
BEGIN
7+
BLOCK "040904E4"
8+
BEGIN
9+
VALUE "CompanyName", "fancy.code"
10+
VALUE "FileDescription", "SampleDLL"
11+
VALUE "FileVersion", "1.0"
12+
VALUE "InternalName", "SampleDLL"
13+
VALUE "LegalCopyright", "Copyright (c) 2004-2015 Joachim Bauch"
14+
VALUE "OriginalFilename", "SampleDLL.dll"
15+
VALUE "ProductName", "MemoryModule"
16+
VALUE "ProductVersion", "0.0.4"
17+
END
18+
END
19+
20+
BLOCK "VarFileInfo"
21+
BEGIN
22+
VALUE "Translation", 0x409, 1252
23+
END
24+
END
25+
26+
27+
#define IDS_HELLO 1
28+
#define IDS_WORLD 20
29+
30+
STRINGTABLE
31+
{
32+
IDS_HELLO, "Hello"
33+
IDS_WORLD, "World!"
34+
}

tests/runtests.sh

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
if [ "$1" = "x86_64" ]; then
3+
export WINEPREFIX=${HOME}/.wine64/
4+
else
5+
export WINEPREFIX=${HOME}/.wine/
6+
fi
7+
8+
read -a TEST_DLLS <<< $2
9+
10+
for filename in "${TEST_DLLS[@]}"
11+
do
12+
:
13+
echo "Testing $filename"
14+
./LoadDll.exe $filename
15+
if [ "$?" != "0" ]; then
16+
exit 1
17+
fi
18+
done
19+
20+
echo "${#TEST_DLLS[@]} tests completed successfully"

0 commit comments

Comments
 (0)