summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/api.c11
-rw-r--r--test/api.c68
2 files changed, 56 insertions, 23 deletions
diff --git a/src/api.c b/src/api.c
index 16cde8c..720c9b4 100644
--- a/src/api.c
+++ b/src/api.c
@@ -185,8 +185,6 @@ static enum MHD_Result handler(
return MHD_YES;
} else {
- pthread_mutex_lock(&api->dht->mutex);
-
if(conn->state == OP_PENDING) {
if(!conn->target_set) {
response = MHD_create_response_from_buffer_static(0, NULL);
@@ -194,12 +192,16 @@ static enum MHD_Result handler(
goto end;
}
+ pthread_mutex_lock(&api->dht->mutex);
+
if(api->dht->lookup.state != OP_EMPTY) {
+ pthread_mutex_unlock(&api->dht->mutex);
response = MHD_create_response_from_buffer_static(0, NULL);
ret = MHD_queue_response(connection, MHD_HTTP_CONFLICT, response);
goto end;
}
+
// Start a lookup
memcpy(&api->dht->lookup.target, &conn->target, sizeof(struct nodeid));
api->dht->lookup.state = OP_PENDING;
@@ -211,7 +213,10 @@ static enum MHD_Result handler(
goto end;
}
- if(api->dht->lookup.state != OP_COMPLETED) {
+ pthread_mutex_lock(&api->dht->mutex);
+
+ if(api->dht->lookup.state != OP_COMPLETED && api->dht->lookup.state != OP_EMPTY) {
+ pthread_mutex_unlock(&api->dht->mutex);
response = MHD_create_response_from_buffer_static(0, NULL);
ret = MHD_queue_response(connection, MHD_HTTP_CONFLICT, response);
goto end;
diff --git a/test/api.c b/test/api.c
index c88ed46..06edc9b 100644
--- a/test/api.c
+++ b/test/api.c
@@ -147,32 +147,35 @@ void test_lookup_get() {
dht.self = (struct nodeid){.inner={0x42424242, 0x42424242, 0x42424242, 0x42424242, 0x42424242}};
api_init(&dht);
- curlRes = curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:6982/lookup");
- TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
+ long code;
+ char *ct;
+ struct memory body = {0};
+ {
+ curlRes = curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:6982/lookup");
+ TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
- curlRes = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_memory);
- TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
+ curlRes = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_memory);
+ TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
- struct memory body = {0};
- curlRes = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &body);
- TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
+ curlRes = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &body);
+ TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
- curlRes = curl_easy_perform(curl);
- TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
+ curlRes = curl_easy_perform(curl);
+ TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
- char *ct;
- curlRes = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
- TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
+ curlRes = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
+ TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
- TEST_ASSERT_EQUAL_STRING("application/json", ct);
+ TEST_ASSERT_EQUAL_STRING("application/json", ct);
- TEST_ASSERT_EQUAL_STRING(
- "{ "
- "\"state\": \"empty\" "
- "}",
- body.body
- );
- body = (struct memory){0};
+ TEST_ASSERT_EQUAL_STRING(
+ "{ "
+ "\"state\": \"empty\" "
+ "}",
+ body.body
+ );
+ body = (struct memory){0};
+ }
{
curlRes = curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
@@ -206,6 +209,31 @@ void test_lookup_get() {
body = (struct memory){0};
}
+ {
+ curlRes = curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
+ TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
+
+ curlRes = curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_from_memory);
+ TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
+
+ struct memory req_body = {
+ .body = "{\"state\": \"empty\"}",
+ .size = strlen(req_body.body),
+ };
+ curlRes = curl_easy_setopt(curl, CURLOPT_READDATA, &req_body);
+ TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
+
+ curlRes = curl_easy_perform(curl);
+ TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
+
+ curlRes = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
+ TEST_ASSERT_EQUAL(CURLE_OK, curlRes);
+ TEST_ASSERT_EQUAL(409, code);
+
+ body = (struct memory){0};
+ }
+
+
// The protocol does whatever and complete the lookup
dht.lookup.state = OP_COMPLETED;