Skip to content

Commit

Permalink
Refactor tests to use pgp_source_t methods instead of functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ni4 committed Dec 3, 2023
1 parent 838cef8 commit 3f8efcc
Show file tree
Hide file tree
Showing 17 changed files with 136 additions and 136 deletions.
2 changes: 1 addition & 1 deletion src/fuzzing/keyring_g10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)

init_mem_src(&memsrc, data, size, false);
ks.load_g10(memsrc);
src_close(&memsrc);
memsrc.close();

return 0;
}
40 changes: 20 additions & 20 deletions src/librepgp/stream-packet.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017-2020, [Ribose Inc](https://www.ribose.com).
* Copyright (c) 2017-2020,2023 [Ribose Inc](https://www.ribose.com).
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
Expand Down Expand Up @@ -77,15 +77,15 @@ get_packet_type(uint8_t ptag)
int
stream_pkt_type(pgp_source_t &src)
{
if (src_eof(&src)) {
if (src.eof()) {
return 0;
}
size_t hdrneed = 0;
if (!stream_pkt_hdr_len(src, hdrneed)) {
return -1;
}
uint8_t hdr[PGP_MAX_HEADER_SIZE];
if (!src_peek_eq(&src, hdr, hdrneed)) {
if (!src.peek_eq(hdr, hdrneed)) {
return -1;
}
return get_packet_type(hdr[0]);
Expand All @@ -96,7 +96,7 @@ stream_pkt_hdr_len(pgp_source_t &src, size_t &hdrlen)
{
uint8_t buf[2];

if (!src_peek_eq(&src, buf, 2) || !(buf[0] & PGP_PTAG_ALWAYS_SET)) {
if (!src.peek_eq(buf, 2) || !(buf[0] & PGP_PTAG_ALWAYS_SET)) {
return false;
}

Expand Down Expand Up @@ -170,16 +170,16 @@ get_pkt_len(uint8_t *hdr, size_t *pktlen)
}

bool
stream_read_pkt_len(pgp_source_t *src, size_t *pktlen)
stream_read_pkt_len(pgp_source_t &src, size_t *pktlen)
{
uint8_t buf[6] = {};
size_t read = 0;

if (!stream_pkt_hdr_len(*src, read)) {
if (!stream_pkt_hdr_len(src, read)) {
return false;
}

if (!src_read_eq(src, buf, read)) {
if (!src.read_eq(buf, read)) {
return false;
}

Expand All @@ -192,7 +192,7 @@ stream_read_partial_chunk_len(pgp_source_t *src, size_t *clen, bool *last)
uint8_t hdr[5] = {};
size_t read = 0;

if (!src_read(src, hdr, 1, &read)) {
if (!src->read(hdr, 1, &read)) {
RNP_LOG("failed to read header");
return false;
}
Expand All @@ -215,15 +215,15 @@ stream_read_partial_chunk_len(pgp_source_t *src, size_t *clen, bool *last)
}
// 2-byte length
if (hdr[0] < 224) {
if (!src_read_eq(src, &hdr[1], 1)) {
if (!src->read_eq(&hdr[1], 1)) {
RNP_LOG("wrong 2-byte length");
return false;
}
*clen = ((size_t)(hdr[0] - 192) << 8) + (size_t) hdr[1] + 192;
return true;
}
// 4-byte length
if (!src_read_eq(src, &hdr[1], 4)) {
if (!src->read_eq(&hdr[1], 4)) {
RNP_LOG("wrong 4-byte length");
return false;
}
Expand All @@ -236,7 +236,7 @@ bool
stream_old_indeterminate_pkt_len(pgp_source_t *src)
{
uint8_t ptag = 0;
if (!src_peek_eq(src, &ptag, 1)) {
if (!src->peek_eq(&ptag, 1)) {
return false;
}
return !(ptag & PGP_PTAG_NEW_FORMAT) &&
Expand All @@ -247,7 +247,7 @@ bool
stream_partial_pkt_len(pgp_source_t *src)
{
uint8_t hdr[2] = {};
if (!src_peek_eq(src, hdr, 2)) {
if (!src->peek_eq(hdr, 2)) {
return false;
}
return (hdr[0] & PGP_PTAG_NEW_FORMAT) && (hdr[1] >= 224) && (hdr[1] < 255);
Expand All @@ -266,7 +266,7 @@ stream_peek_packet_hdr(pgp_source_t *src, pgp_packet_hdr_t *hdr)
memset(hdr, 0, sizeof(*hdr));
if (!stream_pkt_hdr_len(*src, hlen)) {
uint8_t hdr2[2] = {0};
if (!src_peek_eq(src, hdr2, 2)) {
if (!src->peek_eq(hdr2, 2)) {
RNP_LOG("pkt header read failed");
return RNP_ERROR_READ;
}
Expand All @@ -275,7 +275,7 @@ stream_peek_packet_hdr(pgp_source_t *src, pgp_packet_hdr_t *hdr)
return RNP_ERROR_BAD_FORMAT;
}

if (!src_peek_eq(src, hdr->hdr, hlen)) {
if (!src->peek_eq(hdr->hdr, hlen)) {
RNP_LOG("failed to read pkt header");
return RNP_ERROR_READ;
}
Expand All @@ -298,7 +298,7 @@ static rnp_result_t
stream_read_packet_partial(pgp_source_t *src, pgp_dest_t *dst)
{
uint8_t hdr = 0;
if (!src_read_eq(src, &hdr, 1)) {
if (!src->read_eq(&hdr, 1)) {
return RNP_ERROR_READ;
}

Expand All @@ -315,7 +315,7 @@ stream_read_packet_partial(pgp_source_t *src, pgp_dest_t *dst)

while (partlen > 0) {
size_t read = std::min(partlen, (size_t) PGP_INPUT_CACHE_SIZE);
if (!src_read_eq(src, buf, read)) {
if (!src->read_eq(buf, read)) {
free(buf);
return RNP_ERROR_READ;
}
Expand Down Expand Up @@ -826,7 +826,7 @@ rnp_result_t
pgp_packet_body_t::read(pgp_source_t &src) noexcept
{
/* Make sure we have enough data for packet header */
if (!src_peek_eq(&src, hdr_, 2)) {
if (!src.peek_eq(hdr_, 2)) {
return RNP_ERROR_READ;
}

Expand All @@ -835,7 +835,7 @@ pgp_packet_body_t::read(pgp_source_t &src) noexcept
if (!stream_pkt_hdr_len(src, len)) {
return RNP_ERROR_BAD_FORMAT;
}
if (!src_peek_eq(&src, hdr_, len)) {
if (!src.peek_eq(hdr_, len)) {
return RNP_ERROR_READ;
}
hdr_len_ = len;
Expand All @@ -847,7 +847,7 @@ pgp_packet_body_t::read(pgp_source_t &src) noexcept
}
tag_ = (pgp_pkt_type_t) ptag;

if (!stream_read_pkt_len(&src, &len)) {
if (!stream_read_pkt_len(src, &len)) {
return RNP_ERROR_READ;
}

Expand All @@ -870,7 +870,7 @@ pgp_packet_body_t::read(pgp_source_t &src) noexcept
}

size_t read = 0;
if (!src_read(&src, data_.data(), len, &read) || (read != len)) {
if (!src.read(data_.data(), len, &read) || (read != len)) {
RNP_LOG("read %d instead of %d", (int) read, (int) len);
return RNP_ERROR_READ;
}
Expand Down
2 changes: 1 addition & 1 deletion src/librepgp/stream-packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ size_t get_partial_pkt_len(uint8_t blen);
* @param pktlen packet length will be stored here on success. Cannot be NULL.
* @return true on success or false if there is read error or packet length is ill-formed
**/
bool stream_read_pkt_len(pgp_source_t *src, size_t *pktlen);
bool stream_read_pkt_len(pgp_source_t &src, size_t *pktlen);

/** @brief Read partial packet chunk length.
*
Expand Down
2 changes: 1 addition & 1 deletion src/librepgp/stream-sig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@ pgp_signature_t::set_embedded_sig(const pgp_signature_t &esig)
pgp_rawpacket_t esigpkt(esig);
rnp::MemorySource mem(esigpkt.raw);
size_t len = 0;
stream_read_pkt_len(&mem.src(), &len);
stream_read_pkt_len(mem.src(), &len);
if (!len || (len > 0xffff) || (len >= esigpkt.raw.size())) {
RNP_LOG("wrong pkt len");
throw rnp::rnp_exception(RNP_ERROR_BAD_STATE);
Expand Down
2 changes: 1 addition & 1 deletion src/librepgp/stream-write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1924,7 +1924,7 @@ process_stream_sequence(pgp_source_t *src,
/* processing source stream */
while (!src->eof_) {
size_t read = 0;
if (!src_read(src, readbuf.get(), PGP_INPUT_CACHE_SIZE, &read)) {
if (!src->read(readbuf.get(), PGP_INPUT_CACHE_SIZE, &read)) {
RNP_LOG("failed to read from source");
return RNP_ERROR_READ;
} else if (!read) {
Expand Down
2 changes: 1 addition & 1 deletion src/tests/cipher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ read_key_pkt(pgp_key_pkt_t *key, const char *path)
return false;
}
bool res = !key->parse(src);
src_close(&src);
src.close();
return res;
}

Expand Down
2 changes: 1 addition & 1 deletion src/tests/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ TEST_F(rnp_tests, test_cli_rnpkeys_genkey)
assert_rnp_success(init_file_src(&src, GENKEYS "/pubring.gpg"));
assert_true(keystore->load(src));
assert_int_equal(keystore->key_count(), 34);
src_close(&src);
src.close();
assert_int_equal(key_expiration_check(keystore, "expiration_max_32bit@rnp", 4294967295),
1);
assert_int_equal(key_expiration_check(keystore, "expiration_max_32bit_h@rnp", 4294965600),
Expand Down
2 changes: 1 addition & 1 deletion src/tests/data/test_key_validity/case5/generate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ load_transferable_key(pgp_transferable_key_t *key, const char *fname)
{
pgp_source_t src = {};
bool res = !init_file_src(&src, fname) && !process_pgp_key(src, key, false);
src_close(&src);
src.close();
return res;
}

Expand Down
2 changes: 1 addition & 1 deletion src/tests/ffi-key-sig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ TEST_F(rnp_tests, test_ffi_export_revocation)
assert_rnp_success(init_file_src(&src, "alice-revocation.pgp"));
pgp_signature_t sig = {};
assert_rnp_success(sig.parse(src));
src_close(&src);
src.close();
assert_int_equal(sig.type(), PGP_SIG_REV_KEY);
assert_true(sig.has_subpkt(PGP_SIG_SUBPKT_REVOCATION_REASON));
assert_true(sig.has_keyfp());
Expand Down
6 changes: 3 additions & 3 deletions src/tests/ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ file_equals(const char *filename, const void *data, size_t len)
}

res = (msrc.size == len) && !memcmp(mem_src_get_memory(&msrc), data, len);
src_close(&msrc);
msrc.close();
return res;
}

Expand Down Expand Up @@ -911,8 +911,8 @@ test_ffi_check_recovered()
res = (msrc1.size == msrc2.size) &&
!memcmp(mem_src_get_memory(&msrc1), mem_src_get_memory(&msrc2), msrc1.size);
finish:
src_close(&msrc1);
src_close(&msrc2);
msrc1.close();
msrc2.close();
return res;
}

Expand Down
4 changes: 2 additions & 2 deletions src/tests/key-add-userid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ TEST_F(rnp_tests, test_key_add_userid)

assert_rnp_success(init_file_src(&src, "data/keyrings/1/secring.gpg"));
assert_rnp_success(ks->load_pgp(src));
src_close(&src);
src.close();

// locate our key
assert_non_null(key = rnp_tests_get_key_by_id(ks, keyids[0]));
Expand Down Expand Up @@ -161,7 +161,7 @@ TEST_F(rnp_tests, test_key_add_userid)
// read from the saved packets
assert_rnp_success(init_mem_src(&src, mem_dest_get_memory(&dst), dst.writeb, false));
assert_rnp_success(ks->load_pgp(src));
src_close(&src);
src.close();
dst_close(&dst, true);
assert_non_null(key = rnp_tests_get_key_by_id(ks, keyids[0]));

Expand Down
12 changes: 6 additions & 6 deletions src/tests/key-protect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ TEST_F(rnp_tests, test_key_protect_load_pgp)

assert_rnp_success(init_file_src(&src, "data/keyrings/1/secring.gpg"));
assert_rnp_success(ks->load_pgp(src));
src_close(&src);
src.close();

for (size_t i = 0; i < ARRAY_SIZE(keyids); i++) {
pgp_key_t *key = NULL;
Expand Down Expand Up @@ -129,7 +129,7 @@ TEST_F(rnp_tests, test_key_protect_load_pgp)

assert_rnp_success(init_mem_src(&memsrc, pkt.raw.data(), pkt.raw.size(), false));
assert_rnp_success(ks->load_pgp(memsrc));
src_close(&memsrc);
memsrc.close();

// grab the first key
pgp_key_t *reloaded_key = NULL;
Expand Down Expand Up @@ -302,15 +302,15 @@ TEST_F(rnp_tests, test_key_protect_sec_data)
assert_rnp_success(
init_mem_src(&memsrc, skey.rawpkt().raw.data(), skey.rawpkt().raw.size(), false));
assert_rnp_success(skeypkt->parse(memsrc));
src_close(&memsrc);
memsrc.close();
assert_int_not_equal(memcmp(raw_skey, skeypkt->sec_data, 32), 0);
assert_int_equal(skeypkt->sec_protection.s2k.specifier, PGP_S2KS_ITERATED_AND_SALTED);
delete skeypkt;
ssubpkt = new pgp_key_pkt_t();
assert_rnp_success(
init_mem_src(&memsrc, ssub.rawpkt().raw.data(), ssub.rawpkt().raw.size(), false));
assert_rnp_success(ssubpkt->parse(memsrc));
src_close(&memsrc);
memsrc.close();
assert_int_not_equal(memcmp(raw_ssub, ssubpkt->sec_data, 32), 0);
assert_int_equal(ssubpkt->sec_protection.s2k.specifier, PGP_S2KS_ITERATED_AND_SALTED);
delete ssubpkt;
Expand Down Expand Up @@ -342,15 +342,15 @@ TEST_F(rnp_tests, test_key_protect_sec_data)
assert_rnp_success(
init_mem_src(&memsrc, skey.rawpkt().raw.data(), skey.rawpkt().raw.size(), false));
assert_rnp_success(skeypkt->parse(memsrc));
src_close(&memsrc);
memsrc.close();
assert_int_not_equal(memcmp(raw_skey, skeypkt->sec_data, 32), 0);
assert_int_equal(skeypkt->sec_protection.s2k.specifier, PGP_S2KS_ITERATED_AND_SALTED);
delete skeypkt;
ssubpkt = new pgp_key_pkt_t();
assert_rnp_success(
init_mem_src(&memsrc, ssub.rawpkt().raw.data(), ssub.rawpkt().raw.size(), false));
assert_rnp_success(ssubpkt->parse(memsrc));
src_close(&memsrc);
memsrc.close();
assert_int_not_equal(memcmp(raw_ssub, ssubpkt->sec_data, 32), 0);
assert_int_equal(ssubpkt->sec_protection.s2k.specifier, PGP_S2KS_ITERATED_AND_SALTED);
delete ssubpkt;
Expand Down
2 changes: 1 addition & 1 deletion src/tests/key-validate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ key_store_add(rnp::KeyStore *keyring, const char *keypath)
assert_rnp_success(init_file_src(&keysrc, keypath));
assert_rnp_success(process_pgp_key(keysrc, tkey, false));
assert_true(keyring->add_ts_key(tkey));
src_close(&keysrc);
keysrc.close();
}

static bool
Expand Down
2 changes: 1 addition & 1 deletion src/tests/large-mpi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ TEST_F(rnp_tests, test_large_mpi_rsa_pub)
assert_rnp_success(init_file_src(&keysrc, "data/test_large_MPIs/rsa-pub-65535bits.pgp"));
assert_rnp_failure(process_pgp_keys(keysrc, keyseq, false));
assert_true(keyseq.keys.empty());
src_close(&keysrc);
keysrc.close();

assert_rnp_success(rnp_ffi_create(&ffi, "GPG", "GPG"));
assert_rnp_success(
Expand Down
Loading

0 comments on commit 3f8efcc

Please sign in to comment.