blob: 9641bcffc374de185b2617d751a6b5a8b08a2e2d [file] [log] [blame]
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
library test.types;
using zx;
type CopyableStruct = struct {
x int32;
};
type MoveOnlyStruct = resource struct {
h zx.Handle:optional;
};
type VectorStruct = struct {
v vector<uint32>;
};
type StructWithPadding = struct {
a uint8;
b uint32;
};
type StructWithoutPadding = struct {
a uint8;
b uint8;
c uint16;
d uint32;
};
/// Verifies that user code can manipulate these union payloads.
type TestUnion = strict resource union {
1: primitive int32;
2: copyable CopyableStruct;
3: move_only MoveOnlyStruct;
};
type TestXUnion = flexible resource union {
1: primitive int32;
2: copyable CopyableStruct;
3: h zx.Handle;
};
type TestStrictXUnion = strict union {
1: primitive int32;
2: copyable CopyableStruct;
};
type UnionWithUint64 = strict union {
1: value int64;
};
type TestStrictXUnionInStruct = struct {
xu TestStrictXUnion;
};
type TestStrictOptionalXUnionInStruct = struct {
xu TestStrictXUnion:optional;
};
type TestStrictXUnionInArrayInStruct = struct {
arr array<TestStrictXUnion, 10>;
};
type TestNonResourceXUnion = flexible union {
1: primitive int32;
};
type TestFlexibleUnionInStruct = struct {
xu TestNonResourceXUnion;
};
type TestFlexibleUnionInArrayInStruct = struct {
arr array<TestNonResourceXUnion, 10>;
};
/// Verifies that user code can manipulate these bits.
///
/// We use a uint8 since most bitwise operations will cast their operands to
/// int, and therefore special casting is required to properly compile.
type StrictBits = strict bits : uint8 {
B = 2;
D = 4;
E = 8;
};
/// The members should be kept in sync with those in [`StrictBits`], as we have
/// tests parameterized on both bits types.
type FlexibleBits = flexible bits : uint8 {
B = 2;
D = 4;
E = 8;
};
/// Verifies that user code can build and access tables.
type SampleTable = table {
1: x uint8;
2: y uint8;
3: vector_of_struct vector<CopyableStruct>;
4: b bool;
5: s string;
};
/// Verifies that an empty table compiles.
type SampleEmptyTable = table {};
closed protocol TestInterface {
strict TestMethod(resource struct {
u TestUnion;
}) -> (resource struct {
u TestUnion;
});
};
/// Verifies that method argument types don't conflict with user-defined types.
type FooRequest = struct {
bar int32;
};
type FooResponse = struct {
bar int32;
};
type FooEvent = struct {
bar int32;
};
closed protocol Baz {
strict Foo(struct {
req FooRequest;
}) -> (struct {
res FooResponse;
});
strict -> FooEvent(FooEvent);
};
type TableWithSubTables = table {
1: t SampleTable;
2: vt vector<SampleTable>;
3: at array<SampleTable, 3>;
};
type StrictEnum = strict enum : uint32 {
B = 2;
D = 4;
E = 8;
};
type FlexibleEnum = flexible enum : uint32 {
B = 2;
D = 4;
E = 8;
@unknown
CUSTOM = 56;
};
type EmptyFlexibleEnum = flexible enum : uint32 {};
type EmptyStruct = struct {};
type TestTable = table {
1: x uint8;
};
type Uint64Table = table {
1: x uint64;
};
type TestResourceTable = resource table {
1: x uint8;
};
type HandleStruct = resource struct {
h zx.Handle:EVENT;
};
type TestHandleTable = resource table {
1: hs HandleStruct;
};
type NonInlinableHandleStruct = resource struct {
h zx.Handle:EVENT;
i int64;
};
type TestHandleTableNonInlinableField = resource table {
1: hs NonInlinableHandleStruct;
};
type TableMaxOrdinal3WithReserved2 = table {
1: field_1 uint8;
3: field_3 uint8;
};
/// Certain traits are only implemented for top level (i.e. request/response struct)
/// types, since LLCPP only calls encode/decode on those top level types.
/// Types used in tests that exercise these codepaths (e.g. involving HasFlexibleEnvelope)
/// should be put in a request/response struct instead of a regular struct in order
/// to reflect the actual paths exercised within a protocol.
///
/// For example, to test a union MyUnion, instead of:
/// - declaring wrapper struct `struct MyUnionStruct { MyUnion xu };`
/// - writing encode/decode tests in C++ using MyUnionStruct
/// do:
/// - add method `MyUnion() -> (MyUnion result);`
/// - write tests in C++ using MsgWrapper::MyUnionResponse
closed protocol MsgWrapper {
strict TestXUnion() -> (resource struct {
result TestXUnion;
});
strict TestNonResourceXUnion() -> (struct {
result TestNonResourceXUnion;
});
strict TestTable() -> (struct {
result TestTable;
});
strict TestResourceTable() -> (resource struct {
result TestResourceTable;
});
};
closed protocol TypesTest {
strict NonNullableChannel(resource struct {
channel zx.Handle:CHANNEL;
});
};
closed protocol UsesAnonymous {
strict FooMethod(struct {
req_member table {
1: inner_table union {
1: data uint8;
};
};
}) -> () error enum {
FOO = 1;
};
};
closed protocol ErrorSyntax {
strict EmptyPayload() -> () error int32;
strict FooPayload() -> (FooResponse) error int32;
};
type StructOfOptionals = struct {
s string:<128, optional>;
v vector<uint32>:<128, optional>;
t box<CopyableStruct>;
};
type StructWithDefault = struct {
@allow_deprecated_struct_defaults
defaulted int32 = SOME_DEFAULT_VALUE;
};
const SOME_DEFAULT_VALUE int32 = 100;
service TypesTestService {
test client_end:TypesTest;
};