Skip to content

Commit

Permalink
Update code and tests to slightly improve coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
ni4 committed Oct 9, 2024
1 parent 511744f commit ebcdb2e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 27 deletions.
38 changes: 18 additions & 20 deletions src/librepgp/stream-dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,15 +422,11 @@ dst_print_str(pgp_dest_t *dst, const char *name, const std::string &str)

static void
dst_print_algs(pgp_dest_t * dst,
const char * name,
const std::string & name,
const std::vector<uint8_t> &algs,
const id_str_pair map[])
{
if (!name) {
name = "algorithms";
}

dst_printf(dst, "%s: ", name);
dst_printf(dst, "%s: ", name.c_str());
for (size_t i = 0; i < algs.size(); i++) {
auto comma = i + 1 < algs.size() ? ", " : "";
dst_printf(dst, "%s%s", id_str_pair::lookup(map, algs[i], "Unknown"), comma);
Expand All @@ -454,32 +450,27 @@ dst_print_sig_type(pgp_dest_t *dst, const char *name, pgp_sig_type_t sigtype)
}

static void
dst_print_hex(pgp_dest_t *dst, const char *name, const uint8_t *data, size_t len, bool bytes)
dst_print_hex(
pgp_dest_t *dst, const std::string &name, const uint8_t *data, size_t len, bool bytes)
{
char hex[512];
vsnprinthex(hex, sizeof(hex), data, len);
if (bytes) {
dst_printf(dst, "%s: 0x%s (%d bytes)\n", name, hex, (int) len);
dst_printf(dst, "%s: 0x%s (%d bytes)\n", name.c_str(), hex, (int) len);
} else {
dst_printf(dst, "%s: 0x%s\n", name, hex);
dst_printf(dst, "%s: 0x%s\n", name.c_str(), hex);
}
}

static void
dst_print_keyid(pgp_dest_t *dst, const char *name, const pgp_key_id_t &keyid)
dst_print_keyid(pgp_dest_t *dst, const std::string &name, const pgp_key_id_t &keyid)
{
if (!name) {
name = "key id";
}
dst_print_hex(dst, name, keyid.data(), keyid.size(), false);
}

static void
dst_print_fp(pgp_dest_t *dst, const char *name, const pgp_fingerprint_t &fp)
dst_print_fp(pgp_dest_t *dst, const std::string &name, const pgp_fingerprint_t &fp)
{
if (!name) {
name = "fingerprint";
}
dst_print_hex(dst, name, fp.fingerprint, fp.length, true);
}

Expand Down Expand Up @@ -589,6 +580,13 @@ static void stream_dump_signature_pkt(rnp_dump_ctx_t * ctx,
/* Todo: move dumper to pgp::pkt or pgp namespace */
using namespace pgp;

/**
* @brief Dump signature subpacket to the dst.
*
* @param ctx dump context
* @param dst dest
* @param subpkt subpacket itself
*/
static void
signature_dump_subpacket(rnp_dump_ctx_t *ctx, pgp_dest_t *dst, const pkt::sigsub::Raw &subpkt)

Check warning

Code scanning / CodeQL

Poorly documented large function Warning

Poorly documented function: fewer than 2% comments for a function of 168 lines.
{
Expand Down Expand Up @@ -1186,12 +1184,12 @@ stream_dump_pk_session_key(rnp_dump_ctx_t *ctx, pgp_source_t *src, pgp_dest_t *d
dst_printf(dst, "version: %d\n", (int) pkey.version);
#if defined(ENABLE_CRYPTO_REFRESH)
if (pkey.version == PGP_PKSK_V6) {
dst_print_fp(dst, NULL, pkey.fp);
dst_print_fp(dst, "fingerprint", pkey.fp);
} else {
dst_print_keyid(dst, NULL, pkey.key_id);
dst_print_keyid(dst, "key id", pkey.key_id);
}
#else
dst_print_keyid(dst, NULL, pkey.key_id);
dst_print_keyid(dst, "key id", pkey.key_id);
#endif
dst_print_palg(dst, NULL, pkey.alg);
dst_printf(dst, "encrypted material:\n");
Expand Down
37 changes: 30 additions & 7 deletions src/tests/cli_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2544,7 +2544,7 @@ def test_rnp_list_packets(self):
'json raw listing mismatch')
# List packets with all values, JSON output
params = ['--json', '--raw', '--list-packets', KEY_P256, '--mpi', '--grips']
ret, out, err = run_proc(RNP, params)
ret, out, _ = run_proc(RNP, params)
self.assertEqual(ret, 0, 'json all listing failed')
compare_file_ex(data_path('test_list_packets/list_json_all.txt'), out,
'json all listing mismatch')
Expand Down Expand Up @@ -2576,37 +2576,60 @@ def test_rnp_list_packets(self):
self.assertEqual(ret, 0)
self.assertRegex(out, r'(?s)^.*"type.str":"signer\'s user ID".*"length":9.*"uid":"alice@rnp".*$')
# List signature with reason for revocation subpacket
params = ['--list-packets', data_path('test_uid_validity/key-sig-revocation.pgp')]
KEY = data_path('test_uid_validity/key-sig-revocation.pgp')
params = ['--list-packets', KEY]
ret, out, _ = run_proc(RNP, params)
self.assertEqual(ret, 0)
self.assertRegex(out, r'(?s)^.*:type 29, len 24.*reason for revocation: 32 \(No longer valid\).*message: Testing revoked userid.*$')
# JSON list signature with reason for revocation subpacket
params = ['--list-packets', '--json', data_path('test_uid_validity/key-sig-revocation.pgp')]
params = ['--list-packets', '--json', KEY]
ret, out, _ = run_proc(RNP, params)
self.assertEqual(ret, 0)
self.assertRegex(out, r'(?s)^.*"type.str":"reason for revocation".*"code":32.*"message":"Testing revoked userid.".*$')
# v5 public key
params = ['--list-packets', data_path('test_stream_key_load/v5-rsa-pub.asc')]
KEY = data_path('test_stream_key_load/v5-rsa-pub.asc')
params = ['--list-packets', KEY]
ret, out, _ = run_proc(RNP, params)
self.assertEqual(ret, 0)
self.assertRegex(out, r'(?s)^.*v5 public key material length: 391.*v5 public key material length: 391.*$')
self.assertNotRegex(out, r'(?s)^.*v5 s2k length.*$')
self.assertNotRegex(out, r'(?s)^.*v5 secret key data length.*$')
params = ['--list-packets', '--json', data_path('test_stream_key_load/v5-rsa-pub.asc')]
params = ['--list-packets', '--json', KEY]
ret, out, _ = run_proc(RNP, params)
self.assertEqual(ret, 0)
self.assertRegex(out, r'(?s)^.*"v5 public key material length":391.*"v5 public key material length":391.*$')
self.assertNotRegex(out, r'(?s)^.*"v5 s2k length".*$')
self.assertNotRegex(out, r'(?s)^.*"v5 secret key data length".*$')
# v5 secret key
params = ['--list-packets', data_path('test_stream_key_load/v5-rsa-sec.asc')]
KEY = data_path('test_stream_key_load/v5-rsa-sec.asc')
params = ['--list-packets', KEY]
ret, out, _ = run_proc(RNP, params)
self.assertEqual(ret, 0)
self.assertRegex(out, r'(?s)^.*v5 s2k length: 28.*v5 secret key data length: 988.*v5 s2k length: 28.*v5 secret key data length: 987.*$')
params = ['--list-packets', '--json', data_path('test_stream_key_load/v5-rsa-sec.asc')]
params = ['--list-packets', '--json', KEY]
ret, out, _ = run_proc(RNP, params)
self.assertEqual(ret, 0)
self.assertRegex(out, r'(?s)^.*"v5 s2k length":28.*"v5 secret key data length":988.*"v5 s2k length":28.*"v5 secret key data length":987.*$')
# key with designated revoker packet
KEY = data_path('test_stream_key_load/ecc-p256-desigrevoked-25519-pub.asc')
params = ['--list-packets', KEY]
ret, out, _ = run_proc(RNP, params)
self.assertEqual(ret, 0)
self.assertRegex(out, r'(?s)^.*revocation key.*class.*128.*$')
params = ['--list-packets', '--json', KEY]
ret, out, _ = run_proc(RNP, params)
self.assertEqual(ret, 0)
self.assertRegex(out, r'(?s)^.*"revocation key".*"class":128.*$')
# primary userid subpackets
KEY = data_path('test_uid_validity/key-uids-pub.pgp')
params = ['--list-packets', KEY]
ret, out, _ = run_proc(RNP, params)
self.assertEqual(ret, 0)
self.assertRegex(out, r'(?s)^.* primary user ID: 1.*$')
params = ['--list-packets', '--json', KEY]
ret, out, _ = run_proc(RNP, params)
self.assertEqual(ret, 0)
self.assertRegex(out, r'(?s)^.*"primary":true.*$')

def test_rnp_list_packets_edge_cases(self):
KEY_EMPTY_UID = data_path('test_key_edge_cases/key-empty-uid.pgp')
Expand Down

0 comments on commit ebcdb2e

Please sign in to comment.