diff options
Diffstat (limited to 'testing/Messenger_test.c')
-rw-r--r-- | testing/Messenger_test.c | 79 |
1 files changed, 45 insertions, 34 deletions
diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c index a11d374b..51542c6d 100644 --- a/testing/Messenger_test.c +++ b/testing/Messenger_test.c | |||
@@ -1,21 +1,21 @@ | |||
1 | /* Messenger test | 1 | /* Messenger test |
2 | * | 2 | * |
3 | * This program adds a friend and accepts all friend requests with the proper message. | 3 | * This program adds a friend and accepts all friend requests with the proper message. |
4 | * | 4 | * |
5 | * It tries sending a message to the added friend. | 5 | * It tries sending a message to the added friend. |
6 | * | 6 | * |
7 | * If it receives a message from a friend it replies back. | 7 | * If it receives a message from a friend it replies back. |
8 | * | 8 | * |
9 | * | 9 | * |
10 | * This is how I compile it: gcc -O2 -Wall -D VANILLA_NACL -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/Messenger.c ../core/DHT.c ../nacl/build/${HOSTNAME%.*}/lib/amd64/{cpucycles.o,libnacl.a,randombytes.o} Messenger_test.c | 10 | * This is how I compile it: gcc -O2 -Wall -D VANILLA_NACL -o test ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/Messenger.c ../core/DHT.c ../nacl/build/${HOSTNAME%.*}/lib/amd64/{cpucycles.o,libnacl.a,randombytes.o} Messenger_test.c |
11 | * | 11 | * |
12 | * | 12 | * |
13 | * Command line arguments are the ip, port and public_key of a node (for bootstrapping). | 13 | * Command line arguments are the ip, port and public_key of a node (for bootstrapping). |
14 | * | 14 | * |
15 | * EX: ./test 127.0.0.1 33445 CDCFD319CE3460824B33BE58FD86B8941C9585181D8FBD7C79C5721D7C2E9F7C | 15 | * EX: ./test 127.0.0.1 33445 CDCFD319CE3460824B33BE58FD86B8941C9585181D8FBD7C79C5721D7C2E9F7C |
16 | * | 16 | * |
17 | * Or the argument can be the path to the save file. | 17 | * Or the argument can be the path to the save file. |
18 | * | 18 | * |
19 | * EX: ./test Save.bak | 19 | * EX: ./test Save.bak |
20 | * | 20 | * |
21 | * Copyright (C) 2013 Tox project All Rights Reserved. | 21 | * Copyright (C) 2013 Tox project All Rights Reserved. |
@@ -34,7 +34,7 @@ | |||
34 | * | 34 | * |
35 | * You should have received a copy of the GNU General Public License | 35 | * You should have received a copy of the GNU General Public License |
36 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | 36 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. |
37 | * | 37 | * |
38 | */ | 38 | */ |
39 | 39 | ||
40 | #include "../core/Messenger.h" | 40 | #include "../core/Messenger.h" |
@@ -51,6 +51,10 @@ | |||
51 | 51 | ||
52 | #endif | 52 | #endif |
53 | 53 | ||
54 | /* FIXME needed as print_request has to match the interface expected by | ||
55 | * networking_requesthandler and so cannot take a Messenger * */ | ||
56 | static Messenger *m; | ||
57 | |||
54 | void print_request(uint8_t * public_key, uint8_t * data, uint16_t length) | 58 | void print_request(uint8_t * public_key, uint8_t * data, uint16_t length) |
55 | { | 59 | { |
56 | printf("Friend request received from: \n"); | 60 | printf("Friend request received from: \n"); |
@@ -63,7 +67,7 @@ void print_request(uint8_t * public_key, uint8_t * data, uint16_t length) | |||
63 | printf("%hhX", public_key[j]); | 67 | printf("%hhX", public_key[j]); |
64 | } | 68 | } |
65 | printf("\nOf length: %u with data: %s \n", length, data); | 69 | printf("\nOf length: %u with data: %s \n", length, data); |
66 | 70 | ||
67 | if(length != sizeof("Install Gentoo")) | 71 | if(length != sizeof("Install Gentoo")) |
68 | { | 72 | { |
69 | return; | 73 | return; |
@@ -72,14 +76,14 @@ void print_request(uint8_t * public_key, uint8_t * data, uint16_t length) | |||
72 | //if the request contained the message of peace the person is obviously a friend so we add him. | 76 | //if the request contained the message of peace the person is obviously a friend so we add him. |
73 | { | 77 | { |
74 | printf("Friend request accepted.\n"); | 78 | printf("Friend request accepted.\n"); |
75 | m_addfriend_norequest(public_key); | 79 | m_addfriend_norequest(m, public_key); |
76 | } | 80 | } |
77 | } | 81 | } |
78 | 82 | ||
79 | void print_message(int friendnumber, uint8_t * string, uint16_t length) | 83 | void print_message(Messenger *m, int friendnumber, uint8_t * string, uint16_t length) |
80 | { | 84 | { |
81 | printf("Message with length %u received from %u: %s \n", length, friendnumber, string); | 85 | printf("Message with length %u received from %u: %s \n", length, friendnumber, string); |
82 | m_sendmessage(friendnumber, (uint8_t*)"Test1", 6); | 86 | m_sendmessage(m, friendnumber, (uint8_t*)"Test1", 6); |
83 | } | 87 | } |
84 | 88 | ||
85 | int main(int argc, char *argv[]) | 89 | int main(int argc, char *argv[]) |
@@ -88,7 +92,13 @@ int main(int argc, char *argv[]) | |||
88 | printf("usage %s ip port public_key (of the DHT bootstrap node)\n or\n %s Save.bak\n", argv[0], argv[0]); | 92 | printf("usage %s ip port public_key (of the DHT bootstrap node)\n or\n %s Save.bak\n", argv[0], argv[0]); |
89 | exit(0); | 93 | exit(0); |
90 | } | 94 | } |
91 | initMessenger(); | 95 | |
96 | m = initMessenger(); | ||
97 | if( !m ){ | ||
98 | fputs("Failed to allocate messenger datastructure\n", stderr); | ||
99 | exit(0); | ||
100 | } | ||
101 | |||
92 | if(argc > 3) { | 102 | if(argc > 3) { |
93 | IP_Port bootstrap_ip_port; | 103 | IP_Port bootstrap_ip_port; |
94 | bootstrap_ip_port.port = htons(atoi(argv[2])); | 104 | bootstrap_ip_port.port = htons(atoi(argv[2])); |
@@ -100,13 +110,13 @@ int main(int argc, char *argv[]) | |||
100 | int read; | 110 | int read; |
101 | uint8_t buffer[128000]; | 111 | uint8_t buffer[128000]; |
102 | read = fread(buffer, 1, 128000, file); | 112 | read = fread(buffer, 1, 128000, file); |
103 | printf("Messenger loaded: %i\n", Messenger_load(buffer, read)); | 113 | printf("Messenger loaded: %i\n", Messenger_load(m, buffer, read)); |
104 | fclose(file); | 114 | fclose(file); |
105 | 115 | ||
106 | } | 116 | } |
107 | m_callback_friendrequest(print_request); | 117 | m_callback_friendrequest(m, print_request); |
108 | m_callback_friendmessage(print_message); | 118 | m_callback_friendmessage(m, print_message); |
109 | 119 | ||
110 | printf("OUR ID: "); | 120 | printf("OUR ID: "); |
111 | uint32_t i; | 121 | uint32_t i; |
112 | for(i = 0; i < 32; i++) { | 122 | for(i = 0; i < 32; i++) { |
@@ -114,33 +124,34 @@ int main(int argc, char *argv[]) | |||
114 | printf("0"); | 124 | printf("0"); |
115 | printf("%hhX",self_public_key[i]); | 125 | printf("%hhX",self_public_key[i]); |
116 | } | 126 | } |
117 | 127 | ||
118 | setname((uint8_t *)"Anon", 5); | 128 | setname(m, (uint8_t *)"Anon", 5); |
119 | 129 | ||
120 | char temp_id[128]; | 130 | char temp_id[128]; |
121 | printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n"); | 131 | printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n"); |
122 | if(scanf("%s", temp_id) != 1) { | 132 | if(scanf("%s", temp_id) != 1) { |
123 | return 1; | 133 | return 1; |
124 | } | 134 | } |
125 | int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); | 135 | int num = m_addfriend(m, hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); |
126 | 136 | ||
127 | perror("Initialization"); | 137 | perror("Initialization"); |
128 | 138 | ||
129 | while(1) { | 139 | while(1) { |
130 | uint8_t name[128]; | 140 | uint8_t name[128]; |
131 | getname(num, name); | 141 | getname(m, num, name); |
132 | printf("%s\n", name); | 142 | printf("%s\n", name); |
133 | 143 | ||
134 | m_sendmessage(num, (uint8_t*)"Test", 5); | 144 | m_sendmessage(m, num, (uint8_t*)"Test", 5); |
135 | doMessenger(); | 145 | doMessenger(m); |
136 | c_sleep(30); | 146 | c_sleep(30); |
137 | FILE *file = fopen("Save.bak", "wb"); | 147 | FILE *file = fopen("Save.bak", "wb"); |
138 | if ( file==NULL ){return 1;} | 148 | if ( file==NULL ){return 1;} |
139 | uint8_t * buffer = malloc(Messenger_size()); | 149 | uint8_t * buffer = malloc(Messenger_size(m)); |
140 | Messenger_save(buffer); | 150 | Messenger_save(m, buffer); |
141 | size_t write_result = fwrite(buffer, 1, Messenger_size(), file); | 151 | size_t write_result = fwrite(buffer, 1, Messenger_size(m), file); |
142 | if (write_result < Messenger_size()) {return 1;} | 152 | if (write_result < Messenger_size(m)) {return 1;} |
143 | free(buffer); | 153 | free(buffer); |
144 | fclose(file); | 154 | fclose(file); |
145 | } | 155 | } |
156 | cleanupMessenger(m); | ||
146 | } | 157 | } |