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 
GoodConstTestBool()16 bool GoodConstTestBool() {
17     BEGIN_TEST;
18 
19     TestLibrary library(R"FIDL(
20 library example;
21 
22 const bool c = false;
23 )FIDL");
24     ASSERT_TRUE(library.Compile());
25 
26     END_TEST;
27 }
28 
BadConstTestBoolWithString()29 bool BadConstTestBoolWithString() {
30     BEGIN_TEST;
31 
32     TestLibrary library(R"FIDL(
33 library example;
34 
35 const bool c = "foo";
36 )FIDL");
37     ASSERT_FALSE(library.Compile());
38     auto errors = library.errors();
39     ASSERT_GE(errors.size(), 1);
40     ASSERT_STR_STR(errors[0].c_str(), "\"foo\" cannot be interpreted as type bool");
41 
42     END_TEST;
43 }
44 
BadConstTestBoolWithNumeric()45 bool BadConstTestBoolWithNumeric() {
46     BEGIN_TEST;
47 
48     TestLibrary library(R"FIDL(
49 library example;
50 
51 const bool c = 6;
52 )FIDL");
53     ASSERT_FALSE(library.Compile());
54     auto errors = library.errors();
55     ASSERT_GE(errors.size(), 1);
56     ASSERT_STR_STR(errors[0].c_str(), "6 cannot be interpreted as type bool");
57 
58     END_TEST;
59 }
60 
GoodConstTestInt32()61 bool GoodConstTestInt32() {
62     BEGIN_TEST;
63 
64     TestLibrary library(R"FIDL(
65 library example;
66 
67 const int32 c = 42;
68 )FIDL");
69     ASSERT_TRUE(library.Compile());
70 
71     END_TEST;
72 }
73 
GoodConstTestInt32FromOtherConst()74 bool GoodConstTestInt32FromOtherConst() {
75     BEGIN_TEST;
76 
77     TestLibrary library(R"FIDL(
78 library example;
79 
80 const int32 b = 42;
81 const int32 c = b;
82 )FIDL");
83     ASSERT_TRUE(library.Compile());
84 
85     END_TEST;
86 }
87 
BadConstTestInt32WithString()88 bool BadConstTestInt32WithString() {
89     BEGIN_TEST;
90 
91     TestLibrary library(R"FIDL(
92 library example;
93 
94 const int32 c = "foo";
95 )FIDL");
96     ASSERT_FALSE(library.Compile());
97     auto errors = library.errors();
98     ASSERT_GE(errors.size(), 1);
99     ASSERT_STR_STR(errors[0].c_str(), "\"foo\" cannot be interpreted as type int32");
100 
101     END_TEST;
102 }
103 
BadConstTestInt32WithBool()104 bool BadConstTestInt32WithBool() {
105     BEGIN_TEST;
106 
107     TestLibrary library(R"FIDL(
108 library example;
109 
110 const int32 c = true;
111 )FIDL");
112     ASSERT_FALSE(library.Compile());
113     auto errors = library.errors();
114     ASSERT_GE(errors.size(), 1);
115     ASSERT_STR_STR(errors[0].c_str(), "true cannot be interpreted as type int32");
116 
117     END_TEST;
118 }
119 
GoodConstTesUint64()120 bool GoodConstTesUint64() {
121     BEGIN_TEST;
122 
123     TestLibrary library(R"FIDL(
124 library example;
125 
126 const int64 a = 42;
127 )FIDL");
128     ASSERT_TRUE(library.Compile());
129 
130     END_TEST;
131 }
132 
GoodConstTestUint64FromOtherUint32()133 bool GoodConstTestUint64FromOtherUint32() {
134     BEGIN_TEST;
135 
136     TestLibrary library(R"FIDL(
137 library example;
138 
139 const uint32 a = 42;
140 const uint64 b = a;
141 )FIDL");
142     ASSERT_TRUE(library.Compile());
143 
144     END_TEST;
145 }
146 
BadConstTestUint64Negative()147 bool BadConstTestUint64Negative() {
148     BEGIN_TEST;
149 
150     TestLibrary library(R"FIDL(
151 library example;
152 
153 const uint64 a = -42;
154 )FIDL");
155     ASSERT_FALSE(library.Compile());
156     auto errors = library.errors();
157     ASSERT_GE(errors.size(), 1);
158     ASSERT_STR_STR(errors[0].c_str(), "-42 cannot be interpreted as type uint64");
159 
160     END_TEST;
161 }
162 
BadConstTestUint64Overflow()163 bool BadConstTestUint64Overflow() {
164     BEGIN_TEST;
165 
166     TestLibrary library(R"FIDL(
167 library example;
168 
169 const uint64 a = 18446744073709551616;
170 )FIDL");
171     ASSERT_FALSE(library.Compile());
172     auto errors = library.errors();
173     ASSERT_GE(errors.size(), 1);
174     ASSERT_STR_STR(errors[0].c_str(), "18446744073709551616 cannot be interpreted as type uint64");
175 
176     END_TEST;
177 }
178 
GoodConstTestFloat32()179 bool GoodConstTestFloat32() {
180     BEGIN_TEST;
181 
182     TestLibrary library(R"FIDL(
183 library example;
184 
185 const float32 b = 1.61803;
186 const float32 c = -36.46216;
187 )FIDL");
188     ASSERT_TRUE(library.Compile());
189 
190     END_TEST;
191 }
192 
GoodConstTestFloat32HighLimit()193 bool GoodConstTestFloat32HighLimit() {
194     BEGIN_TEST;
195 
196     TestLibrary library(R"FIDL(
197 library example;
198 
199 const float32 hi = 3.402823e38;
200 )FIDL");
201     ASSERT_TRUE(library.Compile());
202 
203     END_TEST;
204 }
205 
GoodConstTestFloat32LowLimit()206 bool GoodConstTestFloat32LowLimit() {
207     BEGIN_TEST;
208 
209     TestLibrary library(R"FIDL(
210 library example;
211 
212 const float32 lo = -3.40282e38;
213 )FIDL");
214     ASSERT_TRUE(library.Compile());
215 
216     END_TEST;
217 }
218 
BadConstTestFloat32HighLimit()219 bool BadConstTestFloat32HighLimit() {
220     BEGIN_TEST;
221 
222     TestLibrary library(R"FIDL(
223 library example;
224 
225 const float32 hi = 3.41e38;
226 )FIDL");
227     ASSERT_FALSE(library.Compile());
228     auto errors = library.errors();
229     ASSERT_GE(errors.size(), 1);
230     ASSERT_STR_STR(errors[0].c_str(), "3.41e38 cannot be interpreted as type float32");
231 
232     END_TEST;
233 }
234 
BadConstTestFloat32LowLimit()235 bool BadConstTestFloat32LowLimit() {
236     BEGIN_TEST;
237 
238     TestLibrary library(R"FIDL(
239 library example;
240 
241 const float32 b = -3.41e38;
242 )FIDL");
243     ASSERT_FALSE(library.Compile());
244     auto errors = library.errors();
245     ASSERT_GE(errors.size(), 1);
246     ASSERT_STR_STR(errors[0].c_str(), "-3.41e38 cannot be interpreted as type float32");
247 
248     END_TEST;
249 }
250 
GoodConstTestString()251 bool GoodConstTestString() {
252     BEGIN_TEST;
253 
254     TestLibrary library(R"FIDL(
255 library example;
256 
257 const string:4 c = "four";
258 )FIDL");
259     ASSERT_TRUE(library.Compile());
260 
261     END_TEST;
262 }
263 
GoodConstTestStringFromOtherConst()264 bool GoodConstTestStringFromOtherConst() {
265     BEGIN_TEST;
266 
267     TestLibrary library(R"FIDL(
268 library example;
269 
270 const string:4 c = "four";
271 const string:5 d = c;
272 )FIDL");
273     ASSERT_TRUE(library.Compile());
274 
275     END_TEST;
276 }
277 
BadConstTestStringWithNumeric()278 bool BadConstTestStringWithNumeric() {
279     BEGIN_TEST;
280 
281     TestLibrary library(R"FIDL(
282 library example;
283 
284 const string c = 4;
285 )FIDL");
286     ASSERT_FALSE(library.Compile());
287     auto errors = library.errors();
288     ASSERT_GE(errors.size(), 1);
289     ASSERT_STR_STR(errors[0].c_str(), "4 cannot be interpreted as type string");
290 
291     END_TEST;
292 }
293 
BadConstTestStringWithBool()294 bool BadConstTestStringWithBool() {
295     BEGIN_TEST;
296 
297     TestLibrary library(R"FIDL(
298 library example;
299 
300 const string c = true;
301 )FIDL");
302     ASSERT_FALSE(library.Compile());
303     auto errors = library.errors();
304     ASSERT_GE(errors.size(), 1);
305     ASSERT_STR_STR(errors[0].c_str(), "true cannot be interpreted as type string");
306 
307     END_TEST;
308 }
309 
BadConstTestStringWithStringTooLong()310 bool BadConstTestStringWithStringTooLong() {
311     BEGIN_TEST;
312 
313     TestLibrary library(R"FIDL(
314 library example;
315 
316 const string:4 c = "hello";
317 )FIDL");
318     ASSERT_FALSE(library.Compile());
319     auto errors = library.errors();
320     ASSERT_GE(errors.size(), 1);
321     ASSERT_STR_STR(errors[0].c_str(),
322                    "\"hello\" (string:5) exceeds the size bound of type string:4");
323 
324     END_TEST;
325 }
326 
GoodConstTestUsing()327 bool GoodConstTestUsing() {
328     BEGIN_TEST;
329 
330     TestLibrary library(R"FIDL(
331 library example;
332 
333 using foo = int32;
334 const foo c = 2;
335 )FIDL");
336     ASSERT_TRUE(library.Compile());
337 
338     END_TEST;
339 }
340 
BadConstTestUsingWithInconvertibleValue()341 bool BadConstTestUsingWithInconvertibleValue() {
342     BEGIN_TEST;
343 
344     TestLibrary library(R"FIDL(
345 library example;
346 
347 using foo = int32;
348 const foo c = "nope";
349 )FIDL");
350     ASSERT_FALSE(library.Compile());
351     auto errors = library.errors();
352     ASSERT_GE(errors.size(), 1);
353     ASSERT_STR_STR(errors[0].c_str(), "\"nope\" cannot be interpreted as type int32");
354 
355     END_TEST;
356 }
357 
BadConstTestNullableString()358 bool BadConstTestNullableString() {
359     BEGIN_TEST;
360 
361     TestLibrary library(R"FIDL(
362 library example;
363 
364 const string? c = "";
365 )FIDL");
366     ASSERT_FALSE(library.Compile());
367     auto errors = library.errors();
368     ASSERT_GE(errors.size(), 1);
369     ASSERT_STR_STR(errors[0].c_str(), "invalid constant type string?");
370 
371     END_TEST;
372 }
373 
BadConstTestEnum()374 bool BadConstTestEnum() {
375     BEGIN_TEST;
376 
377     TestLibrary library(R"FIDL(
378 library example;
379 
380 enum MyEnum : int32 { A = 5; };
381 const MyEnum c = "";
382 )FIDL");
383     ASSERT_FALSE(library.Compile());
384     auto errors = library.errors();
385     ASSERT_GE(errors.size(), 1);
386     ASSERT_STR_STR(errors[0].c_str(), "invalid constant type example/MyEnum");
387 
388     END_TEST;
389 }
390 
BadConstTestArray()391 bool BadConstTestArray() {
392     BEGIN_TEST;
393 
394     TestLibrary library(R"FIDL(
395 library example;
396 
397 const array<int32>:2 c = -1;
398 )FIDL");
399     ASSERT_FALSE(library.Compile());
400     auto errors = library.errors();
401     ASSERT_GE(errors.size(), 1);
402     ASSERT_STR_STR(errors[0].c_str(), "invalid constant type array<int32>:2");
403 
404     END_TEST;
405 }
406 
BadConstTestVector()407 bool BadConstTestVector() {
408     BEGIN_TEST;
409 
410     TestLibrary library(R"FIDL(
411 library example;
412 
413 const vector<int32>:2 c = -1;
414 )FIDL");
415     ASSERT_FALSE(library.Compile());
416     auto errors = library.errors();
417     ASSERT_GE(errors.size(), 1);
418     ASSERT_STR_STR(errors[0].c_str(), "invalid constant type vector<int32>:2");
419 
420     END_TEST;
421 }
422 
BadConstTestHandleOfThread()423 bool BadConstTestHandleOfThread() {
424     BEGIN_TEST;
425 
426     TestLibrary library(R"FIDL(
427 library example;
428 
429 const handle<thread> c = -1;
430 )FIDL");
431     ASSERT_FALSE(library.Compile());
432     auto errors = library.errors();
433     ASSERT_GE(errors.size(), 1);
434     ASSERT_STR_STR(errors[0].c_str(), "invalid constant type handle<thread>");
435 
436     END_TEST;
437 }
438 
439 } // namespace
440 
441 BEGIN_TEST_CASE(consts_tests);
442 
443 RUN_TEST(GoodConstTestBool);
444 RUN_TEST(BadConstTestBoolWithString);
445 RUN_TEST(BadConstTestBoolWithNumeric);
446 
447 RUN_TEST(GoodConstTestInt32);
448 RUN_TEST(GoodConstTestInt32FromOtherConst);
449 RUN_TEST(BadConstTestInt32WithString);
450 RUN_TEST(BadConstTestInt32WithBool);
451 
452 RUN_TEST(GoodConstTesUint64);
453 RUN_TEST(GoodConstTestUint64FromOtherUint32);
454 RUN_TEST(BadConstTestUint64Negative);
455 RUN_TEST(BadConstTestUint64Overflow);
456 
457 RUN_TEST(GoodConstTestFloat32);
458 RUN_TEST(GoodConstTestFloat32HighLimit);
459 RUN_TEST(GoodConstTestFloat32LowLimit);
460 RUN_TEST(BadConstTestFloat32HighLimit);
461 RUN_TEST(BadConstTestFloat32LowLimit);
462 
463 RUN_TEST(GoodConstTestString);
464 RUN_TEST(GoodConstTestStringFromOtherConst);
465 RUN_TEST(BadConstTestStringWithNumeric);
466 RUN_TEST(BadConstTestStringWithBool);
467 RUN_TEST(BadConstTestStringWithStringTooLong);
468 
469 RUN_TEST(GoodConstTestUsing);
470 RUN_TEST(BadConstTestUsingWithInconvertibleValue);
471 
472 RUN_TEST(BadConstTestNullableString);
473 RUN_TEST(BadConstTestEnum);
474 RUN_TEST(BadConstTestArray);
475 RUN_TEST(BadConstTestVector);
476 RUN_TEST(BadConstTestHandleOfThread);
477 
478 END_TEST_CASE(consts_tests);
479