Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
c_bind_exception.test.cpp
Go to the documentation of this file.
3#include "barretenberg/bbapi/generated/bb_dispatch.hpp"
4#include "barretenberg/bbapi/generated/bb_types.hpp"
6#include <gtest/gtest.h>
7#include <stdexcept>
8#include <string_view>
9#include <vector>
10
11using namespace bb;
12
13#ifndef BB_NO_EXCEPTIONS
14
15namespace {
16// Pack a wire-typed command into the bb dispatcher's expected input:
17// `[ [type_name, payload] ]`.
18template <typename WireCmd> std::vector<uint8_t> pack_wire_command(const WireCmd& cmd)
19{
20 msgpack::sbuffer buf;
21 msgpack::packer<msgpack::sbuffer> pk(buf);
22 pk.pack_array(1);
23 pk.pack_array(2);
24 pk.pack(std::string(WireCmd::MSGPACK_SCHEMA_NAME));
25 pk.pack(cmd);
26 return std::vector<uint8_t>(buf.data(), buf.data() + buf.size());
27}
28
29// Extract the response type name from a packed `[name, payload]` response.
30std::string response_type_name(const std::vector<uint8_t>& bytes)
31{
32 auto unpacked = msgpack::unpack(reinterpret_cast<const char*>(bytes.data()), bytes.size());
33 auto obj = unpacked.get();
34 if (obj.type != msgpack::type::ARRAY || obj.via.array.size != 2) {
35 return "";
36 }
37 const auto& name_obj = obj.via.array.ptr[0];
38 return std::string(name_obj.via.str.ptr, name_obj.via.str.size);
39}
40
41// Extract the error message from an ErrorResponse-shaped `[name, {message: ...}]`.
42std::string response_error_message(const std::vector<uint8_t>& bytes)
43{
44 auto unpacked = msgpack::unpack(reinterpret_cast<const char*>(bytes.data()), bytes.size());
45 auto obj = unpacked.get();
46 bbapi::wire::ErrorResponse err;
47 obj.via.array.ptr[1].convert(err);
48 return err.message;
49}
50} // namespace
51
52// Test that exceptions thrown during command execution are caught by the
53// codegen-emitted dispatcher and converted to ErrorResponse.
54TEST(CBind, CatchesExceptionAndReturnsErrorResponse)
55{
56 // SrsInitSrs with num_points=100 requests 6400 bytes but points_buf has only 10.
57 bbapi::wire::SrsInitSrs cmd{ .points_buf = std::vector<uint8_t>(10, 0),
58 .num_points = 100,
59 .g2_point = std::vector<uint8_t>(10, 0) };
60
61 bbapi::BBApiRequest request;
62 auto handler = bbapi::make_bb_handler(request);
63 auto response = handler(pack_wire_command(cmd));
64
65 EXPECT_EQ(response_type_name(response), "ErrorResponse");
66 auto msg = response_error_message(response);
67 EXPECT_FALSE(msg.empty()) << "Error message should not be empty";
68 std::cout << "Successfully caught exception with message: " << msg << '\n';
69}
70
71TEST(CBind, ValidOperationReturnsSuccess)
72{
73 bbapi::wire::Blake2s cmd{ .data = std::vector<uint8_t>{ 1, 2, 3 } };
74
75 bbapi::BBApiRequest request;
76 auto handler = bbapi::make_bb_handler(request);
77
78 auto response = handler(pack_wire_command(cmd));
79 EXPECT_EQ(response_type_name(response), "Blake2sResponse");
80}
81
82#else
83TEST(CBind, ExceptionsDisabled)
84{
85 GTEST_SKIP() << "Skipping exception handling tests when BB_NO_EXCEPTIONS is defined";
86}
87#endif
Non-template handler declarations for the bb service.
Shared type definitions for the Barretenberg RPC API.
TEST(CBind, CatchesExceptionAndReturnsErrorResponse)
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13