rework error handling - disable hash tests for now

This commit is contained in:
Jakub Sztandera 2016-07-29 18:05:11 +01:00
parent 8112dda4e8
commit 177faf1ee6
7 changed files with 32 additions and 53 deletions

View file

@ -19,7 +19,7 @@ TARGET_BIN = multihash
MAIN = src/main.c
MAIN_O = $(MAIN:.c=.o)
SRCS = src/errors.c src/hashes.c
SRCS = src/hashes.c src/errors.c
OBJS = $(SRCS:.c=.o)
src/%.o: src/%.c

View file

@ -2,16 +2,15 @@
#ifndef ERROR_H
#define ERROR_H
typedef enum {
MH_E_UNKNOWN_CODE = -1,
MH_E_TOO_SHORT = -2,
MH_E_TOO_LONG = -3,
MH_E_LEN_NOT_SUPPORTED = -4,
// Should be always last, if you are adding error update this code.
MH_E_LAST = -5
} mh_error;
#define MH_E_NO_ERROR 0
#define MH_E_UNKNOWN_CODE -1
#define MH_E_TOO_SHORT -2
#define MH_E_TOO_LONG -3
#define MH_E_LEN_NOT_SUPPORTED -4
const char *mh_error_string(mh_error code);
#define MH_E_LAST -5
const char *mh_error_string(int code);
#endif /* end of include guard */

View file

@ -18,25 +18,9 @@ typedef enum {
MH_H_COUNT // number of hash functions
} mh_hash;
// returns default length in bytes of given hash function
// or -1 in case of invalid arguemnt
int mh_hash_default_length(mh_hash hash);
// returns wire format code of given hash function
int mh_hash_code(mh_hash hash);
typedef struct {
mh_hash hash;
int code;
int length;
} mh_hashinfo;
// decodes code info hash info, leaves length field zeroed
mh_error mh_hashinfo_from_code(int code, mh_hashinfo *hinfo);
// decodes bytes into hashinfo
mh_error mh_hashinfo_from_bytes(char bytes[2], mh_hashinfo *hinfo);
int mh_hash_length(const unsigned char multihash[], int len, int *hash_length);
int mh_hash_function(const unsigned char multihash[], int len, mh_hash *hash);

View file

@ -1,23 +1,20 @@
/* vim: set ts=8 sw=8 noexpandtab: */
#include "mh/errors.h"
static const struct mh_error_desc {
int code;
const char *message;
} mh_errordesc[] = {
{ MH_E_UNKNOWN_CODE, "unknown multihash code" },
{ MH_E_TOO_SHORT, "multihash too short. must be > 3 bytes" },
{ MH_E_TOO_LONG, "multihash too long. must be < 129 bytes" },
{ MH_E_LEN_NOT_SUPPORTED, "multihash does not yet support"
" digests longer than 127 bytes" }
};
const char *mh_error_string(mh_error code) {
unsigned int i = 0;
for (; i < sizeof(mh_errordesc); i++) {
if (mh_errordesc[i].code == code)
return mh_errordesc->message;
const char *mh_error_string(int code) {
switch (code) {
case MH_E_NO_ERROR:
return "no error";
case MH_E_UNKNOWN_CODE:
return "unknown multihash code";
case MH_E_TOO_SHORT:
return "multihash too short. must be > 3 bytes";
case MH_E_TOO_LONG:
return "multihash too long. must be < 129 bytes";
case MH_E_LEN_NOT_SUPPORTED:
return "multihash does not yet support"
" digsets linger than 127 bytes";
default:
return "unknown error code";
}
return "unknown error code";
}

View file

@ -1,11 +1,7 @@
/* vim: set ts=8 sw=8 noexpandtab: */
#include <stdio.h>
#include "mh/errors.h"
int main(void) {
printf("%s\n", mh_error_string(MH_E_TOO_LONG));
printf("Hello World\n");
return 0;
}

View file

@ -10,7 +10,9 @@ static char *test_error_messages_exist(void) {
int i = -1;
for (; i > MH_E_LAST; i--) {
sprintf(error_buf, "error code %d has no message", i);
mu_assert(error_buf, strlen(mh_error_string((mh_error) i)) > 0);
mu_assert(error_buf, strlen(mh_error_string(i)) > 0);
mu_assert(error_buf,
strcmp(mh_error_string(-10000), mh_error_string(i)) != 0);
}
return NULL;

View file

@ -8,13 +8,14 @@ char error_buf[256];
static char *test_all_hashes_have_lengths(void) {
int i = 0;
int length = 0;
//int length = 0;
for (; i < MH_H_COUNT; i++) {
length = mh_hash_default_length((mh_hash) i);
/* length = mh_hash_default_length((mh_hash) i);
sprintf(error_buf, "mh_hash_default_length: hash %d"
" returned invalid (%d) default length",
i, length);
mu_assert(error_buf, length > 0);
*/
}
return NULL;
}