test/timing_load_creds.c: fix coding style and other (mostly minor) issues

Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com>
Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: David von Oheimb <david.von.oheimb@siemens.com>
(Merged from https://github.com/openssl/openssl/pull/18821)
This commit is contained in:
Dr. David von Oheimb 2022-07-25 09:19:40 +02:00 committed by Dr. David von Oheimb
parent c02036e1ad
commit 45479dcee1

View File

@ -1,21 +1,25 @@
/*
* Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
* Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/err.h>
#include <openssl/bio.h>
#include <sys/time.h>
#include <sys/resource.h>
#ifndef _WIN32
# include <sys/stat.h>
# include <openssl/pem.h>
# include <openssl/x509.h>
# include <openssl/err.h>
# include <openssl/bio.h>
# include <../include/internal/e_os.h>
# if defined(OPENSSL_SYS_UNIX)
# include <sys/resource.h>
# endif
static char *prog;
@ -24,11 +28,15 @@ static void readx509(const char *contents, int size)
X509 *x = NULL;
BIO *b = BIO_new_mem_buf(contents, size);
if (b == NULL)
ERR_print_errors_fp(stderr), exit(1);
if (b == NULL) {
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
PEM_read_bio_X509(b, &x, 0, NULL);
if (x == NULL)
ERR_print_errors_fp(stderr), exit(1);
if (x == NULL) {
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
X509_free(x);
BIO_free(b);
}
@ -38,41 +46,46 @@ static void readpkey(const char *contents, int size)
BIO *b = BIO_new_mem_buf(contents, size);
EVP_PKEY *pkey;
if (b == NULL)
ERR_print_errors_fp(stderr), exit(1);
if (b == NULL) {
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
pkey = PEM_read_bio_PrivateKey(b, NULL, NULL, NULL);
if (pkey == NULL)
ERR_print_errors_fp(stderr), exit(1);
if (pkey == NULL) {
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
EVP_PKEY_free(pkey);
BIO_free(b);
}
static void print(const char *what, struct timeval *tp)
static void print_timeval(const char *what, struct timeval *tp)
{
printf("%s %ld sec %ld microsec\n", what, tp->tv_sec, tp->tv_usec);
printf("%s %d sec %d microsec\n", what, (int)tp->tv_sec, (int)tp->tv_usec);
}
static void usage(void)
{
fprintf(stderr, "Usage: %s [flags] pem-file\n", prog);
fprintf(stderr, "Flags:\n");
fprintf(stderr, "Flags, with the default being '-wc':\n");
fprintf(stderr, " -c # Repeat count\n");
fprintf(stderr, " -d Debugging output (minimal)\n");
fprintf(stderr, " -w C What to load C is a single character:\n");
fprintf(stderr, " c for cert (default)\n");
fprintf(stderr, " -w<T> What to load T is a single character:\n");
fprintf(stderr, " c for cert\n");
fprintf(stderr, " p for private key\n");
exit(1);
exit(EXIT_FAILURE);
}
#endif
int main(int ac, char **av)
{
#ifndef _WIN32
int i, debug = 0, count = 100, what = 'c';
struct stat sb;
FILE *fp;
char *contents;
struct rusage now, start, end, elapsed;
struct rusage start, end, elapsed;
struct timeval e_start, e_end, e_elapsed;
/* Parse JCL. */
@ -81,6 +94,7 @@ int main(int ac, char **av)
switch (i) {
default:
usage();
break;
case 'c':
if ((count = atoi(optarg)) < 0)
usage();
@ -90,10 +104,11 @@ int main(int ac, char **av)
break;
case 'w':
if (optarg[1] != '\0')
usage;
usage();
switch (*optarg) {
default:
usage();
break;
case 'c':
case 'p':
what = *optarg;
@ -108,21 +123,27 @@ int main(int ac, char **av)
/* Read input file. */
if (av[0] == NULL)
usage();
if (stat(av[0], &sb) < 0)
perror(av[0]), exit(1);
contents = malloc(sb.st_size + 1);
if (contents == NULL)
perror("malloc"), exit(1);
if (stat(av[0], &sb) < 0) {
perror(av[0]);
exit(EXIT_FAILURE);
}
contents = OPENSSL_malloc(sb.st_size + 1);
if (contents == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
fp = fopen(av[0], "r");
if (fread(contents, 1, sb.st_size, fp) != sb.st_size)
perror("fread"), exit(1);
if ((long)fread(contents, 1, sb.st_size, fp) != sb.st_size) {
perror("fread");
exit(EXIT_FAILURE);
}
contents[sb.st_size] = '\0';
fclose(fp);
if (debug)
printf(">%s<\n", contents);
/* Try to prep system cache, etc. */
for (i = 10; --i >= 0; ) {
for (i = 10; i > 0; i--) {
switch (what) {
case 'c':
readx509(contents, (int)sb.st_size);
@ -133,11 +154,15 @@ int main(int ac, char **av)
}
}
if (gettimeofday(&e_start, NULL) < 0)
perror("elapsed start"), exit(1);
if (getrusage(RUSAGE_SELF, &start) < 0)
perror("start"), exit(1);
for (i = count; --i >= 0; ) {
if (gettimeofday(&e_start, NULL) < 0) {
perror("elapsed start");
exit(EXIT_FAILURE);
}
if (getrusage(RUSAGE_SELF, &start) < 0) {
perror("start");
exit(EXIT_FAILURE);
}
for (i = count; i > 0; i--) {
switch (what) {
case 'c':
readx509(contents, (int)sb.st_size);
@ -147,17 +172,27 @@ int main(int ac, char **av)
break;
}
}
if (getrusage(RUSAGE_SELF, &end) < 0)
perror("end"), exit(1);
if (gettimeofday(&e_end, NULL) < 0)
perror("end"), exit(1);
if (getrusage(RUSAGE_SELF, &end) < 0) {
perror("getrusage");
exit(EXIT_FAILURE);
}
if (gettimeofday(&e_end, NULL) < 0) {
perror("gettimeofday");
exit(EXIT_FAILURE);
}
timersub(&end.ru_utime, &start.ru_stime, &elapsed.ru_stime);
timersub(&end.ru_utime, &start.ru_utime, &elapsed.ru_utime);
timersub(&e_end, &e_start, &e_elapsed);
print("user ", &elapsed.ru_utime);
print("sys ", &elapsed.ru_stime);
print_timeval("user ", &elapsed.ru_utime);
print_timeval("sys ", &elapsed.ru_stime);
if (debug)
print("elapsed??", &e_elapsed);
return 0;
print_timeval("elapsed??", &e_elapsed);
OPENSSL_free(contents);
return EXIT_SUCCESS;
#else
fprintf(stderr, "This tool is not supported on Windows\n");
exit(EXIT_FAILURE);
#endif
}