File: HTTPCacheTest.cc

package info (click to toggle)
libdap 3.20.11-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 24,568 kB
  • sloc: cpp: 50,809; sh: 41,536; xml: 23,511; ansic: 20,030; yacc: 2,508; exp: 1,544; makefile: 990; lex: 309; perl: 52; fortran: 8
file content (866 lines) | stat: -rw-r--r-- 31,901 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
// -*- 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 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 <unistd.h>   // for access stat
#include <sys/types.h>
#include <sys/stat.h>

#include <cstdio>     // for create_cache_root_test
#include <string>
#include <vector>
#include <algorithm>
#include <memory>
#include <iterator>

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

#include "HTTPCache.h"
#include "HTTPConnect.h"	// Used to generate a response to cache.
#ifndef WIN32			// Signals are exquisitely non-portable.
#include "SignalHandler.h"	// Needed to clean up this singleton.
#endif
#include "RCReader.h"		// ditto

#include "debug.h"
#include "run_tests_cppunit.h"
#include "test_config.h"

using namespace CppUnit;
using namespace std;

#ifdef WIN32
#define F_OK 0
#define W_OK 2
#endif

namespace libdap {

inline static int file_size(string name)
{
    struct stat s;
    stat(name.c_str(), &s);
    return s.st_size;
}

// Note that because this test class uses the fixture 'hc' we must always
// force access to the single user/process lock for the cache. This is
// because a fixture is always created (by setUp) *before* the body of the
// test is run. So by the time we're at the first line of the test, The
// persistent store's lock has already been grabbed. 10/14/02 jhrg

class HTTPCacheTest: public TestFixture {
private:
    HTTPCache *hc;
    HTTPConnect *http_conn;
    string index_file_line;
    string localhost_url;
    string expired;
    int hash_value;
    vector<string> h;

protected:

public:
    HTTPCacheTest() :
        hc(0), http_conn(0)
    {
        putenv((char*) "DODS_CONF=./cache-testsuite/dodsrc");
        http_conn = new HTTPConnect(RCReader::instance());

        DBG2(cerr << "Entering HTTPCacheTest ctor... ");
        hash_value = 656;
        localhost_url = "http://test.opendap.org/test-304.html";
        index_file_line =
            "http://test.opendap.org/test-304.html cache-testsuite/dods_cache/656/dodsKbcD0h \"3f62c-157-139c2680\" 1121283146 -1 343 0 656 1 7351 1121360379 3723 0";

        expired = "http://test.opendap.org/cgi-bin/expires.sh";

        h.push_back("ETag: jhrgjhrgjhrg");
        h.push_back("Last-Modified: Sat, 05 Nov 1994 08:49:37 GMT");
        h.push_back("Expires: Mon, 07 Nov 1994 08:49:37 GMT");
        h.push_back("Date: Sun, 06 Nov 1994 08:49:37 GMT");
        DBG2(cerr << "exiting." << endl);
    }

    ~HTTPCacheTest()
    {
        delete http_conn;
        http_conn = 0;
        DBG2(cerr << "Entering the HTTPCacheTest dtor... ");DBG2(cerr << "exiting." << endl);
    }

#if 0
    static inline bool
    is_hop_by_hop_header(const string &header) {
        return header.find("Connection") != string::npos
        || header.find("Keep-Alive") != string::npos
        || header.find("Proxy-Authenticate") != string::npos
        || header.find("Proxy-Authorization") != string::npos
        || header.find("Transfer-Encoding") != string::npos
        || header.find("Upgrade") != string::npos;
    }
#endif

    void setUp()
    {
        // Called before every test.
        DBG2(cerr << "Entering HTTPCacheTest::setUp... " << endl);
        hc = new HTTPCache("cache-testsuite/dods_cache/", true);
        DBG2(cerr << "exiting setUp" << endl);
    }

    void tearDown()
    {
        // Called after every test.
        DBG2(cerr << "Entering HTTPCacheTest::tearDown... " << endl);
        delete hc;
        hc = 0;
        DBG2(cerr << "exiting tearDown" << endl);
    }

    CPPUNIT_TEST_SUITE (HTTPCacheTest);

    CPPUNIT_TEST (constructor_test);
    CPPUNIT_TEST (cache_index_read_test);
    CPPUNIT_TEST (cache_index_parse_line_test);
    CPPUNIT_TEST (get_entry_from_cache_table_test);
    CPPUNIT_TEST (cache_index_write_test);
    CPPUNIT_TEST (create_cache_root_test);
    CPPUNIT_TEST (set_cache_root_test);
    CPPUNIT_TEST (get_single_user_lock_test);

    CPPUNIT_TEST (release_single_user_lock_test);
    CPPUNIT_TEST (create_hash_directory_test);
    CPPUNIT_TEST (create_location_test);
    CPPUNIT_TEST (parse_headers_test);

    CPPUNIT_TEST (calculate_time_test);
    CPPUNIT_TEST (write_metadata_test);
    CPPUNIT_TEST (cache_response_test);
#if 0
    // This test does not seem to work in New Zealand - maybe because
    // of the dateline??? jhrg 1/31/13
    CPPUNIT_TEST(is_url_valid_test);
#endif
    CPPUNIT_TEST (get_cached_response_test);

    CPPUNIT_TEST (perform_garbage_collection_test);
    CPPUNIT_TEST (purge_cache_and_release_cached_response_test);
    CPPUNIT_TEST (get_conditional_response_headers_test);
    CPPUNIT_TEST (update_response_test);
    CPPUNIT_TEST (cache_gc_test);

    // Make this the last test because when distcheck is run, running
    // it before other tests will break them.
    CPPUNIT_TEST (instance_test);

    CPPUNIT_TEST_SUITE_END();

    void constructor_test()
    {
        DBG(cerr << "hc->cache_index: " << hc->d_http_cache_table->d_cache_index << endl);
        CPPUNIT_ASSERT(hc->d_http_cache_table->d_cache_index == "cache-testsuite/dods_cache/.index");
        CPPUNIT_ASSERT(hc->d_cache_root == "cache-testsuite/dods_cache/");
        DBG(cerr << "Current size: " << hc->d_http_cache_table->d_current_size << endl);
        DBG(cerr << "Block size: " << hc->d_http_cache_table->d_block_size << endl);
        CPPUNIT_ASSERT(hc->d_http_cache_table->d_current_size == hc->d_http_cache_table->d_block_size);
    }

    void cache_index_read_test()
    {
        CPPUNIT_ASSERT(hc->d_http_cache_table->cache_index_read());

        HTTPCacheTable::CacheEntry *e = hc->d_http_cache_table->get_locked_entry_from_cache_table(localhost_url);

        CPPUNIT_ASSERT(e);
        CPPUNIT_ASSERT(e->url == localhost_url);
        e->unlock_read_response();
    }

    void cache_index_parse_line_test()
    {
        HTTPCacheTable::CacheEntry *e = hc->d_http_cache_table->cache_index_parse_line(index_file_line.c_str());

        CPPUNIT_ASSERT(e->url == localhost_url);
        CPPUNIT_ASSERT(e->cachename == "cache-testsuite/dods_cache/656/dodsKbcD0h");
#ifdef WIN32
        char *tmpstr = "\"3f62c-157-139c2680\"";
        CPPUNIT_ASSERT(e->etag == tmpstr);
#else
        CPPUNIT_ASSERT(e->etag == "\"3f62c-157-139c2680\"");
#endif
        CPPUNIT_ASSERT(e->lm == 1121283146);
        // Skip ahead ...
        CPPUNIT_ASSERT(e->must_revalidate == false);

        delete e;
        e = 0;
    }

    // This will also test the add_entry_to_cache_table() method.
    void get_entry_from_cache_table_test()
    {
        HTTPCacheTable::CacheEntry *e = hc->d_http_cache_table->cache_index_parse_line(index_file_line.c_str());

        // Test adding an entry and getting it back.
        hc->d_http_cache_table->add_entry_to_cache_table(e);

        HTTPCacheTable::CacheEntry *e2 = hc->d_http_cache_table->get_locked_entry_from_cache_table(localhost_url);
        CPPUNIT_ASSERT(e2);
        CPPUNIT_ASSERT(e2->url == localhost_url);
        e2->unlock_read_response();

        // Now test what happens when two entries collide.
        HTTPCacheTable::CacheEntry *e3 = hc->d_http_cache_table->cache_index_parse_line(index_file_line.c_str());

        // Change the url so we can tell the difference (the hash is the same)
        e3->url = "http://new.url.same.hash/test/collisions.gif";

        hc->d_http_cache_table->add_entry_to_cache_table(e3);

        // Use the version of get_entry... that lets us pass in the hash
        // value (as opposed to the normal version which calculates the hash
        // from the url. 10/01/02 jhrg
        HTTPCacheTable::CacheEntry *g = hc->d_http_cache_table->get_locked_entry_from_cache_table(hash_value, e3->url);
        CPPUNIT_ASSERT(g);
        CPPUNIT_ASSERT(g->url == e3->url);
        g->unlock_read_response();

        g = hc->d_http_cache_table->get_locked_entry_from_cache_table("http://not.in.table/never.x");
        CPPUNIT_ASSERT(g == 0);
    }

    void cache_index_write_test()
    {
        try {
            HTTPCache * hc_3 = new HTTPCache("cache-testsuite/dods_cache/", true);
            hc_3->d_http_cache_table->add_entry_to_cache_table(
                hc->d_http_cache_table->cache_index_parse_line(index_file_line.c_str()));

            hc_3->d_http_cache_table->d_cache_index = hc->d_cache_root + "test_index";
            hc_3->d_http_cache_table->cache_index_write();

            HTTPCache *hc_4 = new HTTPCache("cache-testsuite/dods_cache/", true);
            hc_4->d_http_cache_table->d_cache_index = hc_3->d_cache_root + "test_index";
            hc_4->d_http_cache_table->cache_index_read();

            HTTPCacheTable::CacheEntry *e = hc_4->d_http_cache_table->get_locked_entry_from_cache_table(localhost_url);
            DBG(cerr << "Got locked entry" << endl);
            CPPUNIT_ASSERT(e);
            CPPUNIT_ASSERT(e->url == localhost_url);
            e->unlock_read_response();

            delete hc_3;
            hc_3 = 0;
            delete hc_4;
            hc_4 = 0;
        }
        catch (Error &e) {
            //cerr << "Fail: " << e.get_error_message() << endl;
            CPPUNIT_FAIL(e.get_error_message());
        }
    }

    void create_cache_root_test()
    {
        hc->create_cache_root("/tmp/silly/");
        CPPUNIT_ASSERT(access("/tmp/silly/", F_OK) == 0);
        remove("/tmp/silly");
#if 0
        // This test doesn't work on some machines where the build is
        // run as root or where /root is owned by some other user (as is
        // the case with OS/X.
        try {
            hc->create_cache_root("/root/very_silly/");
            access("/root/very_silly/", F_OK);
            remove("/root/very_silly/");
            CPPUNIT_ASSERT(!"Should not be able to do this...");
        }
        catch (Error &e) {
            CPPUNIT_ASSERT("This is where we want to be");
            CPPUNIT_ASSERT(access("/root/very_silly/", F_OK) != 0);
        }
#endif
    }

    void set_cache_root_test()
    {
#if 0
        // env var support removed 3/22/11 jhrg
        putenv("DODS_CACHE=/home/jimg");
        hc->set_cache_root();
        CPPUNIT_ASSERT(hc->d_cache_root == "/home/jimg/dods-cache/");
        remove("/home/jimg/w3c-cache/");
#endif
        hc->set_cache_root("/home/jimg/test_cache");
        CPPUNIT_ASSERT(hc->d_cache_root == "/home/jimg/test_cache/");
        remove("/home/jimg/test_cache/");
    }

    void get_single_user_lock_test()
    {
        hc->set_cache_root("/tmp/dods_test_cache");
        hc->release_single_user_lock();

        CPPUNIT_ASSERT(hc->get_single_user_lock());
        CPPUNIT_ASSERT(access("/tmp/dods_test_cache/.lock", F_OK) == 0);

        // Second time should fail
        CPPUNIT_ASSERT(!hc->get_single_user_lock());
    }

    void release_single_user_lock_test()
    {
        hc->set_cache_root("/tmp/dods_test_cache");
        remove("/tmp/dods_test_cache/.lock"); // in case prev. test fails
        hc->d_locked_open_file = 0;

        CPPUNIT_ASSERT(hc->get_single_user_lock());
        CPPUNIT_ASSERT(access("/tmp/dods_test_cache/.lock", F_OK) == 0);

        hc->release_single_user_lock();
        CPPUNIT_ASSERT(hc->get_single_user_lock());
        CPPUNIT_ASSERT(access("/tmp/dods_test_cache/.lock", F_OK) == 0);

        CPPUNIT_ASSERT(!hc->get_single_user_lock());

        remove("/tmp/dods_test_cache/.lock");
    }

    void create_hash_directory_test()
    {
        hc->set_cache_root("/tmp/dods_test_cache");
        CPPUNIT_ASSERT(hc->d_http_cache_table->create_hash_directory(391) == "/tmp/dods_test_cache/391");
        CPPUNIT_ASSERT(access("/tmp/dods_test_cache/391", W_OK) == 0);
#if 0
        // This test doesn't work on some machines where the build is
        // run as root or where /root is owned by some other user (as is
        // the case with OS/X.
        hc->set_cache_root("/root/");
        try {
            hc->create_hash_directory(391);
            CPPUNIT_ASSERT(!"Create in bad directory");
        }
        catch (Error &e) {
        }
#endif
        remove("/tmp/dods_test_cache/391");

    }

    void create_location_test()
    {
        hc->set_cache_root("/tmp/dods_test_cache");
        HTTPCacheTable::CacheEntry *e = new HTTPCacheTable::CacheEntry;
        e->url = localhost_url;
        e->hash = hash_value;
        try {
            hc->d_http_cache_table->create_location(e);
            CPPUNIT_ASSERT(e->cachename != "");
        }
        catch (Error &e) {
            CPPUNIT_ASSERT(true && "could not create entry file");
        }
        remove(e->cachename.c_str());

        delete e;
        e = 0;
    }

    void parse_headers_test()
    {
        HTTPCacheTable::CacheEntry *e = new HTTPCacheTable::CacheEntry;

        hc->d_http_cache_table->parse_headers(e, hc->d_max_entry_size, h);
        CPPUNIT_ASSERT(e->lm == 784025377);

        delete e;
        e = 0;
    }

    void calculate_time_test()
    {
        HTTPCacheTable::CacheEntry *e = new HTTPCacheTable::CacheEntry;

        hc->d_http_cache_table->parse_headers(e, hc->d_max_entry_size, h);
        hc->d_http_cache_table->calculate_time(e, hc->d_default_expiration, time(0));
        CPPUNIT_ASSERT(e->corrected_initial_age > 249300571);
        CPPUNIT_ASSERT(e->freshness_lifetime == 86400);

        delete e;
        e = 0;
    }

    void write_metadata_test()
    {
        hc->set_cache_root("/tmp/dods_test_cache");
        HTTPCacheTable::CacheEntry *e = new HTTPCacheTable::CacheEntry;
        try {
            e->hash = 101;
            hc->d_http_cache_table->create_location(e);
            CPPUNIT_ASSERT(e->cachename != "");
        }
        catch (Error &e) {
            CPPUNIT_ASSERT(true && "could not create entry file");
        }

        hc->write_metadata(e->cachename, h);
        vector<string> headers;
        hc->read_metadata(e->cachename, headers);

        vector<string>::iterator i, j;
        for (i = headers.begin(), j = h.begin(); i != headers.end() && j != h.end(); ++i, ++j) {
            CPPUNIT_ASSERT(*i == *j);
        }

        remove(e->cachename.c_str());
        remove(string(e->cachename + ".meta").c_str());
        delete e;
        e = 0;
    }

    void cache_response_test()
    {
        HTTPResponse *rs = http_conn->fetch_url(localhost_url);
        try {
            time_t now = time(0);
            vector<string> *headers = rs->get_headers();
            hc->cache_response(localhost_url, now, *headers, rs->get_stream());

            CPPUNIT_ASSERT(hc->is_url_in_cache(localhost_url));

            HTTPCacheTable::CacheEntry *e = hc->d_http_cache_table->get_locked_entry_from_cache_table(localhost_url);
            CPPUNIT_ASSERT(file_size(e->cachename) == 343);
            e->unlock_read_response();
            delete rs;
            rs = 0;
        }
        catch (Error &e) {
            delete rs;
            rs = 0;
            cerr << "Error: " << e.get_error_message() << endl;
            CPPUNIT_ASSERT(!"Caught unexpected Error/InternalErr");
        }
    }

    void is_url_valid_test()
    {
        cache_response_test(); // This should get a response into the cache.
        CPPUNIT_ASSERT(hc->is_url_valid(localhost_url));
    }

    void get_cached_response_test()
    {
        cache_response_test(); // Get a response into the cache.
        vector<string> cached_headers;
        FILE *cached_body = hc->get_cached_response(localhost_url, cached_headers);

        HTTPResponse *rs = http_conn->fetch_url(localhost_url);
        vector<string> *headers = rs->get_headers();

        // headers and cached_headers should match, except for the values.
        vector<string>::iterator i, j;
        for (i = cached_headers.begin(), j = headers->begin(); i != cached_headers.end() && j != headers->end();
            ++i, ++j) {
            string ch = (*i).substr(0, (*i).find(": "));
            // Skip over headers that won't be cached. jhrg 7/4/05
            while (is_hop_by_hop_header(*j))
                ++j;
            string h = (*j).substr(0, (*j).find(": "));
            DBG(cerr << "cached: " << ch << ", header: " << h << endl);
            CPPUNIT_ASSERT(ch == h);
        }

#ifdef DODS_DEBUG
#include <iterator>
        std::ostream_iterator<string> out_it(std::cerr, "\n");
        cerr << "Cached headers: ";
        std::copy(cached_headers.begin(), cached_headers.end(), out_it);
        cerr << "Headers: ";
        std::copy(headers->begin(), headers->end(), out_it);
#endif

        CPPUNIT_ASSERT(i == cached_headers.end());
        // This may not be true if. For example, keep-alive might appear in the list of headers
        // received, but not in the list of headers cached.
        // CPPUNIT_ASSERT(j == headers->end());

        // every byte of the cached_body and response body should match.
        while (!feof(rs->get_stream()) && !feof(cached_body) && !ferror(rs->get_stream()) && !ferror(cached_body)) {
            char cb, b;
            int cn = fread(&cb, 1, 1, cached_body);
            int n = fread(&b, 1, 1, rs->get_stream());
            CPPUNIT_ASSERT(cn == n);
            if (cn == 1) CPPUNIT_ASSERT(cb == b);
        }
        CPPUNIT_ASSERT(feof(rs->get_stream()) && feof(cached_body));

        hc->release_cached_response(cached_body);
        delete rs;
        rs = 0;
    }

    void perform_garbage_collection_test()
    {
        try {
            delete hc;
            hc = 0;
            unique_ptr<HTTPCache> gc(new HTTPCache("cache-testsuite/gc_cache", true));
            DBG(cerr << "get_cache_root: " << gc->get_cache_root() << endl);

            HTTPResponse *rs = http_conn->fetch_url(localhost_url);
            gc->cache_response(localhost_url, time(0), *(rs->get_headers()), rs->get_stream());
            CPPUNIT_ASSERT(gc->is_url_in_cache(localhost_url));
            delete rs;
            rs = 0;

            rs = http_conn->fetch_url(expired);
            gc->cache_response(expired, time(0), *(rs->get_headers()), rs->get_stream());
            CPPUNIT_ASSERT(gc->is_url_in_cache(expired));
            delete rs;
            rs = 0;

            sleep(2);

            gc->perform_garbage_collection();
            gc->d_http_cache_table->cache_index_write();

            CPPUNIT_ASSERT(
                !gc->is_url_in_cache(expired) && "This may fail if sleep is not long enough before gc above");
        }
        catch (Error &e) {
            cerr << "Exception: " << e.get_error_message() << endl;
            CPPUNIT_ASSERT(false);
        }
    }

    void purge_cache_and_release_cached_response_test()
    {
        try {
            unique_ptr<HTTPCache> pc(new HTTPCache("cache-testsuite/purge_cache", true));
            DBG(cerr << "get_cache_root: " << pc->get_cache_root() << endl);

            time_t now = time(0);
            HTTPResponse *rs = http_conn->fetch_url(localhost_url);
            pc->cache_response(localhost_url, now, *(rs->get_headers()), rs->get_stream());

            CPPUNIT_ASSERT(pc->is_url_in_cache(localhost_url));
            delete rs;
            rs = 0;

            string expired = "http://test.opendap.org/cgi-bin/expires.sh";
            now = time(0);
            rs = http_conn->fetch_url(expired);
            pc->cache_response(expired, now, *(rs->get_headers()), rs->get_stream());

            CPPUNIT_ASSERT(pc->is_url_in_cache(expired));
            delete rs;
            rs = 0;

            HTTPCacheTable::CacheEntry *e1 = pc->d_http_cache_table->get_locked_entry_from_cache_table(expired);
            HTTPCacheTable::CacheEntry *e2 = pc->d_http_cache_table->get_locked_entry_from_cache_table(localhost_url);
            string e1_file = e1->cachename;
            string e2_file = e2->cachename;
            e1->unlock_read_response();
            e2->unlock_read_response();

            vector<string> headers;
            FILE *b = pc->get_cached_response(expired, headers);

            try {
                pc->purge_cache();
                CPPUNIT_ASSERT(!"This call should throw Error");
            }
            catch (Error &e) {
                CPPUNIT_ASSERT("Caught Error as expected");
            }

            pc->release_cached_response(b);

            pc->purge_cache();

            CPPUNIT_ASSERT(!pc->is_url_in_cache(localhost_url));
            CPPUNIT_ASSERT(!pc->is_url_in_cache(expired));
            CPPUNIT_ASSERT(access(e1_file.c_str(), F_OK) != 0);
            CPPUNIT_ASSERT(access(e2_file.c_str(), F_OK) != 0);
            CPPUNIT_ASSERT(pc->d_http_cache_table->d_current_size == 0);
        }
        catch (Error &e) {
            cerr << "Exception: " << e.get_error_message() << endl;
            CPPUNIT_ASSERT(false);
        }
    }

    void instance_test()
    {
        try {
            // FIXME: Explain
            HTTPCache::delete_instance();

            HTTPCache *c = HTTPCache::instance("cache-testsuite/singleton_cache", true);
            DBG(cerr << "get_cache_root: " << c->get_cache_root() << endl);

            if (!c->is_url_in_cache(localhost_url)) {
                HTTPResponse *rs = http_conn->fetch_url(localhost_url);
                c->cache_response(localhost_url, time(0), *(rs->get_headers()), rs->get_stream());
                delete rs;
                rs = 0;
            }
            CPPUNIT_ASSERT(c->is_url_in_cache(localhost_url));

            if (!c->is_url_in_cache(expired)) {
                HTTPResponse *rs = http_conn->fetch_url(expired);
                c->cache_response(expired, time(0), *(rs->get_headers()), rs->get_stream());
                delete rs;
                rs = 0;
            }
            CPPUNIT_ASSERT(c->is_url_in_cache(expired));

            HTTPCacheTable::CacheEntry *e1 = c->d_http_cache_table->get_locked_entry_from_cache_table(expired);
            HTTPCacheTable::CacheEntry *e2 = c->d_http_cache_table->get_locked_entry_from_cache_table(localhost_url);
            string e1_file = e1->cachename;
            string e2_file = e2->cachename;
            e1->unlock_read_response();
            e2->unlock_read_response();

            c->purge_cache();

            CPPUNIT_ASSERT(!c->is_url_in_cache(localhost_url));
            CPPUNIT_ASSERT(!c->is_url_in_cache(expired));
            CPPUNIT_ASSERT(access(e1_file.c_str(), F_OK) != 0);
            CPPUNIT_ASSERT(access(e2_file.c_str(), F_OK) != 0);
        }
        catch (Error &e) {
            cerr << "Exception: " << e.get_error_message() << endl;
            CPPUNIT_ASSERT(false);
        }

        // Call this here to simulate exiting the program. This ensures that
        // the next test's call to instance() gets a fresh cache. The static
        // method will still be run at exit, but that's OK since it tests the
        // value of _instance and simply returns with it's zero.
        HTTPCache::delete_instance();
#ifndef WIN32
        SignalHandler::delete_instance();
#endif
    }

    void get_conditional_response_headers_test()
    {
        try {
            unique_ptr<HTTPCache> c(new HTTPCache("cache-testsuite/header_cache", true));
            DBG(cerr << "get_cache_root: " << c->get_cache_root() << endl);

            CPPUNIT_ASSERT(c->get_cache_root() == "cache-testsuite/header_cache/");
            if (!c->is_url_in_cache(localhost_url)) {
                HTTPResponse *rs = http_conn->fetch_url(localhost_url);
                c->cache_response(localhost_url, time(0), *(rs->get_headers()), rs->get_stream());
                delete rs;
            }
            CPPUNIT_ASSERT(c->is_url_in_cache(localhost_url));

            if (!c->is_url_in_cache(expired)) {
                HTTPResponse *rs = http_conn->fetch_url(expired);
                c->cache_response(expired, time(0), *(rs->get_headers()), rs->get_stream());
                delete rs;
            }
            CPPUNIT_ASSERT(c->is_url_in_cache(expired));

            vector<string> h = c->get_conditional_request_headers(localhost_url);
            DBG(copy(h.begin(), h.end(), ostream_iterator<string>(cout, "\n")));
            DBG(cerr << "if none match location: " << h[0].find("If-None-Match: ") << endl);
            // I know what the strings should start with...
            CPPUNIT_ASSERT(h[0].find("If-None-Match: ") == 0);

            h = c->get_conditional_request_headers(expired);
            DBG(cerr << "Number of headers: " << h.size() << endl);
            DBG(copy(h.begin(), h.end(), ostream_iterator<string>(cout, "\n")));
            CPPUNIT_ASSERT(h[0].find("If-Modified-Since: ") == 0);
        }
        catch (Error &e) {
            CPPUNIT_FAIL(e.get_error_message());
        }
    }

    void update_response_test()
    {
        try {
            unique_ptr<HTTPCache> c(new HTTPCache("cache-testsuite/singleton_cache", true));
            DBG(cerr << "get_cache_root: " << c->get_cache_root() << endl);

            if (!c->is_url_in_cache(localhost_url)) {
                HTTPResponse *rs = http_conn->fetch_url(localhost_url);
                c->cache_response(localhost_url, time(0), *(rs->get_headers()), rs->get_stream());
                delete rs;
            }

            if (!c->is_url_in_cache(expired)) {
                HTTPResponse *rs = http_conn->fetch_url(expired);
                c->cache_response(expired, time(0), *(rs->get_headers()), rs->get_stream());
                delete rs;
            }

            // Yes, there's stuff here.
            CPPUNIT_ASSERT(c->is_url_in_cache(localhost_url));
            CPPUNIT_ASSERT(c->is_url_in_cache(expired));

            vector<string> orig_h;
            FILE *cr = c->get_cached_response(localhost_url, orig_h);

            DBG(copy(orig_h.begin(), orig_h.end(), ostream_iterator<string>(cerr, "\n")));

            // Before we merge, et c., check that the headers we're going to
            // poke in aren't already there.
            CPPUNIT_ASSERT(find(orig_h.begin(), orig_h.end(), "XHTTPCache: 123456789") == orig_h.end());
            CPPUNIT_ASSERT(find(orig_h.begin(), orig_h.end(), "Date: <invalid date>") == orig_h.end());

            // Make up some new headers.
            vector<string> new_h;
            new_h.push_back("XHTTPCache: 123456789");
            new_h.push_back("Date: <invalid date>");

            c->release_cached_response(cr);

            c->update_response(localhost_url, time(0), new_h);

            vector<string> updated_h;
            cr = c->get_cached_response(localhost_url, updated_h);
            c->release_cached_response(cr);

            DBG(cerr << endl);
            DBG(copy(updated_h.begin(), updated_h.end(), ostream_iterator<string>(cerr, "\n")));

            // The XHTTPCacheTest header should be new, Date should replace the
            // existing Date header.
            // This may not be true when using distcheck and/or when the user
            // has set USE_CACHE to 1 in their .dodsrc. jhrg 9/29/15
            // CPPUNIT_ASSERT(orig_h.size() + 1 == updated_h.size());
            CPPUNIT_ASSERT(find(updated_h.begin(), updated_h.end(), "XHTTPCache: 123456789") != updated_h.end());
            CPPUNIT_ASSERT(find(updated_h.begin(), updated_h.end(), "Date: <invalid date>") != updated_h.end());
        }
        catch (Error &e) {
            CPPUNIT_FAIL(e.get_error_message());
        }
    }

    // Only run this interactively since you need to hit Ctrl-c to generate
    // SIGINT while the cache is doing its thing. 02/10/04 jhrg
    void interrupt_test()
    {
        try {
            unique_ptr<HTTPCache> c(new HTTPCache("cache-testsuite/singleton_cache", true));
            string coads = "http://test.opendap.org/dap/data/nc/coads_climatology.nc";
            if (!c->is_url_in_cache(coads)) {
                HTTPResponse *rs = http_conn->fetch_url(coads);
                cerr << "In interrupt test, hit ctrl-c now... ";
                c->cache_response(coads, time(0), *(rs->get_headers()), rs->get_stream());
                cerr << "to late.";
                delete rs;
            }
        }
        catch (Error &e) {
            CPPUNIT_FAIL(e.get_error_message());
        }
    }

    void cache_gc_test()
    {
        string fnoc1 = "http://test.opendap.org/dap/data/nc/fnoc1.nc.dds";
        string jan = "http://test.opendap.org/dap/data/nc/jan.nc.dds";
        string feb = "http://test.opendap.org/dap/data/nc/feb.nc.dds";
        try {
            unique_ptr<HTTPCache> pc(new HTTPCache("cache-testsuite/purge_cache", true));
#if 0
            // This broke Fedora ppc64le system with XFS system
            CPPUNIT_ASSERT(pc->d_http_cache_table->d_block_size == 4096);
#endif
            // Change the size parameters so that we can run some tests
            pc->d_total_size = 12288; // bytes
            pc->d_folder_size = pc->d_total_size / 10;
            pc->d_gc_buffer = pc->d_total_size / 10;

            // The cache should start empty
            CPPUNIT_ASSERT(pc->d_http_cache_table->d_current_size == 0);

            // Get a url
            HTTPResponse *rs = http_conn->fetch_url(fnoc1);
            pc->cache_response(fnoc1, time(0), *(rs->get_headers()), rs->get_stream());
            CPPUNIT_ASSERT(pc->is_url_in_cache(fnoc1));
            delete rs;
            rs = 0;
            // trigger a hit for fnoc1
            vector<string> h;
            FILE *f = pc->get_cached_response(fnoc1, h);
            pc->release_cached_response(f);

            rs = http_conn->fetch_url(jan);
            pc->cache_response(jan, time(0), *(rs->get_headers()), rs->get_stream());
            CPPUNIT_ASSERT(pc->is_url_in_cache(jan));
            delete rs;
            rs = 0;
            // trigger two hits for jan
            f = pc->get_cached_response(jan, h);
            pc->release_cached_response(f);
            f = pc->get_cached_response(jan, h);
            pc->release_cached_response(f);

            rs = http_conn->fetch_url(feb);
            pc->cache_response(feb, time(0), *(rs->get_headers()), rs->get_stream());
            CPPUNIT_ASSERT(pc->is_url_in_cache(feb));
            delete rs;
            rs = 0;
        }
        catch (Error &e) {
            CPPUNIT_FAIL(e.get_error_message());
        }

        // now that pc is out of scope, its dtor has been run and GC
        // performed. The feb URL should have been deleted.

        try {
            unique_ptr<HTTPCache> pc(new HTTPCache("cache-testsuite/purge_cache", true));
            CPPUNIT_ASSERT(!pc->is_url_in_cache(feb));
        }
        catch (Error &e) {
            CPPUNIT_FAIL(e.get_error_message());
        }
    }

};

CPPUNIT_TEST_SUITE_REGISTRATION (HTTPCacheTest);

} // namespace libdap

int main(int argc, char*argv[])
{
    // Run cleanup here, so that the first run works (since this code now
    // sets up the tests).
    // This gives valgrind fits...
    system("cd cache-testsuite && ./cleanup.sh");

    return run_tests<libdap::HTTPCacheTest>(argc, argv) ? 0: 1;
}