summaryrefslogtreecommitdiff
path: root/tests/unit/plugins/irc/test-irc-nick.cpp
blob: a114e24f0a7df96d4e389419fa9c2535bca4d6e7 (plain)
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
/*
 * test-irc-nick.cpp - test IRC nick functions
 *
 * Copyright (C) 2019-2020 Sébastien Helleu <flashcode@flashtux.org>
 *
 * 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/>.
 */

#include "CppUTest/TestHarness.h"

extern "C"
{
#include "src/plugins/irc/irc-nick.h"
#include "src/plugins/irc/irc-server.h"
}

TEST_GROUP(IrcNick)
{
};

/*
 * Tests functions:
 *   irc_nick_valid
 */

TEST(IrcNick, Valid)
{
    /* TODO: write tests */
}

/*
 * Tests functions:
 *   irc_nick_is_nick
 */

TEST(IrcNick, IsNick)
{
    struct t_irc_server *server;

    /* no server, default utf8mapping = none */

    /* empty nick */
    LONGS_EQUAL(0, irc_nick_is_nick (NULL, NULL));
    LONGS_EQUAL(0, irc_nick_is_nick (NULL, ""));
    LONGS_EQUAL(0, irc_nick_is_nick (NULL, " "));

    /* invalid first char */
    LONGS_EQUAL(0, irc_nick_is_nick (NULL, "0abc"));
    LONGS_EQUAL(0, irc_nick_is_nick (NULL, "9abc"));
    LONGS_EQUAL(0, irc_nick_is_nick (NULL, "-abc"));

    /* invalid chars in nick */
    LONGS_EQUAL(0, irc_nick_is_nick (NULL, "nick test"));
    LONGS_EQUAL(0, irc_nick_is_nick (NULL, "nick,test"));
    LONGS_EQUAL(0, irc_nick_is_nick (NULL, "nick?test"));
    LONGS_EQUAL(0, irc_nick_is_nick (NULL, "nick!test"));
    LONGS_EQUAL(0, irc_nick_is_nick (NULL, "nick@test"));

    /* UTF-8 wide chars in nick */
    LONGS_EQUAL(0, irc_nick_is_nick (NULL, "noël"));
    LONGS_EQUAL(0, irc_nick_is_nick (NULL, "testé"));
    LONGS_EQUAL(0, irc_nick_is_nick (NULL, "\xf0\xa4\xad\xa2"));  /* han char */

    /* valid nicks */
    LONGS_EQUAL(1, irc_nick_is_nick (NULL, "tester"));
    LONGS_EQUAL(1, irc_nick_is_nick (NULL, "bob"));
    LONGS_EQUAL(1, irc_nick_is_nick (NULL, "alice"));
    LONGS_EQUAL(1, irc_nick_is_nick (NULL, "very_long_nick_which_is_valid"));

    /* server with utf8mapping = rfc8265, nicklen = 20 */
    server = irc_server_alloc ("my_ircd");
    CHECK(server);
    if (server->chantypes)
        free (server->chantypes);
    server->utf8mapping =IRC_SERVER_UTF8MAPPING_RFC8265;
    server->nick_max_length = 20;

    /* empty nick */
    LONGS_EQUAL(0, irc_nick_is_nick (server, NULL));
    LONGS_EQUAL(0, irc_nick_is_nick (server, ""));
    LONGS_EQUAL(0, irc_nick_is_nick (server, " "));

    /* nick too long */
    LONGS_EQUAL(0, irc_nick_is_nick (server, "long_nick___length_21"));

    /* valid nicks: first char allowed with utf8mapping = rfc8265 */
    LONGS_EQUAL(1, irc_nick_is_nick (server, "0abc"));
    LONGS_EQUAL(1, irc_nick_is_nick (server, "9abc"));
    LONGS_EQUAL(1, irc_nick_is_nick (server, "-abc"));

    /* invalid chars in nick */
    LONGS_EQUAL(0, irc_nick_is_nick (server, "nick test"));
    LONGS_EQUAL(0, irc_nick_is_nick (server, "nick,test"));
    LONGS_EQUAL(0, irc_nick_is_nick (server, "nick?test"));
    LONGS_EQUAL(0, irc_nick_is_nick (server, "nick!test"));
    LONGS_EQUAL(0, irc_nick_is_nick (server, "nick@test"));

    /* valid nicks: UTF-8 */
    LONGS_EQUAL(1, irc_nick_is_nick (server, "noël"));
    LONGS_EQUAL(1, irc_nick_is_nick (server, "testé"));
    LONGS_EQUAL(1, irc_nick_is_nick (server, "\xf0\xa4\xad\xa2")); /* han char */

    /* valid nicks with UTF-8 wide chars */
    LONGS_EQUAL(1, irc_nick_is_nick (server, "noël"));
    LONGS_EQUAL(1, irc_nick_is_nick (server, "testé"));

    /* valid nicks */
    LONGS_EQUAL(1, irc_nick_is_nick (server, "tester"));
    LONGS_EQUAL(1, irc_nick_is_nick (server, "bob"));
    LONGS_EQUAL(1, irc_nick_is_nick (server, "alice"));
    LONGS_EQUAL(1, irc_nick_is_nick (server, "long_nick__length_20"));

    /* max length: 4 bytes */
    server->nick_max_length = 4;

    /* invalid nick: 8 bytes */
    LONGS_EQUAL(0, irc_nick_is_nick (server, "\xf0\xa4\xad\xa2\xf0\xa4\xad\xa2"));

    /* valid nick: 4 bytes */
    LONGS_EQUAL(1, irc_nick_is_nick (server, "\xf0\xa4\xad\xa2")); /* han char */

    /* max length: 3 bytes */
    server->nick_max_length = 3;

    /* invalid nick: 4 bytes */
    LONGS_EQUAL(0, irc_nick_is_nick (server, "\xf0\xa4\xad\xa2")); /* han char */

    irc_server_free (server);
}