Skip to content
Browse files

VersionList always claimed that all nodes were protocol version 0, fixed

  • Loading branch information...
1 parent faed287 commit 3fedecb9d3c4938f8dcedc985b936677116a44a4 Caleb James DeLisle committed
Showing with 67 additions and 6 deletions.
  1. +11 −5 dht/dhtcore/VersionList.c
  2. +3 −0 dht/dhtcore/test/CMakeLists.txt
  3. +52 −0 dht/dhtcore/test/VersionList_test.c
  4. +1 −1 io/ArrayWriter.c
View
16 dht/dhtcore/VersionList.c
@@ -19,6 +19,7 @@
#include "io/ArrayWriter.h"
#include "io/ArrayReader.h"
#include "util/Endian.h"
+#include <stdio.h>
struct VersionList* VersionList_parse(String* str, struct Allocator* alloc)
{
@@ -38,7 +39,8 @@ struct VersionList* VersionList_parse(String* str, struct Allocator* alloc)
for (int i = 0; i < (int)list->length; i++) {
uint32_t ver = 0;
r->read((uint8_t*) &ver, numberSize, r);
- list->versions[i] = Endian_bigEndianToHost32(ver);
+ ver = Endian_bigEndianToHost32(ver);
+ list->versions[i] = ver >> ((4-numberSize) * 8);
}
return list;
}
@@ -55,13 +57,17 @@ String* VersionList_stringify(struct VersionList* list, struct Allocator* alloc)
}
String* out = String_newBinary(NULL, (numberSize * list->length + 1), alloc);
- out->bytes[0] = numberSize;
- struct Writer* w = ArrayWriter_new(out->bytes + 1, out->len - 1, alloc);
+ struct Writer* w = ArrayWriter_new(out->bytes, out->len, alloc);
+ Writer_write(w, &numberSize, 1);
+
for (int i = 0; i < (int)list->length; i++) {
- uint32_t ver = Endian_hostToBigEndian32(list->versions[i]);
- w->write((uint8_t*) &ver, numberSize, w);
+printf("writing [%d]\n", list->versions[i]);
+ uint32_t ver = list->versions[i] << ((4-numberSize) * 8);
+ ver = Endian_hostToBigEndian32(ver);
+ Writer_write(w, (uint8_t*) &ver, numberSize);
}
+ Writer_write(w, &numberSize, 1);
return out;
}
View
3 dht/dhtcore/test/CMakeLists.txt
@@ -11,11 +11,14 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
set(Test_FILES
SearchStore_test.c
+ VersionList_test.c
)
set(Test_LIBRARIES
dht
dhtcore
cjdmemory
cjdns-util-events-libuv
+ cjdns-crypto-random
+ cjdns-memory-canary
)
include(${CMAKE_SOURCE_DIR}/cmake/modules/Test.cmake)
View
52 dht/dhtcore/test/VersionList_test.c
@@ -0,0 +1,52 @@
+/* vim: set expandtab ts=4 sw=4: */
+/*
+ * You may redistribute this program and/or modify it under the terms of
+ * the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include "memory/MallocAllocator.h"
+#include "memory/CanaryAllocator.h"
+#include "crypto/random/Random.h"
+#include "dht/dhtcore/VersionList.h"
+#include "util/Assert.h"
+#include "util/Hex.h"
+
+int main()
+{
+ struct Allocator* alloc = CanaryAllocator_new(MallocAllocator_new(1<<20), NULL);
+ struct Random* rand = Random_new(alloc, NULL, NULL);
+
+ for (int bytes = 1; bytes < 5; bytes++) {
+ uint32_t count = Random_uint32(rand) % 30;
+ uint32_t max = Random_uint32(rand) % UINT32_MAX >> ((4-bytes) * 8);
+ if (max < 2) {
+ max += 2;
+ }
+ struct VersionList* vl = VersionList_new(count, alloc);
+ for (uint32_t i = 0; i < count; i++) {
+ vl->versions[i] = Random_uint32(rand) % max;
+ }
+
+ String* str = VersionList_stringify(vl, alloc);
+
+ uint8_t* buff = Allocator_malloc(alloc, (str->len+1) * 2 + 1);
+ Hex_encode(buff, (str->len+1) * 2 + 1, (uint8_t*)str->bytes, str->len+1);
+ printf("Got versions [%s]\n", buff);
+
+ struct VersionList* vl2 = VersionList_parse(str, alloc);
+
+ Assert_always(vl->length == vl2->length && vl->length == count);
+ for (uint32_t i = 0; i < count; i++) {
+ printf("[%d] [%d]\n", vl2->versions[i], vl->versions[i]);
+ Assert_always(vl2->versions[i] == vl->versions[i]);
+ }
+ }
+}
View
2 io/ArrayWriter.c
@@ -65,7 +65,7 @@ static int write(const void* toWrite, size_t length, const struct Writer* writer
}
/* Prove that it doesn't run off the end of the buffer or roll over. */
- if (context->pointer + length >= context->endPointer
+ if (context->pointer + length > context->endPointer
|| context->pointer + length < context->pointer)
{
context->returnCode = -1;

0 comments on commit 3fedecb

Please sign in to comment.
Something went wrong with that request. Please try again.