diff options
-rw-r--r-- | auto_tests/dht_test.c | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/auto_tests/dht_test.c b/auto_tests/dht_test.c index 6ed84098..5533593f 100644 --- a/auto_tests/dht_test.c +++ b/auto_tests/dht_test.c | |||
@@ -669,10 +669,103 @@ START_TEST(test_dht_create_packet) | |||
669 | } | 669 | } |
670 | END_TEST | 670 | END_TEST |
671 | 671 | ||
672 | #define MAX_COUNT 3 | ||
673 | |||
674 | static void dht_pack_unpack(const Node_format *nodes, size_t size, uint8_t *data, size_t length) | ||
675 | { | ||
676 | int packed_size = pack_nodes(data, length, nodes, size); | ||
677 | ck_assert_msg(packed_size != -1, "Wrong pack_nodes result"); | ||
678 | |||
679 | uint16_t processed = 0; | ||
680 | VLA(Node_format, nodes_unpacked, size); | ||
681 | const uint8_t tcp_enabled = 1; | ||
682 | |||
683 | int unpacked_count = unpack_nodes(nodes_unpacked, size, &processed, data, length, tcp_enabled); | ||
684 | ck_assert_msg(unpacked_count == size, "Wrong unpack_nodes result"); | ||
685 | ck_assert_msg(processed == packed_size, "unpack_nodes did not process all data"); | ||
686 | |||
687 | for (size_t i = 0; i < size; i++) { | ||
688 | const IP_Port *ipp1 = &nodes[i].ip_port; | ||
689 | const IP_Port *ipp2 = &nodes_unpacked[i].ip_port; | ||
690 | ck_assert_msg(ip_equal(&ipp1->ip, &ipp2->ip), "Unsuccessful ip unpack"); | ||
691 | ck_assert_msg(ipp1->port == ipp2->port, "Unsuccessful port unpack"); | ||
692 | |||
693 | const uint8_t *pk1 = nodes[i].public_key; | ||
694 | const uint8_t *pk2 = nodes_unpacked[i].public_key; | ||
695 | ck_assert_msg(!memcmp(pk1, pk2, CRYPTO_PUBLIC_KEY_SIZE), "Unsuccessful pk unpack"); | ||
696 | } | ||
697 | } | ||
698 | |||
699 | static void random_ip(IP_Port *ipp, int family) | ||
700 | { | ||
701 | uint8_t *ip = NULL; | ||
702 | size_t size; | ||
703 | |||
704 | if (family == AF_INET || family == TCP_INET) { | ||
705 | ip = (uint8_t *)&ipp->ip.ip4; | ||
706 | size = sizeof(ipp->ip.ip4); | ||
707 | } else if (family == AF_INET6 || family == TCP_INET6) { | ||
708 | ip = (uint8_t *)&ipp->ip.ip6; | ||
709 | size = sizeof(ipp->ip.ip6); | ||
710 | } else { | ||
711 | return; | ||
712 | } | ||
713 | |||
714 | uint8_t *port = (uint8_t *)&ipp->port; | ||
715 | random_bytes(port, sizeof(ipp->port)); | ||
716 | random_bytes(ip, size); | ||
717 | ipp->ip.family = family; | ||
718 | |||
719 | } | ||
720 | |||
721 | #define PACKED_NODES_SIZE (SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE) | ||
722 | |||
723 | START_TEST(test_dht_node_packing) | ||
724 | { | ||
725 | const uint16_t length = MAX_COUNT * PACKED_NODES_SIZE; | ||
726 | uint8_t *data = (uint8_t *)malloc(length); | ||
727 | |||
728 | Node_format nodes[MAX_COUNT]; | ||
729 | const size_t pk_size = sizeof(nodes[0].public_key); | ||
730 | |||
731 | random_bytes(nodes[0].public_key, pk_size); | ||
732 | random_bytes(nodes[1].public_key, pk_size); | ||
733 | random_bytes(nodes[2].public_key, pk_size); | ||
734 | |||
735 | random_ip(&nodes[0].ip_port, AF_INET); | ||
736 | random_ip(&nodes[1].ip_port, AF_INET); | ||
737 | random_ip(&nodes[2].ip_port, AF_INET); | ||
738 | dht_pack_unpack(nodes, 3, data, length); | ||
739 | |||
740 | random_ip(&nodes[0].ip_port, AF_INET); | ||
741 | random_ip(&nodes[1].ip_port, AF_INET); | ||
742 | random_ip(&nodes[2].ip_port, TCP_INET); | ||
743 | dht_pack_unpack(nodes, 3, data, length); | ||
744 | |||
745 | random_ip(&nodes[0].ip_port, AF_INET); | ||
746 | random_ip(&nodes[1].ip_port, AF_INET6); | ||
747 | random_ip(&nodes[2].ip_port, TCP_INET6); | ||
748 | dht_pack_unpack(nodes, 3, data, length); | ||
749 | |||
750 | random_ip(&nodes[0].ip_port, TCP_INET); | ||
751 | random_ip(&nodes[1].ip_port, TCP_INET6); | ||
752 | random_ip(&nodes[2].ip_port, TCP_INET); | ||
753 | dht_pack_unpack(nodes, 3, data, length); | ||
754 | |||
755 | random_ip(&nodes[0].ip_port, AF_INET6); | ||
756 | random_ip(&nodes[1].ip_port, AF_INET6); | ||
757 | random_ip(&nodes[2].ip_port, AF_INET6); | ||
758 | dht_pack_unpack(nodes, 3, data, length); | ||
759 | |||
760 | free(data); | ||
761 | } | ||
762 | END_TEST | ||
763 | |||
672 | static Suite *dht_suite(void) | 764 | static Suite *dht_suite(void) |
673 | { | 765 | { |
674 | Suite *s = suite_create("DHT"); | 766 | Suite *s = suite_create("DHT"); |
675 | DEFTESTCASE(dht_create_packet); | 767 | DEFTESTCASE(dht_create_packet); |
768 | DEFTESTCASE(dht_node_packing); | ||
676 | 769 | ||
677 | DEFTESTCASE_SLOW(list, 20); | 770 | DEFTESTCASE_SLOW(list, 20); |
678 | DEFTESTCASE_SLOW(DHT_test, 50); | 771 | DEFTESTCASE_SLOW(DHT_test, 50); |