PageRenderTime 3ms CodeModel.GetById 44ms app.highlight 41ms RepoModel.GetById 1ms app.codeStats 0ms

/nodemailer/node_modules/simplesmtp/node_modules/rai/test/rai.js

https://github.com/bharat-daffodilsw/pajhwoklive
JavaScript | 750 lines | 601 code | 148 blank | 1 comment | 19 complexity | 9335c3d49489a359c1a1bf4441629693 MD5 | raw file
  1process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
  2
  3var RAIServer = require("../lib/rai").RAIServer,
  4    runClientMockup = require("../lib/rai").runClientMockup,
  5    testCase = require('nodeunit').testCase,
  6    utillib = require("util"),
  7    netlib = require("net"),
  8    crypto = require("crypto"),
  9    tlslib = require("tls");
 10
 11var PORT_NUMBER = 8397;
 12
 13// monkey patch net and tls to support nodejs 0.4
 14if(!netlib.connect && netlib.createConnection){
 15    netlib.connect = netlib.createConnection;
 16}
 17
 18if(!tlslib.connect && tlslib.createConnection){
 19    tlslib.connect = tlslib.createConnection;
 20}
 21
 22exports["General tests"] = {
 23    "Create and close a server": function(test){
 24        var server = new RAIServer();
 25        server.listen(PORT_NUMBER, function(err){
 26            test.ifError(err);
 27            server.end(function(){
 28                test.ok(1, "Server closed");
 29                test.done();
 30            });
 31        });
 32    },
 33    "Create a secure server": function(test){
 34        var server = new RAIServer({secureConnection: true});
 35        server.listen(PORT_NUMBER, function(err){
 36            test.ifError(err);
 37            server.end(function(){
 38                test.ok(1, "Server closed");
 39                test.done();
 40            });
 41        });
 42    },
 43    "Duplicate server fails": function(test){
 44        var server = new RAIServer();
 45        server.listen(PORT_NUMBER, function(err){
 46            test.ifError(err);
 47            
 48            var duplicate = new RAIServer();
 49            duplicate.listen(PORT_NUMBER, function(err){
 50                test.ok(err, "Responds with error");
 51                server.end(function(){
 52                    test.ok(1, "Server closed");
 53                    test.done();
 54                });
 55            });
 56            
 57        });
 58    },
 59    "Connection event": function(test){
 60        var server = new RAIServer();
 61        test.expect(3);
 62        server.listen(PORT_NUMBER, function(err){
 63            
 64            server.on("connect", function(socket){
 65                test.ok(socket, "Client connected");
 66                
 67                socket.on("end", function(){
 68                    test.ok(1, "Connection closed");
 69                    
 70                    server.end(function(){
 71                        test.done();
 72                    });
 73                });
 74                
 75                socket.on("error", function(err){
 76                    test.isError(err);
 77                });
 78            });
 79            
 80            var client = netlib.connect(PORT_NUMBER, function(){
 81                test.ok(1, "Connected to server");
 82                client.end();
 83            });
 84
 85        });
 86    },
 87    "Close client socket":  function(test){
 88        var server = new RAIServer();
 89        test.expect(4);
 90        server.listen(PORT_NUMBER, function(err){
 91            
 92            server.on("connect", function(socket){
 93                test.ok(socket, "Client connected");
 94                
 95                socket.on("end", function(){
 96                    test.ok(1, "Connection closed");
 97                    
 98                    server.end(function(){
 99                        test.done();
100                    });
101                });
102                
103                socket.on("error", function(err){
104                    test.isError(err);
105                });
106                
107                socket.end();
108            });
109            
110            var client = netlib.connect(PORT_NUMBER, function(){
111                test.ok(1, "Connected to server");
112            });
113            client.on("end", function(){
114                test.ok(1, "Connection closed by host");
115            });
116
117        });
118    },
119    "Send data to client":  function(test){
120        var server = new RAIServer();
121        server.listen(PORT_NUMBER, function(err){
122            
123            server.on("connect", function(socket){
124                
125                socket.send("HELLO");
126
127                socket.on("end", function(){
128                    server.end(function(){
129                        test.done();
130                    });
131                });
132                
133                socket.on("error", function(err){
134                    test.isError(err);
135                });
136            });
137            
138            var client = netlib.connect(PORT_NUMBER, function(){
139                client.on("data", function(chunk){
140                    test.equal(chunk.toString(), "HELLO\r\n");
141                    client.end();
142                });
143            });
144
145        });
146    }
147};
148
149exports["Secure connection"] = {
150    "STARTTLS with event":  function(test){
151        var server = new RAIServer();
152        server.listen(PORT_NUMBER, function(err){
153            
154            test.expect(2);
155            
156            server.on("connect", function(socket){
157                
158                socket.startTLS();
159                socket.on("tls", function(){
160                    test.ok(1, "Secure connection opened");
161                    socket.send("TEST");
162                });
163                
164                socket.on("end", function(){
165                    server.end(function(){
166                        test.done();
167                    });
168                });
169                
170                socket.on("error", function(err){
171                    test.isError(err);
172                });
173            });
174            
175            var client = netlib.connect(PORT_NUMBER, function(){
176                var sslcontext = crypto.createCredentials();
177                var pair = tlslib.createSecurePair(sslcontext, false);
178                
179                pair.encrypted.pipe(client);
180                client.pipe(pair.encrypted);
181                pair.fd = client.fd;
182                
183                pair.on("secure", function(){
184                    pair.cleartext.on("data", function(chunk){
185                        test.equal(chunk.toString(), "TEST\r\n");
186                        pair.cleartext.end();
187                    });
188                });
189            });
190
191        });
192    },
193    "STARTTLS Callback":  function(test){
194        var server = new RAIServer();
195        server.listen(PORT_NUMBER, function(err){
196            
197            test.expect(2);
198            
199            server.on("connect", function(socket){
200                
201                socket.startTLS(function(){
202                    test.ok(1, "Secure connection opened");
203                    socket.send("TEST");
204                });
205                
206                socket.on("tls", function(){
207                    test.ok(0, "Should not occur");
208                });
209                
210                socket.on("end", function(){
211                    server.end(function(){
212                        test.done();
213                    });
214                });
215                
216                socket.on("error", function(err){
217                    test.isError(err);
218                });
219            });
220            
221            var client = netlib.connect(PORT_NUMBER, function(){
222                var sslcontext = crypto.createCredentials();
223                var pair = tlslib.createSecurePair(sslcontext, false);
224                
225                pair.encrypted.pipe(client);
226                client.pipe(pair.encrypted);
227                pair.fd = client.fd;
228                
229                pair.on("secure", function(){
230                    pair.cleartext.on("data", function(chunk){
231                        test.equal(chunk.toString(), "TEST\r\n");
232                        pair.cleartext.end();
233                    });
234                });
235            });
236
237        });
238    },
239    "STARTTLS clears command buffer":  function(test){
240        var server = new RAIServer();
241        server.listen(PORT_NUMBER, function(err){
242            
243            test.expect(2);
244            
245            server.on("connect", function(socket){
246                
247                socket.on("command", function(command){
248                    if(command == "STARTTLS"){
249                        socket.startTLS();
250                        socket.send("OK");
251                    }else if(command == "KILL"){
252                        test.ok(0, "Should not occur");
253                    }else if(command == "OK"){
254                        test.ok(1, "OK");
255                    }
256                    
257                });
258                
259                socket.on("tls", function(){
260                    test.ok(1, "Secure connection opened");
261                    socket.send("TEST");
262                });
263                
264                socket.on("end", function(){
265                    server.end(function(){
266                        test.done();
267                    });
268                });
269                
270                socket.on("error", function(err){
271                    test.isError(err);
272                });
273            });
274            
275            var client = netlib.connect(PORT_NUMBER, function(){
276                
277                client.write("STARTTLS\r\nKILL\r\n");
278                
279                client.on("data", function(chunk){
280                    if(chunk.toString("utf-8").trim() == "OK"){
281                        var sslcontext = crypto.createCredentials();
282                        var pair = tlslib.createSecurePair(sslcontext, false);
283                        
284                        pair.encrypted.pipe(client);
285                        client.pipe(pair.encrypted);
286                        pair.fd = client.fd;
287                        
288                        pair.on("secure", function(){
289                            pair.cleartext.write("OK\r\n");
290                            pair.cleartext.end();
291                        });
292                    }
293                });
294                
295            });
296
297        });
298    },
299    "STARTTLS on secure server fails":  function(test){
300        var server = new RAIServer({secureConnection: true});
301        server.listen(PORT_NUMBER, function(err){
302            
303            test.expect(2);
304            server.on("connect", function(socket){
305                
306                socket.on("error", function(err){
307                    test.ok(err);
308                    socket.end();
309                    server.end(function(){
310                        test.done();
311                    });
312                });
313                
314                socket.on("command", (function(command){
315                    process.nextTick(socket.startTLS.bind(socket, function(){
316                        test.ok(false, "Secure connection opened"); // should not occur
317                        server.end(function(){
318                            test.done();
319                        });
320                    }));
321                    
322                }).bind(this));
323                
324                socket.on("tls", function(){
325                    test.ok(0, "Should not occur");
326                });
327                
328            });
329            
330            var client = tlslib.connect(PORT_NUMBER, function(){
331                test.ok(true);
332                client.write("HELLO!\r\n");
333            });
334
335        });
336    }
337};
338
339exports["Client commands"] = {
340    "Receive Simple Command":  function(test){
341        var server = new RAIServer();
342        server.listen(PORT_NUMBER, function(err){
343            
344            server.on("connect", function(socket){
345                
346                socket.on("command", function(command, payload){
347                    test.equal(command, "STATUS");
348                    test.equal(payload.toString(), "");
349                    socket.end();
350                    server.end(function(){
351                        test.done();
352                    });
353                });
354                
355                socket.on("error", function(err){
356                    test.isError(err);
357                });
358            });
359            
360            var client = netlib.connect(PORT_NUMBER, function(){
361                client.write("STATUS\r\n");
362            });
363
364        });
365    },
366    "Receive Command with payload":  function(test){
367        var server = new RAIServer();
368        server.listen(PORT_NUMBER, function(err){
369            
370            server.on("connect", function(socket){
371                
372                socket.on("command", function(command, payload){
373                    test.equal(command, "MAIL");
374                    test.equal(payload.toString(), "TO:");
375                    socket.end();
376                    
377                    server.end(function(){
378                        test.done();
379                    });
380                });
381                
382                socket.on("error", function(err){
383                    test.isError(err);
384                });
385            });
386            
387            var client = netlib.connect(PORT_NUMBER, function(){
388                client.write("MAIL TO:\r\n");
389            });
390
391        });
392    }
393};
394
395exports["Data mode"] = {
396    "DATA mode": function(test){
397        var server = new RAIServer(),
398            datapayload = "tere\r\nvana kere";
399        server.listen(PORT_NUMBER, function(err){
400            
401            server.on("connect", function(socket){
402                
403                socket.startDataMode();
404
405                test.expect(2);
406
407                socket.on("data", function(chunk){
408                    test.equal(datapayload, chunk.toString());
409                });
410                
411                socket.on("ready", function(){
412                    test.ok(1,"Data ready");
413                    server.end(function(){
414                        test.done();
415                    });
416                });
417                
418                socket.on("error", function(err){
419                    test.isError(err);
420                });
421            });
422            
423            var client = netlib.connect(PORT_NUMBER, function(){
424                client.write(datapayload+"\r\n.\r\n");
425                client.end();
426            });
427
428        });
429    },
430    "Small chunks DATA mode": function(test){
431        var server = new RAIServer(),
432            datapayload = "tere\r\nvana kere õäöü\r\n.\r",
433            databytes = [],
434            fullpayload = datapayload+"\r\n.\r\n";
435        server.listen(PORT_NUMBER, function(err){
436            
437            server.on("connect", function(socket){
438                
439                socket.startDataMode();
440
441                test.expect(1);
442
443                socket.on("data", function(chunk){
444                    databytes = databytes.concat(Array.prototype.slice.call(chunk));
445                });
446                
447                socket.on("ready", function(){
448                    test.equal(new Buffer(databytes).toString("utf-8"), datapayload);
449                    server.end(function(){
450                        test.done();
451                    });
452                });
453                
454                socket.on("error", function(err){
455                    test.isError(err);
456                });
457                
458                for(var i=0, len = fullpayload.length; i<len; i++){
459                    socket._onReceiveData(new Buffer(fullpayload.charAt(i), "utf-8").toString("binary"));
460                }
461                
462            });
463            
464            var client = netlib.connect(PORT_NUMBER, function(){
465                client.end();
466            });
467
468        });
469    }
470};
471
472exports["Pipelining support"] = {
473    "Pipelining": function(test){
474        var server = new RAIServer();
475        server.listen(PORT_NUMBER, function(err){
476            
477            test.expect(8);
478            
479            server.on("connect", function(socket){
480                
481                socket.on("command", function(command, payload){
482                    if(command == "STATUS"){
483                        test.ok(1, "Status received");
484                    }else if(command=="DATA"){
485                        test.ok(1, "data command received");
486                        socket.startDataMode();
487                    }else if(command=="END"){
488                        test.ok(1, "all received");
489                    }else{
490                        test.ok(0, "Unexpected command: "+command);
491                    }
492                });
493                
494                socket.on("data", function(chunk){
495                    test.equal(chunk.toString(), "TE\r\nST");
496                });
497                
498                socket.on("ready", function(){
499                    test.ok(1, "Data mode ended");
500                });
501                
502                socket.on("end", function(){
503                    test.ok(1, "All ready");
504                    server.end(function(){
505                        test.done();
506                    });
507                });
508                
509                socket.on("error", function(err){
510                    test.isError(err);
511                });
512            });
513            
514            var client = netlib.connect(PORT_NUMBER, function(){
515                client.write("STATUS\r\nSTATUS\r\nSTATUS\r\nDATA\r\nTE\r\nST\r\n.\r\nEND\r\n");
516                client.end();
517            });
518
519        });
520    }
521};
522
523exports["Timeout tests"] = {
524    "Timeout": function(test){
525        var server = new RAIServer({timeout: 300, disconnectOnTimeout: true});
526        test.expect(3);
527        server.listen(PORT_NUMBER, function(err){
528            
529            server.on("connect", function(socket){
530                test.ok(socket, "Client connected");
531                
532                socket.on("timeout", function(){
533                    test.ok(1, "Connection closed");
534                    
535                    server.end(function(){
536                        test.done();
537                    });
538                });
539                
540                socket.on("error", function(err){
541                    test.isError(err);
542                });
543            });
544            
545            var client = netlib.connect(PORT_NUMBER, function(){
546                test.ok(1, "Connected to server");
547            });
548
549        });
550    },
551    "Timeout with TLS":  function(test){
552        var server = new RAIServer({timeout: 300, disconnectOnTimeout: true});
553        server.listen(PORT_NUMBER, function(err){
554            
555            test.expect(3);
556            
557            server.on("connect", function(socket){
558                
559                socket.startTLS();
560                socket.on("tls", function(){
561                    test.ok(1, "Secure connection opened");
562                    socket.send("TEST");
563                });
564                
565                socket.on("timeout", function(){
566                    test.ok(1, "Timeout occurred");
567                    server.end(function(){
568                        test.done();
569                    });
570                });
571                
572                socket.on("error", function(err){
573                    test.isError(err);
574                });
575            });
576            
577            var client = netlib.connect(PORT_NUMBER, function(){
578                var sslcontext = crypto.createCredentials();
579                var pair = tlslib.createSecurePair(sslcontext, false);
580                
581                pair.encrypted.pipe(client);
582                client.pipe(pair.encrypted);
583                pair.fd = client.fd;
584                
585                pair.on("secure", function(){
586                    test.ok(1, "secure connection");
587                });
588            });
589
590        });
591    } 
592};
593
594exports["Client Mockup"] = {
595    "All command": function(test){
596        var server = new RAIServer();
597        server.listen(PORT_NUMBER, function(err){
598            server.on("connect", function(socket){
599                socket.send("220 Welcome");
600                socket.on("command", function(command, payload){
601                    switch(command) {
602                        case "HELO": socket.send("250 HI"); break;
603                        case "NOOP": socket.send("250 OK"); break;
604                        case "QUIT": socket.send("221 Bye"); socket.end(); break;
605                        default: socket.send("500");
606                    }
607                });
608
609                socket.on("error", function(err){
610                    test.isError(err);
611                });
612            });
613
614            var cmds = ["HELO", "NOOP", "QUIT"];
615            runClientMockup(PORT_NUMBER, "localhost", cmds, function(lastResponse, allResponses){
616                allResponses = allResponses.map(function(value) { return value.toString("utf-8"); });
617                test.deepEqual(allResponses, [ "220 Welcome\r\n", "250 HI\r\n", "250 OK\r\n", "221 Bye\r\n" ]);
618                server.end(function(){
619                    test.done();
620                });
621
622            });
623
624        });
625    },
626    "Last commands":  function(test){
627        var server = new RAIServer();
628        server.listen(PORT_NUMBER, function(err){
629            server.on("connect", function(socket){
630                socket.send("220 HI");
631                socket.on("command", function(command, payload){
632                    switch(command) {
633                        case "HELO": socket.send("250 HI"); break;
634                        case "NOOP": socket.send("250 OK"); break;
635                        case "QUIT": socket.send("221 Bye"); socket.end(); break;
636                        default: socket.send("500");
637                    }
638                });
639
640                socket.on("error", function(err){
641                    test.isError(err);
642                });
643            });
644
645            var cmds = ["HELO", "NOOP", "QUIT"];
646            runClientMockup(PORT_NUMBER, "localhost", cmds, function(lastResponse){
647                test.equal(lastResponse.toString("utf-8"), "221 Bye\r\n");
648                server.end(function(){
649                    test.done();
650                });
651
652            });
653
654        });
655    },
656    "All command(STARTTLS)": function(test){
657        var server = new RAIServer();
658        server.listen(PORT_NUMBER, function(err){
659
660            test.expect(2);
661
662            server.on("connect", function(socket){
663                socket.tlsStatus = 0;
664                socket.send("220 Welcome");
665                socket.on("command", function(command, payload){
666                    switch(command){
667                        case "EHLO":
668                            if(socket.tlsStatus===0){
669                                socket.send("250-HI\r\n250 STARTTLS");
670                            }else{
671                                socket.send("250 HI");
672                            }
673                            break;
674                        case "NOOP": socket.send("250 OK"); break;
675                        case "QUIT": socket.send("221 Bye"); socket.end(); break;
676                        case "STARTTLS": socket.startTLS(); socket.send("220 Go ahead"); break;
677                        default: socket.send("500");
678                    }
679                });
680
681                socket.on("tls", function(){
682                    test.ok(1, "Secure connection opened");
683                    socket.tlsStatus = 1;
684                });
685
686                socket.on("error", function(err){
687                    test.isError(err);
688                });
689            });
690
691            var cmds = ["EHLO", "STARTTLS", "EHLO", "NOOP", "QUIT"];
692            runClientMockup(PORT_NUMBER, "localhost", cmds, function(lastResponse, allResponses){
693                allResponses = allResponses.map(function(value) { return value.toString("utf-8"); });
694                test.deepEqual(allResponses, ["220 Welcome\r\n", "250-HI\r\n250 STARTTLS\r\n", "220 Go ahead\r\n",
695                                              "250 HI\r\n", "250 OK\r\n", "221 Bye\r\n" ]);
696                server.end(function(){
697                    test.done();
698                });
699
700            });
701
702        });
703    },
704    "Last commands(STARTTLS)":  function(test){
705        var server = new RAIServer();
706        server.listen(PORT_NUMBER, function(err){
707
708            test.expect(2);
709
710            server.on("connect", function(socket){
711                socket.tlsStatus = 0;
712                socket.send("220 Welcome");
713                socket.on("command", function(command, payload){
714                    switch(command){
715                        case "EHLO":
716                            if(socket.tlsStatus===0){
717                                socket.send("250-HI\r\n250 STARTTLS");
718                            }else{
719                                socket.send("250 HI");
720                            }
721                            break;
722                        case "NOOP": socket.send("250 OK"); break;
723                        case "QUIT": socket.send("221 Bye"); socket.end(); break;
724                        case "STARTTLS": socket.startTLS(); socket.send("220 Go ahead"); break;
725                        default: socket.send("500");
726                    }
727                });
728
729                socket.on("tls", function(){
730                    test.ok(1, "Secure connection opened");
731                    socket.tlsStatus = 1;
732                });
733
734                socket.on("error", function(err){
735                    test.isError(err);
736                });
737            });
738
739            var cmds = ["EHLO", "STARTTLS", "EHLO", "NOOP", "QUIT"];
740            runClientMockup(PORT_NUMBER, "localhost", cmds, function(lastResponse){
741                test.equal(lastResponse.toString("utf-8"), "221 Bye\r\n");
742                server.end(function(){
743                    test.done();
744                });
745
746            });
747
748        });
749    }
750};