Test improvements

And some related fixes.
This commit is contained in:
Roger A. Light 2019-02-26 12:39:33 +00:00
parent c506c8335b
commit 3b6b6d5fa8
6 changed files with 102 additions and 24 deletions

View File

@ -261,6 +261,7 @@ static void db__message_remove(struct mosquitto_db *db, struct mosquitto *contex
context->last_inflight_msg = NULL; context->last_inflight_msg = NULL;
} }
} }
mosquitto_property_free_all(&(*msg)->properties);
mosquitto__free(*msg); mosquitto__free(*msg);
if(last){ if(last){
*msg = last->next; *msg = last->next;

View File

@ -254,11 +254,12 @@ int handle__publish(struct mosquitto_db *db, struct mosquitto *context)
log__printf(NULL, MOSQ_LOG_DEBUG, "Dropped too large PUBLISH from %s (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))", context->id, dup, qos, retain, mid, topic, (long)payloadlen); log__printf(NULL, MOSQ_LOG_DEBUG, "Dropped too large PUBLISH from %s (d%d, q%d, r%d, m%d, '%s', ... (%ld bytes))", context->id, dup, qos, retain, mid, topic, (long)payloadlen);
goto process_bad_message; goto process_bad_message;
} }
if(UHPA_ALLOC(payload, payloadlen+1) == 0){ if(UHPA_ALLOC(payload, payloadlen) == 0){
mosquitto__free(topic); mosquitto__free(topic);
mosquitto_property_free_all(&msg_properties); mosquitto_property_free_all(&msg_properties);
return MOSQ_ERR_NOMEM; return MOSQ_ERR_NOMEM;
} }
if(packet__read_bytes(&context->in_packet, UHPA_ACCESS(payload, payloadlen), payloadlen)){ if(packet__read_bytes(&context->in_packet, UHPA_ACCESS(payload, payloadlen), payloadlen)){
mosquitto__free(topic); mosquitto__free(topic);
UHPA_FREE(payload, payloadlen); UHPA_FREE(payload, payloadlen);

View File

@ -770,6 +770,7 @@ static int persist__msg_store_chunk_restore(struct mosquitto_db *db, FILE *db_fp
mosquitto__free(load); mosquitto__free(load);
fclose(db_fptr); fclose(db_fptr);
mosquitto__free(source.id); mosquitto__free(source.id);
mosquitto__free(source.id);
return rc; return rc;
} }
@ -784,6 +785,7 @@ static int persist__msg_store_chunk_restore(struct mosquitto_db *db, FILE *db_fp
mosquitto__free(load); mosquitto__free(load);
fclose(db_fptr); fclose(db_fptr);
mosquitto__free(source.id); mosquitto__free(source.id);
mosquitto__free(source.username);
mosquitto__free(topic); mosquitto__free(topic);
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory."); log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
return MOSQ_ERR_NOMEM; return MOSQ_ERR_NOMEM;
@ -793,6 +795,9 @@ static int persist__msg_store_chunk_restore(struct mosquitto_db *db, FILE *db_fp
rc = db__message_store(db, &source, source_mid, topic, qos, payloadlen, &payload, retain, &stored, 0, NULL, store_id); rc = db__message_store(db, &source, source_mid, topic, qos, payloadlen, &payload, retain, &stored, 0, NULL, store_id);
mosquitto__free(source.id); mosquitto__free(source.id);
source.id = NULL;
mosquitto__free(source.username);
source.username = NULL;
if(rc == MOSQ_ERR_SUCCESS){ if(rc == MOSQ_ERR_SUCCESS){
load->db_id = stored->db_id; load->db_id = stored->db_id;
@ -812,6 +817,7 @@ error:
log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", err); log__printf(NULL, MOSQ_LOG_ERR, "Error: %s.", err);
fclose(db_fptr); fclose(db_fptr);
mosquitto__free(source.id); mosquitto__free(source.id);
mosquitto__free(source.username);
mosquitto__free(topic); mosquitto__free(topic);
UHPA_FREE(payload, payloadlen); UHPA_FREE(payload, payloadlen);
return 1; return 1;

View File

@ -720,25 +720,28 @@ static int pwfile__parse(const char *file, struct mosquitto__unpwd **root)
return MOSQ_ERR_SUCCESS; return MOSQ_ERR_SUCCESS;
} }
static int unpwd__file_parse(struct mosquitto__unpwd **unpwd, const char *password_file)
{
int rc;
#ifdef WITH_TLS #ifdef WITH_TLS
static void unpwd__free_item(struct mosquitto__unpwd **unpwd, struct mosquitto__unpwd *item)
{
mosquitto__free(item->username);
mosquitto__free(item->password);
mosquitto__free(item->salt);
HASH_DEL(*unpwd, item);
mosquitto__free(item);
}
static int unpwd__decode_passwords(struct mosquitto__unpwd **unpwd)
{
struct mosquitto__unpwd *u, *tmp; struct mosquitto__unpwd *u, *tmp;
char *token; char *token;
unsigned char *salt; unsigned char *salt;
unsigned int salt_len; unsigned int salt_len;
unsigned char *password; unsigned char *password;
unsigned int password_len; unsigned int password_len;
#endif int rc;
if(!unpwd) return MOSQ_ERR_INVAL;
if(!password_file) return MOSQ_ERR_SUCCESS;
rc = pwfile__parse(password_file, unpwd);
#ifdef WITH_TLS
if(rc) return rc;
HASH_ITER(hh, *unpwd, u, tmp){ HASH_ITER(hh, *unpwd, u, tmp){
/* Need to decode password into hashed data + salt. */ /* Need to decode password into hashed data + salt. */
@ -760,30 +763,49 @@ static int unpwd__file_parse(struct mosquitto__unpwd **unpwd, const char *passwo
u->password_len = password_len; u->password_len = password_len;
}else{ }else{
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to decode password for user %s, removing entry.", u->username); log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to decode password for user %s, removing entry.", u->username);
HASH_DEL(*unpwd, u); unpwd__free_item(unpwd, u);
} }
}else{ }else{
log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid password hash for user %s, removing entry.", u->username); log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid password hash for user %s, removing entry.", u->username);
HASH_DEL(*unpwd, u); unpwd__free_item(unpwd, u);
} }
}else{ }else{
log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to decode password salt for user %s, removing entry.", u->username); log__printf(NULL, MOSQ_LOG_ERR, "Error: Unable to decode password salt for user %s, removing entry.", u->username);
HASH_DEL(*unpwd, u); unpwd__free_item(unpwd, u);
} }
}else{ }else{
log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid password hash for user %s, removing entry.", u->username); log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid password hash for user %s, removing entry.", u->username);
HASH_DEL(*unpwd, u); unpwd__free_item(unpwd, u);
} }
}else{ }else{
log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid password hash for user %s, removing entry.", u->username); log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid password hash for user %s, removing entry.", u->username);
HASH_DEL(*unpwd, u); unpwd__free_item(unpwd, u);
} }
}else{ }else{
log__printf(NULL, MOSQ_LOG_ERR, "Error: Missing password hash for user %s, removing entry.", u->username); log__printf(NULL, MOSQ_LOG_ERR, "Error: Missing password hash for user %s, removing entry.", u->username);
HASH_DEL(*unpwd, u); unpwd__free_item(unpwd, u);
} }
} }
return MOSQ_ERR_SUCCESS;
}
#endif #endif
static int unpwd__file_parse(struct mosquitto__unpwd **unpwd, const char *password_file)
{
int rc;
if(!unpwd) return MOSQ_ERR_INVAL;
if(!password_file) return MOSQ_ERR_SUCCESS;
rc = pwfile__parse(password_file, unpwd);
#ifdef WITH_TLS
if(rc) return rc;
rc = unpwd__decode_passwords(unpwd);
#endif
return rc; return rc;
} }
@ -1063,21 +1085,43 @@ int pw__digest(const char *password, const unsigned char *salt, unsigned int sal
int base64__decode(char *in, unsigned char **decoded, unsigned int *decoded_len) int base64__decode(char *in, unsigned char **decoded, unsigned int *decoded_len)
{ {
BIO *bmem, *b64; BIO *bmem, *b64;
int slen;
slen = strlen(in);
b64 = BIO_new(BIO_f_base64()); b64 = BIO_new(BIO_f_base64());
if(!b64){
return 1;
}
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
bmem = BIO_new(BIO_s_mem()); bmem = BIO_new(BIO_s_mem());
if(!bmem){
BIO_free_all(b64);
return 1;
}
b64 = BIO_push(b64, bmem); b64 = BIO_push(b64, bmem);
BIO_write(bmem, in, strlen(in)); BIO_write(bmem, in, slen);
if(BIO_flush(bmem) != 1){ if(BIO_flush(bmem) != 1){
BIO_free_all(b64); BIO_free_all(b64);
return 1; return 1;
} }
*decoded = mosquitto__calloc(strlen(in), 1); *decoded = mosquitto__calloc(slen, 1);
*decoded_len = BIO_read(b64, *decoded, strlen(in)); if(!(*decoded)){
BIO_free_all(b64);
return 1;
}
*decoded_len = BIO_read(b64, *decoded, slen);
BIO_free_all(b64); BIO_free_all(b64);
if(*decoded_len <= 0){
mosquitto__free(*decoded);
*decoded = NULL;
*decoded_len = 0;
return 1;
}
return 0; return 0;
} }

View File

@ -155,7 +155,7 @@ Contributors:
# define UHPA_ALLOC(u, size) ((u).ptr = uhpa_malloc(size)) # define UHPA_ALLOC(u, size) ((u).ptr = uhpa_malloc(size))
# define UHPA_ACCESS(u, size) (u).ptr # define UHPA_ACCESS(u, size) (u).ptr
# define UHPA_FREE(u, size) uhpa_free((u).ptr); (u).ptr = NULL; # define UHPA_FREE(u, size) uhpa_free((u).ptr); (u).ptr = NULL;
# define UHPA_MOVE(dest, src, src_size) {(dest).ptr = (src).ptr; (src).ptr = NULL} # define UHPA_MOVE(dest, src, src_size) {(dest).ptr = (src).ptr; (src).ptr = NULL;}
#else #else
# define UHPA_ALLOC(u, size) UHPA_ALLOC_CHK(u, size) # define UHPA_ALLOC(u, size) UHPA_ALLOC_CHK(u, size)
# define UHPA_ACCESS(u, size) UHPA_ACCESS_CHK(u, size) # define UHPA_ACCESS(u, size) UHPA_ACCESS_CHK(u, size)

View File

@ -8,7 +8,17 @@ import time
import mqtt5_props import mqtt5_props
import __main__
import atexit
vg_index = 1
vg_logfiles = []
def start_broker(filename, cmd=None, port=0, use_conf=False, expect_fail=False): def start_broker(filename, cmd=None, port=0, use_conf=False, expect_fail=False):
global vg_index
global vg_logfiles
delay = 0.1 delay = 0.1
if use_conf == True: if use_conf == True:
@ -26,7 +36,10 @@ def start_broker(filename, cmd=None, port=0, use_conf=False, expect_fail=False):
port = 1888 port = 1888
if os.environ.get('MOSQ_USE_VALGRIND') is not None: if os.environ.get('MOSQ_USE_VALGRIND') is not None:
cmd = ['valgrind', '-q', '--trace-children=yes', '--leak-check=full', '--show-leak-kinds=all', '--log-file='+filename+'.vglog'] + cmd logfile = filename+'.'+str(vg_index)+'.vglog'
cmd = ['valgrind', '-q', '--trace-children=yes', '--leak-check=full', '--show-leak-kinds=all', '--log-file='+logfile] + cmd
vg_logfiles.append(logfile)
vg_index += 1
delay = 1 delay = 1
#print(port) #print(port)
@ -567,3 +580,16 @@ def get_lib_port():
return int(sys.argv[2]) return int(sys.argv[2])
else: else:
return 1888 return 1888
@atexit.register
def test_cleanup():
global vg_logfiles
if os.environ.get('MOSQ_USE_VALGRIND') is not None:
for f in vg_logfiles:
try:
if os.stat(f).st_size == 0:
os.remove(f)
except OSError:
pass