1 /*
2  * This file is part of Licq, an instant messaging client for UNIX.
3  * Copyright (C) 2007-2013 Licq developers <licq-dev@googlegroups.com>
4  *
5  * Licq is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * Licq is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Licq; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #include "../dynamiclibrary.h"
21 
22 #include <gtest/gtest.h>
23 
24 #ifdef __APPLE__
25 #define LIBM "libm.dylib"
26 #else
27 #define LIBM "libm.so"
28 #endif
29 
testfunc()30 extern "C" int testfunc()
31 {
32   return 42;
33 }
34 
35 using namespace LicqDaemon;
36 
37 namespace LicqTest {
38 
TEST(DynamicLibrary,loadLib)39 TEST(DynamicLibrary, loadLib)
40 {
41   ASSERT_NO_THROW(DynamicLibrary libm(LIBM));
42 }
43 
TEST(DynamicLibrary,getSymbol)44 TEST(DynamicLibrary, getSymbol)
45 {
46   DynamicLibrary libm(LIBM);
47 
48   double (*cos)(double) = NULL;
49   ASSERT_NO_THROW(libm.getSymbol("cos", &cos));
50   EXPECT_NEAR(cos(3.14), -1.0, 0.01);
51 }
52 
TEST(DynamicLibrary,getName)53 TEST(DynamicLibrary, getName)
54 {
55   DynamicLibrary libm(LIBM);
56   EXPECT_EQ(LIBM, libm.getName());
57 }
58 
TEST(DynamicLibrary,getMissingSymbolShouldFail)59 TEST(DynamicLibrary, getMissingSymbolShouldFail)
60 {
61   DynamicLibrary libm(LIBM);
62 
63   double (*cos)(double);
64   EXPECT_THROW(libm.getSymbol("missingcos", &cos), DynamicLibrary::Exception);
65 }
66 
TEST(DynamicLibrary,loadMissingLibShouldFail)67 TEST(DynamicLibrary, loadMissingLibShouldFail)
68 {
69   ASSERT_THROW(DynamicLibrary("missinglib.foo"), DynamicLibrary::Exception);
70 }
71 
TEST(DynamicLibrary,loadSelf)72 TEST(DynamicLibrary, loadSelf)
73 {
74   DynamicLibrary self("");
75 
76   int (*testfuncSymbol)() = NULL;
77   ASSERT_NO_THROW(self.getSymbol("testfunc", &testfuncSymbol));
78   EXPECT_TRUE(testfunc == testfuncSymbol);
79   EXPECT_EQ(42, testfuncSymbol());
80 }
81 
TEST(DynamicLibrary,exceptionGetSystemError)82 TEST(DynamicLibrary, exceptionGetSystemError)
83 {
84   try {
85     DynamicLibrary dummy("missinglib.foo");
86     FAIL() << "Should not get here";
87   }
88   catch (DynamicLibrary::Exception& ex) {
89     std::string error = ex.getSystemError();
90     EXPECT_FALSE(error.empty());
91   }
92 }
93 
94 } // namespace LicqTest
95