aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/CodecExactReadTest.cpp
blob: 7e0d8eaccc5d4255f267ac61050ff63ba85fd4e0 (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
/*
 * Copyright 2017 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "Resources.h"
#include "Test.h"

#include "SkBitmap.h"
#include "SkCodec.h"
#include "SkData.h"
#include "SkStream.h"

namespace {
// This class emits a skiatest failure if a client attempts to read beyond its
// end. Since it is used with complete, valid images, and contains nothing
// after the encoded image data, it will emit a failure if the client attempts
// to read beyond the logical end of the data.
class MyStream : public SkStream {
public:
    static MyStream* Make(const char* path, skiatest::Reporter* r) {
        SkASSERT(path);
        sk_sp<SkData> data(GetResourceAsData(path));
        if (!data) {
            return nullptr;
        }

        return new MyStream(path, std::move(data), r);
    }

    size_t read(void* buf, size_t bytes) override {
        const size_t remaining = fStream.getLength() - fStream.getPosition();
        if (bytes > remaining) {
            ERRORF(fReporter, "Tried to read %lu bytes (only %lu remaining) from %s",
                   bytes, remaining, fPath);
        }
        return fStream.read(buf, bytes);
    }

    bool rewind() override {
        return fStream.rewind();
    }

    bool isAtEnd() const override {
        return fStream.isAtEnd();
    }
private:
    const char* fPath;
    SkMemoryStream fStream;
    skiatest::Reporter* fReporter;  // Unowned

    MyStream(const char* path, sk_sp<SkData> data, skiatest::Reporter* r)
        : fPath(path)
        , fStream(std::move(data))
        , fReporter(r)
    {}
};
} // namespace

// Test that SkPngCodec does not attempt to read its input beyond the logical
// end of its data. Some other SkCodecs do, but some Android apps rely on not
// doing so for PNGs.
DEF_TEST(Codec_end, r) {
    for (const char* path : { "plane.png",
                              "yellow_rose.png",
                              "plane_interlaced.png" }) {
        std::unique_ptr<MyStream> stream(MyStream::Make(path, r));
        if (!stream) {
            continue;
        }

        std::unique_ptr<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
        if (!codec) {
            ERRORF(r, "Failed to create a codec from %s\n", path);
            continue;
        }

        auto info = codec->getInfo().makeColorType(kN32_SkColorType);
        SkBitmap bm;
        bm.allocPixels(info);

        auto result = codec->getPixels(bm.info(), bm.getPixels(), bm.rowBytes());
        if (result != SkCodec::kSuccess) {
            ERRORF(r, "Failed to getPixels from %s. error %i", path, result);
            continue;
        }

        // Rewind and do an incremental decode.
        result = codec->startIncrementalDecode(bm.info(), bm.getPixels(), bm.rowBytes());
        if (result != SkCodec::kSuccess) {
            ERRORF(r, "Failed to startIncrementalDecode from %s. error %i", path, result);
            continue;
        }

        result = codec->incrementalDecode();
        if (result != SkCodec::kSuccess) {
            ERRORF(r, "Failed to incrementalDecode from %s. error %i", path, result);
        }
    }
}