summaryrefslogtreecommitdiff
path: root/cpp/test/IceUtil/stacktrace/Client.cpp
blob: 24cba1edfa20a35a9e74c50973e0ebd480ae2301 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// **********************************************************************
//
// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <IceUtil/StringUtil.h>
#include <TestCommon.h>

#include <fstream>

using namespace IceUtil;
using namespace std;

#if defined(__GNUC__) && !defined(__sun) && !defined(__FreeBSD__) && !defined(__MINGW32__) && \
    !(defined(__ARMEL__) && defined(__linux))
#  define HAS_STACK_TRACES
#endif

#if defined(_WIN32) && !defined(ICE_OS_WINRT) && !defined(__MINGW32__)
#  define HAS_STACK_TRACES
#endif

namespace IceUtilInternal
{
extern bool ICE_UTIL_API printStackTraces;
}

namespace
{
class Thrower : public IceUtil::Shared
{
public:

    Thrower() : _idx(0)
    {
    }

    void first()
    {
        _idx++;
        second();
    }

    void second()
    {
        _idx++;
        third();
    }

    void third()
    {
        _idx++;
        forth();
    }

    void forth()
    {
        _idx++;
        fifth();
    }

    void fifth()
    {
        _idx++;
        throw IceUtil::NullHandleException(__FILE__, __LINE__);
    }

private:

    int _idx;
};
typedef IceUtil::Handle<Thrower> ThrowerPtr;

#ifdef _WIN32
string
getIceHome()
{
    vector<wchar_t> buf(256);
    DWORD ret = GetEnvironmentVariableW(L"ICE_HOME", &buf[0], static_cast<DWORD>(buf.size()));
    string iceHome = (ret > 0 && ret < buf.size()) ? IceUtil::wstringToString(&buf[0]) : string("");
    if(!iceHome.empty())
    {
	return iceHome;
    }
    else
    {
        HKEY hKey;

        string key = string("SOFTWARE\\ZeroC\\Ice ") + ICE_STRING_VERSION;
        const wstring keyName = IceUtil::stringToWstring(key);

        if(RegOpenKeyExW(HKEY_LOCAL_MACHINE, keyName.c_str(), 0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
        {
            return "";
        }

        WCHAR buf[512];
        DWORD bufSize = sizeof(buf);
        if(RegQueryValueExW(hKey, L"InstallDir", 0, NULL, (LPBYTE)buf, &bufSize) != ERROR_SUCCESS)
        {
            return "";
        }
        return IceUtil::wstringToString(wstring(buf));
    }
}
#endif

#if defined(__APPLE__)
void
standardizeVersion(string& str)
{
    string v1(ICE_STRING_VERSION);

    vector<string> split;
    IceUtilInternal::splitString(v1, ".", split);
    string v2(split[0] + split[1]);

    size_t pos = 0;
    while((pos = str.find(v1, pos)) != string::npos)
    {
        str.replace(pos, v1.length(), v2);
        pos += v2.length();
    }
}
#else
vector<string>
splitLines(const string& str)
{
    vector<string> result;
    istringstream is(str);
    string line;
    while(std::getline(is, line)) {
	result.push_back(line);
    };
    return result;
}
#endif
}

int main(int argc, char* argv[])
{
#ifdef HAS_STACK_TRACES

    bool optimized = false;
#ifdef NDEBUG
    optimized = true;
#endif

#if defined(_WIN32)
    bool binDist = false;
    vector<wchar_t> buf(256);
    DWORD ret = GetEnvironmentVariableW(L"USE_BIN_DIST", &buf[0], static_cast<DWORD>(buf.size()));
    string valstr = (ret > 0 && ret < buf.size()) ? IceUtil::wstringToString(&buf[0]) : string("");
    binDist = valstr == "yes";

    if(binDist)
    {
        //
        // For Windows we only run the test against bindist if PDBs were installed
        //
    	string pdb = getIceHome() + "\\bin\\icebox.pdb";
    	if(!ifstream(pdb))
    	{
    	   cout << "Test requires PDBs to be installed" << endl;
	       return EXIT_SUCCESS;
	   }
    }
    else if(optimized)
    {
        //
        // Only support debug srcdist Windows builds
        //
        return EXIT_SUCCESS;
    }
#endif

    cout << "checking stacktrace... ";

    IceUtilInternal::printStackTraces = true;

    string filename = "StackTrace.";

#if defined(__APPLE__)
    bool binDist = false;
    const char* s = getenv("USE_BIN_DIST");
    if(s && *s != '\0')
    {
       binDist = string(s) == "yes";
    }

    if(binDist && !optimized)
    {
        filename += "debug-release";
    }
    else if(optimized)
#else
    if(optimized)
#endif
    {
        filename += "release";
#if defined(_MSC_VER) && (_MSC_VER == 1800)
        filename += "-vc120";
#endif
    }
    else
    {
        filename += "debug";
    }

#if defined(_WIN32)
    filename += ".Win32";
#elif defined(__APPLE__)
    filename += ".OSX";
#else
    filename += ".Linux";
#endif

    ifstream ifs(filename.c_str());
    stringstream sstr;
    sstr << ifs.rdbuf();
#if defined(__APPLE__)
    string expected = sstr.str();
    standardizeVersion(expected);
#else
    vector<string> expected = splitLines(sstr.str());
#endif

    ThrowerPtr thrower = new Thrower();
    try
    {
        thrower->first();
    }
    catch(const IceUtil::Exception& ex)
    {
        string stack = ex.ice_stackTrace();
#ifdef __APPLE__
        standardizeVersion(stack);
        if(expected.size() < stack.size())
        {
            test(stack.compare(0, expected.size(), expected) == 0);
        }
        else
        {
            test(stack == expected);
        }
#else
        vector<string> actual = splitLines(stack);
	    test(expected.size() <= actual.size());
	    for(size_t i = 0; i < expected.size(); ++i)
	    {
            test(actual[i].find(expected[i]) != string::npos);
	    }
#endif
    }
    cout << "ok" << endl;
#else
    cout << "Test not supported on this platform" << endl;
#endif

    return EXIT_SUCCESS;
}