rework error handling - disable hash tests for now
This commit is contained in:
parent
8112dda4e8
commit
177faf1ee6
7 changed files with 32 additions and 53 deletions
2
Makefile
2
Makefile
|
@ -19,7 +19,7 @@ TARGET_BIN = multihash
|
||||||
MAIN = src/main.c
|
MAIN = src/main.c
|
||||||
MAIN_O = $(MAIN:.c=.o)
|
MAIN_O = $(MAIN:.c=.o)
|
||||||
|
|
||||||
SRCS = src/errors.c src/hashes.c
|
SRCS = src/hashes.c src/errors.c
|
||||||
OBJS = $(SRCS:.c=.o)
|
OBJS = $(SRCS:.c=.o)
|
||||||
|
|
||||||
src/%.o: src/%.c
|
src/%.o: src/%.c
|
||||||
|
|
|
@ -2,16 +2,15 @@
|
||||||
#ifndef ERROR_H
|
#ifndef ERROR_H
|
||||||
#define 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.
|
#define MH_E_NO_ERROR 0
|
||||||
MH_E_LAST = -5
|
#define MH_E_UNKNOWN_CODE -1
|
||||||
} mh_error;
|
#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 */
|
#endif /* end of include guard */
|
||||||
|
|
|
@ -18,25 +18,9 @@ typedef enum {
|
||||||
MH_H_COUNT // number of hash functions
|
MH_H_COUNT // number of hash functions
|
||||||
} mh_hash;
|
} mh_hash;
|
||||||
|
|
||||||
// returns default length in bytes of given hash function
|
int mh_hash_length(const unsigned char multihash[], int len, int *hash_length);
|
||||||
// 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_function(const unsigned char multihash[], int len, mh_hash *hash);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
33
src/errors.c
33
src/errors.c
|
@ -1,23 +1,20 @@
|
||||||
/* vim: set ts=8 sw=8 noexpandtab: */
|
/* vim: set ts=8 sw=8 noexpandtab: */
|
||||||
#include "mh/errors.h"
|
#include "mh/errors.h"
|
||||||
|
|
||||||
static const struct mh_error_desc {
|
const char *mh_error_string(int code) {
|
||||||
int code;
|
switch (code) {
|
||||||
const char *message;
|
case MH_E_NO_ERROR:
|
||||||
} mh_errordesc[] = {
|
return "no error";
|
||||||
{ MH_E_UNKNOWN_CODE, "unknown multihash code" },
|
case MH_E_UNKNOWN_CODE:
|
||||||
{ MH_E_TOO_SHORT, "multihash too short. must be > 3 bytes" },
|
return "unknown multihash code";
|
||||||
{ MH_E_TOO_LONG, "multihash too long. must be < 129 bytes" },
|
case MH_E_TOO_SHORT:
|
||||||
{ MH_E_LEN_NOT_SUPPORTED, "multihash does not yet support"
|
return "multihash too short. must be > 3 bytes";
|
||||||
" digests longer than 127 bytes" }
|
case MH_E_TOO_LONG:
|
||||||
};
|
return "multihash too long. must be < 129 bytes";
|
||||||
|
case MH_E_LEN_NOT_SUPPORTED:
|
||||||
const char *mh_error_string(mh_error code) {
|
return "multihash does not yet support"
|
||||||
unsigned int i = 0;
|
" digsets linger than 127 bytes";
|
||||||
for (; i < sizeof(mh_errordesc); i++) {
|
default:
|
||||||
if (mh_errordesc[i].code == code)
|
return "unknown error code";
|
||||||
return mh_errordesc->message;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return "unknown error code";
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,7 @@
|
||||||
/* vim: set ts=8 sw=8 noexpandtab: */
|
/* vim: set ts=8 sw=8 noexpandtab: */
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
#include "mh/errors.h"
|
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
printf("%s\n", mh_error_string(MH_E_TOO_LONG));
|
|
||||||
printf("Hello World\n");
|
printf("Hello World\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,9 @@ static char *test_error_messages_exist(void) {
|
||||||
int i = -1;
|
int i = -1;
|
||||||
for (; i > MH_E_LAST; i--) {
|
for (; i > MH_E_LAST; i--) {
|
||||||
sprintf(error_buf, "error code %d has no message", 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;
|
return NULL;
|
||||||
|
|
|
@ -8,13 +8,14 @@ char error_buf[256];
|
||||||
|
|
||||||
static char *test_all_hashes_have_lengths(void) {
|
static char *test_all_hashes_have_lengths(void) {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
int length = 0;
|
//int length = 0;
|
||||||
for (; i < MH_H_COUNT; i++) {
|
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"
|
sprintf(error_buf, "mh_hash_default_length: hash %d"
|
||||||
" returned invalid (%d) default length",
|
" returned invalid (%d) default length",
|
||||||
i, length);
|
i, length);
|
||||||
mu_assert(error_buf, length > 0);
|
mu_assert(error_buf, length > 0);
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue