blob: e9501a34498444b3d57c8d65a31973e06b01730c [file] [log] [blame]
{{/*
// Copyright 2022 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.
*/}}
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include <fidl/test.conformance/cpp/wire.h>
#include <gtest/gtest.h>
#include "src/lib/fidl/c/walker_tests/conformance_test_utils.h"
#ifdef __Fuchsia__
#include <zircon/syscalls.h>
#include "sdk/cts/tests/pkg/fidl/cpp/test/handle_util.h"
#endif
{{ range .EncodeSuccessCases }}
{{- if .FuchsiaOnly }}
#ifdef __Fuchsia__
{{- end }}
TEST(C_Conformance, {{ .Name }}_Encode) {
{{- if .HandleDefs }}
const std::vector<zx_handle_t> handle_defs = {{ .HandleDefs }};
{{- end }}
[[maybe_unused]] fidl::Arena<ZX_CHANNEL_MAX_MSG_BYTES> allocator;
{{ .ValueBuild }}
const auto expected_bytes = {{ .Bytes }};
const auto expected_handles = {{ .Handles }};
alignas(FIDL_ALIGNMENT) auto obj = {{ .ValueVar }};
EXPECT_TRUE(c_conformance_utils::EncodeSuccess(
{{ .WireFormatVersion }}, &obj, expected_bytes, expected_handles, {{ .CheckHandleRights }}));
}
{{- if .FuchsiaOnly }}
#endif // __Fuchsia__
{{- end }}
{{ end }}
{{ range .DecodeSuccessCases }}
{{- if .FuchsiaOnly }}
#ifdef __Fuchsia__
{{- end }}
TEST(C_Conformance, {{ .Name }}_Decode) {
{{- if .HandleDefs }}
const std::vector<zx_handle_info_t> handle_defs = {{ .HandleDefs }};
std::vector<zx_koid_t> {{ .HandleKoidVectorName }};
for (zx_handle_info_t def : handle_defs) {
zx_info_handle_basic_t info;
ASSERT_EQ(ZX_OK, zx_object_get_info(def.handle, ZX_INFO_HANDLE_BASIC, &info, sizeof(info), nullptr, nullptr));
{{ .HandleKoidVectorName }}.push_back(info.koid);
}
{{- end }}
[[maybe_unused]] fidl::Arena<ZX_CHANNEL_MAX_MSG_BYTES> allocator;
{{ .ValueBuild }}
std::vector<uint8_t> bytes = {{ .Bytes }};
std::vector<zx_handle_info_t> handles = {{ .Handles }};
auto obj = {{ .ValueVar }};
auto equality_check = [&](void* raw_actual_value) -> bool {
{{ .ValueType }}& {{ .Equality.InputVar }} = *static_cast<{{ .ValueType }}*>(raw_actual_value);
{{ .Equality.HelperStatements }}
return {{ .Equality.Expr }};
};
EXPECT_TRUE(c_conformance_utils::DecodeSuccess(
{{ .WireFormatVersion }}, fidl::DeprecatedCTypeTraits<decltype(obj)>::kType, std::move(bytes), std::move(handles), equality_check));
}
TEST(C_Conformance, {{ .Name }}_Validate) {
{{- if .HandleDefs }}
const std::vector<zx_handle_info_t> handle_defs = {{ .HandleDefs }};
{{- end }}
[[maybe_unused]] fidl::Arena<ZX_CHANNEL_MAX_MSG_BYTES> allocator;
{{ .ValueBuild }}
std::vector<uint8_t> bytes = {{ .Bytes }};
std::vector<zx_handle_info_t> handles = {{ .Handles }};
auto obj = {{ .ValueVar }};
EXPECT_TRUE(c_conformance_utils::ValidateSuccess(
{{ .WireFormatVersion }}, fidl::DeprecatedCTypeTraits<decltype(obj)>::kType, std::move(bytes), handles));
}
{{- if .FuchsiaOnly }}
#endif // __Fuchsia__
{{- end }}
{{ end }}
{{ range .DecodeFailureCases }}
{{- if .FuchsiaOnly }}
#ifdef __Fuchsia__
{{- end }}
TEST(C_Conformance, {{ .Name }}_Decode_Failure) {
{{- if .HandleDefs }}
const std::vector<zx_handle_info_t> handle_defs = {{ .HandleDefs }};
{{- end }}
std::vector<uint8_t> bytes = {{ .Bytes }};
std::vector<zx_handle_info_t> handles = {{ .Handles }};
EXPECT_TRUE(c_conformance_utils::DecodeFailure({{ .WireFormatVersion }}, fidl::DeprecatedCTypeTraits<{{ .ValueType }}>::kType, std::move(bytes), std::move(handles), {{ .ErrorCode }}));
{{- if .HandleDefs }}
for (const zx_handle_info_t handle_info : handle_defs) {
EXPECT_EQ(ZX_ERR_BAD_HANDLE, zx_object_get_info(handle_info.handle, ZX_INFO_HANDLE_VALID, nullptr, 0, nullptr, nullptr));
}
{{- end }}
}
TEST(C_Conformance, {{ .Name }}_Validate_Failure) {
{{- if .HandleDefs }}
const std::vector<zx_handle_info_t> handle_defs = {{ .HandleDefs }};
{{- end }}
std::vector<uint8_t> bytes = {{ .Bytes }};
std::vector<zx_handle_info_t> handles = {{ .Handles }};
EXPECT_TRUE(c_conformance_utils::ValidateFailure({{ .WireFormatVersion }}, fidl::DeprecatedCTypeTraits<{{ .ValueType }}>::kType, std::move(bytes), handles, {{ .ErrorCode }}));
{{- if .HandleDefs }}
for (const zx_handle_info_t handle_info : handle_defs) {
EXPECT_EQ(ZX_OK, zx_object_get_info(handle_info.handle, ZX_INFO_HANDLE_VALID, nullptr, 0, nullptr, nullptr));
}
{{- end }}
}
{{- if .FuchsiaOnly }}
#endif // __Fuchsia__
{{- end }}
{{ end }}