master
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24#include "unitcheck.h"
25#include "urldata.h"
26#include "mqtt.h"
27#include "select.h"
28#include "url.h"
30#ifndef CURL_DISABLE_MQTT
32enum {
33 T1693_CONNECT_LEN = 26,
34 T1693_PUBLISH_LEN = 15,
35 T1693_DISCONNECT_LEN = 2,
36 T1693_PUBLISH_END = T1693_CONNECT_LEN + T1693_PUBLISH_LEN,
37 T1693_WIRE_LEN = T1693_PUBLISH_END + T1693_DISCONNECT_LEN
38};
40struct t1693_send {
41 size_t n;
42 CURLcode result;
43};
45struct t1693_ctx {
46 const struct t1693_send *sends;
47 size_t nsends;
48 size_t send_index;
49 size_t recv_index;
50 size_t written;
51 size_t pending;
52 unsigned char expected[T1693_WIRE_LEN];
53 unsigned char output[T1693_WIRE_LEN];
54};
56static CURLcode t1693_send(struct Curl_easy *data, int8_t sockindex,
57 const uint8_t *buf, size_t len, bool eos,
58 size_t *pnwritten)
59{
60 struct t1693_ctx *ctx = data->set.private_data;
61 const struct t1693_send *send;
62 size_t remaining;
63 (void)sockindex;
64 (void)eos;
65 *pnwritten = 0;
67 abort_unless(ctx->send_index < ctx->nsends, "unexpected send");
68 send = &ctx->sends[ctx->send_index++];
69 if(ctx->send_index == 1) {
70 /* The CONNECT client ID is random. Save the packet for retry checks. */
71 abort_unless(len == T1693_CONNECT_LEN, "unexpected CONNECT size");
72 memcpy(ctx->expected, buf, len);
73 fail_unless(buf[0] == 0x10 && buf[1] == 0x18,
74 "unexpected CONNECT header");
75 }
77 if(ctx->written < T1693_CONNECT_LEN)
78 remaining = T1693_CONNECT_LEN - ctx->written;
79 else if(ctx->written < T1693_PUBLISH_END)
80 remaining = T1693_PUBLISH_END - ctx->written;
81 else
82 remaining = sizeof(ctx->expected) - ctx->written;
83 abort_unless(len == remaining, "packet sent before previous packet drained");
84 fail_unless(!memcmp(buf, ctx->expected + ctx->written, len),
85 "queued packet bytes changed");
86 ctx->pending = len;
87 if(send->result)
88 return send->result;
90 abort_unless(send->n <= len, "send script exceeds packet size");
91 memcpy(ctx->output + ctx->written, buf, send->n);
92 ctx->written += send->n;
93 ctx->pending -= send->n;
94 *pnwritten = send->n;
95 return CURLE_OK;
97unit_test_abort:
98 return CURLE_SEND_ERROR;
99}
101static CURLcode t1693_recv(struct Curl_easy *data, int8_t sockindex,
102 char *buf, size_t len, size_t *pnread)
103{
104 static const unsigned char connack[] = { 0x20, 0x02, 0x00, 0x00 };
105 struct t1693_ctx *ctx = data->set.private_data;
106 size_t n = sizeof(connack) - ctx->recv_index;
107 (void)sockindex;
108 *pnread = 0;
109 abort_unless(ctx->written == T1693_CONNECT_LEN,
110 "read while an outgoing packet is queued");
111 abort_unless(n, "unexpected read after CONNACK");
112 if(n > len)
113 n = len;
114 memcpy(buf, connack + ctx->recv_index, n);
115 ctx->recv_index += n;
116 *pnread = n;
117 return CURLE_OK;
119unit_test_abort:
120 return CURLE_RECV_ERROR;
121}
123static void t1693_meta_dtor(void *p)
124{
125 /* MQTT metadata supplies its own destructor. */
126 (void)p;
127}
129static void t1693_run(const struct t1693_send *sends, size_t nsends,
130 CURLcode expected_result)
131{
132 static const unsigned char post[] = {
133 0x30, 0x0d, 0x00, 0x04, 't', 'e', 's', 't',
134 'p', 'a', 'y', 'l', 'o', 'a', 'd', 0xe0, 0x00
135 };
136 struct t1693_ctx ctx;
137 struct Curl_easy *data = NULL;
138 struct connectdata *conn = NULL;
139 struct easy_pollset ps;
140 CURLcode result;
141 bool done = FALSE;
142 size_t i;
144 Curl_pollset_init(&ps);
145 memset(&ctx, 0, sizeof(ctx));
146 ctx.sends = sends;
147 ctx.nsends = nsends;
148 memcpy(ctx.expected + T1693_CONNECT_LEN, post, sizeof(post));
150 data = curl_easy_init();
151 abort_unless(data, "easy handle allocation failed");
152 conn = curlx_calloc(1, sizeof(*conn));
153 abort_unless(conn, "connection allocation failed");
154 Curl_hash_init(&conn->meta_hash, 23, CURL_HASH_TYPE_BYTES, t1693_meta_dtor);
155 data->conn = conn;
156 conn->sock[FIRSTSOCKET] = 42;
157 conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD;
158 conn->send[FIRSTSOCKET] = t1693_send;
159 conn->recv[FIRSTSOCKET] = t1693_recv;
160 data->set.private_data = &ctx;
161 data->set.postfields = CURL_UNCONST("payload");
162 data->state.httpreq = HTTPREQ_POST;
163 data->state.up.path = curlx_strdup("/test");
164 abort_unless(data->state.up.path, "path allocation failed");
165 result = Curl_protocol_mqtt.setup_connection(data, conn);
166 abort_unless(!result, "MQTT setup failed");
168 result = Curl_protocol_mqtt.do_it(data, &done);
169 for(i = 0; !result && !done && i < nsends + 4; ++i) {
170 Curl_pollset_reset(&ps);
171 result = Curl_protocol_mqtt.doing_pollset(data, &ps);
172 abort_unless(!result, "MQTT pollset failed");
173 abort_unless(ps.n == 1 && ps.sockets[0] == 42,
174 "unexpected pollset socket");
175 fail_unless(ps.actions[0] == (ctx.pending ? CURL_POLL_OUT : CURL_POLL_IN),
176 "polling the wrong direction for queued output");
177 result = Curl_protocol_mqtt.doing(data, &done);
178 fail_unless(!done || ctx.written == sizeof(ctx.output),
179 "transfer completed with unsent packet bytes");
180 }
181 fail_unless(result == expected_result, "unexpected transfer result");
182 fail_unless(ctx.send_index == nsends, "not all scripted sends were used");
183 fail_unless(!memcmp(ctx.output, ctx.expected, ctx.written),
184 "packets were not sent in order");
185 if(!expected_result) {
186 fail_unless(done, "transfer did not complete");
187 fail_unless(ctx.written == sizeof(ctx.output), "missing packet bytes");
188 }
189 else
190 fail_unless(!done, "send failure marked the transfer complete");
192unit_test_abort:
193 Curl_pollset_cleanup(&ps);
194 if(conn)
195 Curl_conn_free(data, conn);
196 if(data) {
197 data->conn = NULL;
198 curl_easy_cleanup(data);
199 }
200}
202#endif /* CURL_DISABLE_MQTT */
204static CURLcode test_unit1693(const char *arg)
205{
206 UNITTEST_BEGIN_SIMPLE
208#ifndef CURL_DISABLE_MQTT
209 static const struct t1693_send full[] = {
210 { T1693_CONNECT_LEN, CURLE_OK }, { T1693_PUBLISH_LEN, CURLE_OK },
211 { T1693_DISCONNECT_LEN, CURLE_OK }
212 };
213 static const struct t1693_send partial[] = {
214 { 3, CURLE_OK }, { 0, CURLE_AGAIN }, { 4, CURLE_OK }, { 19, CURLE_OK },
215 { 3, CURLE_OK }, { 4, CURLE_OK }, { 0, CURLE_AGAIN }, { 8, CURLE_OK },
216 { 0, CURLE_AGAIN }, { 1, CURLE_OK }, { 0, CURLE_AGAIN }, { 1, CURLE_OK }
217 };
218 static const struct t1693_send zero[] = {
219 { 0, CURLE_OK }, { T1693_CONNECT_LEN, CURLE_OK },
220 { 0, CURLE_OK }, { T1693_PUBLISH_LEN, CURLE_OK },
221 { 0, CURLE_OK }, { T1693_DISCONNECT_LEN, CURLE_OK }
222 };
223 static const struct t1693_send send_error[] = {
224 { T1693_CONNECT_LEN, CURLE_OK }, { 4, CURLE_OK }, { 0, CURLE_SEND_ERROR }
225 };
227 abort_unless(!curl_global_init(CURL_GLOBAL_ALL), "global init failed");
228 t1693_run(full, CURL_ARRAYSIZE(full), CURLE_OK);
229 t1693_run(partial, CURL_ARRAYSIZE(partial), CURLE_OK);
230 t1693_run(zero, CURL_ARRAYSIZE(zero), CURLE_OK);
231 t1693_run(send_error, CURL_ARRAYSIZE(send_error), CURLE_SEND_ERROR);
232 curl_global_cleanup();
233#endif
235 UNITTEST_END_SIMPLE
236}
Diagnostics4 git calls · 138 ms
CallStatusSizeTime0138 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2002.6 KiB132 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=132 git.response.bytes=2630 git.response.cache=MISS git.response.duration_ms=132 git.fetch.wants=f8bbb811cbde01be8866c4578b3b86d819ca44a0 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2630 git.pkt.payload.max=2609 git.sideband.band1.bytes=2608 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e2e721fa19ecfa
- Request
- 34dfd50c…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format