1313#include <stdio.h>
1414
1515#include "mesode.h"
16+ #include "rand.h"
1617#include "resolver.h"
1718#include "test.h"
1819
@@ -84,7 +85,6 @@ static const unsigned char data3[] = {
8485 0x6f , 0x6f , 0x67 , 0x6c , 0x65 , 0x03 , 0x63 , 0x6f ,
8586 0x6d , 0x00 ,
8687};
87-
8888/* res_query("_xmpp-client._tcp.jabber.calyxinstitute.org", C_IN, T_SRV, ...) */
8989static const unsigned char data4 [] = {
9090 0x8d , 0x58 , 0x81 , 0x80 , 0x00 , 0x01 , 0x00 , 0x02 , 0x00 , 0x00 , // .X........
@@ -103,6 +103,34 @@ static const unsigned char data4[] = {
103103 0x61 , 0x6c , 0x79 , 0x78 , 0x69 , 0x6e , 0x73 , 0x74 , 0x69 , 0x74 , // alyxinstit
104104 0x75 , 0x74 , 0x65 , 0x03 , 0x6f , 0x72 , 0x67 , 0x00 , // ute.org.
105105};
106+ /* res_query("_xmpp-client._tcp.jabber.org", C_IN, T_SRV, ...) with pointers */
107+ static const unsigned char data5 [] = {
108+ 0x00 , 0x00 , 0x81 , 0x80 , 0x00 , 0x01 , 0x00 , 0x02 , 0x00 , 0x00 , // ..........
109+ 0x00 , 0x00 , 0x0c , 0x5f , 0x78 , 0x6d , 0x70 , 0x70 , 0x2d , 0x63 , // ..._xmpp-c
110+ 0x6c , 0x69 , 0x65 , 0x6e , 0x74 , 0x04 , 0x5f , 0x74 , 0x63 , 0x70 , // lient._tcp
111+ 0x06 , 0x6a , 0x61 , 0x62 , 0x62 , 0x65 , 0x72 , 0x03 , 0x6f , 0x72 , // .jabber.or
112+ 0x67 , 0x00 , 0x00 , 0x21 , 0x00 , 0x01 , 0xc0 , 0x0c , 0x00 , 0x21 , // g..!.....!
113+ 0x00 , 0x01 , 0x00 , 0x00 , 0x01 , 0x9e , 0x00 , 0x12 , 0x00 , 0x1f , // ..........
114+ 0x00 , 0x1e , 0x14 , 0x66 , 0x09 , 0x68 , 0x65 , 0x72 , 0x6d , 0x65 , // ...f.herme
115+ 0x73 , 0x32 , 0x76 , 0x36 , 0xc0 , 0x1e , 0xc0 , 0x0c , 0x00 , 0x21 , // s2v6.....!
116+ 0x00 , 0x01 , 0x00 , 0x00 , 0x01 , 0x9e , 0x00 , 0x10 , 0x00 , 0x1e , // ..........
117+ 0x00 , 0x1e , 0x14 , 0x66 , 0x07 , 0x68 , 0x65 , 0x72 , 0x6d , 0x65 , // ...f.herme
118+ 0x73 , 0x32 , 0xc0 , 0x1e , // s2..
119+ };
120+ /* hacked data2 with two empty-string targets. */
121+ static const unsigned char data6 [] = {
122+ 0xf2 , 0x98 , 0x81 , 0x80 , 0x00 , 0x01 , 0x00 , 0x02 ,
123+ 0x00 , 0x00 , 0x00 , 0x00 , 0x0c , 0x5f , 0x78 , 0x6d ,
124+ 0x70 , 0x70 , 0x2d , 0x63 , 0x6c , 0x69 , 0x65 , 0x6e ,
125+ 0x74 , 0x04 , 0x5f , 0x74 , 0x63 , 0x70 , 0x06 , 0x6a ,
126+ 0x61 , 0x62 , 0x62 , 0x65 , 0x72 , 0x03 , 0x6f , 0x72 ,
127+ 0x67 , 0x00 , 0x00 , 0x21 , 0x00 , 0x01 , 0xc0 , 0x0c ,
128+ 0x00 , 0x21 , 0x00 , 0x01 , 0x00 , 0x00 , 0x03 , 0x83 ,
129+ 0x00 , 0x07 , 0x00 , 0x1e , 0x00 , 0x1e , 0x14 , 0x66 ,
130+ 0x00 , 0xc0 , 0x0c , 0x00 , 0x21 , 0x00 , 0x01 , 0x00 ,
131+ 0x00 , 0x03 , 0x83 , 0x00 , 0x08 , 0x00 , 0x1f , 0x00 ,
132+ 0x1e , 0x14 , 0x66 , 0xc0 , 0x40 ,
133+ };
106134
107135static const struct {
108136 const unsigned char * data ;
@@ -139,22 +167,38 @@ static const struct {
139167 .port = 5222 ,
140168 .target_nr = 2 ,
141169 },
170+ {
171+ .data = data5 ,
172+ .len = sizeof (data5 ),
173+ .target = "hermes2.jabber.org" ,
174+ .port = 5222 ,
175+ .target_nr = 2 ,
176+ },
177+ {
178+ .data = data6 ,
179+ .len = sizeof (data6 ),
180+ .target = "" ,
181+ .port = 5222 ,
182+ .target_nr = 2 ,
183+ },
142184};
143185
144186static int srv_rr_list_len (resolver_srv_rr_t * list )
145187{
146188 int nr ;
147189
148- for (nr = 0 ; list != NULL ; ++ nr , list = list -> next );
190+ for (nr = 0 ; list != NULL ; ++ nr , list = list -> next );
149191
150192 return nr ;
151193}
152194
153195int main (int argc , char * * argv )
154196{
155197 xmpp_ctx_t * ctx ;
198+ xmpp_rand_t * rand ;
156199 resolver_srv_rr_t * srv_rr_list ;
157200 char * domain ;
201+ unsigned char * buf ;
158202 unsigned short port ;
159203 size_t i ;
160204 int ret ;
@@ -187,6 +231,25 @@ int main(int argc, char **argv)
187231 resolver_srv_free (ctx , srv_rr_list );
188232 }
189233
234+ /*
235+ * The next test case must not crash and is supposed to be checked
236+ * under valgrind.
237+ */
238+ printf ("Test of a broken message: " );
239+ rand = xmpp_rand_new (ctx );
240+ assert (rand != NULL );
241+ assert (sizeof (data2 ) > 64 );
242+ buf = xmpp_alloc (ctx , sizeof (data2 ));
243+ assert (buf != NULL );
244+ memcpy (buf , data2 , 64 );
245+ xmpp_rand_bytes (rand , & buf [64 ], sizeof (data2 ) - 64 );
246+ ret = resolver_srv_lookup_buf (ctx , buf , sizeof (data2 ), & srv_rr_list );
247+ if (ret == XMPP_DOMAIN_FOUND && srv_rr_list != NULL )
248+ resolver_srv_free (ctx , srv_rr_list );
249+ xmpp_free (ctx , buf );
250+ xmpp_rand_free (ctx , rand );
251+ printf ("ok\n" );
252+
190253 xmpp_ctx_free (ctx );
191254
192255 return 0 ;
0 commit comments