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
#include "gate/tests.hpp"
#include "gate/encode/zlibstreams.hpp"
#include "gate/encode/zipstreams.hpp"

#if !defined(GATE_ENCODE_NO_ADVANCED_COMPRESSION)
#include "gate/encode/bzip2streams.hpp"
#include "gate/encode/xzstreams.hpp"
#endif

using namespace gate;
using namespace gate::enc;

static StaticString const content = 
    "What is Lorem Ipsum? "
    "Lorem Ipsum is simply dummy text of the printing and typesetting industry. "
    "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, "
    "when an unknown printer took a galley of type and scrambled it to make a type "
    "specimen book. It has survived not only five centuries, but also the leap into "
    "electronic typesetting, remaining essentially unchanged. It was popularised "
    "in the 1960s with the release of Letraset sheets containing Lorem Ipsum "
    "passages, and more recently with desktop publishing software like "
    "Aldus PageMaker including versions of Lorem Ipsum."
    "Why do we use it? "
    "It is a long established fact that a reader will be distracted by the "
    "readable content of a page when looking at its layout. The point of using "
    "Lorem Ipsum is that it has a more-or-less normal distribution of letters, "
    "as opposed to using 'Content here, content here', making it look like readable "
    "English. Many desktop publishing packages and web page editors now use "
    "Lorem Ipsum as their default model text, and a search for 'lorem ipsum' "
    "will uncover many web sites still in their infancy. Various versions have "
    "evolved over the years, sometimes by accident, sometimes on purpose "
    "(injected humour and the like)."
;

static void transcode_data_streams(Stream& instream, Stream& outstream)
{
    char buffer[128];
    GATEXX_TEST_CHECK_NOTHROW(Stream::transferViaBuffer(instream, outstream, buffer, sizeof(buffer)));
    GATEXX_TEST_CHECK_NOTHROW(outstream.flush());
}

template<class ENCODER, class DECODER>
void test_encoder_decoder()
{
    StringStream plainData(content);
    MemFileStream encodedData;

    Stream encoder = NullStream();<--- Variable 'encoder' is reassigned a value before the old one has been used.
    {
        GATEXX_TEST_CHECK_NOTHROW(encoder = ENCODER::create(encodedData));<--- Variable 'encoder' is reassigned a value before the old one has been used.
        GATEXX_TEST_CHECK_NOTHROW(transcode_data_streams(plainData, encoder));
        GATEXX_TEST_CHECK(!encoder.getInterfaceName().empty());
        char buffer[64];
        GATEXX_TEST_CHECK_THROW(encoder.peek(buffer, sizeof(buffer)));
        GATEXX_TEST_CHECK_THROW(encoder.read(buffer, sizeof(buffer)));
    }

    encodedData.seek(0, ControlStream::Seek_Begin);

    StringStream decodedData;

    Stream decoder = NullStream();<--- Variable 'decoder' is reassigned a value before the old one has been used.
    {
        GATEXX_TEST_CHECK_NOTHROW(decoder = DECODER::create(encodedData));<--- Variable 'decoder' is reassigned a value before the old one has been used.
        if (decoder.implementsInterface(GATE_INTERFACE_NAME_CONTROLSTREAM))
        {
            gate_controlstream_t* ptr_control = (gate_controlstream_t*)(decoder.c_impl());
            ControlStream control(ptr_control);
            gate_object_retain(ptr_control);
            GATEXX_TEST_CHECK_NOTHROW(control.canRead());
            GATEXX_TEST_CHECK_NOTHROW(control.canWrite());
            GATEXX_TEST_CHECK_NOTHROW(control.getSize());
            GATEXX_TEST_CHECK_NOTHROW(control.getAvailable());
            control.seek(0, ControlStream::Seek_Current);
        }
        GATEXX_TEST_CHECK_NOTHROW(transcode_data_streams(decoder, decodedData));
        GATEXX_TEST_CHECK(!decoder.getInterfaceName().empty());
        GATEXX_TEST_CHECK_THROW(decoder.write("test", 4));
        GATEXX_TEST_CHECK_THROW(decoder.flush());
    }

    String decodedText = decodedData.toString();
    GATEXX_TEST_CHECK_EQUAL(content, decodedText);

}

#if !defined(GATE_ENCODE_NO_ADVANCED_COMPRESSION) && defined(GATE_EXTLIB_BZIP2)

GATEXX_TEST_UNIT(Bzip2)
{
    test_encoder_decoder<Bzip2Encoder, Bzip2Decoder>();
}

#endif

#if !defined(GATE_ENCODE_NO_ADVANCED_COMPRESSION) && defined(GATE_EXTLIB_LZMA)

GATEXX_TEST_UNIT(Xz)
{
    test_encoder_decoder<XzEncoder, XzDecoder>();
}

#endif

GATEXX_TEST_UNIT(Zlib)
{
    test_encoder_decoder<ZlibEncoder, ZlibDecoder>();

    test_encoder_decoder<GZipEncoder, GZipDecoder>();
}

GATEXX_TEST_UNIT(Zip)
{
    MemFileStream zip_output;
    {
        ZipWriter writer(zip_output);
        ZipEntry entry;

        entry.setPath("/test.txt");
        entry.setSize(content.size());
        entry.setAttributes(File::Attrib_File);
        entry.setAccessBits(File::Access_AllRead | File::Access_AllWrite);
        entry.setModifiedDate(Time::now());
        writer.add(entry, content.c_str());

        entry.setPath("/test2.txt");
        writer.add(entry, content.c_str());

        writer.flush();
    }

    {
        MemFileStream zip_input = zip_output.share();
        ZipReader reader(zip_input);
        ZipEntry entry = reader.getFirstEntry();
        GATEXX_TEST_CHECK_EQUAL(entry.getPath(), "/test.txt");
        GATEXX_TEST_CHECK_EQUAL(entry.getSize(), content.size());

        const bool_t secondEntryFound = reader.getNextEntry(entry);
        GATEXX_TEST_CHECK(secondEntryFound);
        GATEXX_TEST_CHECK_EQUAL(entry.getPath(), "/test2.txt");
        GATEXX_TEST_CHECK_EQUAL(entry.getSize(), content.size());
    }

    {
        MemFileStream zip_input = zip_output.share();
        ZipReader reader(zip_input);
        ZipEntry entry = reader.findEntry("/test.txt");
        GATEXX_TEST_CHECK_EQUAL(entry.getPath(), "/test.txt");
        GATEXX_TEST_CHECK_EQUAL(entry.getSize(), content.size());

        StringStream extractedData;
        reader.extractContent(extractedData);
        GATEXX_TEST_CHECK_EQUAL(content, extractedData.toView());
    }

    {
        MemFileStream zip_input = zip_output.share();
        ZipReader reader(zip_input);
        ZipEntry entry = reader.findEntry("/test2.txt");
        GATEXX_TEST_CHECK_EQUAL(entry.getPath(), "/test2.txt");
        GATEXX_TEST_CHECK_EQUAL(entry.getSize(), content.size());

        StringStream extractedData;
        reader.extractContent(extractedData);
        GATEXX_TEST_CHECK_EQUAL(content, extractedData.toView());
    }

}