Add LCOV_EXCL* markers to crypto.c and crypto_s2k.c

This marks some lines as unreachable by the unit tests, and as
therefore excluded from test coverage.

(Note: This convention is only for lines that are absolutely
unreachable.  Don't use it anywhere you wouldn't add a
tor_fragile_assert().)
This commit is contained in:
Nick Mathewson 2016-04-12 21:13:33 -04:00
parent 4043f2c95f
commit 0630f1982d
2 changed files with 14 additions and 8 deletions

View File

@ -134,7 +134,7 @@ crypto_get_rsa_padding_overhead(int padding)
switch (padding) switch (padding)
{ {
case RSA_PKCS1_OAEP_PADDING: return PKCS1_OAEP_PADDING_OVERHEAD; case RSA_PKCS1_OAEP_PADDING: return PKCS1_OAEP_PADDING_OVERHEAD;
default: tor_assert(0); return -1; default: tor_assert(0); return -1; // LCOV_EXCL_LINE
} }
} }
@ -146,7 +146,7 @@ crypto_get_rsa_padding(int padding)
switch (padding) switch (padding)
{ {
case PK_PKCS1_OAEP_PADDING: return RSA_PKCS1_OAEP_PADDING; case PK_PKCS1_OAEP_PADDING: return RSA_PKCS1_OAEP_PADDING;
default: tor_assert(0); return -1; default: tor_assert(0); return -1; // LCOV_EXCL_LINE
} }
} }
@ -1739,8 +1739,8 @@ crypto_digest_algorithm_get_length(digest_algorithm_t alg)
case DIGEST_SHA3_512: case DIGEST_SHA3_512:
return DIGEST512_LEN; return DIGEST512_LEN;
default: default:
tor_assert(0); tor_assert(0); // LCOV_EXCL_LINE
return 0; /* Unreachable */ return 0; /* Unreachable */ // LCOV_EXCL_LINE
} }
} }
@ -1783,8 +1783,8 @@ crypto_digest_alloc_bytes(digest_algorithm_t alg)
case DIGEST_SHA3_512: case DIGEST_SHA3_512:
return END_OF_FIELD(d.sha3); return END_OF_FIELD(d.sha3);
default: default:
tor_assert(0); tor_assert(0); // LCOV_EXCL_LINE
return 0; return 0; // LCOV_EXCL_LINE
} }
#undef END_OF_FIELD #undef END_OF_FIELD
#undef STRUCT_FIELD_SIZE #undef STRUCT_FIELD_SIZE
@ -1914,6 +1914,7 @@ crypto_digest_get_digest(crypto_digest_t *digest,
case DIGEST_SHA512: case DIGEST_SHA512:
SHA512_Final(r, &tmpenv.d.sha512); SHA512_Final(r, &tmpenv.d.sha512);
break; break;
//LCOV_EXCL_START
case DIGEST_SHA3_256: /* FALLSTHROUGH */ case DIGEST_SHA3_256: /* FALLSTHROUGH */
case DIGEST_SHA3_512: case DIGEST_SHA3_512:
log_warn(LD_BUG, "Handling unexpected algorithm %d", digest->algorithm); log_warn(LD_BUG, "Handling unexpected algorithm %d", digest->algorithm);
@ -1921,6 +1922,7 @@ crypto_digest_get_digest(crypto_digest_t *digest,
default: default:
tor_assert(0); /* Unreachable. */ tor_assert(0); /* Unreachable. */
break; break;
//LCOV_EXCL_STOP
} }
memcpy(out, r, out_len); memcpy(out, r, out_len);
memwipe(r, 0, sizeof(r)); memwipe(r, 0, sizeof(r));
@ -2760,10 +2762,12 @@ crypto_strongest_rand(uint8_t *out, size_t out_len)
while (out_len) { while (out_len) {
crypto_rand((char*) inp, DLEN); crypto_rand((char*) inp, DLEN);
if (crypto_strongest_rand_raw(inp+DLEN, DLEN) < 0) { if (crypto_strongest_rand_raw(inp+DLEN, DLEN) < 0) {
// LCOV_EXCL_START
log_err(LD_CRYPTO, "Failed to load strong entropy when generating an " log_err(LD_CRYPTO, "Failed to load strong entropy when generating an "
"important key. Exiting."); "important key. Exiting.");
/* Die with an assertion so we get a stack trace. */ /* Die with an assertion so we get a stack trace. */
tor_assert(0); tor_assert(0);
// LCOV_EXCL_STOP
} }
if (out_len >= DLEN) { if (out_len >= DLEN) {
SHA512(inp, sizeof(inp), out); SHA512(inp, sizeof(inp), out);

View File

@ -57,7 +57,8 @@
#define SCRYPT_KEY_LEN 32 #define SCRYPT_KEY_LEN 32
/** Given an algorithm ID (one of S2K_TYPE_*), return the length of the /** Given an algorithm ID (one of S2K_TYPE_*), return the length of the
* specifier part of it, without the prefix type byte. */ * specifier part of it, without the prefix type byte. Return -1 if it is not
* a valid algorithm ID. */
static int static int
secret_to_key_spec_len(uint8_t type) secret_to_key_spec_len(uint8_t type)
{ {
@ -86,7 +87,8 @@ secret_to_key_key_len(uint8_t type)
case S2K_TYPE_SCRYPT: case S2K_TYPE_SCRYPT:
return DIGEST256_LEN; return DIGEST256_LEN;
default: default:
return -1; tor_fragile_assert(); // LCOV_EXCL_LINE
return -1; // LCOV_EXCL_LINE
} }
} }