1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <unittest/unittest.h>
6 
7 #include <fidl/flat_ast.h>
8 #include <fidl/lexer.h>
9 #include <fidl/parser.h>
10 #include <fidl/source_file.h>
11 
12 #include "test_library.h"
13 
14 namespace {
15 
16 class MaxHandlesLibrary : public TestLibrary {
17 public:
MaxHandlesLibrary()18     MaxHandlesLibrary()
19         : TestLibrary("max_handles.fidl", R"FIDL(
20 library fidl.test.maxhandles;
21 
22 struct OneBool {
23   bool b;
24 };
25 
26 struct OneHandle {
27   handle h;
28 };
29 
30 struct HandleArray {
31   array<handle>:8 ha;
32 };
33 
34 struct NullableHandleArray {
35   array<handle?>:8 ha;
36 };
37 
38 struct HandleVector {
39   vector<handle>:8 hv;
40 };
41 
42 struct HandleNullableVector {
43   vector<handle>:8? hv;
44 };
45 
46 struct UnboundedHandleVector {
47   vector<handle> hv;
48 };
49 
50 struct HandleStructVector {
51   vector<OneHandle>:8 sv;
52 };
53 
54 struct HandleTableVector {
55   vector<TableWithOneHandle>:8 sv;
56 };
57 
58 table TableWithOneBool {
59   1: bool b;
60 };
61 
62 table TableWithOneHandle {
63   1: handle h;
64 };
65 
66 table TableWithHandleArray {
67   1: array<handle>:8 ha;
68 };
69 
70 table TableWithNullableHandleArray {
71   1: array<handle?>:8 ha;
72 };
73 
74 table TableWithHandleVector {
75   1: vector<handle>:8 hv;
76 };
77 
78 table TableWithUnboundedHandleVector {
79   1: vector<handle> hv;
80 };
81 
82 table TableWithHandleStructVector {
83   1: vector<OneHandle>:8 sv;
84 };
85 
86 table TableWithHandleTableVector {
87   1: vector<OneHandle>:8 sv;
88 };
89 
90 union NoHandleUnion {
91   OneBool one_bool;
92   uint32 integer;
93 };
94 
95 union OneHandleUnion {
96   OneHandle one_handle;
97   OneBool one_bool;
98   uint32 integer;
99 };
100 
101 union ManyHandleUnion {
102   OneHandle one_handle;
103   HandleArray handle_array;
104   HandleVector handle_vector;
105 };
106 
107 )FIDL") {}
108 };
109 
simple_structs(void)110 static bool simple_structs(void) {
111     BEGIN_TEST;
112 
113     MaxHandlesLibrary test_library;
114     EXPECT_TRUE(test_library.Compile());
115 
116     auto one_bool = test_library.LookupStruct("OneBool");
117     EXPECT_NONNULL(one_bool);
118     EXPECT_EQ(one_bool->typeshape.MaxHandles(), 0);
119 
120     auto one_handle = test_library.LookupStruct("OneHandle");
121     EXPECT_NONNULL(one_handle);
122     EXPECT_EQ(one_handle->typeshape.MaxHandles(), 1);
123 
124     END_TEST;
125 }
126 
simple_tables(void)127 static bool simple_tables(void) {
128     BEGIN_TEST;
129 
130     MaxHandlesLibrary test_library;
131     EXPECT_TRUE(test_library.Compile());
132 
133     auto one_bool = test_library.LookupTable("TableWithOneBool");
134     EXPECT_NONNULL(one_bool);
135     EXPECT_EQ(one_bool->typeshape.MaxHandles(), 0);
136 
137     auto one_handle = test_library.LookupTable("TableWithOneHandle");
138     EXPECT_NONNULL(one_handle);
139     EXPECT_EQ(one_handle->typeshape.MaxHandles(), 1);
140 
141     END_TEST;
142 }
143 
arrays(void)144 static bool arrays(void) {
145     BEGIN_TEST;
146 
147     MaxHandlesLibrary test_library;
148     EXPECT_TRUE(test_library.Compile());
149 
150     auto handle_array = test_library.LookupStruct("HandleArray");
151     EXPECT_NONNULL(handle_array);
152     EXPECT_EQ(handle_array->typeshape.MaxHandles(), 8);
153 
154     auto table_with_handle_array = test_library.LookupTable("TableWithHandleArray");
155     EXPECT_NONNULL(table_with_handle_array);
156     EXPECT_EQ(table_with_handle_array->typeshape.MaxHandles(), 8);
157 
158     auto nullable_handle_array = test_library.LookupStruct("NullableHandleArray");
159     EXPECT_NONNULL(nullable_handle_array);
160     EXPECT_EQ(nullable_handle_array->typeshape.MaxHandles(), 8);
161 
162     auto table_with_nullable_handle_array = test_library.LookupTable("TableWithNullableHandleArray");
163     EXPECT_NONNULL(table_with_nullable_handle_array);
164     EXPECT_EQ(table_with_nullable_handle_array->typeshape.MaxHandles(), 8);
165 
166     END_TEST;
167 }
168 
vectors(void)169 static bool vectors(void) {
170     BEGIN_TEST;
171 
172     MaxHandlesLibrary test_library;
173     EXPECT_TRUE(test_library.Compile());
174 
175     auto handle_vector = test_library.LookupStruct("HandleVector");
176     EXPECT_NONNULL(handle_vector);
177     EXPECT_EQ(handle_vector->typeshape.MaxHandles(), 8);
178 
179     auto table_with_handle_vector = test_library.LookupTable("TableWithHandleVector");
180     EXPECT_NONNULL(table_with_handle_vector);
181     EXPECT_EQ(table_with_handle_vector->typeshape.MaxHandles(), 8);
182 
183     auto handle_nullable_vector = test_library.LookupStruct("HandleNullableVector");
184     EXPECT_NONNULL(handle_nullable_vector);
185     EXPECT_EQ(handle_nullable_vector->typeshape.MaxHandles(), 8);
186 
187     auto unbounded_handle_vector = test_library.LookupStruct("UnboundedHandleVector");
188     EXPECT_NONNULL(unbounded_handle_vector);
189     EXPECT_EQ(unbounded_handle_vector->typeshape.MaxHandles(), std::numeric_limits<uint32_t>::max());
190 
191     auto table_with_unbounded_handle_vector = test_library.LookupTable("TableWithUnboundedHandleVector");
192     EXPECT_NONNULL(table_with_unbounded_handle_vector);
193     EXPECT_EQ(table_with_unbounded_handle_vector->typeshape.MaxHandles(), std::numeric_limits<uint32_t>::max());
194 
195     auto handle_struct_vector = test_library.LookupStruct("HandleStructVector");
196     EXPECT_NONNULL(handle_struct_vector);
197     EXPECT_EQ(handle_struct_vector->typeshape.MaxHandles(), 8);
198 
199     auto handle_table_vector = test_library.LookupStruct("HandleTableVector");
200     EXPECT_NONNULL(handle_table_vector);
201     EXPECT_EQ(handle_table_vector->typeshape.MaxHandles(), 8);
202 
203     auto table_with_handle_struct_vector = test_library.LookupTable("TableWithHandleStructVector");
204     EXPECT_NONNULL(table_with_handle_struct_vector);
205     EXPECT_EQ(table_with_handle_struct_vector->typeshape.MaxHandles(), 8);
206 
207     END_TEST;
208 }
209 
unions(void)210 static bool unions(void) {
211     BEGIN_TEST;
212 
213     MaxHandlesLibrary test_library;
214     EXPECT_TRUE(test_library.Compile());
215 
216     auto no_handle_union = test_library.LookupUnion("NoHandleUnion");
217     EXPECT_NONNULL(no_handle_union);
218     EXPECT_EQ(no_handle_union->typeshape.MaxHandles(), 0);
219 
220     auto one_handle_union = test_library.LookupUnion("OneHandleUnion");
221     EXPECT_NONNULL(one_handle_union);
222     EXPECT_EQ(one_handle_union->typeshape.MaxHandles(), 1);
223 
224     auto many_handle_union = test_library.LookupUnion("ManyHandleUnion");
225     EXPECT_NONNULL(many_handle_union);
226     EXPECT_EQ(many_handle_union->typeshape.MaxHandles(), 8);
227 
228     END_TEST;
229 }
230 
231 } // namespace
232 
233 BEGIN_TEST_CASE(max_handles_tests);
234 RUN_TEST(simple_structs);
235 RUN_TEST(simple_tables);
236 RUN_TEST(arrays);
237 RUN_TEST(vectors);
238 RUN_TEST(unions);
239 END_TEST_CASE(max_handles_tests);
240