• 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; }
SetCount(SkDescriptor * desc,uint32_t count)18     static void SetCount(SkDescriptor* desc, uint32_t count) { desc->fCount = count; }
19 };
20 
DEF_TEST(Descriptor_empty,r)21 DEF_TEST(Descriptor_empty, r) {
22     const size_t size = sizeof(SkDescriptor);
23 
24     auto desc = SkDescriptor::Alloc(size);
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     SkScalerContextRec rec;
35     desc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec);
36     REPORTER_ASSERT(r, desc->isValid());
37     REPORTER_ASSERT(r, desc->getLength() == size);
38 
39     SkDescriptorTestHelper::SetLength(desc.get(), size - 4);
40     REPORTER_ASSERT(r, !desc->isValid());
41 }
42 
DEF_TEST(Descriptor_valid_simple_extra_space,r)43 DEF_TEST(Descriptor_valid_simple_extra_space, r) {
44     const size_t extra_space = 100;
45     const size_t size =
46             sizeof(SkDescriptor) + sizeof(SkDescriptor::Entry) + sizeof(SkScalerContextRec);
47 
48     auto desc = SkDescriptor::Alloc(size + extra_space);
49     SkScalerContextRec rec;
50     desc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec);
51     REPORTER_ASSERT(r, desc->isValid());
52     REPORTER_ASSERT(r, desc->getLength() == size);
53 
54     SkDescriptorTestHelper::SetLength(desc.get(), size - 4);
55     REPORTER_ASSERT(r, !desc->isValid());
56 }
57 
DEF_TEST(Descriptor_valid_more_tags,r)58 DEF_TEST(Descriptor_valid_more_tags, r) {
59     const size_t effectSize = 16;
60     const size_t testSize = 32;
61     const size_t size = sizeof(SkDescriptor) + 3 * sizeof(SkDescriptor::Entry) +
62                         sizeof(SkScalerContextRec) + effectSize + testSize;
63 
64     auto desc = SkDescriptor::Alloc(size);
65     SkScalerContextRec rec;
66     desc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec);
67     desc->addEntry(kEffects_SkDescriptorTag, effectSize, nullptr);
68     desc->addEntry(SkSetFourByteTag('t', 'e', 's', 't'), testSize, nullptr);
69     REPORTER_ASSERT(r, desc->isValid());
70     REPORTER_ASSERT(r, desc->getLength() == size);
71 
72     SkDescriptorTestHelper::SetLength(desc.get(), size - 4);
73     REPORTER_ASSERT(r, !desc->isValid());
74 }
75 
DEF_TEST(Descriptor_invalid_rec_size,r)76 DEF_TEST(Descriptor_invalid_rec_size, r) {
77     const size_t size =
78             sizeof(SkDescriptor) + sizeof(SkDescriptor::Entry) + sizeof(SkScalerContextRec) - 4;
79 
80     auto desc = SkDescriptor::Alloc(size);
81     SkScalerContextRec rec;
82     desc->addEntry(kRec_SkDescriptorTag, sizeof(rec) - 4, &rec);
83     REPORTER_ASSERT(r, desc->getLength() == size);
84     REPORTER_ASSERT(r, !desc->isValid());
85 }
86 
DEF_TEST(Descriptor_invalid_length,r)87 DEF_TEST(Descriptor_invalid_length, r) {
88     const size_t size = sizeof(SkDescriptor) + sizeof(SkDescriptor::Entry);
89     const size_t effect_size = 1000;
90 
91     auto desc = SkDescriptor::Alloc(size);
92     desc->addEntry(kEffects_SkDescriptorTag, effect_size, nullptr);
93 
94     SkDescriptorTestHelper::SetLength(desc.get(), size);
95     REPORTER_ASSERT(r, !desc->isValid());
96 
97     SkDescriptorTestHelper::SetLength(desc.get(), size + effect_size);
98     REPORTER_ASSERT(r, desc->isValid());
99 }
100 
DEF_TEST(Descriptor_entry_too_big,r)101 DEF_TEST(Descriptor_entry_too_big, r) {
102     const size_t size = sizeof(SkDescriptor) + sizeof(SkDescriptor::Entry) + 4;
103     // Must be less than fLength, but big enough to be bigger then fLength when added.
104     const size_t effect_size = sizeof(SkDescriptor) + sizeof(SkDescriptor::Entry);
105 
106     auto desc = SkDescriptor::Alloc(size);
107 
108     desc->addEntry(kEffects_SkDescriptorTag, effect_size, nullptr);
109 
110     SkDescriptorTestHelper::SetLength(desc.get(), size);
111     SkDescriptorTestHelper::SetCount(desc.get(), 2);
112     REPORTER_ASSERT(r, !desc->isValid());
113 
114     SkDescriptorTestHelper::SetLength(desc.get(), size);
115     SkDescriptorTestHelper::SetCount(desc.get(), 1);
116     REPORTER_ASSERT(r, !desc->isValid());
117 }
118 
DEF_TEST(Descriptor_entry_over_end,r)119 DEF_TEST(Descriptor_entry_over_end, r) {
120     auto desc = SkDescriptor::Alloc(36);
121 
122     // Make the start of the Entry be in the SkDescriptor, but the second half falls out side the
123     // SkDescriptor. So: 12 (for descriptor) + 8 (for entry) + 12 (for entry length) = 32. An
124     // An Entry is 8 bytes, so 4 bytes are < 36 and 4 bytes > 36.
125     desc->addEntry(kEffects_SkDescriptorTag, 12, nullptr);
126 
127     SkDescriptorTestHelper::SetLength(desc.get(), 36);
128     SkDescriptorTestHelper::SetCount(desc.get(), 2);
129     REPORTER_ASSERT(r, !desc->isValid());
130 }
131