diff options
Diffstat (limited to 'mmc_updater/src/tests')
-rw-r--r-- | mmc_updater/src/tests/CMakeLists.txt | 47 | ||||
-rw-r--r-- | mmc_updater/src/tests/TestFileUtils.cpp | 79 | ||||
-rw-r--r-- | mmc_updater/src/tests/TestFileUtils.h | 10 | ||||
-rw-r--r-- | mmc_updater/src/tests/TestParseScript.cpp | 24 | ||||
-rw-r--r-- | mmc_updater/src/tests/TestParseScript.h | 8 | ||||
-rw-r--r-- | mmc_updater/src/tests/TestUtils.h | 108 | ||||
-rw-r--r-- | mmc_updater/src/tests/file_list.xml | 37 | ||||
-rw-r--r-- | mmc_updater/src/tests/new_app.cpp | 8 | ||||
-rw-r--r-- | mmc_updater/src/tests/old_app.cpp | 7 | ||||
-rw-r--r-- | mmc_updater/src/tests/test.manifest | 27 | ||||
-rw-r--r-- | mmc_updater/src/tests/test.rc | 28 |
11 files changed, 383 insertions, 0 deletions
diff --git a/mmc_updater/src/tests/CMakeLists.txt b/mmc_updater/src/tests/CMakeLists.txt new file mode 100644 index 00000000..08501a98 --- /dev/null +++ b/mmc_updater/src/tests/CMakeLists.txt @@ -0,0 +1,47 @@ + +include_directories("${CMAKE_CURRENT_SOURCE_DIR}/..") + +if (APPLE) + set(HELPER_SHARED_SOURCES ../StlSymbolsLeopard.cpp) +endif() + +# # Create helper binaries for unit tests +# add_executable(oldapp +# old_app.cpp +# ${HELPER_SHARED_SOURCES} +# ) +# add_executable(newapp +# new_app.cpp +# ${HELPER_SHARED_SOURCES} +# ) + +# Install data files required by unit tests +set(TEST_FILES + file_list.xml +) + +foreach(TEST_FILE ${TEST_FILES}) + execute_process( + COMMAND + "${CMAKE_COMMAND}" -E copy_if_different "${CMAKE_CURRENT_SOURCE_DIR}/${TEST_FILE}" "${CMAKE_CURRENT_BINARY_DIR}" + ) +endforeach() + +# Add unit test binaries +macro(ADD_UPDATER_TEST CLASS) + set(TEST_TARGET updater_${CLASS}) + unset(srcs) + list(APPEND srcs ${CLASS}.cpp) + if (WIN32) + list(APPEND srcs ${CMAKE_CURRENT_SOURCE_DIR}/test.rc) + endif() + add_executable(${TEST_TARGET} ${srcs}) + target_link_libraries(${TEST_TARGET} updatershared) + add_test(NAME ${TEST_TARGET} COMMAND ${TEST_TARGET}) + if (APPLE) + set_target_properties(${TEST_TARGET} PROPERTIES LINK_FLAGS "-framework Security -framework Cocoa") + endif() +endmacro() + +add_updater_test(TestParseScript) +add_updater_test(TestFileUtils) diff --git a/mmc_updater/src/tests/TestFileUtils.cpp b/mmc_updater/src/tests/TestFileUtils.cpp new file mode 100644 index 00000000..f8535a28 --- /dev/null +++ b/mmc_updater/src/tests/TestFileUtils.cpp @@ -0,0 +1,79 @@ +#include "TestFileUtils.h" + +#include "FileUtils.h" +#include "TestUtils.h" + +void TestFileUtils::testDirName() +{ + std::string dirName; + std::string fileName; + +#ifdef PLATFORM_WINDOWS + // absolute paths + dirName = FileUtils::dirname("E:/Some Dir/App.exe"); + TEST_COMPARE(dirName,"E:/Some Dir"); + fileName = FileUtils::fileName("E:/Some Dir/App.exe"); + TEST_COMPARE(fileName,"App.exe"); + + dirName = FileUtils::dirname("C:/Users/kitteh/AppData/Local/Temp/MultiMC5-yidaaa/MultiMC.exe"); + TEST_COMPARE(dirName,"C:/Users/kitteh/AppData/Local/Temp/MultiMC5-yidaaa"); + fileName = FileUtils::fileName("C:/Users/kitteh/AppData/Local/Temp/MultiMC5-yidaaa/MultiMC.exe"); + TEST_COMPARE(fileName,"MultiMC.exe"); + +#else + // absolute paths + dirName = FileUtils::dirname("/home/tester/foo bar/baz"); + TEST_COMPARE(dirName,"/home/tester/foo bar"); + fileName = FileUtils::fileName("/home/tester/foo bar/baz"); + TEST_COMPARE(fileName,"baz"); +#endif + // current directory + dirName = FileUtils::dirname("App.exe"); + TEST_COMPARE(dirName,"."); + fileName = FileUtils::fileName("App.exe"); + TEST_COMPARE(fileName,"App.exe"); + + // relative paths + dirName = FileUtils::dirname("Foo/App.exe"); + TEST_COMPARE(dirName,"Foo"); + fileName = FileUtils::fileName("Foo/App.exe"); + TEST_COMPARE(fileName,"App.exe"); +} + +void TestFileUtils::testIsRelative() +{ +#ifdef PLATFORM_WINDOWS + TEST_COMPARE(FileUtils::isRelative("temp"),true); + TEST_COMPARE(FileUtils::isRelative("D:/temp"),false); + TEST_COMPARE(FileUtils::isRelative("d:/temp"),false); +#else + TEST_COMPARE(FileUtils::isRelative("/tmp"),false); + TEST_COMPARE(FileUtils::isRelative("tmp"),true); +#endif +} + +void TestFileUtils::testSymlinkFileExists() +{ +#ifdef PLATFORM_UNIX + const char* linkName = "link-name"; + FileUtils::removeFile(linkName); + FileUtils::createSymLink(linkName, "target-that-does-not-exist"); + TEST_COMPARE(FileUtils::fileExists(linkName), true); +#endif +} + +void TestFileUtils::testStandardDirs() +{ + std::string tmpDir = FileUtils::tempPath(); + TEST_COMPARE(FileUtils::fileExists(tmpDir.data()), true); +} + +int main(int,char**) +{ + TestList<TestFileUtils> tests; + tests.addTest(&TestFileUtils::testDirName); + tests.addTest(&TestFileUtils::testIsRelative); + tests.addTest(&TestFileUtils::testSymlinkFileExists); + tests.addTest(&TestFileUtils::testStandardDirs); + return TestUtils::runTest(tests); +} diff --git a/mmc_updater/src/tests/TestFileUtils.h b/mmc_updater/src/tests/TestFileUtils.h new file mode 100644 index 00000000..1a45164b --- /dev/null +++ b/mmc_updater/src/tests/TestFileUtils.h @@ -0,0 +1,10 @@ +#pragma once + +class TestFileUtils +{ + public: + void testDirName(); + void testIsRelative(); + void testSymlinkFileExists(); + void testStandardDirs(); +}; diff --git a/mmc_updater/src/tests/TestParseScript.cpp b/mmc_updater/src/tests/TestParseScript.cpp new file mode 100644 index 00000000..f4453957 --- /dev/null +++ b/mmc_updater/src/tests/TestParseScript.cpp @@ -0,0 +1,24 @@ +#include "TestParseScript.h" + +#include "TestUtils.h" +#include "UpdateScript.h" + +#include <iostream> +#include <algorithm> + +void TestParseScript::testParse() +{ + UpdateScript script; + + script.parse("file_list.xml"); + + TEST_COMPARE(script.isValid(),true); +} + +int main(int,char**) +{ + TestList<TestParseScript> tests; + tests.addTest(&TestParseScript::testParse); + return TestUtils::runTest(tests); +} + diff --git a/mmc_updater/src/tests/TestParseScript.h b/mmc_updater/src/tests/TestParseScript.h new file mode 100644 index 00000000..528e97a8 --- /dev/null +++ b/mmc_updater/src/tests/TestParseScript.h @@ -0,0 +1,8 @@ +#pragma once + +class TestParseScript +{ + public: + void testParse(); +}; + diff --git a/mmc_updater/src/tests/TestUtils.h b/mmc_updater/src/tests/TestUtils.h new file mode 100644 index 00000000..68d97da5 --- /dev/null +++ b/mmc_updater/src/tests/TestUtils.h @@ -0,0 +1,108 @@ +#pragma once + +#include <iostream> +#include <functional> +#include <string> +#include <vector> + +template <class T> +class TestList +{ + public: + void addTest(void (T::*test)()) + { + m_tests.push_back(std::mem_fun(test)); + } + + int size() const + { + return m_tests.size(); + } + + void runTest(T* testInstance, int i) + { + m_tests.at(i)(testInstance); + } + + private: + std::vector<std::mem_fun_t<void,T> > m_tests; +}; + +class TestUtils +{ + public: + template <class X, class Y> + static void compare(const X& x, const Y& y, const char* xString, const char* yString) + { + if (x != y) + { + throw "Actual and expected values differ. " + "Actual: " + toString(x,xString) + + " Expected: " + toString(y,yString); + } + } + + template <typename T> + static std::string toString(T value, const char* context) + { + return "Unprintable: " + std::string(context); + } + + template <class T> + static int runTest(class TestList<T>& tests) throw () + { + std::string errorText; + try + { + T testInstance; + for (int i=0; i < tests.size(); i++) + { + tests.runTest(&testInstance,i); + } + } + catch (const std::exception& ex) + { + errorText = ex.what(); + } + catch (const std::string& error) + { + errorText = error; + } + catch (...) + { + errorText = "Unknown exception"; + } + + if (errorText.empty()) + { + std::cout << "Test passed" << std::endl; + return 0; + } + else + { + std::cout << "Test failed: " << errorText << std::endl; + return 1; + } + } +}; + +template <> +inline std::string TestUtils::toString(const std::string& value, const char*) +{ + return value; +} +template <> +inline std::string TestUtils::toString(std::string value, const char*) +{ + return value; +} +template <> +inline std::string TestUtils::toString(const char* value, const char*) +{ + return value; +} + +#define TEST_COMPARE(x,y) \ + TestUtils::compare(x,y,#x,#y); + + diff --git a/mmc_updater/src/tests/file_list.xml b/mmc_updater/src/tests/file_list.xml new file mode 100644 index 00000000..06ba501d --- /dev/null +++ b/mmc_updater/src/tests/file_list.xml @@ -0,0 +1,37 @@ +<?xml version="1.0"?> +<update version="3"> + <install> + <file> + <name>$APP_FILENAME</name> + <hash>$UPDATED_APP_HASH</hash> + <size>$UPDATED_APP_SIZE</size> + <permissions>0755</permissions> + <package>app-pkg</package> + <is-main-binary>true</is-main-binary> + </file> + <file> + <name>$UPDATER_FILENAME</name> + <hash>$UPDATER_HASH</hash> + <size>$UPDATER_SIZE</size> + <permissions>0755</permissions> + </file> + <!-- Test symlink !--> + <file> + <name>test-dir/app-symlink</name> + <target>../app</target> + </file> + <!-- Test file in new directory !--> + <file> + <name>new-dir/new-dir2/new-file.txt</name> + <hash>$TEST_FILENAME</hash> + <size>$TEST_SIZE</size> + <package>app-pkg</package> + <permissions>0644</permissions> + </file> + </install> + <uninstall> + <!-- TODO - List some files to uninstall here !--> + <file>file-to-uninstall.txt</file> + <file>symlink-to-file-to-uninstall.txt</file> + </uninstall> +</update> diff --git a/mmc_updater/src/tests/new_app.cpp b/mmc_updater/src/tests/new_app.cpp new file mode 100644 index 00000000..7fad1380 --- /dev/null +++ b/mmc_updater/src/tests/new_app.cpp @@ -0,0 +1,8 @@ +#include <iostream> + +int main(int,char**) +{ + std::cout << "new app starting" << std::endl; + return 0; +} + diff --git a/mmc_updater/src/tests/old_app.cpp b/mmc_updater/src/tests/old_app.cpp new file mode 100644 index 00000000..476a3405 --- /dev/null +++ b/mmc_updater/src/tests/old_app.cpp @@ -0,0 +1,7 @@ +#include <iostream> + +int main(int,char**) +{ + std::cout << "old app starting" << std::endl; + return 0; +} diff --git a/mmc_updater/src/tests/test.manifest b/mmc_updater/src/tests/test.manifest new file mode 100644 index 00000000..8b4dbb98 --- /dev/null +++ b/mmc_updater/src/tests/test.manifest @@ -0,0 +1,27 @@ +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> +<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3"> + <assemblyIdentity name="MultiMC.Test.0" type="win32" version="5.0.0.0" /> + <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3"> + <security> + <requestedPrivileges> + <requestedExecutionLevel level="asInvoker" uiAccess="false"/> + </requestedPrivileges> + </security> + </trustInfo> + <dependency> + <dependentAssembly> + <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="x86" publicKeyToken="6595b64144ccf1df" language="*"/> + </dependentAssembly> + </dependency> + <description>Custom Minecraft launcher for managing multiple installs.</description> + <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> + <application> + <!--The ID below indicates app support for Windows Vista --> + <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/> + <!--The ID below indicates app support for Windows 7 --> + <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/> + <!--The ID below indicates app support for Windows Developer Preview / Windows 8 --> + <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/> + </application> + </compatibility> +</assembly>
\ No newline at end of file diff --git a/mmc_updater/src/tests/test.rc b/mmc_updater/src/tests/test.rc new file mode 100644 index 00000000..a288dba6 --- /dev/null +++ b/mmc_updater/src/tests/test.rc @@ -0,0 +1,28 @@ +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include <windows.h> + +1 RT_MANIFEST "test.manifest" + +VS_VERSION_INFO VERSIONINFO +FILEVERSION 1,0,0,0 +FILEOS VOS_NT_WINDOWS32 +FILETYPE VFT_APP +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "000004b0" + BEGIN + VALUE "CompanyName", "MultiMC Contributors" + VALUE "FileDescription", "Testcase" + VALUE "FileVersion", "1.0.0.0" + VALUE "ProductName", "MultiMC Testcase" + VALUE "ProductVersion", "5" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x0000, 0x04b0 // Unicode + END +END |