1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
|
/*
* SPDX-FileCopyrightText: 2023-2025 Sébastien Helleu <flashcode@flashtux.org>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
* This file is part of WeeChat, the extensible chat client.
*
* WeeChat is free software; you can redistribute it 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.
*
* WeeChat 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 WeeChat. If not, see <http://www.gnu.org/licenses/>.
*/
/* Test relay IRC protocol */
#include "CppUTest/TestHarness.h"
#include "tests.h"
extern "C"
{
#include <stdio.h>
#include <string.h>
#include "src/core/core-arraylist.h"
#include "src/core/core-config-file.h"
#include "src/core/core-hashtable.h"
#include "src/core/core-hook.h"
#include "src/core/core-string.h"
#include "src/plugins/plugin.h"
#include "src/plugins/irc/irc-server.h"
#include "src/plugins/relay/relay.h"
#include "src/plugins/relay/relay-client.h"
#include "src/plugins/relay/relay-config.h"
#include "src/plugins/relay/relay-server.h"
#include "src/plugins/relay/irc/relay-irc.h"
extern int relay_irc_command_relayed (const char *irc_command);
extern int relay_irc_command_ignored (const char *irc_command);
extern int relay_irc_search_server_capability (const char *capability);
extern struct t_hashtable *relay_irc_message_parse (const char *message);
extern void relay_irc_sendf (struct t_relay_client *client,
const char *format, ...);
extern void relay_irc_parse_cap_message (struct t_relay_client *client,
struct t_hashtable *parsed_msg);
extern void relay_irc_parse_ctcp (const char *message,
char **ctcp_type, char **ctcp_params);
extern int relay_irc_tag_relay_client_id (const char *tags);
extern void relay_irc_input_send (struct t_relay_client *client,
const char *irc_channel,
const char *options,
const char *format, ...);
extern int relay_irc_cap_enabled (struct t_relay_client *client,
const char *capability);
extern int relay_irc_get_supported_caps (struct t_relay_client *client);
extern struct t_arraylist *relay_irc_get_list_caps ();
}
#define CLIENT_RECV(__irc_msg) \
test_client_recv (__irc_msg);
#define CLIENT_SEND(__irc_msg) \
test_client_send (__irc_msg);
#define CHECK_SENT_CLIENT(__message) \
if ((__message != NULL) \
&& !arraylist_search (sent_messages_client, (void *)__message, \
NULL, NULL)) \
{ \
char **msg = test_build_error ( \
"Message not sent to the relay client", \
NULL, \
__message, \
NULL, \
"All messages sent"); \
sent_msg_dump (sent_messages_client, msg); \
FAIL(string_dyn_free (msg, 0)); \
} \
else if ((__message == NULL) \
&& (arraylist_size (sent_messages_client) > 0)) \
{ \
char **msg = test_build_error ( \
"Unexpected message(s) sent to the relay client", \
NULL, \
NULL, \
NULL, \
NULL); \
sent_msg_dump (sent_messages_client, msg); \
FAIL(string_dyn_free (msg, 0)); \
}
#define CHECK_SENT_IRC(__message) \
if ((__message != NULL) \
&& !arraylist_search (sent_messages_irc, (void *)__message, \
NULL, NULL)) \
{ \
char **msg = test_build_error ( \
"Message not sent to the IRC server", \
NULL, \
__message, \
NULL, \
"All messages sent"); \
sent_msg_dump (sent_messages_irc, msg); \
FAIL(string_dyn_free (msg, 0)); \
} \
else if ((__message == NULL) \
&& (arraylist_size (sent_messages_irc) > 0)) \
{ \
char **msg = test_build_error ( \
"Unexpected message(s) sent to the IRC server", \
NULL, \
NULL, \
NULL, \
NULL); \
sent_msg_dump (sent_messages_irc, msg); \
FAIL(string_dyn_free (msg, 0)); \
}
TEST_GROUP(RelayIrc)
{
};
TEST_GROUP(RelayIrcWithClient)
{
struct t_irc_server *ptr_server = NULL;
struct t_relay_server *ptr_relay_server = NULL;
struct t_relay_client *ptr_relay_client = NULL;
struct t_arraylist *sent_messages_client = NULL;
struct t_arraylist *sent_messages_irc = NULL;
struct t_hook *hook_modifier_relay_irc_out = NULL;
struct t_hook *hook_signal_irc_input_send = NULL;
void test_client_recv (const char *data)
{
arraylist_clear (sent_messages_client);
arraylist_clear (sent_messages_irc);
relay_irc_recv (ptr_relay_client, data);
}
void test_client_send (const char *data)
{
arraylist_clear (sent_messages_client);
arraylist_clear (sent_messages_irc);
relay_irc_sendf (ptr_relay_client, "%s", data);
}
char **test_build_error (const char *msg1,
const char *prefix,
const char *message,
const char *tags,
const char *msg2)
{
char **msg;
msg = string_dyn_alloc (1024);
string_dyn_concat (msg, msg1, -1);
if (message)
{
string_dyn_concat (msg, ": prefix=\"", -1);
string_dyn_concat (msg, prefix, -1);
string_dyn_concat (msg, "\", message=\"", -1);
string_dyn_concat (msg, message, -1);
string_dyn_concat (msg, "\", tags=\"", -1);
string_dyn_concat (msg, tags, -1);
string_dyn_concat (msg, "\"\n", -1);
}
else
{
string_dyn_concat (msg, ":\n", -1);
}
if (msg2)
{
string_dyn_concat (msg, msg2, -1);
string_dyn_concat (msg, ":\n", -1);
}
return msg;
}
static char *modifier_relay_irc_out_cb (const void *pointer,
void *data,
const char *modifier,
const char *modifier_data,
const char *string)
{
/* make C++ compiler happy */
(void) data;
(void) modifier;
(void) modifier_data;
if (string)
arraylist_add ((struct t_arraylist *)pointer, strdup (string));
return NULL;
}
static int signal_irc_input_send_cb (const void *pointer, void *data,
const char *signal,
const char *type_data,
void *signal_data)
{
/* make C++ compiler happy */
(void) data;
(void) signal;
(void) type_data;
if (signal_data)
{
arraylist_add ((struct t_arraylist *)pointer,
strdup ((const char *)signal_data));
}
return WEECHAT_RC_OK;
}
static int sent_msg_cmp_cb (void *data, struct t_arraylist *arraylist,
void *pointer1, void *pointer2)
{
/* make C++ compiler happy */
(void) data;
(void) arraylist;
return strcmp ((char *)pointer1, (char *)pointer2);
}
static void sent_msg_free_cb (void *data, struct t_arraylist *arraylist,
void *pointer)
{
/* make C++ compiler happy */
(void) data;
(void) arraylist;
free (pointer);
}
void sent_msg_dump (struct t_arraylist *sent_messages, char **msg)
{
int i;
for (i = 0; i < arraylist_size (sent_messages); i++)
{
string_dyn_concat (msg, " \"", -1);
string_dyn_concat (msg,
(const char *)arraylist_get (sent_messages, i),
-1);
string_dyn_concat (msg, "\"\n", -1);
}
}
void setup ()
{
/* initialize list of messages sent to the relay client */
if (sent_messages_client)
{
arraylist_clear (sent_messages_client);
}
else
{
sent_messages_client = arraylist_new (16, 0, 1,
&sent_msg_cmp_cb, NULL,
&sent_msg_free_cb, NULL);
}
/* initialize list of messages sent to the IRC server */
if (sent_messages_irc)
{
arraylist_clear (sent_messages_irc);
}
else
{
sent_messages_irc = arraylist_new (16, 0, 1,
&sent_msg_cmp_cb, NULL,
&sent_msg_free_cb, NULL);
}
/* disable auto-open of relay buffer */
config_file_option_set (relay_config_look_auto_open_buffer, "off", 1);
/* set relay password */
config_file_option_set (relay_config_network_password, "secret", 1);
if (!hook_modifier_relay_irc_out)
{
hook_modifier_relay_irc_out = hook_modifier (
NULL,
"relay_client_irc_out1",
&modifier_relay_irc_out_cb, sent_messages_client, NULL);
}
if (!hook_signal_irc_input_send)
{
hook_signal_irc_input_send = hook_signal (
NULL,
"irc_input_send",
&signal_irc_input_send_cb, sent_messages_irc, NULL);
}
/* create a fake server (no I/O) */
run_cmd_quiet ("/mute /server add test fake:127.0.0.1 "
"-nicks=nick1,nick2,nick3");
/* get the server pointer */
ptr_server = irc_server_search ("test");
/* connect to the fake server */
run_cmd_quiet ("/connect test");
/* simulate connection OK to server */
run_cmd_quiet ("/command -buffer irc.server.test irc "
"/server fakerecv "
"\":server 001 alice :Welcome on this server, nick1!\"");
/* create a relay server */
ptr_relay_server = relay_server_new (
"irc.test",
RELAY_PROTOCOL_IRC,
"test",
9001,
NULL, /* path */
1, /* ipv4 */
0, /* ipv6 */
0, /* tls */
0); /* unix_socket */
/* create a fake relay client (no I/O) */
ptr_relay_client = relay_client_new (-1, "test", ptr_relay_server);
}
void teardown ()
{
relay_client_free (ptr_relay_client);
ptr_relay_client = NULL;
relay_server_free (ptr_relay_server);
ptr_relay_server = NULL;
/* disconnect and delete the fake server */
run_cmd_quiet ("/mute /disconnect test");
run_cmd_quiet ("/mute /server del test");
ptr_server = NULL;
/* restore auto-open of relay buffer */
config_file_option_reset (relay_config_look_auto_open_buffer, 1);
/* restore relay password */
config_file_option_reset (relay_config_network_password, 1);
}
};
/*
* Tests functions:
* relay_irc_command_relayed
*/
TEST(RelayIrc, CommandRelayed)
{
LONGS_EQUAL(0, relay_irc_command_relayed (NULL));
LONGS_EQUAL(0, relay_irc_command_relayed (""));
LONGS_EQUAL(0, relay_irc_command_relayed ("unknown"));
LONGS_EQUAL(1, relay_irc_command_relayed ("privmsg"));
LONGS_EQUAL(1, relay_irc_command_relayed ("PRIVMSG"));
LONGS_EQUAL(1, relay_irc_command_relayed ("notice"));
LONGS_EQUAL(1, relay_irc_command_relayed ("Notice"));
}
/*
* Tests functions:
* relay_irc_command_ignored
*/
TEST(RelayIrc, CommandIgnored)
{
LONGS_EQUAL(0, relay_irc_command_ignored (NULL));
LONGS_EQUAL(0, relay_irc_command_ignored (""));
LONGS_EQUAL(0, relay_irc_command_ignored ("unknown"));
LONGS_EQUAL(1, relay_irc_command_ignored ("cap"));
LONGS_EQUAL(1, relay_irc_command_ignored ("CAP"));
LONGS_EQUAL(1, relay_irc_command_ignored ("pong"));
LONGS_EQUAL(1, relay_irc_command_ignored ("Pong"));
LONGS_EQUAL(1, relay_irc_command_ignored ("quit"));
}
/*
* Tests functions:
* relay_irc_search_backlog_commands_tags
*/
TEST(RelayIrc, SearchBacklogCommandsTags)
{
LONGS_EQUAL(-1, relay_irc_search_backlog_commands_tags (NULL));
LONGS_EQUAL(-1, relay_irc_search_backlog_commands_tags (""));
LONGS_EQUAL(-1, relay_irc_search_backlog_commands_tags ("unknown"));
LONGS_EQUAL(-1, relay_irc_search_backlog_commands_tags ("IRC_JOIN"));
CHECK(relay_irc_search_backlog_commands_tags ("irc_join") >= 0);
CHECK(relay_irc_search_backlog_commands_tags ("irc_privmsg") >= 0);
}
/*
* Tests functions:
* relay_irc_search_server_capability
*/
TEST(RelayIrc, SearchServerCapability)
{
LONGS_EQUAL(-1, relay_irc_search_server_capability (NULL));
LONGS_EQUAL(-1, relay_irc_search_server_capability (""));
LONGS_EQUAL(-1, relay_irc_search_server_capability ("unknown"));
CHECK(relay_irc_search_server_capability ("server-time") >= 0);
CHECK(relay_irc_search_server_capability ("echo-message") >= 0);
}
/*
* Tests functions:
* relay_irc_message_parse
*/
TEST(RelayIrc, MessageParse)
{
struct t_hashtable *hashtable;
POINTERS_EQUAL(NULL, relay_irc_message_parse (NULL));
hashtable = relay_irc_message_parse ("");
CHECK(hashtable);
LONGS_EQUAL(14, hashtable->items_count);
STRCMP_EQUAL("", (const char *)hashtable_get (hashtable, "tags"));
STRCMP_EQUAL("", (const char *)hashtable_get (hashtable, "message_without_tags"));
STRCMP_EQUAL("", (const char *)hashtable_get (hashtable, "nick"));
STRCMP_EQUAL("", (const char *)hashtable_get (hashtable, "user"));
STRCMP_EQUAL("", (const char *)hashtable_get (hashtable, "host"));
STRCMP_EQUAL("", (const char *)hashtable_get (hashtable, "command"));
STRCMP_EQUAL("", (const char *)hashtable_get (hashtable, "channel"));
STRCMP_EQUAL("", (const char *)hashtable_get (hashtable, "arguments"));
STRCMP_EQUAL("", (const char *)hashtable_get (hashtable, "text"));
STRCMP_EQUAL("0", (const char *)hashtable_get (hashtable, "num_params"));
STRCMP_EQUAL("-1", (const char *)hashtable_get (hashtable, "pos_command"));
STRCMP_EQUAL("-1", (const char *)hashtable_get (hashtable, "pos_arguments"));
STRCMP_EQUAL("-1", (const char *)hashtable_get (hashtable, "pos_channel"));
STRCMP_EQUAL("-1", (const char *)hashtable_get (hashtable, "pos_text"));
hashtable_free (hashtable);
hashtable = relay_irc_message_parse (
"@time=2015-06-27T16:40:35.000Z :nick!user@host PRIVMSG #weechat :Hello world!");
CHECK(hashtable);
LONGS_EQUAL(17, hashtable->items_count);
STRCMP_EQUAL("time=2015-06-27T16:40:35.000Z", (const char *)hashtable_get (hashtable, "tags"));
STRCMP_EQUAL("2015-06-27T16:40:35.000Z", (const char *)hashtable_get (hashtable, "tag_time"));
STRCMP_EQUAL(":nick!user@host PRIVMSG #weechat :Hello world!", (const char *)hashtable_get (hashtable, "message_without_tags"));
STRCMP_EQUAL("nick", (const char *)hashtable_get (hashtable, "nick"));
STRCMP_EQUAL("user", (const char *)hashtable_get (hashtable, "user"));
STRCMP_EQUAL("nick!user@host", (const char *)hashtable_get (hashtable, "host"));
STRCMP_EQUAL("PRIVMSG", (const char *)hashtable_get (hashtable, "command"));
STRCMP_EQUAL("#weechat", (const char *)hashtable_get (hashtable, "channel"));
STRCMP_EQUAL("#weechat :Hello world!", (const char *)hashtable_get (hashtable, "arguments"));
STRCMP_EQUAL("Hello world!", (const char *)hashtable_get (hashtable, "text"));
STRCMP_EQUAL("2", (const char *)hashtable_get (hashtable, "num_params"));
STRCMP_EQUAL("#weechat", (const char *)hashtable_get (hashtable, "param1"));
STRCMP_EQUAL("Hello world!", (const char *)hashtable_get (hashtable, "param2"));
STRCMP_EQUAL("47", (const char *)hashtable_get (hashtable, "pos_command"));
STRCMP_EQUAL("55", (const char *)hashtable_get (hashtable, "pos_arguments"));
STRCMP_EQUAL("55", (const char *)hashtable_get (hashtable, "pos_channel"));
STRCMP_EQUAL("65", (const char *)hashtable_get (hashtable, "pos_text"));
hashtable_free (hashtable);
}
/*
* Tests functions:
* relay_irc_sendf
*/
TEST(RelayIrcWithClient, Sendf)
{
relay_irc_sendf (NULL, NULL);
relay_irc_sendf (NULL, "test");
relay_irc_sendf (ptr_relay_client, NULL);
CLIENT_SEND("PING");
CHECK_SENT_CLIENT("PING");
CLIENT_SEND("PRIVMSG #test :test message");
CHECK_SENT_CLIENT("PRIVMSG #test :test message");
}
/*
* Tests functions:
* relay_irc_parse_cap_message
*/
TEST(RelayIrcWithClient, ParseCapMessage)
{
struct t_hashtable *hashtable;
LONGS_EQUAL(0, RELAY_IRC_DATA(ptr_relay_client, irc_cap_echo_message));
/* CAP NAK: ignored */
hashtable = relay_irc_message_parse (":server CAP * NAK echo-message");
relay_irc_parse_cap_message (ptr_relay_client, hashtable);
LONGS_EQUAL(0, RELAY_IRC_DATA(ptr_relay_client, irc_cap_echo_message));
hashtable_free (hashtable);
/* CAP ACK with unknown capability */
hashtable = relay_irc_message_parse (":server CAP * ACK unknown");
relay_irc_parse_cap_message (ptr_relay_client, hashtable);
LONGS_EQUAL(0, RELAY_IRC_DATA(ptr_relay_client, irc_cap_echo_message));
hashtable_free (hashtable);
/* CAP ACK with extended-join and echo-message */
hashtable = relay_irc_message_parse (":server CAP * ACK extended-join echo-message");
relay_irc_parse_cap_message (ptr_relay_client, hashtable);
LONGS_EQUAL(1, RELAY_IRC_DATA(ptr_relay_client, irc_cap_echo_message));
hashtable_free (hashtable);
/* CAP ACK with -extended-join and -echo-message */
hashtable = relay_irc_message_parse (":server CAP * ACK -extended-join -echo-message");
relay_irc_parse_cap_message (ptr_relay_client, hashtable);
LONGS_EQUAL(0, RELAY_IRC_DATA(ptr_relay_client, irc_cap_echo_message));
hashtable_free (hashtable);
}
/*
* Tests functions:
* relay_irc_signal_irc_in2_cb
*/
TEST(RelayIrc, SignalIrcIn2Cb)
{
/* TODO: write tests */
}
/*
* Tests functions:
* relay_irc_tag_relay_client_id
*/
TEST(RelayIrc, TagRelayClientId)
{
LONGS_EQUAL(-1, relay_irc_tag_relay_client_id (NULL));
LONGS_EQUAL(-1, relay_irc_tag_relay_client_id (""));
LONGS_EQUAL(-1, relay_irc_tag_relay_client_id ("zzz"));
LONGS_EQUAL(-1, relay_irc_tag_relay_client_id ("relay_client_abc"));
LONGS_EQUAL(0, relay_irc_tag_relay_client_id ("relay_client_0"));
LONGS_EQUAL(123, relay_irc_tag_relay_client_id ("relay_client_123"));
}
/*
* Tests functions:
* relay_irc_signal_irc_outtags_cb
*/
TEST(RelayIrc, SignalIrcOuttagsCb)
{
/* TODO: write tests */
}
/*
* Tests functions:
* relay_irc_signal_irc_disc_cb
*/
TEST(RelayIrc, SignalIrcDiscCb)
{
/* TODO: write tests */
}
/*
* Tests functions:
* relay_irc_hsignal_irc_redir_cb
*/
TEST(RelayIrc, HsignalIrcRedirCb)
{
/* TODO: write tests */
}
/*
* Tests functions:
* relay_irc_get_line_info
*/
TEST(RelayIrc, GetLineInfo)
{
/* TODO: write tests */
}
/*
* Tests functions:
* relay_irc_send_channel_backlog
*/
TEST(RelayIrc, SendChannelBacklog)
{
/* TODO: write tests */
}
/*
* Tests functions:
* relay_irc_send_join
*/
TEST(RelayIrc, SendJoin)
{
/* TODO: write tests */
}
/*
* Tests functions:
* relay_irc_send_join_channels
*/
TEST(RelayIrc, SendJoinChannels)
{
/* TODO: write tests */
}
/*
* Tests functions:
* relay_irc_input_send
*/
TEST(RelayIrcWithClient, InputSend)
{
arraylist_clear (sent_messages_irc);
relay_irc_input_send (ptr_relay_client, "#test", "priority_high",
"this is a test");
CHECK_SENT_IRC("test;#test;priority_high;relay_client_1;this is a test");
}
/*
* Tests functions:
* relay_irc_hook_signals
*/
TEST(RelayIrc, HookSignals)
{
/* TODO: write tests */
}
/*
* Tests functions:
* relay_irc_capability_compare_cb
*/
TEST(RelayIrc, CapabilityCompareCb)
{
/* TODO: write tests */
}
/*
* Tests functions:
* relay_irc_capability_free_db
*/
TEST(RelayIrc, CapabilityFreeDb)
{
/* TODO: write tests */
}
/*
* Tests functions:
* relay_irc_cap_enabled
*/
TEST(RelayIrcWithClient, CapEnabled)
{
LONGS_EQUAL(0, relay_irc_cap_enabled (NULL, NULL));
LONGS_EQUAL(0, relay_irc_cap_enabled (NULL, "echo-message"));
LONGS_EQUAL(0, relay_irc_cap_enabled (ptr_relay_client, NULL));
LONGS_EQUAL(0, relay_irc_cap_enabled (ptr_relay_client, ""));
LONGS_EQUAL(0, relay_irc_cap_enabled (ptr_relay_client, "echo-message"));
hashtable_set (ptr_server->cap_list, "echo-message", NULL);
LONGS_EQUAL(1, relay_irc_cap_enabled (ptr_relay_client, "echo-message"));
hashtable_remove (ptr_server->cap_list, "echo-message");
LONGS_EQUAL(0, relay_irc_cap_enabled (ptr_relay_client, "echo-message"));
}
/*
* Tests functions:
* relay_irc_get_supported_caps
*/
TEST(RelayIrcWithClient, GetSupportedCaps)
{
int supported_caps;
supported_caps = relay_irc_get_supported_caps (ptr_relay_client);
LONGS_EQUAL(1 << RELAY_IRC_CAPAB_SERVER_TIME, supported_caps);
hashtable_set (ptr_server->cap_list, "echo-message", NULL);
supported_caps = relay_irc_get_supported_caps (ptr_relay_client);
LONGS_EQUAL((1 << RELAY_IRC_CAPAB_SERVER_TIME)
| (1 << RELAY_IRC_CAPAB_ECHO_MESSAGE),
supported_caps);
hashtable_remove (ptr_server->cap_list, "echo-message");
}
/*
* Tests functions:
* relay_irc_get_list_caps
*/
TEST(RelayIrc, RelayGetListCaps)
{
struct t_arraylist *list_caps;
int i, size;
list_caps = relay_irc_get_list_caps ();
CHECK(list_caps);
size = arraylist_size (list_caps);
LONGS_EQUAL(RELAY_IRC_NUM_CAPAB, size);
/* check that it's properly sorted */
for (i = 1; i < size; i++)
{
CHECK(strcmp ((const char *)arraylist_get (list_caps, i - 1),
(const char *)arraylist_get (list_caps, i)) < 0);
}
arraylist_free (list_caps);
}
/*
* Tests functions:
* relay_irc_recv_command_capab
*/
TEST(RelayIrcWithClient, RecvCommandCapab)
{
relay_client_set_status (ptr_relay_client, RELAY_STATUS_CONNECTING);
LONGS_EQUAL(0, RELAY_IRC_DATA(ptr_relay_client, server_capabilities));
LONGS_EQUAL(0, RELAY_IRC_DATA(ptr_relay_client, cap_ls_received));
LONGS_EQUAL(0, RELAY_IRC_DATA(ptr_relay_client, cap_end_received));
/* not enough parameters */
CLIENT_RECV(":alice!user@host CAP");
/* list supported capabilities */
CLIENT_RECV(":alice!user@host CAP LS");
CHECK_SENT_CLIENT(":weechat.relay.irc CAP nick LS :server-time");
LONGS_EQUAL(1, RELAY_IRC_DATA(ptr_relay_client, cap_ls_received));
LONGS_EQUAL(0, RELAY_IRC_DATA(ptr_relay_client, cap_end_received));
/* enable "echo-message" in IRC server and list supported capabilities */
hashtable_set (ptr_server->cap_list, "echo-message", NULL);
CLIENT_RECV(":alice!user@host CAP LS");
CHECK_SENT_CLIENT(":weechat.relay.irc CAP nick LS :echo-message server-time");
LONGS_EQUAL(1, RELAY_IRC_DATA(ptr_relay_client, cap_ls_received));
LONGS_EQUAL(0, RELAY_IRC_DATA(ptr_relay_client, cap_end_received));
hashtable_remove (ptr_server->cap_list, "echo-message");
/* request unknown capability: reject */
CLIENT_RECV(":alice!user@host CAP REQ unknown");
CHECK_SENT_CLIENT(":weechat.relay.irc CAP nick NAK :unknown");
LONGS_EQUAL(0, RELAY_IRC_DATA(ptr_relay_client, server_capabilities));
LONGS_EQUAL(0, RELAY_IRC_DATA(ptr_relay_client, cap_end_received));
/* request 1 supported capability: accept */
CLIENT_RECV(":alice!user@host CAP REQ server-time");
CHECK_SENT_CLIENT(":weechat.relay.irc CAP nick ACK :server-time");
CHECK(RELAY_IRC_DATA(ptr_relay_client, server_capabilities)
& (1 << RELAY_IRC_CAPAB_SERVER_TIME));
LONGS_EQUAL(0, RELAY_IRC_DATA(ptr_relay_client, cap_end_received));
RELAY_IRC_DATA(ptr_relay_client, server_capabilities) = 0;
/* request 2 supported capabilities: accept */
hashtable_set (ptr_server->cap_list, "echo-message", NULL);
CLIENT_RECV(":alice!user@host CAP REQ :server-time echo-message");
CHECK_SENT_CLIENT(":weechat.relay.irc CAP nick ACK :server-time echo-message");
CHECK(RELAY_IRC_DATA(ptr_relay_client, server_capabilities)
& ((1 << RELAY_IRC_CAPAB_SERVER_TIME)
| (1 << RELAY_IRC_CAPAB_ECHO_MESSAGE)));
LONGS_EQUAL(0, RELAY_IRC_DATA(ptr_relay_client, cap_end_received));
RELAY_IRC_DATA(ptr_relay_client, server_capabilities) = 0;
hashtable_remove (ptr_server->cap_list, "echo-message");
/* request unknown + supported capabilities: reject both */
CLIENT_RECV(":alice!user@host CAP REQ :server-time unknown");
CHECK_SENT_CLIENT(":weechat.relay.irc CAP nick NAK :server-time unknown");
LONGS_EQUAL(0, RELAY_IRC_DATA(ptr_relay_client, server_capabilities));
LONGS_EQUAL(0, RELAY_IRC_DATA(ptr_relay_client, cap_end_received));
/* request with empty list: end of capability negotiation */
CLIENT_RECV(":alice!user@host CAP REQ :");
CHECK_SENT_CLIENT(":weechat.relay.irc CAP nick NAK :");
LONGS_EQUAL(1, RELAY_IRC_DATA(ptr_relay_client, cap_end_received));
RELAY_IRC_DATA(ptr_relay_client, cap_end_received) = 0;
/* end capability negotiation */
CLIENT_RECV(":alice!user@host CAP END");
LONGS_EQUAL(1, RELAY_IRC_DATA(ptr_relay_client, cap_end_received));
}
/*
* Tests functions:
* relay_irc_parse_ctcp
*/
TEST(RelayIrcWithClient, ParseCtcp)
{
char *ctcp_type, *ctcp_params;
relay_irc_parse_ctcp (NULL, NULL, NULL);
relay_irc_parse_ctcp ("test", NULL, NULL);
ctcp_type = (char *)0x01;
ctcp_params = (char *)0x01;
relay_irc_parse_ctcp (NULL, &ctcp_type, &ctcp_params);
STRCMP_EQUAL(NULL, ctcp_type);
STRCMP_EQUAL(NULL, ctcp_params);
ctcp_type = (char *)0x01;
ctcp_params = (char *)0x01;
relay_irc_parse_ctcp ("\001ACTION is testing\001", &ctcp_type, &ctcp_params);
STRCMP_EQUAL("ACTION", ctcp_type);
STRCMP_EQUAL("is testing", ctcp_params);
free (ctcp_type);
free (ctcp_params);
ctcp_type = (char *)0x01;
ctcp_params = (char *)0x01;
relay_irc_parse_ctcp ("\001ACTION is testing \001 extra", &ctcp_type, &ctcp_params);
STRCMP_EQUAL("ACTION", ctcp_type);
STRCMP_EQUAL(" is testing ", ctcp_params);
free (ctcp_type);
free (ctcp_params);
ctcp_type = (char *)0x01;
ctcp_params = (char *)0x01;
relay_irc_parse_ctcp ("\001VERSION\001", &ctcp_type, &ctcp_params);
STRCMP_EQUAL("VERSION", ctcp_type);
STRCMP_EQUAL(NULL, ctcp_params);
free (ctcp_type);
ctcp_type = (char *)0x01;
ctcp_params = (char *)0x01;
relay_irc_parse_ctcp ("\001ACTION is testing", &ctcp_type, &ctcp_params);
STRCMP_EQUAL(NULL, ctcp_type);
STRCMP_EQUAL(NULL, ctcp_params);
ctcp_type = (char *)0x01;
ctcp_params = (char *)0x01;
relay_irc_parse_ctcp ("\001VERSION", &ctcp_type, &ctcp_params);
STRCMP_EQUAL(NULL, ctcp_type);
STRCMP_EQUAL(NULL, ctcp_params);
ctcp_type = (char *)0x01;
ctcp_params = (char *)0x01;
relay_irc_parse_ctcp ("test", &ctcp_type, &ctcp_params);
STRCMP_EQUAL(NULL, ctcp_type);
STRCMP_EQUAL(NULL, ctcp_params);
ctcp_type = (char *)0x01;
ctcp_params = (char *)0x01;
relay_irc_parse_ctcp ("", &ctcp_type, &ctcp_params);
STRCMP_EQUAL(NULL, ctcp_type);
STRCMP_EQUAL(NULL, ctcp_params);
}
/*
* Tests functions:
* relay_irc_recv
*/
TEST(RelayIrcWithClient, Recv)
{
relay_client_set_status (ptr_relay_client, RELAY_STATUS_CONNECTING);
/* NICK */
CLIENT_RECV("NICK alice");
STRCMP_EQUAL("alice", RELAY_IRC_DATA(ptr_relay_client, nick));
CLIENT_RECV("NICK bob");
STRCMP_EQUAL("bob", RELAY_IRC_DATA(ptr_relay_client, nick));
/* PASS */
LONGS_EQUAL(0, RELAY_IRC_DATA(ptr_relay_client, password_ok));
CLIENT_RECV("PASS invalid");
LONGS_EQUAL(RELAY_STATUS_CONNECTING, ptr_relay_client->status);
CLIENT_RECV("PASS secret");
LONGS_EQUAL(1, RELAY_IRC_DATA(ptr_relay_client, password_ok));
LONGS_EQUAL(RELAY_STATUS_CONNECTED, ptr_relay_client->status);
free (ptr_relay_client->protocol_args);
ptr_relay_client->protocol_args = NULL;
relay_client_set_status (ptr_relay_client, RELAY_STATUS_CONNECTING);
RELAY_IRC_DATA(ptr_relay_client, password_ok) = 0;
CLIENT_RECV("PASS test2:secret");
STRCMP_EQUAL("test2", ptr_relay_client->protocol_args);
LONGS_EQUAL(1, RELAY_IRC_DATA(ptr_relay_client, password_ok));
LONGS_EQUAL(RELAY_STATUS_CONNECTED, ptr_relay_client->status);
free (ptr_relay_client->protocol_args);
ptr_relay_client->protocol_args = strdup ("test");
/* USER */
relay_client_set_status (ptr_relay_client, RELAY_STATUS_CONNECTING);
CLIENT_RECV("USER alice 0 * :alice");
LONGS_EQUAL(1, RELAY_IRC_DATA(ptr_relay_client, user_received));
LONGS_EQUAL(1, RELAY_IRC_DATA(ptr_relay_client, connected));
STRCMP_EQUAL("alice", RELAY_IRC_DATA(ptr_relay_client, nick));
CHECK_SENT_CLIENT(":bob!proxy NICK :alice");
CHECK_SENT_CLIENT(":weechat.relay.irc 001 alice :Welcome to the "
"Internet Relay Chat Network alice!weechat@proxy");
/* JOIN */
CLIENT_RECV("JOIN #test");
CHECK_SENT_IRC("test;;priority_high;relay_client_1;/join #test");
/* PART */
CLIENT_RECV("PART #test");
CHECK_SENT_IRC("test;;priority_high;relay_client_1;/part #test");
/* PING */
CLIENT_RECV("PING :12345");
CHECK_SENT_CLIENT(":weechat.relay.irc PONG weechat.relay.irc :12345");
/* NOTICE */
CLIENT_RECV("NOTICE bob :a notice");
CHECK_SENT_IRC("test;;priority_high;relay_client_1;/notice bob a notice");
/* PRIVMSG to channel */
CLIENT_RECV("PRIVMSG #test :message to channel");
CHECK_SENT_IRC("test;#test;priority_high,user_message;relay_client_1;"
"message to channel");
/* PRIVMSG to user */
CLIENT_RECV("PRIVMSG bob :private message");
CHECK_SENT_IRC("test;;priority_high;relay_client_1;"
"/query bob private message");
/* WHOIS */
CLIENT_RECV("WHOIS bob");
CHECK_SENT_IRC("test;;priority_high;relay_client_1;/quote WHOIS bob");
}
/*
* Tests functions:
* relay_irc_close_connection
*/
TEST(RelayIrc, CloseConnection)
{
/* TODO: write tests */
}
/*
* Tests functions:
* relay_irc_alloc
*/
TEST(RelayIrc, Alloc)
{
/* TODO: write tests */
}
/*
* Tests functions:
* relay_irc_alloc_with_infolist
*/
TEST(RelayIrc, AllocWithInfolist)
{
/* TODO: write tests */
}
/*
* Tests functions:
* relay_irc_get_initial_status
*/
TEST(RelayIrc, GetInitialStatus)
{
/* TODO: write tests */
}
/*
* Tests functions:
* relay_irc_free
*/
TEST(RelayIrc, Free)
{
/* TODO: write tests */
}
/*
* Tests functions:
* relay_irc_add_to_infolist
*/
TEST(RelayIrc, AddToInfolist)
{
/* TODO: write tests */
}
/*
* Tests functions:
* relay_irc_print_log
*/
TEST(RelayIrc, PrintLog)
{
/* TODO: write tests */
}
|