File: RCReaderTest.cc

package info (click to toggle)
libdap 3.21.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,256 kB
  • sloc: cpp: 46,310; sh: 44,047; xml: 23,535; ansic: 19,973; yacc: 2,505; exp: 1,544; makefile: 581; lex: 309; fortran: 8
file content (348 lines) | stat: -rw-r--r-- 13,313 bytes parent folder | download
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// -*- mode: c++; c-basic-offset:4 -*-

// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.

// Copyright (c) 2002,2003,2006 OPeNDAP, Inc.
// Author: James Gallagher <jgallagher@opendap.org>
//
// 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, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.

#include <sys/stat.h>
#include <sys/types.h>

#include "config.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <cstdlib>
#include <cstring>

#include <fstream>
#include <iostream>
#include <string>

#include <cppunit/TextTestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>

// #define DODS_DEBUG
#include "RCReader.h"
#include "debug.h"
#include "run_tests_cppunit.h"
#include "test_config.h"

using namespace CppUnit;
using namespace std;

static char dods_conf_ev[1024] = "";

namespace libdap {

class RCReaderTest : public TestFixture {
private:
    RCReader *rcr;

protected:
public:
    RCReaderTest() : rcr(RCReader::instance()) {}

    void setUp() {}

    void tearDown() {}

    /** Put values in an environment variable. This should be used only
     * for the env var 'DODS_CONF' and should never pass in values longer
     * than 1024 characters. The 'value' must include the DODS_CONF= text.
     *
     * @note this is used because environment variables use static storage,
     * so any value passed in from the heap that might be freed or from the
     * stack that could be popped will cause a memory access error.
     */
    void my_putenv(const string &value) {
        strncpy(dods_conf_ev, value.c_str(), 1024);
        ;
        putenv(dods_conf_ev);
    }

    CPPUNIT_TEST_SUITE(RCReaderTest);

    CPPUNIT_TEST(check_env_var_test1);
    CPPUNIT_TEST(check_env_var_test2);
    CPPUNIT_TEST(check_env_var_test3);
    CPPUNIT_TEST(check_env_var_test4);
    CPPUNIT_TEST(check_env_var_test5);
    CPPUNIT_TEST(instance_test1);
    CPPUNIT_TEST(instance_test2);
    CPPUNIT_TEST(proxy_test1);
    CPPUNIT_TEST(proxy_test2);
    CPPUNIT_TEST(proxy_test3);
    CPPUNIT_TEST(proxy_test4);
    CPPUNIT_TEST(proxy_test5);
    CPPUNIT_TEST(validate_ssl_test);

    CPPUNIT_TEST_SUITE_END();

    void check_env_var_test1() {
        my_putenv("DODS_CONF=");
        CPPUNIT_ASSERT(rcr->check_env_var("DODS_CONF") == "");
    }

    void check_env_var_test2() {
        my_putenv("DODS_CONF=Nothing_sensible");
        CPPUNIT_ASSERT(rcr->check_env_var("DODS_CONF") == "");
    }

    void check_env_var_test3() {
        my_putenv("DODS_CONF=/etc/passwd");
        CPPUNIT_ASSERT(rcr->check_env_var("DODS_CONF") == "/etc/passwd");
    }

    void check_env_var_test4() {
        // Set DODS_CONF to the CWD plus .dodsrc, create file called .dodsrc
        // in the CWD and test to see if check_env_var finds it.
        char cwd[1024];
        getcwd(cwd, 1024);

        string rc = string(cwd) + string("/.dodsrc");
        ifstream ifp(rc.c_str()); // This should create .dodsrc in the CWD

        my_putenv(string("DODS_CONF=") + string(cwd));

        // Return existing file
        CPPUNIT_ASSERT(rcr->check_env_var("DODS_CONF") == rc);
        remove(rc.c_str());
    }

    void check_env_var_test5() {
        // In this test we *don't* create the file, just set DODS_CONF to the
        // directory and see if check_env_var() makes the RC file.
        char cwd[1024];
        getcwd(cwd, 1024);

        my_putenv(string("DODS_CONF=") + string(cwd));

        // Create the file.
        string rc = string(cwd) + string("/.dodsrc");
        CPPUNIT_ASSERT(rcr->check_env_var("DODS_CONF") == rc);
        struct stat stat_info;
        CPPUNIT_ASSERT(stat(rc.c_str(), &stat_info) == 0 && S_ISREG(stat_info.st_mode));
        remove(rc.c_str());
    }

    void instance_test1() {
        // This test assumes that HOME *is* defined. We should find the
        // .dodsrc there. If it's not there, we should create one there.
        my_putenv("DODS_CONF=");
        string home = getenv("HOME");
        if (*home.rbegin() != '/')
            home += "/";
        RCReader::delete_instance();
        RCReader::initialize_instance();
        RCReader *reader = RCReader::instance();
        CPPUNIT_ASSERT(reader->d_rc_file_path == home + string(".dodsrc"));
        DBG(cerr << "Cache root: " << reader->get_dods_cache_root() << endl);
        CPPUNIT_ASSERT(reader->get_dods_cache_root() == home + string(".dods_cache/"));
    }

    void instance_test2() {
        // Set DODS_CONF to create a .dodsrc in the CWD, then check to make
        // sure that .dodsrc has the correct cache root.
        char cwd[1024];
        getcwd(cwd, 1024);
        DBG(cerr << "CWD: " << cwd << endl);
        string rc = cwd;
        rc += "/.dodsrc";
        DBG(cerr << "RC: " << rc << endl);
        remove(rc.c_str()); // make sure the RC does not exist

        my_putenv(string("DODS_CONF=") + string(cwd));

        RCReader::delete_instance();
        RCReader::initialize_instance();
        RCReader *reader = RCReader::instance();
        DBG(cerr << "RC path: " << reader->d_rc_file_path << endl);
        CPPUNIT_ASSERT(reader->d_rc_file_path == string(cwd) + string("/.dodsrc"));
        DBG(cerr << "Cache root: " << reader->get_dods_cache_root() << endl);
        CPPUNIT_ASSERT(reader->get_dods_cache_root() == string(cwd) + string("/.dods_cache/"));
    }

    // Read the proxy info from rcreader-testsuite/test1.rc
    void proxy_test1() {
        string rc = (string) "DODS_CONF=" + TEST_SRC_DIR + "/rcreader-testsuite/test1.rc";
        DBG(cerr << "rc: " << rc << endl);
        my_putenv(rc);

        RCReader::delete_instance();
        RCReader::initialize_instance();
        RCReader *reader = RCReader::instance();
        DBG(cerr << "RC path: " << reader->d_rc_file_path << endl);
        CPPUNIT_ASSERT(reader->d_rc_file_path == (string)TEST_SRC_DIR + "/rcreader-testsuite/test1.rc");
        CPPUNIT_ASSERT(reader->get_proxy_server_protocol() == "http");

        string proxy = reader->get_proxy_server_host_url();
        CPPUNIT_ASSERT(proxy == "jimg:mypass@proxy.local.org:8080");

        CPPUNIT_ASSERT(reader->get_proxy_server_host() == "proxy.local.org");
        CPPUNIT_ASSERT(reader->get_proxy_server_port() == 8080);
        CPPUNIT_ASSERT(reader->get_proxy_server_userpw() == "jimg:mypass");

        CPPUNIT_ASSERT(reader->is_no_proxy_for_used());
        CPPUNIT_ASSERT(reader->get_no_proxy_for_protocol() == "http");
        CPPUNIT_ASSERT(reader->get_no_proxy_for_host() == "local.org");
    }

    void proxy_test2() {
        string rc = (string) "DODS_CONF=" + TEST_SRC_DIR + "/rcreader-testsuite/test2.rc";
        DBG(cerr << "rc: " << rc << endl);
        my_putenv(rc);

        RCReader::delete_instance();
        RCReader::initialize_instance();
        RCReader *reader = RCReader::instance();
        DBG(cerr << "RC path: " << reader->d_rc_file_path << endl);
        CPPUNIT_ASSERT(reader->d_rc_file_path == (string)TEST_SRC_DIR + "/rcreader-testsuite/test2.rc");
        CPPUNIT_ASSERT(reader->get_proxy_server_protocol() == "http");

        string proxy = reader->get_proxy_server_host_url();
        DBG(cerr << "get_proxy_server_host_url(): " << proxy << endl);
        CPPUNIT_ASSERT(proxy == "proxy.local.org:80");

        CPPUNIT_ASSERT(reader->get_proxy_server_host() == "proxy.local.org");
        CPPUNIT_ASSERT(reader->get_proxy_server_port() == 80);
        CPPUNIT_ASSERT(reader->get_proxy_server_userpw() == "");
    }

    void proxy_test3() {
        string rc = (string) "DODS_CONF=" + TEST_SRC_DIR + "/rcreader-testsuite/test3.rc";
        DBG(cerr << "rc: " << rc << endl);
        my_putenv(rc);

        try {
            RCReader::delete_instance();
            RCReader::initialize_instance();
            CPPUNIT_ASSERT(!"initialize_instance() should throw Error.");
        } catch (Error &e) {
            DBG(cerr << e.get_error_message() << endl);
            CPPUNIT_ASSERT(e.get_error_message() != "");
        }
    }

    void proxy_test4() {
        string rc = (string) "DODS_CONF=" + TEST_SRC_DIR + "/rcreader-testsuite/test4.rc";
        DBG(cerr << "rc: " << rc << endl);
        my_putenv(rc);

        try {
            RCReader::delete_instance();
            RCReader::initialize_instance();
            RCReader *reader = RCReader::instance();
            DBG(cerr << "RC path: " << reader->d_rc_file_path << endl);
            CPPUNIT_ASSERT(reader->d_rc_file_path == (string)TEST_SRC_DIR + "/rcreader-testsuite/test4.rc");
            CPPUNIT_ASSERT(reader->get_proxy_server_protocol() == "http");

            string proxy = reader->get_proxy_server_host_url();
            DBG(cerr << "get_proxy_server_host_url(): " << proxy << endl);
            CPPUNIT_ASSERT(proxy == "jimg:test@proxy.local.org:3128");

            CPPUNIT_ASSERT(reader->get_proxy_server_host() == "proxy.local.org");
            CPPUNIT_ASSERT(reader->get_proxy_server_port() == 3128);
            CPPUNIT_ASSERT(reader->get_proxy_server_userpw() == "jimg:test");
        } catch (Error &e) {
            DBG(cerr << e.get_error_message() << endl);
            CPPUNIT_ASSERT(e.get_error_message() != "");
        }
    }

    void proxy_test5() {
        string rc = (string) "DODS_CONF=" + TEST_SRC_DIR + "/rcreader-testsuite/test5.rc";
        DBG(cerr << "rc: " << rc << endl);
        my_putenv(rc);

        try {
            RCReader::delete_instance();
            RCReader::initialize_instance();
            RCReader *reader = RCReader::instance();
            DBG(cerr << "RC path: " << reader->d_rc_file_path << endl);
            CPPUNIT_ASSERT(reader->d_rc_file_path == (string)TEST_SRC_DIR + "/rcreader-testsuite/test5.rc");
            string proxy = reader->get_proxy_server_host_url();
            DBG(cerr << "get_proxy_server_host_url(): " << proxy << endl);
            CPPUNIT_ASSERT(reader->get_proxy_server_protocol() == "http");

            CPPUNIT_ASSERT(proxy == "jimg:test@proxy.local.org:3128");

            CPPUNIT_ASSERT(reader->get_proxy_server_host() == "proxy.local.org");
            CPPUNIT_ASSERT(reader->get_proxy_server_port() == 3128);
            CPPUNIT_ASSERT(reader->get_proxy_server_userpw() == "jimg:test");
        } catch (Error &e) {
            DBG(cerr << e.get_error_message() << endl);
            CPPUNIT_ASSERT(e.get_error_message() != "");
        }
    }

    // This simple test checks to see that the VALIDATE_SSL parameter is
    // read correctly.
    void validate_ssl_test() {
        string rc = (string) "DODS_CONF=" + TEST_SRC_DIR + "/rcreader-testsuite/dodssrc_ssl_1";
        DBG(cerr << "rc: " << rc << endl);
        my_putenv(rc);

        RCReader::delete_instance();
        RCReader::initialize_instance();
        RCReader *reader = RCReader::instance();
        reader->read_rc_file(string(TEST_SRC_DIR) + "/rcreader-testsuite/dodssrc_ssl_1");
        // No param set in file
        DBG(cerr << "reader->get_validate_ssl(): " << reader->get_validate_ssl() << endl);
        CPPUNIT_ASSERT(reader->get_validate_ssl() == 1);

        // Param set in file
        // Note that string rc, rc2, and rc3 are used (instead of reusing one
        // string object) because the code casts away the const-ness of the
        // char* returned by c_str() and putenv does odd stuff with it. There's
        // nothing good about using env vars...
        rc = (string) "DODS_CONF=" + TEST_SRC_DIR + "/rcreader-testsuite/dodssrc_ssl_2";
        DBG(cerr << "rc: " << rc << endl);

        my_putenv(rc);

        RCReader::delete_instance();
        RCReader::initialize_instance();
        reader = RCReader::instance();
        DBG(cerr << "reader->check_env_var(\"DODS_CONF\"): " << reader->check_env_var("DODS_CONF") << endl);
        DBG(cerr << "reader->get_validate_ssl(): " << reader->get_validate_ssl() << endl);
        CPPUNIT_ASSERT(reader->get_validate_ssl() == 1);

        // Param cleared in file
        rc = (string) "DODS_CONF=" + TEST_SRC_DIR + "/rcreader-testsuite/dodsrc_ssl_3";
        DBG(cerr << "rc: " << rc << endl);

        my_putenv(rc);

        RCReader::delete_instance();
        RCReader::initialize_instance();
        reader = RCReader::instance();
        DBG(cerr << "reader->get_validate_ssl(): " << reader->get_validate_ssl() << endl);
        CPPUNIT_ASSERT(reader->get_validate_ssl() == 0);
    }
};

CPPUNIT_TEST_SUITE_REGISTRATION(RCReaderTest);

} // namespace libdap

int main(int argc, char *argv[]) { return run_tests<libdap::RCReaderTest>(argc, argv) ? 0 : 1; }