2019-03-12 20:31:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-05-06 21:17:19 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/core/SkDescriptor.h"
|
2019-05-06 21:17:19 +00:00
|
|
|
#include "src/core/SkScalerContext.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tests/Test.h"
|
2019-03-12 20:31:30 +00:00
|
|
|
|
2019-05-06 21:17:19 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2019-03-12 20:31:30 +00:00
|
|
|
class SkDescriptorTestHelper {
|
|
|
|
public:
|
|
|
|
static void SetLength(SkDescriptor* desc, size_t length) { desc->fLength = length; }
|
2019-10-24 14:53:53 +00:00
|
|
|
static void SetCount(SkDescriptor* desc, uint32_t count) { desc->fCount = count; }
|
2019-03-12 20:31:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DEF_TEST(Descriptor_empty, r) {
|
|
|
|
const size_t size = sizeof(SkDescriptor);
|
|
|
|
|
|
|
|
auto desc = SkDescriptor::Alloc(size);
|
|
|
|
REPORTER_ASSERT(r, desc->isValid());
|
|
|
|
REPORTER_ASSERT(r, desc->getLength() == size);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(Descriptor_valid_simple, r) {
|
|
|
|
const size_t size =
|
|
|
|
sizeof(SkDescriptor) + sizeof(SkDescriptor::Entry) + sizeof(SkScalerContextRec);
|
|
|
|
|
|
|
|
auto desc = SkDescriptor::Alloc(size);
|
|
|
|
SkScalerContextRec rec;
|
|
|
|
desc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec);
|
|
|
|
REPORTER_ASSERT(r, desc->isValid());
|
|
|
|
REPORTER_ASSERT(r, desc->getLength() == size);
|
|
|
|
|
|
|
|
SkDescriptorTestHelper::SetLength(desc.get(), size - 4);
|
|
|
|
REPORTER_ASSERT(r, !desc->isValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(Descriptor_valid_simple_extra_space, r) {
|
|
|
|
const size_t extra_space = 100;
|
|
|
|
const size_t size =
|
|
|
|
sizeof(SkDescriptor) + sizeof(SkDescriptor::Entry) + sizeof(SkScalerContextRec);
|
|
|
|
|
|
|
|
auto desc = SkDescriptor::Alloc(size + extra_space);
|
|
|
|
SkScalerContextRec rec;
|
|
|
|
desc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec);
|
|
|
|
REPORTER_ASSERT(r, desc->isValid());
|
|
|
|
REPORTER_ASSERT(r, desc->getLength() == size);
|
|
|
|
|
|
|
|
SkDescriptorTestHelper::SetLength(desc.get(), size - 4);
|
|
|
|
REPORTER_ASSERT(r, !desc->isValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(Descriptor_valid_more_tags, r) {
|
|
|
|
const size_t effectSize = 16;
|
|
|
|
const size_t testSize = 32;
|
|
|
|
const size_t size = sizeof(SkDescriptor) + 3 * sizeof(SkDescriptor::Entry) +
|
|
|
|
sizeof(SkScalerContextRec) + effectSize + testSize;
|
|
|
|
|
|
|
|
auto desc = SkDescriptor::Alloc(size);
|
|
|
|
SkScalerContextRec rec;
|
|
|
|
desc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec);
|
|
|
|
desc->addEntry(kEffects_SkDescriptorTag, effectSize, nullptr);
|
|
|
|
desc->addEntry(SkSetFourByteTag('t', 'e', 's', 't'), testSize, nullptr);
|
|
|
|
REPORTER_ASSERT(r, desc->isValid());
|
|
|
|
REPORTER_ASSERT(r, desc->getLength() == size);
|
|
|
|
|
|
|
|
SkDescriptorTestHelper::SetLength(desc.get(), size - 4);
|
|
|
|
REPORTER_ASSERT(r, !desc->isValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(Descriptor_invalid_rec_size, r) {
|
|
|
|
const size_t size =
|
2019-03-12 22:40:46 +00:00
|
|
|
sizeof(SkDescriptor) + sizeof(SkDescriptor::Entry) + sizeof(SkScalerContextRec) - 4;
|
2019-03-12 20:31:30 +00:00
|
|
|
|
|
|
|
auto desc = SkDescriptor::Alloc(size);
|
|
|
|
SkScalerContextRec rec;
|
2019-03-12 22:40:46 +00:00
|
|
|
desc->addEntry(kRec_SkDescriptorTag, sizeof(rec) - 4, &rec);
|
2019-03-12 20:31:30 +00:00
|
|
|
REPORTER_ASSERT(r, desc->getLength() == size);
|
|
|
|
REPORTER_ASSERT(r, !desc->isValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(Descriptor_invalid_length, r) {
|
|
|
|
const size_t size = sizeof(SkDescriptor) + sizeof(SkDescriptor::Entry);
|
|
|
|
const size_t effect_size = 1000;
|
|
|
|
|
|
|
|
auto desc = SkDescriptor::Alloc(size);
|
|
|
|
desc->addEntry(kEffects_SkDescriptorTag, effect_size, nullptr);
|
|
|
|
|
|
|
|
SkDescriptorTestHelper::SetLength(desc.get(), size);
|
|
|
|
REPORTER_ASSERT(r, !desc->isValid());
|
|
|
|
|
|
|
|
SkDescriptorTestHelper::SetLength(desc.get(), size + effect_size);
|
|
|
|
REPORTER_ASSERT(r, desc->isValid());
|
|
|
|
}
|
2019-10-24 14:53:53 +00:00
|
|
|
|
|
|
|
DEF_TEST(Descriptor_entry_too_big, r) {
|
|
|
|
const size_t size = sizeof(SkDescriptor) + sizeof(SkDescriptor::Entry) + 4;
|
|
|
|
// Must be less than fLength, but big enough to be bigger then fLength when added.
|
|
|
|
const size_t effect_size = sizeof(SkDescriptor) + sizeof(SkDescriptor::Entry);
|
|
|
|
|
|
|
|
auto desc = SkDescriptor::Alloc(size);
|
|
|
|
|
|
|
|
desc->addEntry(kEffects_SkDescriptorTag, effect_size, nullptr);
|
|
|
|
|
|
|
|
SkDescriptorTestHelper::SetLength(desc.get(), size);
|
|
|
|
SkDescriptorTestHelper::SetCount(desc.get(), 2);
|
|
|
|
REPORTER_ASSERT(r, !desc->isValid());
|
|
|
|
|
|
|
|
SkDescriptorTestHelper::SetLength(desc.get(), size);
|
|
|
|
SkDescriptorTestHelper::SetCount(desc.get(), 1);
|
|
|
|
REPORTER_ASSERT(r, !desc->isValid());
|
|
|
|
}
|
2019-11-04 17:03:51 +00:00
|
|
|
|
|
|
|
DEF_TEST(Descriptor_entry_over_end, r) {
|
|
|
|
auto desc = SkDescriptor::Alloc(36);
|
|
|
|
|
|
|
|
// Make the start of the Entry be in the SkDescriptor, but the second half falls out side the
|
|
|
|
// SkDescriptor. So: 12 (for descriptor) + 8 (for entry) + 12 (for entry length) = 32. An
|
|
|
|
// An Entry is 8 bytes, so 4 bytes are < 36 and 4 bytes > 36.
|
|
|
|
desc->addEntry(kEffects_SkDescriptorTag, 12, nullptr);
|
|
|
|
|
|
|
|
SkDescriptorTestHelper::SetLength(desc.get(), 36);
|
|
|
|
SkDescriptorTestHelper::SetCount(desc.get(), 2);
|
|
|
|
REPORTER_ASSERT(r, !desc->isValid());
|
|
|
|
}
|