|
| 1 | +defmodule GRPC.Server.Adapters.Cowboy.HandlerTest do |
| 2 | + use ExUnit.Case, async: false |
| 3 | + |
| 4 | + import ExUnit.CaptureLog |
| 5 | + |
| 6 | + # -------------------------------------------------------------------------- |
| 7 | + # Minimal server used across all tests |
| 8 | + # -------------------------------------------------------------------------- |
| 9 | + |
| 10 | + defmodule HelloServer do |
| 11 | + use GRPC.Server, service: Helloworld.Greeter.Service |
| 12 | + |
| 13 | + def say_hello(req, _stream) do |
| 14 | + %Helloworld.HelloReply{message: "Hello, #{req.name}"} |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + # -------------------------------------------------------------------------- |
| 19 | + # Helpers |
| 20 | + # -------------------------------------------------------------------------- |
| 21 | + |
| 22 | + # Build a gRPC length-prefixed message frame (no compression). |
| 23 | + defp grpc_frame(proto_binary) do |
| 24 | + <<0::8, byte_size(proto_binary)::32, proto_binary::binary>> |
| 25 | + end |
| 26 | + |
| 27 | + defp grpc_request_headers do |
| 28 | + [ |
| 29 | + {"content-type", "application/grpc+proto"}, |
| 30 | + {"te", "trailers"} |
| 31 | + ] |
| 32 | + end |
| 33 | + |
| 34 | + # Open an HTTP/2 cleartext connection to the server and return the conn pid. |
| 35 | + defp open_h2(port) do |
| 36 | + {:ok, conn} = :gun.open(~c"localhost", port, %{protocols: [:http2]}) |
| 37 | + {:ok, :http2} = :gun.await_up(conn, 5_000) |
| 38 | + conn |
| 39 | + end |
| 40 | + |
| 41 | + # Collect all gun frames for *stream_ref* until END_STREAM, then return the |
| 42 | + # final grpc-status value found in either the response headers or trailers. |
| 43 | + defp collect_grpc_status(conn, stream_ref) do |
| 44 | + collect_grpc_status(conn, stream_ref, nil) |
| 45 | + end |
| 46 | + |
| 47 | + defp collect_grpc_status(conn, stream_ref, last_status) do |
| 48 | + case :gun.await(conn, stream_ref, 5_000) do |
| 49 | + {:response, :fin, _http_status, headers} -> |
| 50 | + find_grpc_status(headers) || last_status |
| 51 | + |
| 52 | + {:response, :nofin, _http_status, headers} -> |
| 53 | + collect_grpc_status(conn, stream_ref, find_grpc_status(headers)) |
| 54 | + |
| 55 | + {:data, :fin, _data} -> |
| 56 | + last_status |
| 57 | + |
| 58 | + {:data, :nofin, _data} -> |
| 59 | + collect_grpc_status(conn, stream_ref, last_status) |
| 60 | + |
| 61 | + {:trailers, trailers} -> |
| 62 | + find_grpc_status(trailers) || last_status |
| 63 | + |
| 64 | + {:error, reason} -> |
| 65 | + flunk("gun error: #{inspect(reason)}") |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + defp find_grpc_status(headers) do |
| 70 | + case List.keyfind(headers, "grpc-status", 0) do |
| 71 | + {"grpc-status", v} -> v |
| 72 | + nil -> nil |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + # -------------------------------------------------------------------------- |
| 77 | + # Tests: max_body_size enforcement |
| 78 | + # -------------------------------------------------------------------------- |
| 79 | + |
| 80 | + describe "max_body_size" do |
| 81 | + test "rejects a body that exceeds max_body_size with RESOURCE_EXHAUSTED (8)" do |
| 82 | + capture_log(fn -> |
| 83 | + run_server_with_opts([HelloServer], [max_body_size: 64], fn port -> |
| 84 | + # Build a gRPC frame whose total size is well above the 64-byte cap. |
| 85 | + large_name = String.duplicate("x", 200) |
| 86 | + |
| 87 | + body = |
| 88 | + grpc_frame(Protobuf.encode(%Helloworld.HelloRequest{name: large_name})) |
| 89 | + |
| 90 | + assert byte_size(body) > 64, |
| 91 | + "test body (#{byte_size(body)} bytes) must exceed max_body_size: 64" |
| 92 | + |
| 93 | + conn = open_h2(port) |
| 94 | + ref = :gun.post(conn, "/helloworld.Greeter/SayHello", grpc_request_headers(), body) |
| 95 | + |
| 96 | + assert collect_grpc_status(conn, ref) == "8" |
| 97 | + |
| 98 | + :gun.close(conn) |
| 99 | + end) |
| 100 | + end) |
| 101 | + end |
| 102 | + |
| 103 | + test "allows a body within max_body_size and returns OK (0)" do |
| 104 | + run_server_with_opts([HelloServer], [max_body_size: 4096], fn port -> |
| 105 | + body = grpc_frame(Protobuf.encode(%Helloworld.HelloRequest{name: "hi"})) |
| 106 | + |
| 107 | + assert byte_size(body) < 4096, |
| 108 | + "test body (#{byte_size(body)} bytes) must fit within max_body_size: 4096" |
| 109 | + |
| 110 | + conn = open_h2(port) |
| 111 | + ref = :gun.post(conn, "/helloworld.Greeter/SayHello", grpc_request_headers(), body) |
| 112 | + |
| 113 | + assert collect_grpc_status(conn, ref) == "0" |
| 114 | + |
| 115 | + :gun.close(conn) |
| 116 | + end) |
| 117 | + end |
| 118 | + |
| 119 | + test "default max_body_size is 4 MB – normal requests succeed without explicit option" do |
| 120 | + run_server_with_opts([HelloServer], [], fn port -> |
| 121 | + body = grpc_frame(Protobuf.encode(%Helloworld.HelloRequest{name: "default limit"})) |
| 122 | + |
| 123 | + conn = open_h2(port) |
| 124 | + ref = :gun.post(conn, "/helloworld.Greeter/SayHello", grpc_request_headers(), body) |
| 125 | + |
| 126 | + assert collect_grpc_status(conn, ref) == "0" |
| 127 | + |
| 128 | + :gun.close(conn) |
| 129 | + end) |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + # -------------------------------------------------------------------------- |
| 134 | + # Tests: read timeout – no :infinity when grpc-timeout is absent |
| 135 | + # -------------------------------------------------------------------------- |
| 136 | + |
| 137 | + describe "read timeout" do |
| 138 | + test "omitting grpc-timeout header still completes a normal request" do |
| 139 | + # If timeout_left_opt/1 incorrectly passed :infinity to cowboy for a |
| 140 | + # nil timer, normal unary requests would still succeed – the regression |
| 141 | + # is that a slow-trickle attack could hold the connection indefinitely. |
| 142 | + # This smoke-test verifies the nil-timer path doesn't break normal calls. |
| 143 | + run_server_with_opts([HelloServer], [], fn port -> |
| 144 | + # Deliberately omit the grpc-timeout header. |
| 145 | + headers = grpc_request_headers() |
| 146 | + body = grpc_frame(Protobuf.encode(%Helloworld.HelloRequest{name: "no timeout header"})) |
| 147 | + |
| 148 | + conn = open_h2(port) |
| 149 | + ref = :gun.post(conn, "/helloworld.Greeter/SayHello", headers, body) |
| 150 | + |
| 151 | + assert collect_grpc_status(conn, ref) == "0" |
| 152 | + |
| 153 | + :gun.close(conn) |
| 154 | + end) |
| 155 | + end |
| 156 | + end |
| 157 | + |
| 158 | + # -------------------------------------------------------------------------- |
| 159 | + # Private helper: start a server with specific opts and run a test function |
| 160 | + # -------------------------------------------------------------------------- |
| 161 | + |
| 162 | + defp run_server_with_opts(servers, opts, func) do |
| 163 | + {:ok, _pid, port} = |
| 164 | + start_supervised(%{ |
| 165 | + id: {GRPC.Server, System.unique_integer([:positive])}, |
| 166 | + start: {GRPC.Server, :start, [servers, 0, opts]}, |
| 167 | + type: :worker, |
| 168 | + restart: :permanent, |
| 169 | + shutdown: 500 |
| 170 | + }) |
| 171 | + |
| 172 | + try do |
| 173 | + func.(port) |
| 174 | + after |
| 175 | + GRPC.Server.stop(servers) |
| 176 | + end |
| 177 | + end |
| 178 | +end |
0 commit comments