• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/core/SkTypes.h"
9 #include "src/core/SkDescriptor.h"
10 #include "src/core/SkScalerContext.h"
11 #include "tests/Test.h"
12 
13 #include <memory>
14 
15 class SkDescriptorTestHelper {
16 public:
SetLength(SkDescriptor * desc,size_t length)17     static void SetLength(SkDescriptor* desc, size_t length) { desc->fLength = length; }
18 };
19 
DEF_TEST(Descriptor_empty,r)20 DEF_TEST(Descriptor_empty, r) {
21     const size_t size = sizeof(SkDescriptor);
22 
23     auto desc = SkDescriptor::Alloc(size);
24     desc->init();
25     REPORTER_ASSERT(r, desc->isValid());
26     REPORTER_ASSERT(r, desc->getLength() == size);
27 }
28 
DEF_TEST(Descriptor_valid_simple,r)29 DEF_TEST(Descriptor_valid_simple, r) {
30     const size_t size =
31             sizeof(SkDescriptor) + sizeof(SkDescriptor::Entry) + sizeof(SkScalerContextRec);
32 
33     auto desc = SkDescriptor::Alloc(size);
34     desc->init();
35     SkScalerContextRec rec;
36     desc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec);
37     REPORTER_ASSERT(r, desc->isValid());
38     REPORTER_ASSERT(r, desc->getLength() == size);
39 
40     SkDescriptorTestHelper::SetLength(desc.get(), size - 4);
41     REPORTER_ASSERT(r, !desc->isValid());
42 }
43 
DEF_TEST(Descriptor_valid_simple_extra_space,r)44 DEF_TEST(Descriptor_valid_simple_extra_space, r) {
45     const size_t extra_space = 100;
46     const size_t size =
47             sizeof(SkDescriptor) + sizeof(SkDescriptor::Entry) + sizeof(SkScalerContextRec);
48 
49     auto desc = SkDescriptor::Alloc(size + extra_space);
50     desc->init();
51     SkScalerContextRec rec;
52     desc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec);
53     REPORTER_ASSERT(r, desc->isValid());
54     REPORTER_ASSERT(r, desc->getLength() == size);
55 
56     SkDescriptorTestHelper::SetLength(desc.get(), size - 4);
57     REPORTER_ASSERT(r, !desc->isValid());
58 }
59 
DEF_TEST(Descriptor_valid_more_tags,r)60 DEF_TEST(Descriptor_valid_more_tags, r) {
61     const size_t effectSize = 16;
62     const size_t testSize = 32;
63     const size_t size = sizeof(SkDescriptor) + 3 * sizeof(SkDescriptor::Entry) +
64                         sizeof(SkScalerContextRec) + effectSize + testSize;
65 
66     auto desc = SkDescriptor::Alloc(size);
67     desc->init();
68     SkScalerContextRec rec;
69     desc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec);
70     desc->addEntry(kEffects_SkDescriptorTag, effectSize, nullptr);
71     desc->addEntry(SkSetFourByteTag('t', 'e', 's', 't'), testSize, nullptr);
72     REPORTER_ASSERT(r, desc->isValid());
73     REPORTER_ASSERT(r, desc->getLength() == size);
74 
75     SkDescriptorTestHelper::SetLength(desc.get(), size - 4);
76     REPORTER_ASSERT(r, !desc->isValid());
77 }
78 
DEF_TEST(Descriptor_invalid_rec_size,r)79 DEF_TEST(Descriptor_invalid_rec_size, r) {
80     const size_t size =
81             sizeof(SkDescriptor) + sizeof(SkDescriptor::Entry) + sizeof(SkScalerContextRec) - 4;
82 
83     auto desc = SkDescriptor::Alloc(size);
84     desc->init();
85     SkScalerContextRec rec;
86     desc->addEntry(kRec_SkDescriptorTag, sizeof(rec) - 4, &rec);
87     REPORTER_ASSERT(r, desc->getLength() == size);
88     REPORTER_ASSERT(r, !desc->isValid());
89 }
90 
DEF_TEST(Descriptor_invalid_length,r)91 DEF_TEST(Descriptor_invalid_length, r) {
92     const size_t size = sizeof(SkDescriptor) + sizeof(SkDescriptor::Entry);
93     const size_t effect_size = 1000;
94 
95     auto desc = SkDescriptor::Alloc(size);
96     desc->init();
97     SkScalerContextRec rec;
98     desc->addEntry(kEffects_SkDescriptorTag, effect_size, nullptr);
99 
100     SkDescriptorTestHelper::SetLength(desc.get(), size);
101     REPORTER_ASSERT(r, !desc->isValid());
102 
103     SkDescriptorTestHelper::SetLength(desc.get(), size + effect_size);
104     REPORTER_ASSERT(r, desc->isValid());
105 }
106