/* pic_xfer_open: URL schemes, and a whole libfossil sync round trip over an * in-memory stream standing in for net.c (no network). HOME must be a * private directory: a scratch repository is made there. */ #include #include #include #include #include #include "xfer.h" typedef struct { char host[256]; int port, tls, idle, connects, closes; char req[1 << 16]; size_t req_len; const char *reply; size_t reply_len, reply_off; int fail_read, unclean; } mem; static int mem_connect(void *state, const char *host, int port, int tls, int idle_seconds) { mem *m = state; (void)snprintf(m->host, sizeof m->host, "%s", host); m->port = port; m->tls = tls; m->idle = idle_seconds; m->connects++; m->req_len = 0; m->reply_off = 0; return 0; } static long mem_read(void *state, void *buf, size_t cap) { mem *m = state; size_t n = m->reply_len - m->reply_off; if (m->fail_read) { return -1; } if (n == 0) { return m->unclean ? PIC_STREAM_UNCLEAN : 0; } n = n < cap ? n : cap; memcpy(buf, m->reply + m->reply_off, n); m->reply_off += n; return (long)n; } static int mem_write(void *state, const void *buf, size_t len) { mem *m = state; assert(m->req_len + len < sizeof m->req); memcpy(m->req + m->req_len, buf, len); m->req_len += len; m->req[m->req_len] = '\0'; return 0; } static void mem_close(void *state) { ((mem *)state)->closes++; } static const char *mem_error(void *state) { (void)state; return "the in-memory peer failed"; } static void schemes(void) { static const char *const bad[] = {"zit:/home/x/repo", "ftp://example.org/r", "example.org/r", "", NULL}; static const char *const want[] = {"zit:", "ftp://", "https://", "https://", NULL}; for (int i = 0; bad[i]; i++) { fsl_xfer_config cfg = fsl_xfer_config_empty; pic_xfer *x = NULL; char err[256] = ""; assert(pic_xfer_open(bad[i], NULL, &cfg, &x, err, sizeof err) == -1 && !x); if (!strstr(err, want[i])) { fprintf(stderr, "%s: %s\n", bad[i], err); assert(0); } fsl_xfer_config_cleanup(&cfg); } puts("ok schemes"); } static const char *errmsg(fsl_cx *f) { char const *msg = NULL; (void)fsl_cx_err_get(f, &msg, NULL); return msg ? msg : "(none)"; } static fsl_cx *scratch_repo(const char *path) { fsl_cx *f = NULL; fsl_repo_create_opt opt = fsl_repo_create_opt_empty; (void)unlink(path); assert(fsl_cx_init(&f, NULL) == 0); opt.filename = path; opt.username = "tester"; assert(fsl_repo_create(f, &opt) == 0); return f; } static int config_has(fsl_cx *f, const char *needle) { int32_t n = 0; assert(fsl_db_get_int32(fsl_cx_db_repo(f), &n, "SELECT count(*) FROM repo.config WHERE instr(CAST(value AS TEXT), %Q) OR instr(name, %Q)", needle, needle) == 0); return n > 0; } /* libfossil keeps saved sync passwords in the global config db, ~/.fossil. */ static int global_sync_pw_saved(const char *home) { char path[1024]; sqlite3 *db = NULL; sqlite3_stmt *q = NULL; int n = 0; assert(snprintf(path, sizeof path, "%s/.fossil", home) < (int)sizeof path); if (access(path, F_OK) != 0) { return 0; } assert(sqlite3_open_v2(path, &db, SQLITE_OPEN_READONLY, NULL) == SQLITE_OK); if (sqlite3_prepare_v2(db, "SELECT count(*) FROM global_config WHERE name LIKE '%sync-pw%'", -1, &q, NULL) == SQLITE_OK && sqlite3_step(q) == SQLITE_ROW) { n = sqlite3_column_int(q, 0); } sqlite3_finalize(q); sqlite3_close(db); return n > 0; } /* One pull round against a canned reply. */ static void round_trip(void) { static const char body[] = "pragma server-version 22800 20260101 000000\n"; static char reply[512]; char path[1024], err[256] = ""; const char *home = getenv("HOME"); mem m; pic_stream s = {mem_connect, mem_read, mem_write, mem_close, mem_error, &m}; fsl_xfer_config cfg = fsl_xfer_config_empty; pic_xfer *x = NULL; fsl_cx *f; int rc; memset(&m, 0, sizeof m); (void)snprintf(reply, sizeof reply, "HTTP/1.1 200 OK\r\nContent-Type: application/x-fossil-uncompressed\r\nContent-Length: %zu\r\n\r\n%s", sizeof body - 1, body); m.reply = reply; m.reply_len = strlen(reply); m.unclean = 1; /* a TLS peer that just hangs up after a sized reply */ assert(home && snprintf(path, sizeof path, "%s/test_xfer.fossil", home) < (int)sizeof path); f = scratch_repo(path); assert(pic_xfer_open("https://tester:s3ntinel-pw@example.org:8443/r/proj", &s, &cfg, &x, err, sizeof err) == 0); cfg.op.pull = true; cfg.compressTraffic = false; /* so the cards can be read below */ rc = fsl_sync_client(f, &cfg, NULL); if (rc != 0) { fprintf(stderr, "sync: %s: %s\n", fsl_rc_cstr(rc), errmsg(f)); assert(0); } assert(m.connects == 1 && m.closes >= 1); assert(strcmp(m.host, "example.org") == 0 && m.port == 8443 && m.tls == 1 && m.idle == 120); assert(strncmp(m.req, "POST /r/proj", 12) == 0); assert(strstr(m.req, "\r\nHost: example.org:8443\r\n")); assert(strstr(m.req, "\npull ")); /* Neither the password nor anything derived from the URL with it is kept. */ assert(!config_has(f, "s3ntinel-pw")); assert(!config_has(f, "last-sync-pw")); assert(!global_sync_pw_saved(home)); fsl_xfer_config_cleanup(&cfg); pic_xfer_close(x); /* http:// defaults to port 80 without TLS; a failed read fails the sync with the stream's message. */ cfg = fsl_xfer_config_empty; memset(&m, 0, sizeof m); m.fail_read = 1; assert(pic_xfer_open("http://127.0.0.1/proj", &s, &cfg, &x, err, sizeof err) == 0); cfg.op.pull = true; rc = fsl_sync_client(f, &cfg, NULL); assert(rc != 0); assert(m.port == 80 && m.tls == 0 && strcmp(m.host, "127.0.0.1") == 0); if (!strstr(errmsg(f), "the in-memory peer failed")) { fprintf(stderr, "error: %s\n", errmsg(f)); assert(0); } fsl_xfer_config_cleanup(&cfg); pic_xfer_close(x); fsl_cx_finalize(f); (void)unlink(path); puts("ok round trip over an in-memory stream"); } int main(void) { schemes(); round_trip(); puts("test_xfer: all passed"); return 0; }