From patchwork Wed Aug 7 14:11:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chandra Pratap X-Patchwork-Id: 13756359 Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 844F72AF10 for ; Wed, 7 Aug 2024 14:16:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723040218; cv=none; b=L4dvQZh1CHOL5tzLqOIMl8+/UhjQ4bhlLZhSWGa/DVr7vlxbkII7FllYSiFYJGyhy5Ha1uIa8wOcvtnddi554K6PvaXKR2BKp9pCrhhqcCQUFkkxNVcs/O+xxGtwmusB1XJm1WWZuZLqYvZfoQ9Q+jrmnFmEQbOLsj2rWsqf/50= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723040218; c=relaxed/simple; bh=stqmcweIMPMEas1OCJSOLfDrAW5QfFO9g8qE4FHE+hc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sMAcvtJN5pqBC+nLUvhz1CPyfuK0FK1GQn/X5RvWPsUmuCY/qnawVuCqK3OvVo/W9Sivv2nP6P07dia7GtV+YSA1X0zE2728WweoNCmtRDsRNiYRey8ajRRT9BFrJUBxww11pGEzytt4NVZFIZbKIgIgfAKN2LfjPWfRw/pwAd0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ODHFcc6D; arc=none smtp.client-ip=209.85.216.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ODHFcc6D" Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-2cb63ceff6dso1455363a91.1 for ; Wed, 07 Aug 2024 07:16:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723040215; x=1723645015; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=SAUWDxzybE8j+ucUI5K6++zQJVuVePlW53lS1vKvI08=; b=ODHFcc6Dshu5CVAYIRlHo2cjusI4zsN2I7RS4nGhm+BZfFiSvPEq/m/OUsB5bBMkjA 39E6/hKio8e5BpLH8pFA4nXFbxd2JdBD0ZAdFJJ5IsrpoCWgyyXSdpDA0It6Xqyyezoj Y2Miu8vuY5s+z9paMQlCwtXs4GKJ8AiSD69GhlE5zkACeB5nqc1uMXbjiSGlUFIx43a2 qmwWSYLhqER7Bkn5cfB8JZUPaXY0xIUPxQ7IEF7c68rFGkV7xqpRXqsThXQP8CZnQ9Td unvc2DLr/ydYB59Ov+fh+sa//lVJh7Q0ywBTT1n4+SBvnx/U/vk+bwOgMbOpUsIlUipr SS0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723040215; x=1723645015; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SAUWDxzybE8j+ucUI5K6++zQJVuVePlW53lS1vKvI08=; b=kzWgDkg+Xn6iIbWYeKDAb14d6sIvniWXqgN6aKqfRr1D6S70PcDSyCh4DP4v6OB51X oafPUiv5WSmFc46Gxkfsnu2cGamKIyGBBz5XIVv3tElxMehBHzPR8ThTQqrAOg6yFqv+ De7lf/SQeRHSIoRbqtEqlkhY5St4/f489kSx4OT6JrpAc8dRfEPgR9BezaH8IIMCKZCN 0RL7Y+iwh2gRRUPji+/PTxl+M6YB0Zx1rY0g94GbZcytJPjAWO6J3LCyhcUNd1pK97ro GdJk6qeMOx1oLgQakAJYQb5YVZ3d6nfeY8Q6M0rW/RONOn7/gjznEvviw45HKFwbx5nA 1E2w== X-Gm-Message-State: AOJu0Yz05DvbAZOk/BMg0tu1mboSVh4GhXHN9vBuKBS6pO04tESilI6w QSa3ZIJN/cTjusvr3prcebaSQn3VBmfAsUj8XDWQlLCWIvbVleQqFAK2VK1o X-Google-Smtp-Source: AGHT+IE+g1EHB0ygzXugMHoY7PMoag9WP2xyI3fac6dkRcFU0iiz/SRcK7XMIuSDukJdUmWundtJVw== X-Received: by 2002:a17:90b:1d04:b0:2cd:2992:e8dc with SMTP id 98e67ed59e1d1-2cff93c4d44mr16912895a91.5.1723040215006; Wed, 07 Aug 2024 07:16:55 -0700 (PDT) Received: from Ubuntu.. ([106.205.252.196]) by smtp.googlemail.com with ESMTPSA id 98e67ed59e1d1-2d1b374b3cesm1635059a91.48.2024.08.07.07.16.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Aug 2024 07:16:54 -0700 (PDT) From: Chandra Pratap To: git@vger.kernel.org Cc: Chandra Pratap , Patrick Steinhardt , Christian Couder Subject: [PATCH 1/5] t: move reftable/readwrite_test.c to the unit testing framework Date: Wed, 7 Aug 2024 19:41:57 +0530 Message-ID: <20240807141608.4524-2-chandrapratap3519@gmail.com> X-Mailer: git-send-email 2.45.GIT In-Reply-To: <20240807141608.4524-1-chandrapratap3519@gmail.com> References: <20240807141608.4524-1-chandrapratap3519@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 reftable/readwrite_test.c exercises the functions defined in reftable/reader.{c,h} and reftable/writer.{c,h}. Migrate reftable/readwrite_test.c to the unit testing framework. Migration involves refactoring the tests to use the unit testing framework instead of reftable's test framework and renaming the tests to align with unit-tests' naming conventions. Since some tests in reftable/readwrite_test.c use the functions set_test_hash(), noop_flush() and strbuf_add_void() defined in reftable/test_framework.{c,h} but these files are not #included in the ported unit test, copy these functions in the new test file. While at it, ensure structs are 0-initialized with '= { 0 }' instead of '= { NULL }'. Mentored-by: Patrick Steinhardt Mentored-by: Christian Couder Signed-off-by: Chandra Pratap --- Makefile | 2 +- reftable/reftable-tests.h | 1 - t/helper/test-reftable.c | 1 - .../unit-tests/t-reftable-readwrite.c | 418 +++++++++--------- 4 files changed, 210 insertions(+), 212 deletions(-) rename reftable/readwrite_test.c => t/unit-tests/t-reftable-readwrite.c (70%) diff --git a/Makefile b/Makefile index 3863e60b66..76e4d1c1ec 100644 --- a/Makefile +++ b/Makefile @@ -1341,6 +1341,7 @@ UNIT_TEST_PROGRAMS += t-oidtree UNIT_TEST_PROGRAMS += t-prio-queue UNIT_TEST_PROGRAMS += t-reftable-basics UNIT_TEST_PROGRAMS += t-reftable-merged +UNIT_TEST_PROGRAMS += t-reftable-readwrite UNIT_TEST_PROGRAMS += t-reftable-record UNIT_TEST_PROGRAMS += t-strbuf UNIT_TEST_PROGRAMS += t-strcmp-offset @@ -2682,7 +2683,6 @@ REFTABLE_OBJS += reftable/writer.o REFTABLE_TEST_OBJS += reftable/block_test.o REFTABLE_TEST_OBJS += reftable/dump.o REFTABLE_TEST_OBJS += reftable/pq_test.o -REFTABLE_TEST_OBJS += reftable/readwrite_test.o REFTABLE_TEST_OBJS += reftable/stack_test.o REFTABLE_TEST_OBJS += reftable/test_framework.o REFTABLE_TEST_OBJS += reftable/tree_test.o diff --git a/reftable/reftable-tests.h b/reftable/reftable-tests.h index d5e03dcc1b..7d955393d2 100644 --- a/reftable/reftable-tests.h +++ b/reftable/reftable-tests.h @@ -13,7 +13,6 @@ int basics_test_main(int argc, const char **argv); int block_test_main(int argc, const char **argv); int pq_test_main(int argc, const char **argv); int record_test_main(int argc, const char **argv); -int readwrite_test_main(int argc, const char **argv); int stack_test_main(int argc, const char **argv); int tree_test_main(int argc, const char **argv); int reftable_dump_main(int argc, char *const *argv); diff --git a/t/helper/test-reftable.c b/t/helper/test-reftable.c index 9d378427da..d371e9f9dd 100644 --- a/t/helper/test-reftable.c +++ b/t/helper/test-reftable.c @@ -8,7 +8,6 @@ int cmd__reftable(int argc, const char **argv) block_test_main(argc, argv); tree_test_main(argc, argv); pq_test_main(argc, argv); - readwrite_test_main(argc, argv); stack_test_main(argc, argv); return 0; } diff --git a/reftable/readwrite_test.c b/t/unit-tests/t-reftable-readwrite.c similarity index 70% rename from reftable/readwrite_test.c rename to t/unit-tests/t-reftable-readwrite.c index f411abfe9c..235e3d94c7 100644 --- a/reftable/readwrite_test.c +++ b/t/unit-tests/t-reftable-readwrite.c @@ -6,37 +6,48 @@ license that can be found in the LICENSE file or at https://developers.google.com/open-source/licenses/bsd */ -#include "system.h" - -#include "basics.h" -#include "block.h" -#include "blocksource.h" -#include "reader.h" -#include "record.h" -#include "test_framework.h" -#include "reftable-tests.h" -#include "reftable-writer.h" +#include "test-lib.h" +#include "reftable/reader.h" +#include "reftable/blocksource.h" +#include "reftable/reftable-error.h" +#include "reftable/reftable-writer.h" static const int update_index = 5; -static void test_buffer(void) +static void set_test_hash(uint8_t *p, int i) +{ + memset(p, (uint8_t)i, hash_size(GIT_SHA1_FORMAT_ID)); +} + +static ssize_t strbuf_add_void(void *b, const void *data, size_t sz) +{ + strbuf_add(b, data, sz); + return sz; +} + +static int noop_flush(void *arg) +{ + return 0; +} + +static void t_buffer(void) { struct strbuf buf = STRBUF_INIT; - struct reftable_block_source source = { NULL }; - struct reftable_block out = { NULL }; + struct reftable_block_source source = { 0 }; + struct reftable_block out = { 0 }; int n; uint8_t in[] = "hello"; strbuf_add(&buf, in, sizeof(in)); block_source_from_strbuf(&source, &buf); - EXPECT(block_source_size(&source) == 6); + check_int(block_source_size(&source), ==, 6); n = block_source_read_block(&source, &out, 0, sizeof(in)); - EXPECT(n == sizeof(in)); - EXPECT(!memcmp(in, out.data, n)); + check_int(n, ==, sizeof(in)); + check(!memcmp(in, out.data, n)); reftable_block_done(&out); n = block_source_read_block(&source, &out, 1, 2); - EXPECT(n == 2); - EXPECT(!memcmp(out.data, "el", 2)); + check_int(n, ==, 2); + check(!memcmp(out.data, "el", 2)); reftable_block_done(&out); block_source_close(&source); @@ -52,9 +63,9 @@ static void write_table(char ***names, struct strbuf *buf, int N, }; struct reftable_writer *w = reftable_new_writer(&strbuf_add_void, &noop_flush, buf, &opts); - struct reftable_ref_record ref = { NULL }; + struct reftable_ref_record ref = { 0 }; int i = 0, n; - struct reftable_log_record log = { NULL }; + struct reftable_log_record log = { 0 }; const struct reftable_stats *stats = NULL; REFTABLE_CALLOC_ARRAY(*names, N + 1); @@ -73,7 +84,7 @@ static void write_table(char ***names, struct strbuf *buf, int N, (*names)[i] = xstrdup(name); n = reftable_writer_add_ref(w, &ref); - EXPECT(n == 0); + check_int(n, ==, 0); } for (i = 0; i < N; i++) { @@ -89,27 +100,25 @@ static void write_table(char ***names, struct strbuf *buf, int N, log.value.update.message = (char *) "message"; n = reftable_writer_add_log(w, &log); - EXPECT(n == 0); + check_int(n, ==, 0); } n = reftable_writer_close(w); - EXPECT(n == 0); + check_int(n, ==, 0); stats = reftable_writer_stats(w); for (i = 0; i < stats->ref_stats.blocks; i++) { int off = i * opts.block_size; - if (off == 0) { - off = header_size( - (hash_id == GIT_SHA256_FORMAT_ID) ? 2 : 1); - } - EXPECT(buf->buf[off] == 'r'); + if (!off) + off = header_size((hash_id == GIT_SHA256_FORMAT_ID) ? 2 : 1); + check(buf->buf[off] == 'r'); } - EXPECT(stats->log_stats.blocks > 0); + check(stats->log_stats.blocks > 0); reftable_writer_free(w); } -static void test_log_buffer_size(void) +static void t_log_buffer_size(void) { struct strbuf buf = STRBUF_INIT; struct reftable_write_options opts = { @@ -140,14 +149,14 @@ static void test_log_buffer_size(void) } reftable_writer_set_limits(w, update_index, update_index); err = reftable_writer_add_log(w, &log); - EXPECT_ERR(err); + check(!err); err = reftable_writer_close(w); - EXPECT_ERR(err); + check(!err); reftable_writer_free(w); strbuf_release(&buf); } -static void test_log_overflow(void) +static void t_log_overflow(void) { struct strbuf buf = STRBUF_INIT; char msg[256] = { 0 }; @@ -177,12 +186,12 @@ static void test_log_overflow(void) memset(msg, 'x', sizeof(msg) - 1); reftable_writer_set_limits(w, update_index, update_index); err = reftable_writer_add_log(w, &log); - EXPECT(err == REFTABLE_ENTRY_TOO_BIG_ERROR); + check_int(err, ==, REFTABLE_ENTRY_TOO_BIG_ERROR); reftable_writer_free(w); strbuf_release(&buf); } -static void test_log_write_read(void) +static void t_log_write_read(void) { int N = 2; char **names = reftable_calloc(N + 1, sizeof(*names)); @@ -190,13 +199,13 @@ static void test_log_write_read(void) struct reftable_write_options opts = { .block_size = 256, }; - struct reftable_ref_record ref = { NULL }; + struct reftable_ref_record ref = { 0 }; int i = 0; - struct reftable_log_record log = { NULL }; + struct reftable_log_record log = { 0 }; int n; - struct reftable_iterator it = { NULL }; - struct reftable_reader rd = { NULL }; - struct reftable_block_source source = { NULL }; + struct reftable_iterator it = { 0 }; + struct reftable_reader rd = { 0 }; + struct reftable_block_source source = { 0 }; struct strbuf buf = STRBUF_INIT; struct reftable_writer *w = reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts); @@ -204,17 +213,17 @@ static void test_log_write_read(void) reftable_writer_set_limits(w, 0, N); for (i = 0; i < N; i++) { char name[256]; - struct reftable_ref_record ref = { NULL }; + struct reftable_ref_record ref = { 0 }; snprintf(name, sizeof(name), "b%02d%0*d", i, 130, 7); names[i] = xstrdup(name); ref.refname = name; ref.update_index = i; err = reftable_writer_add_ref(w, &ref); - EXPECT_ERR(err); + check(!err); } for (i = 0; i < N; i++) { - struct reftable_log_record log = { NULL }; + struct reftable_log_record log = { 0 }; log.refname = names[i]; log.update_index = i; @@ -223,33 +232,33 @@ static void test_log_write_read(void) set_test_hash(log.value.update.new_hash, i + 1); err = reftable_writer_add_log(w, &log); - EXPECT_ERR(err); + check(!err); } n = reftable_writer_close(w); - EXPECT(n == 0); + check_int(n, ==, 0); stats = reftable_writer_stats(w); - EXPECT(stats->log_stats.blocks > 0); + check_int(stats->log_stats.blocks, >, 0); reftable_writer_free(w); w = NULL; block_source_from_strbuf(&source, &buf); err = init_reader(&rd, &source, "file.log"); - EXPECT_ERR(err); + check(!err); reftable_reader_init_ref_iterator(&rd, &it); err = reftable_iterator_seek_ref(&it, names[N - 1]); - EXPECT_ERR(err); + check(!err); err = reftable_iterator_next_ref(&it, &ref); - EXPECT_ERR(err); + check(!err); /* end of iteration. */ err = reftable_iterator_next_ref(&it, &ref); - EXPECT(0 < err); + check_int(err, >, 0); reftable_iterator_destroy(&it); reftable_ref_record_release(&ref); @@ -257,23 +266,21 @@ static void test_log_write_read(void) reftable_reader_init_log_iterator(&rd, &it); err = reftable_iterator_seek_log(&it, ""); - EXPECT_ERR(err); + check(!err); i = 0; while (1) { int err = reftable_iterator_next_log(&it, &log); - if (err > 0) { + if (err > 0) break; - } - - EXPECT_ERR(err); - EXPECT_STREQ(names[i], log.refname); - EXPECT(i == log.update_index); + check(!err); + check_str(names[i], log.refname); + check_int(i, ==, log.update_index); i++; reftable_log_record_release(&log); } - EXPECT(i == N); + check_int(i, ==, N); reftable_iterator_destroy(&it); /* cleanup. */ @@ -282,7 +289,7 @@ static void test_log_write_read(void) reader_close(&rd); } -static void test_log_zlib_corruption(void) +static void t_log_zlib_corruption(void) { struct reftable_write_options opts = { .block_size = 256, @@ -316,13 +323,13 @@ static void test_log_zlib_corruption(void) reftable_writer_set_limits(w, 1, 1); err = reftable_writer_add_log(w, &log); - EXPECT_ERR(err); + check(!err); n = reftable_writer_close(w); - EXPECT(n == 0); + check_int(n, ==, 0); stats = reftable_writer_stats(w); - EXPECT(stats->log_stats.blocks > 0); + check_int(stats->log_stats.blocks, >, 0); reftable_writer_free(w); w = NULL; @@ -332,11 +339,11 @@ static void test_log_zlib_corruption(void) block_source_from_strbuf(&source, &buf); err = init_reader(&rd, &source, "file.log"); - EXPECT_ERR(err); + check(!err); reftable_reader_init_log_iterator(&rd, &it); err = reftable_iterator_seek_log(&it, "refname"); - EXPECT(err == REFTABLE_ZLIB_ERROR); + check_int(err, ==, REFTABLE_ZLIB_ERROR); reftable_iterator_destroy(&it); @@ -345,14 +352,14 @@ static void test_log_zlib_corruption(void) reader_close(&rd); } -static void test_table_read_write_sequential(void) +static void t_table_read_write_sequential(void) { char **names; struct strbuf buf = STRBUF_INIT; int N = 50; - struct reftable_iterator it = { NULL }; - struct reftable_block_source source = { NULL }; - struct reftable_reader rd = { NULL }; + struct reftable_iterator it = { 0 }; + struct reftable_block_source source = { 0 }; + struct reftable_reader rd = { 0 }; int err = 0; int j = 0; @@ -361,26 +368,25 @@ static void test_table_read_write_sequential(void) block_source_from_strbuf(&source, &buf); err = init_reader(&rd, &source, "file.ref"); - EXPECT_ERR(err); + check(!err); reftable_reader_init_ref_iterator(&rd, &it); err = reftable_iterator_seek_ref(&it, ""); - EXPECT_ERR(err); + check(!err); while (1) { - struct reftable_ref_record ref = { NULL }; + struct reftable_ref_record ref = { 0 }; int r = reftable_iterator_next_ref(&it, &ref); - EXPECT(r >= 0); - if (r > 0) { + check_int(r, >=, 0); + if (r > 0) break; - } - EXPECT(0 == strcmp(names[j], ref.refname)); - EXPECT(update_index == ref.update_index); + check_str(names[j], ref.refname); + check_int(update_index, ==, ref.update_index); j++; reftable_ref_record_release(&ref); } - EXPECT(j == N); + check_int(j, ==, N); reftable_iterator_destroy(&it); strbuf_release(&buf); free_names(names); @@ -388,90 +394,88 @@ static void test_table_read_write_sequential(void) reader_close(&rd); } -static void test_table_write_small_table(void) +static void t_table_write_small_table(void) { char **names; struct strbuf buf = STRBUF_INIT; int N = 1; write_table(&names, &buf, N, 4096, GIT_SHA1_FORMAT_ID); - EXPECT(buf.len < 200); + check_int(buf.len, <, 200); strbuf_release(&buf); free_names(names); } -static void test_table_read_api(void) +static void t_table_read_api(void) { char **names; struct strbuf buf = STRBUF_INIT; int N = 50; - struct reftable_reader rd = { NULL }; - struct reftable_block_source source = { NULL }; + struct reftable_reader rd = { 0 }; + struct reftable_block_source source = { 0 }; int err; int i; - struct reftable_log_record log = { NULL }; - struct reftable_iterator it = { NULL }; + struct reftable_log_record log = { 0 }; + struct reftable_iterator it = { 0 }; write_table(&names, &buf, N, 256, GIT_SHA1_FORMAT_ID); block_source_from_strbuf(&source, &buf); err = init_reader(&rd, &source, "file.ref"); - EXPECT_ERR(err); + check(!err); reftable_reader_init_ref_iterator(&rd, &it); err = reftable_iterator_seek_ref(&it, names[0]); - EXPECT_ERR(err); + check(!err); err = reftable_iterator_next_log(&it, &log); - EXPECT(err == REFTABLE_API_ERROR); + check_int(err, ==, REFTABLE_API_ERROR); strbuf_release(&buf); - for (i = 0; i < N; i++) { + for (i = 0; i < N; i++) reftable_free(names[i]); - } reftable_iterator_destroy(&it); reftable_free(names); reader_close(&rd); strbuf_release(&buf); } -static void test_table_read_write_seek(int index, int hash_id) +static void t_table_read_write_seek(int index, int hash_id) { char **names; struct strbuf buf = STRBUF_INIT; int N = 50; - struct reftable_reader rd = { NULL }; - struct reftable_block_source source = { NULL }; + struct reftable_reader rd = { 0 }; + struct reftable_block_source source = { 0 }; int err; int i = 0; - struct reftable_iterator it = { NULL }; + struct reftable_iterator it = { 0 }; struct strbuf pastLast = STRBUF_INIT; - struct reftable_ref_record ref = { NULL }; + struct reftable_ref_record ref = { 0 }; write_table(&names, &buf, N, 256, hash_id); block_source_from_strbuf(&source, &buf); err = init_reader(&rd, &source, "file.ref"); - EXPECT_ERR(err); - EXPECT(hash_id == reftable_reader_hash_id(&rd)); + check(!err); + check_int(hash_id, ==, reftable_reader_hash_id(&rd)); - if (!index) { + if (!index) rd.ref_offsets.index_offset = 0; - } else { - EXPECT(rd.ref_offsets.index_offset > 0); - } + else + check_int(rd.ref_offsets.index_offset, >, 0); for (i = 1; i < N; i++) { reftable_reader_init_ref_iterator(&rd, &it); err = reftable_iterator_seek_ref(&it, names[i]); - EXPECT_ERR(err); + check(!err); err = reftable_iterator_next_ref(&it, &ref); - EXPECT_ERR(err); - EXPECT(0 == strcmp(names[i], ref.refname)); - EXPECT(REFTABLE_REF_VAL1 == ref.value_type); - EXPECT(i == ref.value.val1[0]); + check(!err); + check_str(names[i], ref.refname); + check_int(REFTABLE_REF_VAL1, ==, ref.value_type); + check_int(i, ==, ref.value.val1[0]); reftable_ref_record_release(&ref); reftable_iterator_destroy(&it); @@ -483,40 +487,39 @@ static void test_table_read_write_seek(int index, int hash_id) reftable_reader_init_ref_iterator(&rd, &it); err = reftable_iterator_seek_ref(&it, pastLast.buf); if (err == 0) { - struct reftable_ref_record ref = { NULL }; + struct reftable_ref_record ref = { 0 }; int err = reftable_iterator_next_ref(&it, &ref); - EXPECT(err > 0); + check_int(err, >, 0); } else { - EXPECT(err > 0); + check_int(err, >, 0); } strbuf_release(&pastLast); reftable_iterator_destroy(&it); strbuf_release(&buf); - for (i = 0; i < N; i++) { + for (i = 0; i < N; i++) reftable_free(names[i]); - } reftable_free(names); reader_close(&rd); } -static void test_table_read_write_seek_linear(void) +static void t_table_read_write_seek_linear(void) { - test_table_read_write_seek(0, GIT_SHA1_FORMAT_ID); + t_table_read_write_seek(0, GIT_SHA1_FORMAT_ID); } -static void test_table_read_write_seek_linear_sha256(void) +static void t_table_read_write_seek_linear_sha256(void) { - test_table_read_write_seek(0, GIT_SHA256_FORMAT_ID); + t_table_read_write_seek(0, GIT_SHA256_FORMAT_ID); } -static void test_table_read_write_seek_index(void) +static void t_table_read_write_seek_index(void) { - test_table_read_write_seek(1, GIT_SHA1_FORMAT_ID); + t_table_read_write_seek(1, GIT_SHA1_FORMAT_ID); } -static void test_table_refs_for(int indexed) +static void t_table_refs_for(int indexed) { int N = 50; char **want_names = reftable_calloc(N + 1, sizeof(*want_names)); @@ -526,18 +529,18 @@ static void test_table_refs_for(int indexed) struct reftable_write_options opts = { .block_size = 256, }; - struct reftable_ref_record ref = { NULL }; + struct reftable_ref_record ref = { 0 }; int i = 0; int n; int err; struct reftable_reader rd; - struct reftable_block_source source = { NULL }; + struct reftable_block_source source = { 0 }; struct strbuf buf = STRBUF_INIT; struct reftable_writer *w = reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts); - struct reftable_iterator it = { NULL }; + struct reftable_iterator it = { 0 }; int j; set_test_hash(want_hash, 4); @@ -546,7 +549,7 @@ static void test_table_refs_for(int indexed) uint8_t hash[GIT_SHA1_RAWSZ]; char fill[51] = { 0 }; char name[100]; - struct reftable_ref_record ref = { NULL }; + struct reftable_ref_record ref = { 0 }; memset(hash, i, sizeof(hash)); memset(fill, 'x', 50); @@ -563,16 +566,15 @@ static void test_table_refs_for(int indexed) */ /* blocks. */ n = reftable_writer_add_ref(w, &ref); - EXPECT(n == 0); + check_int(n, ==, 0); if (!memcmp(ref.value.val2.value, want_hash, GIT_SHA1_RAWSZ) || - !memcmp(ref.value.val2.target_value, want_hash, GIT_SHA1_RAWSZ)) { + !memcmp(ref.value.val2.target_value, want_hash, GIT_SHA1_RAWSZ)) want_names[want_names_len++] = xstrdup(name); - } } n = reftable_writer_close(w); - EXPECT(n == 0); + check_int(n, ==, 0); reftable_writer_free(w); w = NULL; @@ -580,33 +582,30 @@ static void test_table_refs_for(int indexed) block_source_from_strbuf(&source, &buf); err = init_reader(&rd, &source, "file.ref"); - EXPECT_ERR(err); - if (!indexed) { + check(!err); + if (!indexed) rd.obj_offsets.is_present = 0; - } reftable_reader_init_ref_iterator(&rd, &it); err = reftable_iterator_seek_ref(&it, ""); - EXPECT_ERR(err); + check(!err); reftable_iterator_destroy(&it); err = reftable_reader_refs_for(&rd, &it, want_hash); - EXPECT_ERR(err); + check(!err); j = 0; while (1) { int err = reftable_iterator_next_ref(&it, &ref); - EXPECT(err >= 0); - if (err > 0) { + check_int(err, >=, 0); + if (err > 0) break; - } - - EXPECT(j < want_names_len); - EXPECT(0 == strcmp(ref.refname, want_names[j])); + check_int(j, <, want_names_len); + check_str(ref.refname, want_names[j]); j++; reftable_ref_record_release(&ref); } - EXPECT(j == want_names_len); + check_int(j, ==, want_names_len); strbuf_release(&buf); free_names(want_names); @@ -614,54 +613,54 @@ static void test_table_refs_for(int indexed) reader_close(&rd); } -static void test_table_refs_for_no_index(void) +static void t_table_refs_for_no_index(void) { - test_table_refs_for(0); + t_table_refs_for(0); } -static void test_table_refs_for_obj_index(void) +static void t_table_refs_for_obj_index(void) { - test_table_refs_for(1); + t_table_refs_for(1); } -static void test_write_empty_table(void) +static void t_write_empty_table(void) { struct reftable_write_options opts = { 0 }; struct strbuf buf = STRBUF_INIT; struct reftable_writer *w = reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts); - struct reftable_block_source source = { NULL }; + struct reftable_block_source source = { 0 }; struct reftable_reader *rd = NULL; - struct reftable_ref_record rec = { NULL }; - struct reftable_iterator it = { NULL }; + struct reftable_ref_record rec = { 0 }; + struct reftable_iterator it = { 0 }; int err; reftable_writer_set_limits(w, 1, 1); err = reftable_writer_close(w); - EXPECT(err == REFTABLE_EMPTY_TABLE_ERROR); + check_int(err, ==, REFTABLE_EMPTY_TABLE_ERROR); reftable_writer_free(w); - EXPECT(buf.len == header_size(1) + footer_size(1)); + check_int(buf.len, ==, header_size(1) + footer_size(1)); block_source_from_strbuf(&source, &buf); err = reftable_new_reader(&rd, &source, "filename"); - EXPECT_ERR(err); + check(!err); reftable_reader_init_ref_iterator(rd, &it); err = reftable_iterator_seek_ref(&it, ""); - EXPECT_ERR(err); + check(!err); err = reftable_iterator_next_ref(&it, &rec); - EXPECT(err > 0); + check_int(err, >, 0); reftable_iterator_destroy(&it); reftable_reader_free(rd); strbuf_release(&buf); } -static void test_write_object_id_min_length(void) +static void t_write_object_id_min_length(void) { struct reftable_write_options opts = { .block_size = 75, @@ -686,17 +685,17 @@ static void test_write_object_id_min_length(void) snprintf(name, sizeof(name), "ref%05d", i); ref.refname = name; err = reftable_writer_add_ref(w, &ref); - EXPECT_ERR(err); + check(!err); } err = reftable_writer_close(w); - EXPECT_ERR(err); - EXPECT(reftable_writer_stats(w)->object_id_len == 2); + check(!err); + check_int(reftable_writer_stats(w)->object_id_len, ==, 2); reftable_writer_free(w); strbuf_release(&buf); } -static void test_write_object_id_length(void) +static void t_write_object_id_length(void) { struct reftable_write_options opts = { .block_size = 75, @@ -722,17 +721,17 @@ static void test_write_object_id_length(void) ref.refname = name; ref.value.val1[15] = i; err = reftable_writer_add_ref(w, &ref); - EXPECT_ERR(err); + check(!err); } err = reftable_writer_close(w); - EXPECT_ERR(err); - EXPECT(reftable_writer_stats(w)->object_id_len == 16); + check(!err); + check_int(reftable_writer_stats(w)->object_id_len, ==, 16); reftable_writer_free(w); strbuf_release(&buf); } -static void test_write_empty_key(void) +static void t_write_empty_key(void) { struct reftable_write_options opts = { 0 }; struct strbuf buf = STRBUF_INIT; @@ -747,15 +746,15 @@ static void test_write_empty_key(void) reftable_writer_set_limits(w, 1, 1); err = reftable_writer_add_ref(w, &ref); - EXPECT(err == REFTABLE_API_ERROR); + check_int(err, ==, REFTABLE_API_ERROR); err = reftable_writer_close(w); - EXPECT(err == REFTABLE_EMPTY_TABLE_ERROR); + check_int(err, ==, REFTABLE_EMPTY_TABLE_ERROR); reftable_writer_free(w); strbuf_release(&buf); } -static void test_write_key_order(void) +static void t_write_key_order(void) { struct reftable_write_options opts = { 0 }; struct strbuf buf = STRBUF_INIT; @@ -782,15 +781,15 @@ static void test_write_key_order(void) reftable_writer_set_limits(w, 1, 1); err = reftable_writer_add_ref(w, &refs[0]); - EXPECT_ERR(err); + check(!err); err = reftable_writer_add_ref(w, &refs[1]); - EXPECT(err == REFTABLE_API_ERROR); + check_int(err, ==, REFTABLE_API_ERROR); reftable_writer_close(w); reftable_writer_free(w); strbuf_release(&buf); } -static void test_write_multiple_indices(void) +static void t_write_multiple_indices(void) { struct reftable_write_options opts = { .block_size = 100, @@ -817,7 +816,7 @@ static void test_write_multiple_indices(void) ref.refname = buf.buf, err = reftable_writer_add_ref(writer, &ref); - EXPECT_ERR(err); + check(!err); } for (i = 0; i < 100; i++) { @@ -835,7 +834,7 @@ static void test_write_multiple_indices(void) log.refname = buf.buf, err = reftable_writer_add_log(writer, &log); - EXPECT_ERR(err); + check(!err); } reftable_writer_close(writer); @@ -845,13 +844,13 @@ static void test_write_multiple_indices(void) * for each of the block types. */ stats = reftable_writer_stats(writer); - EXPECT(stats->ref_stats.index_offset > 0); - EXPECT(stats->obj_stats.index_offset > 0); - EXPECT(stats->log_stats.index_offset > 0); + check_int(stats->ref_stats.index_offset, >, 0); + check_int(stats->obj_stats.index_offset, >, 0); + check_int(stats->log_stats.index_offset, >, 0); block_source_from_strbuf(&source, &writer_buf); err = reftable_new_reader(&reader, &source, "filename"); - EXPECT_ERR(err); + check(!err); /* * Seeking the log uses the log index now. In case there is any @@ -859,7 +858,7 @@ static void test_write_multiple_indices(void) */ reftable_reader_init_log_iterator(reader, &it); err = reftable_iterator_seek_log(&it, ""); - EXPECT_ERR(err); + check(!err); reftable_iterator_destroy(&it); reftable_writer_free(writer); @@ -868,7 +867,7 @@ static void test_write_multiple_indices(void) strbuf_release(&buf); } -static void test_write_multi_level_index(void) +static void t_write_multi_level_index(void) { struct reftable_write_options opts = { .block_size = 100, @@ -895,7 +894,7 @@ static void test_write_multi_level_index(void) ref.refname = buf.buf, err = reftable_writer_add_ref(writer, &ref); - EXPECT_ERR(err); + check(!err); } reftable_writer_close(writer); @@ -904,18 +903,18 @@ static void test_write_multi_level_index(void) * multi-level index. */ stats = reftable_writer_stats(writer); - EXPECT(stats->ref_stats.max_index_level == 2); + check_int(stats->ref_stats.max_index_level, ==, 2); block_source_from_strbuf(&source, &writer_buf); err = reftable_new_reader(&reader, &source, "filename"); - EXPECT_ERR(err); + check(!err); /* * Seeking the last ref should work as expected. */ reftable_reader_init_ref_iterator(reader, &it); err = reftable_iterator_seek_ref(&it, "refs/heads/199"); - EXPECT_ERR(err); + check(!err); reftable_iterator_destroy(&it); reftable_writer_free(writer); @@ -924,56 +923,57 @@ static void test_write_multi_level_index(void) strbuf_release(&buf); } -static void test_corrupt_table_empty(void) +static void t_corrupt_table_empty(void) { struct strbuf buf = STRBUF_INIT; - struct reftable_block_source source = { NULL }; - struct reftable_reader rd = { NULL }; + struct reftable_block_source source = { 0 }; + struct reftable_reader rd = { 0 }; int err; block_source_from_strbuf(&source, &buf); err = init_reader(&rd, &source, "file.log"); - EXPECT(err == REFTABLE_FORMAT_ERROR); + check_int(err, ==, REFTABLE_FORMAT_ERROR); } -static void test_corrupt_table(void) +static void t_corrupt_table(void) { uint8_t zeros[1024] = { 0 }; struct strbuf buf = STRBUF_INIT; - struct reftable_block_source source = { NULL }; - struct reftable_reader rd = { NULL }; + struct reftable_block_source source = { 0 }; + struct reftable_reader rd = { 0 }; int err; strbuf_add(&buf, zeros, sizeof(zeros)); block_source_from_strbuf(&source, &buf); err = init_reader(&rd, &source, "file.log"); - EXPECT(err == REFTABLE_FORMAT_ERROR); + check_int(err, ==, REFTABLE_FORMAT_ERROR); strbuf_release(&buf); } -int readwrite_test_main(int argc, const char *argv[]) +int cmd_main(int argc, const char *argv[]) { - RUN_TEST(test_log_zlib_corruption); - RUN_TEST(test_corrupt_table); - RUN_TEST(test_corrupt_table_empty); - RUN_TEST(test_log_write_read); - RUN_TEST(test_write_key_order); - RUN_TEST(test_table_read_write_seek_linear_sha256); - RUN_TEST(test_log_buffer_size); - RUN_TEST(test_table_write_small_table); - RUN_TEST(test_buffer); - RUN_TEST(test_table_read_api); - RUN_TEST(test_table_read_write_sequential); - RUN_TEST(test_table_read_write_seek_linear); - RUN_TEST(test_table_read_write_seek_index); - RUN_TEST(test_table_refs_for_no_index); - RUN_TEST(test_table_refs_for_obj_index); - RUN_TEST(test_write_empty_key); - RUN_TEST(test_write_empty_table); - RUN_TEST(test_log_overflow); - RUN_TEST(test_write_object_id_length); - RUN_TEST(test_write_object_id_min_length); - RUN_TEST(test_write_multiple_indices); - RUN_TEST(test_write_multi_level_index); - return 0; + TEST(t_buffer(), "strbuf works as blocksource"); + TEST(t_corrupt_table(), "read-write on corrupted table"); + TEST(t_corrupt_table_empty(), "read-write on an empty table"); + TEST(t_log_buffer_size(), "buffer extension for log compression"); + TEST(t_log_overflow(), "log overflow returns expected error"); + TEST(t_log_write_read(), "read-write on log records"); + TEST(t_log_zlib_corruption(), "reading corrupted log record returns expected error"); + TEST(t_table_read_api(), "read on a table"); + TEST(t_table_read_write_seek_index(), "read-write on a table with index"); + TEST(t_table_read_write_seek_linear(), "read-write on a table without index (SHA1)"); + TEST(t_table_read_write_seek_linear_sha256(), "read-write on a table without index (SHA256)"); + TEST(t_table_read_write_sequential(), "sequential read-write on a table"); + TEST(t_table_refs_for_no_index(), "refs-only table with no index"); + TEST(t_table_refs_for_obj_index(), "refs-only table with index"); + TEST(t_table_write_small_table(), "write_table works"); + TEST(t_write_empty_key(), "write on refs with empty keys"); + TEST(t_write_empty_table(), "read-write on empty tables"); + TEST(t_write_key_order(), "refs must be written in increasing order"); + TEST(t_write_multi_level_index(), "table with multi-level index"); + TEST(t_write_multiple_indices(), "table with indices for multiple block types"); + TEST(t_write_object_id_length(), "prefix compression on writing refs"); + TEST(t_write_object_id_min_length(), "prefix compression on writing refs"); + + return test_done(); } From patchwork Wed Aug 7 14:11:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chandra Pratap X-Patchwork-Id: 13756360 Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 16DFB7BAFF for ; Wed, 7 Aug 2024 14:16:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723040220; cv=none; b=dIT1kfaA9nJ58zJUBMji2pIxj+9xXbNj81FQAga9z02ab+naYGdsZjzZZ9WnGRo/33KJE8EgzI+Saq9OOiTqN9CEIaU6hGO+MdnYrBmtSoe6Bqyl60A/utNRR+jQ/nsHu9PRdeRrsPgW++PXpN6xaduzhy54lc6qf8Pip1La3ro= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723040220; c=relaxed/simple; bh=W7USWDooWkcEvUhlGe44fC9J7Hisd3kdMpQXKM6JAOM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bDIvzUKQIN5Uo1JB/wmgU+FFt7PLgME0jrta62v7h2DQfHnNrxKlp62r/PZ4MioLJNGBl6Aglab45QGiTikaUO+cvlUf9yJXVMwxgQB/b4ATCtcXpaDySFvHjIp7MGn7Ub9R6LFVCMrXknxB/fM1QD/wv4sqfUSrqHT0PYXb6JI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=RNXV0LZi; arc=none smtp.client-ip=209.85.216.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="RNXV0LZi" Received: by mail-pj1-f45.google.com with SMTP id 98e67ed59e1d1-2cb5e0b020eso1407089a91.2 for ; Wed, 07 Aug 2024 07:16:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723040218; x=1723645018; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=j7OgYFBtkBdCtX7syiausLF4DmD8poKJ5EHK6kAOs/8=; b=RNXV0LZidZ7aZVUz1pd9QJ98/gEJwvNDXNLhOgOZODGdw2HkzO3ktLOunh4pgZl45E f9OxVQwihSpU1VdDG9lI/fU7+Fb5F7lxYyg14Co+Rrw2NReHvJaR5j3cXq8bkeRmAgUz Haqn70IhE5W3sZhxIUuVRXSKDemFpf7l0ocA8fB0H2khuVpzCQYXnoCOWFmFB3/+kqlf yr/Bf4/+wj5ogAoMIoKRAAJ0PCvJoo3uZFdll8KREqbIOKgwwEVXhzNSzOzKRKeo29am 028sH5lpn6SBOM7JFEYYwSMFyMgc3AekE13ohLOshxI7UA1WKHlEJ+LSDZeCEq0Y+B6D IzJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723040218; x=1723645018; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=j7OgYFBtkBdCtX7syiausLF4DmD8poKJ5EHK6kAOs/8=; b=qDHLBx+pFWZO0rOeYnFHx6xTkZKo4JX3iZT5hk+lvMuhptVQEHRr8XNGpDhWTWaXv9 bAslEyIx2TbCZmtPJ19lyNNjAK1OvRtKmAR31to9p1O7NU3GjSlLWrd7XGZQO3FsfOJP N48VHqMmMQdfaSFXtqN2YqXzGO3vbCgO9D+1GtpdBWti04MGSHxllcTaKdtYe5JLdU+z WV6AlyMobmYCE7h7gextB0XuZVDm1UaQDH+ycDmVMJH8ooBRGEEO9dsyAFw5kNPcK5xO yZz9/uyfFUff1IAbRmEQ8McRJt3bk6jfz96x+y7EjPZid43GEOUxU7S/8xYgXusW6igr U94w== X-Gm-Message-State: AOJu0YweK1NoEE5ptINdnG1FY3Eu42iGYa2MxlhIWhyv2STKt2XZD1T9 qx7niT7x+6VEbOFs6eVYLYKjccJLeGqwOlmwCQneI17sWMODyiZRS21vB3f7 X-Google-Smtp-Source: AGHT+IHx/2KfrA1DsOOgDyum/YEkft1Php/BgrkGXVcVoWLpcxqXBfzMXSwE+OEMfmkALpchGEBkUA== X-Received: by 2002:a17:90b:30d5:b0:2c9:9f2a:2b20 with SMTP id 98e67ed59e1d1-2cff9488090mr22705804a91.22.1723040217799; Wed, 07 Aug 2024 07:16:57 -0700 (PDT) Received: from Ubuntu.. ([106.205.252.196]) by smtp.googlemail.com with ESMTPSA id 98e67ed59e1d1-2d1b374b3cesm1635059a91.48.2024.08.07.07.16.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Aug 2024 07:16:57 -0700 (PDT) From: Chandra Pratap To: git@vger.kernel.org Cc: Chandra Pratap , Patrick Steinhardt , Christian Couder Subject: [PATCH 2/5] t-reftable-readwrite: use free_names() instead of a for loop Date: Wed, 7 Aug 2024 19:41:58 +0530 Message-ID: <20240807141608.4524-3-chandrapratap3519@gmail.com> X-Mailer: git-send-email 2.45.GIT In-Reply-To: <20240807141608.4524-1-chandrapratap3519@gmail.com> References: <20240807141608.4524-1-chandrapratap3519@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 free_names() as defined by reftable/basics.{c,h} frees a NULL terminated array of malloced strings along with the array itself. Use this function instead of a for loop to free such an array. Mentored-by: Patrick Steinhardt Mentored-by: Christian Couder Signed-off-by: Chandra Pratap --- t/unit-tests/t-reftable-readwrite.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/t/unit-tests/t-reftable-readwrite.c b/t/unit-tests/t-reftable-readwrite.c index 235e3d94c7..e90f2bf9de 100644 --- a/t/unit-tests/t-reftable-readwrite.c +++ b/t/unit-tests/t-reftable-readwrite.c @@ -413,7 +413,6 @@ static void t_table_read_api(void) struct reftable_reader rd = { 0 }; struct reftable_block_source source = { 0 }; int err; - int i; struct reftable_log_record log = { 0 }; struct reftable_iterator it = { 0 }; @@ -432,10 +431,8 @@ static void t_table_read_api(void) check_int(err, ==, REFTABLE_API_ERROR); strbuf_release(&buf); - for (i = 0; i < N; i++) - reftable_free(names[i]); + free_names(names); reftable_iterator_destroy(&it); - reftable_free(names); reader_close(&rd); strbuf_release(&buf); } @@ -498,9 +495,7 @@ static void t_table_read_write_seek(int index, int hash_id) reftable_iterator_destroy(&it); strbuf_release(&buf); - for (i = 0; i < N; i++) - reftable_free(names[i]); - reftable_free(names); + free_names(names); reader_close(&rd); } From patchwork Wed Aug 7 14:11:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chandra Pratap X-Patchwork-Id: 13756361 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B309880043 for ; Wed, 7 Aug 2024 14:17:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723040223; cv=none; b=cZEf51YGNERlrs6cqUSDLv8AQkLS4egFSXHZXfoSqnGHCDpa5twY4FDDLh+qHPSOnD0uU37E+pahhpgH2zODMi1g/xAzSzLsuZXQIoYddDdaVv475m7Hg3+Cf6ckV517EapSkDOOkCCd5nEMOvBts68bOPG4okVldTNyTX8yPno= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723040223; c=relaxed/simple; bh=rNjYv8v/nNvu5fkAterIaONp+7KgWiJuJ3zrzWZNg3E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hrROc+UWmlJN+ApxybvCLtRiVYQHKuAELYTOJv/+BbgMA4sQi/zDWNeoC4x8+G+81MkAKjwyQ2//Klol/lOXbJCKOduCcjpQ38x/K8/TBVc15DrVE22mClDUe5YUmK/POJ5z4EIC2sbaDaS7nvIDbaF6HCWCJ0Q5tXu3OjS/1VQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=XXfvek29; arc=none smtp.client-ip=209.85.216.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XXfvek29" Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-2cd5e3c27c5so1359065a91.3 for ; Wed, 07 Aug 2024 07:17:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723040220; x=1723645020; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=dxcNKp4a5xaumj3da3OqlAmj+xm5gsA3osHbMT4TyUs=; b=XXfvek29eo6AuEg8n498yFoWp5wL2RAQaq/cQBgC76hATBR4mcE6dWWP6Ks7suPWpi pG/nmGE3GqDGd4k8XogGqBQoJwWic2SmJGEBy/Cti3SfW2LxyQUMu2ympUEEBckUqmsQ w9g+nQU7gbA8nFcwQpnE3/Nb7XYqSMPUXYGWAPUn20CyNEmvyE/ripYzbsrykj1v3/7r E7U0wtJxsWlx1nEysqT/3a/swFRLeQKRZUtcS8k9DK+FAiZbfQJ8+w4XskUUUUoZeqo7 2GKAUT81QcuooJ0QnnZBYhKthha6Gd7HFk2ILw4fvDosGFQfklCHAqAwWAWZOUqedUu3 wx/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723040220; x=1723645020; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dxcNKp4a5xaumj3da3OqlAmj+xm5gsA3osHbMT4TyUs=; b=rQMsE74egZImhdz9HF3qfBPhAFntcbAaUxdPXZ+WkgmcizCd1rj80ctoxdQAa3ZKRS rYZ77iFHluDCd26gFtXS3ss887iDEKllp0PpeHTlFjQGcvki4VGCz2ch/aNVeTjoTiYw gcwu803W1nH+/3A9aLhTlx4t+66GYdFSg1KyjWykVbQlKEmbjNOo9Vc8E0lRPYRnM1E0 moof0odO/uPsosTkN0FsYn7bDlUxMDxE/rHwOb5bTVWPB/K5JKsGYZFE0zjGBorEIimD 5uCkdz23bxjHwAC3cX9F7I0a+nEcYje8/6phrj+Wl0YICEzSfNht8lJeWLBHUhsoYQYG YsLA== X-Gm-Message-State: AOJu0Yz+avnOy1q1v+gqUZ4iCyTW0eMlntbM0gBfnnbcORsH972ZKzpe TiYLyKaN5U1szoP/MJ5DGthNRKAOULFKi+JgD2S4BtJREyo61ZHi6b0L3n2b X-Google-Smtp-Source: AGHT+IF7G80YzH8uCJ8MGHTjX5r00NcOmPlUJ5CfZHo7mVYDaBv7AF2ms/iqfPeApVL8B1xpXnONUw== X-Received: by 2002:a17:90a:c70c:b0:2c9:7803:1cf6 with SMTP id 98e67ed59e1d1-2cff94478a8mr17633121a91.20.1723040220429; Wed, 07 Aug 2024 07:17:00 -0700 (PDT) Received: from Ubuntu.. ([106.205.252.196]) by smtp.googlemail.com with ESMTPSA id 98e67ed59e1d1-2d1b374b3cesm1635059a91.48.2024.08.07.07.16.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Aug 2024 07:17:00 -0700 (PDT) From: Chandra Pratap To: git@vger.kernel.org Cc: Chandra Pratap , Patrick Steinhardt , Christian Couder Subject: [PATCH 3/5] t-reftable-readwrite: use 'for' in place of infinite 'while' loops Date: Wed, 7 Aug 2024 19:41:59 +0530 Message-ID: <20240807141608.4524-4-chandrapratap3519@gmail.com> X-Mailer: git-send-email 2.45.GIT In-Reply-To: <20240807141608.4524-1-chandrapratap3519@gmail.com> References: <20240807141608.4524-1-chandrapratap3519@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Using a for loop with an empty conditional statement is more concise and easier to read than an infinite 'while' loop in instances where we need a loop variable. Hence, replace such instances of a 'while' loop with the equivalent 'for' loop. Mentored-by: Patrick Steinhardt Mentored-by: Christian Couder Signed-off-by: Chandra Pratap --- t/unit-tests/t-reftable-readwrite.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/t/unit-tests/t-reftable-readwrite.c b/t/unit-tests/t-reftable-readwrite.c index e90f2bf9de..7daf28ec6d 100644 --- a/t/unit-tests/t-reftable-readwrite.c +++ b/t/unit-tests/t-reftable-readwrite.c @@ -268,15 +268,13 @@ static void t_log_write_read(void) err = reftable_iterator_seek_log(&it, ""); check(!err); - i = 0; - while (1) { + for (i = 0; ; i++) { int err = reftable_iterator_next_log(&it, &log); if (err > 0) break; check(!err); check_str(names[i], log.refname); check_int(i, ==, log.update_index); - i++; reftable_log_record_release(&log); } @@ -374,7 +372,7 @@ static void t_table_read_write_sequential(void) err = reftable_iterator_seek_ref(&it, ""); check(!err); - while (1) { + for (j = 0; ; j++) { struct reftable_ref_record ref = { 0 }; int r = reftable_iterator_next_ref(&it, &ref); check_int(r, >=, 0); @@ -382,8 +380,6 @@ static void t_table_read_write_sequential(void) break; check_str(names[j], ref.refname); check_int(update_index, ==, ref.update_index); - - j++; reftable_ref_record_release(&ref); } check_int(j, ==, N); @@ -589,15 +585,13 @@ static void t_table_refs_for(int indexed) err = reftable_reader_refs_for(&rd, &it, want_hash); check(!err); - j = 0; - while (1) { + for (j = 0; ; j++) { int err = reftable_iterator_next_ref(&it, &ref); check_int(err, >=, 0); if (err > 0) break; check_int(j, <, want_names_len); check_str(ref.refname, want_names[j]); - j++; reftable_ref_record_release(&ref); } check_int(j, ==, want_names_len); From patchwork Wed Aug 7 14:12:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chandra Pratap X-Patchwork-Id: 13756362 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7B6598060A for ; Wed, 7 Aug 2024 14:17:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723040225; cv=none; b=lvw4LRrMrrnNPjBxP39FHtkjFFp99wlcKJJvl/BZwxa4YBXNg7zrrKwZWgLs6BskNsLjR/RczGs7Q2VnU8JAIITrpuGRdgxNw1ijevKoBSUAbVFPX/6DPPDXuQ3Gsc9Hd/oZWBy6I9Y2fTtOqrSTZVDWu5JGaGg9SuGzwkkocEA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723040225; c=relaxed/simple; bh=CS77GZaKmbqjoRwfTMFlhcsQ9bukzaXFxtqoDthO32k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hkPGWbe+jS4T36/PzE/9MdbnytqZ3kDVC1rIAr9GMPRiYYy/GVkAr47i3TpLzjDkM6PAZj5SwmhVyPUE3jBkbVDlwIkKefPduOCdkgJq4cupYOsBkkLQ+FamORxlCdDg8baXuvKKEScQiJ6ZYF0N+gmxwWBnWq8IVTHJiNxMnWo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=U1suW3Ig; arc=none smtp.client-ip=209.85.215.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="U1suW3Ig" Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-7a1be7b7bb5so1205513a12.0 for ; Wed, 07 Aug 2024 07:17:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723040223; x=1723645023; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ay7cmSxiwFmgG+lInLAEoiXWb4RJh7Hitfijj9jFTcw=; b=U1suW3IgX8Vtfbe5sY46Ob1K8vzE5V/ko/VsEtAEwY0iSfoPHxKKeW7ETGksGrvdE6 Gx03h6jS0S7h1/mUFBF4Oa2vqGw+avd4ZANT+65ziua2p/S28XbWx19MnkIi0ZAneAU0 TYEQZT5CAN/VIJxN1VZW+eMgyPOhfeiNBB7qy/7lLl8+UTDGGPIuQrZ2wob3mH2dyyjT dwNVj87OBDaOVLhfcs32Yl08+BPLwX6N9HB4U46RR5IBY1QKDMcO8ibRVoRhAn5Fkwzy hiPksXr0VIpAO5YA02TK2XGUT60n2PGNZpMDldAhqpBtdm3nS3uaFV+mNOiYofJ2KtjO XTTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723040223; x=1723645023; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ay7cmSxiwFmgG+lInLAEoiXWb4RJh7Hitfijj9jFTcw=; b=Y5sPBNpdSqehSEsf0yJTF4PP3XoIQOCR6E2kNLfwapMRry3UbKX/4uEnkKmpeX1/DH MJnckEXvnWPQJ/miG3XLzV0oeEmVdhte4Jul6lK0qbCKHpMuw9w8ESKE1GZekVB6k897 Tkiqwvmc9+fJI8IHgFm23d/Ibeom6GVnBNgOwO8wjl0pA74TuEpIcWqR5jgLKQK51hU2 b58mzc6054E9FuDSyGYb63F3ABGHFB+M7qAZNAIbRqCOMwACnp/GpqncjLkWos5P3b0M Im0bxCcy6AHvU+kGaT41yhzfzSiubTTP4esNMAnZq6Ze5GrhpUhHa+xmHQeLBq/S1hO7 ZcvA== X-Gm-Message-State: AOJu0Yz9MvOExD0fTc0cqI7MaRFtOF11oCX8Eg9xySNlFkCj9awhpVCK XO75p0lKuuEPDxJTyxpwEGIkd6lB/ROlLeLz/IC6ab9JWW+n4U2ndsUljEMO X-Google-Smtp-Source: AGHT+IEOnLWxZLwi4uz1QGqC1Oi1yXOVLr+cdbRAutgwZ40xm7Soojo5+Oxx4e1FS7RVjxltQvUxWA== X-Received: by 2002:a17:90b:1c8c:b0:2cd:619:6826 with SMTP id 98e67ed59e1d1-2cff952bd3amr21673814a91.34.1723040223120; Wed, 07 Aug 2024 07:17:03 -0700 (PDT) Received: from Ubuntu.. ([106.205.252.196]) by smtp.googlemail.com with ESMTPSA id 98e67ed59e1d1-2d1b374b3cesm1635059a91.48.2024.08.07.07.17.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Aug 2024 07:17:02 -0700 (PDT) From: Chandra Pratap To: git@vger.kernel.org Cc: Chandra Pratap , Patrick Steinhardt , Christian Couder Subject: [PATCH 4/5] t-reftable-readwrite: add test for known error Date: Wed, 7 Aug 2024 19:42:00 +0530 Message-ID: <20240807141608.4524-5-chandrapratap3519@gmail.com> X-Mailer: git-send-email 2.45.GIT In-Reply-To: <20240807141608.4524-1-chandrapratap3519@gmail.com> References: <20240807141608.4524-1-chandrapratap3519@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 When using reftable_writer_add_ref() to add a ref record to a reftable writer, The update_index of the ref record must be within the limits set by reftable_writer_set_limits(), or REFTABLE_API_ERROR is returned. This scenario is currently left untested. Add a test case for the same. Mentored-by: Patrick Steinhardt Mentored-by: Christian Couder Signed-off-by: Chandra Pratap --- t/unit-tests/t-reftable-readwrite.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/t/unit-tests/t-reftable-readwrite.c b/t/unit-tests/t-reftable-readwrite.c index 7daf28ec6d..a5462441d3 100644 --- a/t/unit-tests/t-reftable-readwrite.c +++ b/t/unit-tests/t-reftable-readwrite.c @@ -773,6 +773,11 @@ static void t_write_key_order(void) check(!err); err = reftable_writer_add_ref(w, &refs[1]); check_int(err, ==, REFTABLE_API_ERROR); + + refs[0].update_index = 2; + err = reftable_writer_add_ref(w, &refs[0]); + check_int(err, ==, REFTABLE_API_ERROR); + reftable_writer_close(w); reftable_writer_free(w); strbuf_release(&buf); From patchwork Wed Aug 7 14:12:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chandra Pratap X-Patchwork-Id: 13756363 Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 35B876F30F for ; Wed, 7 Aug 2024 14:17:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723040228; cv=none; b=jeygqqwmkCKMvBnPZerL4G2ZoBjAlxF6SBwyfSp8uhjIQBAsp73BigisPfuecV9vjo8NOemUkkZ+hVVDH130qz227I/xirXG1ia93tTlM+V+GXZhV6r9OAQt5939wf2Hqq5bE6Ave4K9xiR0u9gTMMNp/TxgfaQ4Ph1FjRRgHZQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723040228; c=relaxed/simple; bh=SssNslPjSsfYZ9shahd5fXGEigqzQQQFD9Ov1Q10OOA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RLGH2WrEcYe1visx9D9Ivwyxc6MOZ29VtjDYD4XnTAlS6MZE24oFtoilyl+Ng7UNABFtgcrc7iuD3qt0+gTKgZwTQ2kIXN/EY5OFvGQZok+f8jo2bnWOhVlEmXV5kpk58OutVGP6ZqFqeo8qs9I3YfcI3Mngn2aaJOR7StEb604= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=bruDmJGo; arc=none smtp.client-ip=209.85.216.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="bruDmJGo" Received: by mail-pj1-f43.google.com with SMTP id 98e67ed59e1d1-2cb5243766dso1517352a91.0 for ; Wed, 07 Aug 2024 07:17:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723040226; x=1723645026; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ieCvH3IpQOFsuNFQLYDD1RJgReIjXcY6lWpvV54jsI8=; b=bruDmJGou0+pNjuj8HXP60R3/gVbwCh8FM0a7UiRq08+IGqcRWU0qVpywczRgL+rNc tfyqRTCWe7BCEVxh5c2iV5bMJOce+HYAcOXSjrfpHMd6Q31YzwwB+Q3oQKDARwqXjSmP WNjsIBrXb+JoMNce/Q/XlDcxCunt+BlIHumbXkymgbzd9i2AyXIoTi4hWRmkwCS57MKP yVmiCn3KcOd35u4tNmbLQbvor8tADO7aXkqJnkypLHMPwE53SUpj3cTZqqI2SxHMjFlV LPQl8fWo43LZE18ay/YddePIpMz9T0r8aEFLqoDMmevHsbSfJiYFnCP6uZwDLpqctSap E2fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723040226; x=1723645026; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ieCvH3IpQOFsuNFQLYDD1RJgReIjXcY6lWpvV54jsI8=; b=CutRoGgrJlrDuLr1crHjGDIn1B7mgxin1UF6YQs0JrRTpX/3uyvwdEL1xIoZaGDBj7 t5c1T22H84TluyAOCoE4ohGnWOzHY0LXKkj9pwdqzorVyL9u6DR+jWgW8INZ/dipmL4m oRcplcY7r8qTQrMPjKu/WSw9M2Rzqfcqu1qxHVkJXcIj4CC791Kf2kxnHWAsdXSBu700 au1GY0bKW4AxzwbwdfK8Tq5UIwzRgkK4f1tUoKlJfPoTqFnd1IOORU4jj1HwfmzfJDmw lBQqsEYP0Agp6KErjug454rxs5pJQoNIeoLpzEErQhlhrRyv9pq9T8TiRVDwJsKVT8VM tNdQ== X-Gm-Message-State: AOJu0YwNmTzRVH2UJ2AusZGEg3yCcvRCEpvB2QJnKyMrTVjxPhMe9hes ys9q0vCzvA+MvE7CZbO30RumtxTpSeaNnRS/ijMIIFAtgKl/yTKXQ6PCLXPv X-Google-Smtp-Source: AGHT+IEcYWB6sWTLBN/jYu4VCEHDSpdsm9tX/x4com40ZRQgcfv1eymrzWiqK9qdOIusKlAYKMgyRQ== X-Received: by 2002:a17:90a:7307:b0:2c9:6f06:8005 with SMTP id 98e67ed59e1d1-2cff9524412mr20880793a91.26.1723040225816; Wed, 07 Aug 2024 07:17:05 -0700 (PDT) Received: from Ubuntu.. ([106.205.252.196]) by smtp.googlemail.com with ESMTPSA id 98e67ed59e1d1-2d1b374b3cesm1635059a91.48.2024.08.07.07.17.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Aug 2024 07:17:05 -0700 (PDT) From: Chandra Pratap To: git@vger.kernel.org Cc: Chandra Pratap , Patrick Steinhardt , Christian Couder Subject: [PATCH 5/5] t-reftable-readwrite: add tests for print functions Date: Wed, 7 Aug 2024 19:42:01 +0530 Message-ID: <20240807141608.4524-6-chandrapratap3519@gmail.com> X-Mailer: git-send-email 2.45.GIT In-Reply-To: <20240807141608.4524-1-chandrapratap3519@gmail.com> References: <20240807141608.4524-1-chandrapratap3519@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 reftable/reftable-reader.h lists two print functions useful in debugging, reftable_reader_print_file() and reftable_reader_print_blocks(). As of now, both these functions are left unexercised by all of the reftable tests. Add a test function to exercise both these functions. This has the added benefit of testing reftable_block_source_from_file(), which currently remains untested as well. Mentored-by: Patrick Steinhardt Mentored-by: Christian Couder Signed-off-by: Chandra Pratap --- t/unit-tests/t-reftable-readwrite.c | 75 +++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/t/unit-tests/t-reftable-readwrite.c b/t/unit-tests/t-reftable-readwrite.c index a5462441d3..8c6f2f1f5d 100644 --- a/t/unit-tests/t-reftable-readwrite.c +++ b/t/unit-tests/t-reftable-readwrite.c @@ -11,6 +11,8 @@ license that can be found in the LICENSE file or at #include "reftable/blocksource.h" #include "reftable/reftable-error.h" #include "reftable/reftable-writer.h" +#include "tempfile.h" +#include "write-or-die.h" static const int update_index = 5; @@ -25,11 +27,23 @@ static ssize_t strbuf_add_void(void *b, const void *data, size_t sz) return sz; } +static ssize_t fd_write(void *b, const void *data, size_t sz) +{ + int *fdp = (int *)b; + return write_in_full(*fdp, data, sz); +} + static int noop_flush(void *arg) { return 0; } +static int fd_flush(void *arg) +{ + int *fdp = (int *)arg; + return fsync_component(FSYNC_COMPONENT_REFERENCE, *fdp); +} + static void t_buffer(void) { struct strbuf buf = STRBUF_INIT; @@ -944,6 +958,66 @@ static void t_corrupt_table(void) strbuf_release(&buf); } +static void t_table_print(void) +{ + char name[100]; + struct reftable_write_options opts = { + .block_size = 512, + .hash_id = GIT_SHA1_FORMAT_ID, + }; + struct reftable_ref_record ref = { 0 }; + struct reftable_log_record log = { 0 }; + struct reftable_writer *w = NULL; + struct tempfile *tmp = NULL; + size_t i, N = 3; + int n, fd; + + xsnprintf(name, sizeof(name), "t-reftable-readwrite-%d-XXXXXX", __LINE__); + tmp = mks_tempfile_t(name); + fd = get_tempfile_fd(tmp); + w = reftable_new_writer(&fd_write, &fd_flush, &fd, &opts); + reftable_writer_set_limits(w, 0, update_index); + + for (i = 0; i < N; i++) { + xsnprintf(name, sizeof(name), "refs/heads/branch%02"PRIuMAX, (uintmax_t)i); + ref.refname = name; + ref.update_index = i; + ref.value_type = REFTABLE_REF_VAL1; + set_test_hash(ref.value.val1, i); + + n = reftable_writer_add_ref(w, &ref); + check_int(n, ==, 0); + } + + for (i = 0; i < N; i++) { + xsnprintf(name, sizeof(name), "refs/heads/branch%02"PRIuMAX, (uintmax_t)i); + log.refname = name; + log.update_index = i; + log.value_type = REFTABLE_LOG_UPDATE; + set_test_hash(log.value.update.new_hash, i); + log.value.update.name = (char *) "John Doe"; + log.value.update.email = (char *) "johndoe@anon.org"; + log.value.update.time = 0x6673e5b9; + log.value.update.message = (char *) "message"; + + n = reftable_writer_add_log(w, &log); + check_int(n, ==, 0); + } + + n = reftable_writer_close(w); + check_int(n, ==, 0); + + test_msg("testing printing functionality:"); + n = reftable_reader_print_file(tmp->filename.buf); + check_int(n, ==, 0); + n = reftable_reader_print_blocks(tmp->filename.buf); + /* end of blocks is denoted by a return value of 1 */ + check_int(n, ==, 1); + + delete_tempfile(&tmp); + reftable_writer_free(w); +} + int cmd_main(int argc, const char *argv[]) { TEST(t_buffer(), "strbuf works as blocksource"); @@ -953,6 +1027,7 @@ int cmd_main(int argc, const char *argv[]) TEST(t_log_overflow(), "log overflow returns expected error"); TEST(t_log_write_read(), "read-write on log records"); TEST(t_log_zlib_corruption(), "reading corrupted log record returns expected error"); + TEST(t_table_print(), "print tables and blocks"); TEST(t_table_read_api(), "read on a table"); TEST(t_table_read_write_seek_index(), "read-write on a table with index"); TEST(t_table_read_write_seek_linear(), "read-write on a table without index (SHA1)");