1 #include "config.h"
2
3 #include <string.h>
4 #include <stdio.h>
5
6 #include <telepathy-glib/telepathy-glib.h>
7
8 #include <idle-text.h>
9 #include <idle-connection.h>
10
11 #define fail(message, ...) \
12 G_STMT_START \
13 { \
14 fprintf (stderr, message "\n", ##__VA_ARGS__); \
15 fprintf (stderr, "- msg: %s\n", g_strescape (msg, "")); \
16 if (i >= 0) \
17 fprintf (stderr, "- line #%d: %s\n", i, g_strescape (line, "")); \
18 fprintf (stderr, "- type: %d\n", type); \
19 return FALSE; \
20 } \
21 G_STMT_END
22
23
24 static gboolean
test(TpChannelTextMessageType type,gchar * msg)25 test (TpChannelTextMessageType type,
26 gchar *msg)
27 {
28 gchar *recipient = "ircuser";
29 gchar **bodies;
30 gchar **output = idle_text_encode_and_split (type, recipient, msg, 510, &bodies, NULL);
31 GString *reconstituted_msg = g_string_sized_new (strlen (msg));
32 int i = -1;
33 char *line = NULL, *c = NULL;
34
35 char *expected_prefixes[3] = {
36 "PRIVMSG ",
37 "PRIVMSG ",
38 "NOTICE ",
39 };
40
41 char *expected_infixes[3] = {
42 " :",
43 " :\001ACTION ",
44 " :",
45 };
46
47 char *expected_suffixes[3] = {
48 "",
49 "\001",
50 "",
51 };
52
53 if (output == NULL)
54 {
55 fail ("total reality failure, idle_text_encode_and_split returned NULL");
56 }
57
58 for (i = 0; output[i] != NULL; i++)
59 {
60 line = output[i];
61 c = line;
62
63 if (strlen (line) > IRC_MSG_MAXLEN)
64 {
65 fail ("resulting line longer than maximum length %d", IRC_MSG_MAXLEN);
66 }
67
68 if (!g_str_has_prefix (c, expected_prefixes[type]))
69 {
70 fail ("resulting line missing prefix '%s'",
71 g_strescape (expected_prefixes[type], ""));
72 }
73 c += strlen (expected_prefixes[type]);
74
75 if (!g_str_has_prefix (c, recipient))
76 {
77 fail ("resulting line missing recipient");
78 }
79 c += strlen (recipient);
80
81 if (!g_str_has_prefix (c, expected_infixes[type]))
82 {
83 fail ("resulting line missing infix '%s'",
84 g_strescape (expected_infixes[type], ""));
85 }
86 c += strlen (expected_infixes[type]);
87
88 if (!g_str_has_suffix (c, expected_suffixes[type]))
89 {
90 fail ("resulting line missing suffix '%s'",
91 g_strescape (expected_suffixes[type], ""));
92 }
93
94 if (strncmp (c, bodies[i], strlen (c) - strlen (expected_suffixes[type])))
95 {
96 fail ("body of '%s' doesn't match alleged body '%s'", c, bodies[i]);
97 }
98
99 g_string_append_len (reconstituted_msg, c,
100 strlen (c) - strlen (expected_suffixes[type]));
101 }
102
103 i = -1;
104
105 {
106 /* Remove newlines from original string; you can't tell whether the string
107 * was split because of length or because of newlines, but as long as the
108 * result is the same modulo newlines that's okay.
109 */
110 gchar **lines = g_strsplit (msg, "\n", 0);
111 gchar *newlineless = g_strjoinv ("", lines);
112 g_strfreev (lines);
113
114 if (tp_strdiff (reconstituted_msg->str, newlineless))
115 {
116 fail ("recombining split message yielded a different string\n"
117 "- result: \"%s\"\n- msg sans newlines: \"%s\"",
118 reconstituted_msg->str, newlineless);
119 }
120 }
121
122 return TRUE;
123 }
124
125
126 int
main(int argc,char ** argv)127 main (int argc,
128 char **argv)
129 {
130 gchar *msgs[] = {
131 "This is a short message.",
132 "This message\ncontains newlines.",
133 "one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty twenty-one twenty-two twenty-three twenty-four twenty-five twenty-six twenty-seven twenty-eight twenty-nine thirty thirty-one thirty-two thirty-three thirty-four thirty-five thirty-six thirty-seven thirty-eight thirty-nine forty forty-one forty-two forty-three forty-four forty-five forty-six forty-seven forty-eight forty-nine fifty fifty-one fifty-two fifty-three fifty-four fifty-five fifty-six fifty-seven fifty-eight fifty-nine sixty sixty-one sixty-two sixty-three sixty-four sixty-five sixty-six sixty-seven sixty-eight sixty-nine",
134 "one two three four\nfive six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty twenty-one twenty-two twenty-three twenty-four twenty-five twenty-six twenty-seven twenty-eight twenty-nine thirty thirty-one thirty-two thirty-three thirty-four thirty-five thirty-six thirty-seven thirty-eight thirty-nine forty forty-one forty-two forty-three forty-four forty-five forty-six forty-seven forty-eight forty-nine fifty fifty-one fifty-two fifty-three fifty-four fifty-five fifty-six fifty-seven fifty-eight fifty-nine sixty sixty-one sixty-two sixty-three sixty-four sixty-five sixty-six sixty-seven sixty-eight sixty-nine",
135 NULL
136 };
137 gboolean sad_face = FALSE;
138
139 for (int i = 0; msgs[i] != NULL; i++)
140 {
141 for (TpChannelTextMessageType j = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
142 j <= TP_CHANNEL_TEXT_MESSAGE_TYPE_NOTICE;
143 j++)
144 {
145 gboolean yay = test(j, msgs[i]);
146 if (!yay)
147 sad_face = TRUE;
148 }
149 }
150
151 if (sad_face)
152 {
153 fprintf (stderr, " :'(\n");
154 return 1;
155 }
156
157 return 0;
158 }
159