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.
4#include <gtest/gtest.h>
5#include <stdexcept>
6#include <string_view>
7
8using namespace bb::bbapi;
9
10#ifndef BB_NO_EXCEPTIONS
11
12// Test that exceptions thrown during command execution are caught and converted to ErrorResponse
13TEST(CBind, CatchesExceptionAndReturnsErrorResponse)
14{
15 // Create an SrsInitSrs command with invalid data that will cause an exception
16 // The from_buffer calls in bbapi_srs.cpp will read past buffer boundaries
17 SrsInitSrs cmd;
18 cmd.num_points = 100; // Request 100 points (6400 bytes needed)
19 cmd.points_buf = std::vector<uint8_t>(10, 0); // Only provide 10 bytes - will cause out of bounds access
20 cmd.g2_point = std::vector<uint8_t>(10, 0); // Also too small (needs 128 bytes)
21
22 Command command = std::move(cmd);
23
24 // Call bbapi - exception should be caught and converted to ErrorResponse
25 CommandResponse response = bbapi(std::move(command));
26
27 // Check that we got an ErrorResponse using get_type_name()
28 std::string_view type_name = response.get_type_name();
29 EXPECT_EQ(type_name, "ErrorResponse") << "Expected ErrorResponse but got: " << type_name;
30
31 // Also verify using std::holds_alternative on the underlying variant
32 bool is_error = std::holds_alternative<ErrorResponse>(response.get());
33 EXPECT_TRUE(is_error) << "Expected ErrorResponse variant";
34
35 if (is_error) {
36 const auto& error = std::get<ErrorResponse>(response.get());
37 EXPECT_FALSE(error.message.empty()) << "Error message should not be empty";
38 std::cout << "Successfully caught exception with message: " << error.message << '\n';
39 }
40}
41
42// Test that valid operations still work correctly (no false positives)
43TEST(CBind, ValidOperationReturnsSuccess)
44{
45 // Create a Shutdown command which should succeed without throwing
46 Shutdown shutdown_cmd;
47 Command command = shutdown_cmd;
48
49 // Call bbapi - should return success response
50 CommandResponse response = bbapi(std::move(command));
51
52 // Check that we got a ShutdownResponse, not an ErrorResponse
53 std::string_view type_name = response.get_type_name();
54 EXPECT_NE(type_name, "ErrorResponse") << "Valid command should not return ErrorResponse";
55 EXPECT_EQ(type_name, "ShutdownResponse") << "Expected ShutdownResponse";
56
57 // Also verify using std::holds_alternative on the underlying variant
58 bool is_shutdown = std::holds_alternative<Shutdown::Response>(response.get());
59 EXPECT_TRUE(is_shutdown) << "Expected Shutdown::Response variant";
60}
61
62#else
63TEST(CBind, ExceptionsDisabled)
64{
65 GTEST_SKIP() << "Skipping exception handling tests when BB_NO_EXCEPTIONS is defined";
66}
67#endif
SRS (Structured Reference String) initialization command definitions for the Barretenberg RPC API.
TEST(CBind, CatchesExceptionAndReturnsErrorResponse)
A wrapper around std::variant that provides msgpack serialization based on type names.
VariantType & get()
std::string_view get_type_name() const
CommandResponse bbapi(Command &&command)
Main API function that processes commands and returns responses.
Definition c_bind.cpp:24
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
Initialize BN254 SRS with G1 and G2 points.
Definition bbapi_srs.hpp:20
std::vector< uint8_t > points_buf
Definition bbapi_srs.hpp:30
std::vector< uint8_t > g2_point
Definition bbapi_srs.hpp:32