# recipes for integration/unit testing
# NOTE: integration requires all target
-integration: all
+integration:
$(MAKE) -C test/integration check
unit:
$(MAKE) -C test/unit check
unsigned char peer_id[PEER_PEER_ID_SIZE];
uint8_t infohash[PEER_INFOHASH_SIZE];
- struct torrent *torrent;
-
uint8_t *bitfield;
size_t bitfield_size;
int peer_choke_received(struct peer*,ssize_t);
void peer_free(struct peer*);
int peer_handshake(struct peer*);
+int peer_handshake_received(struct peer*);
int peer_hash_reject(struct peer*);
int peer_hash_reject_received(struct peer*,ssize_t);
int peer_hash_request(struct peer*);
int peer_hashes_received(struct peer*,ssize_t);
int peer_have(struct peer*,size_t);
int peer_have_received(struct peer*,ssize_t);
+void peer_id(void*,size_t);
int peer_init(struct peer**);
int peer_interested(struct peer*);
int peer_interested_received(struct peer*,ssize_t);
#include<hashmap.h>
#include<log.h>
+#include<peer.h>
#include<torrent.h>
struct session_torrents {
struct session {
struct session_torrents torrents;
- unsigned char peer_id[20];
+ unsigned char peer_id[PEER_PEER_ID_SIZE];
};
extern struct session session;
#define HEADER_LENGTH HANDSHAKE_STRING_LENGTH+PEER_INFOHASH_SIZE+PEER_PEER_ID_SIZE
-static int peer_handshake_receive(struct peer*);
-
int peer_handshake(struct peer *info) {
char header[HEADER_LENGTH] = HANDSHAKE_STRING;
- if(peer_handshake_receive(info)<0) { return -1; }
- if(!info->handshake) { return 0; }
+// if(peer_handshake_receive(info)<0) { return -1; }
+// if(!info->handshake) { return 0; }
// copy infohash to buffer
memcpy(
info->infohash,
PEER_INFOHASH_SIZE);
- // copy peer id to buffer
+ // copy session->peer_id to buffer
memcpy(
&(header[HANDSHAKE_STRING_LENGTH+PEER_INFOHASH_SIZE]),
- info->peer_id,
+ session.peer_id,
PEER_PEER_ID_SIZE);
if(net_send(info,header,HEADER_LENGTH)<0) { return -1; }
return 1;
}
-static int peer_handshake_receive(struct peer *info) {
+int peer_handshake_received(struct peer *info) {
char header[HEADER_LENGTH];
int i;
&(header[HANDSHAKE_STRING_LENGTH+PEER_INFOHASH_SIZE]),
PEER_INFOHASH_SIZE);
- info->torrent = session_find_torrent(info->infohash,PEER_INFOHASH_SIZE);
- if(NULL==info->torrent) { return -1; }
-
info->handshake = 1;
return 1;
--- /dev/null
+#include<peer.h>
+
+void peer_id(void *buf, size_t size) {
+ assert(PEER_PEER_ID_SIZE==size);
+
+ randombytes_buf(buf,PEER_PEER_ID_SIZE);
+}
int session_setup() {
if(session_init(&session)<0) { return -1; }
+ peer_id(session.peer_id,PEER_PEER_ID_SIZE);
+
if(0!=atexit(&session_clean)) {
perror("atexit");
return -1;
qbittorrent_tests_SOURCES = \
$(common_SOURCES) \
- qbittorrent.tests.c
+ qbittorrent.tests.c \
+ $(top_srcdir)/src/net/cache.c \
+ $(top_srcdir)/src/net/send.c \
+ $(top_srcdir)/src/net/wait.c \
+ $(top_srcdir)/src/peer/free.c \
+ $(top_srcdir)/src/peer/handshake.c \
+ $(top_srcdir)/src/peer/id.c \
+ $(top_srcdir)/src/peer/init.c
clean-local:
-rm -rf $(TEST_DIRECTORY)
#include<test_utils.h>
+#include<peer.h>
+
int main();
void qbittorrent_integration_test();
+/* mocks and structs */
+struct session session;
+int session_setup();
+
int main() {
setup_env();
qbittorrent_integration_test();
-
reset_env();
return EXIT_SUCCESS;
void qbittorrent_integration_test() {
struct addrinfo hints, *res;
- int sock_fd;
+ struct peer *info;
+ const char infohash[] = {0x25, 0x11, 0xdf, 0x9f, 0x74, 0xdc, 0x6f, 0xc1, 0x8e, 0xc9, 0x46, 0x6c, 0xdd, 0x52, 0xf5, 0xa1, 0x83, 0x27, 0xe2, 0x6e};
+
+ assert(1==peer_init(&info));
memset(&hints,0,sizeof(hints));
hints.ai_family = AF_UNSPEC;
getaddrinfo("127.0.0.1",QBITTORRENT_PORT,&hints,&res);
- sock_fd = socket(res->ai_family,res->ai_socktype,res->ai_protocol);
- assert(sock_fd>0);
+ info->sock = socket(res->ai_family,res->ai_socktype,res->ai_protocol);
+ assert(info->sock>0);
+
+ assert(0==connect(info->sock,res->ai_addr,res->ai_addrlen));
+
+ assert(1==session_setup());
+ memcpy(info->infohash,infohash,PEER_INFOHASH_SIZE);
+
+ assert(1==peer_handshake(info));
+ assert(1==peer_handshake_received(info));
+
+ assert(1==peer_bitfield(info));
+
+ close(info->sock);
+ peer_free(info);
+}
+
+int session_init() {
+ return 1;
+}
- assert(0==connect(sock_fd,res->ai_addr,res->ai_addrlen));
+int session_setup() {
+ assert(1==session_init(&session));
+ peer_id(session.peer_id,PEER_PEER_ID_SIZE);
- close(sock_fd);
+ return 1;
}