Skip to content
This repository
  • 3 commits
  • 12 files changed
  • 0 comments
  • 1 contributor
7 History.md
Source Rendered
... ... @@ -1,3 +1,10 @@
  1 +0.0.15 / 2012-07-04
  2 +
  3 + * Default channels (Sebastian A. Espindola)
  4 + * Migrated tests to Mocha
  5 + * Tested against Node 0.8
  6 + * Locked down module versions
  7 +
1 8 0.0.14 / 2012-06-05
2 9
3 10 * Added connection passwords (PASS)
2  Makefile
... ... @@ -1,4 +1,4 @@
1 1 test:
2   - ./node_modules/.bin/nodeunit test/*.test.js
  2 + ./node_modules/.bin/mocha --reporter list -c --ui exports test/*.test.js
3 3
4 4 .PHONY: test
1  README.textile
Source Rendered
@@ -83,4 +83,3 @@ GNU General Public License for more details.
83 83
84 84 You should have received a copy of the GNU General Public License
85 85 along with this program. If not, see "http://www.gnu.org/licenses/":http://www.gnu.org/licenses/.
86   -
26 lib/server.js
@@ -34,8 +34,7 @@ var net = require('net'),
34 34 EventEmitter = require('events').EventEmitter,
35 35 serverCommands = require('./commands'),
36 36 winston = require('winston'),
37   - commander = require('commander'),
38   - server;
  37 + commander = require('commander');
39 38
40 39 function AbstractConnection(stream) {
41 40 this.stream = stream;
@@ -142,10 +141,10 @@ Server.prototype = {
142 141 },
143 142
144 143 valueExists: function(value, collection, field) {
145   - var context = this;
146   - value = context.normalizeName(value);
  144 + var self = this;
  145 + value = this.normalizeName(value);
147 146 return collection.some(function(u) {
148   - return context.normalizeName(u[field]) === value;
  147 + return self.normalizeName(u[field]) === value;
149 148 })
150 149 },
151 150
@@ -204,11 +203,11 @@ Server.prototype = {
204 203 },
205 204
206 205 createDefaultChannels: function() {
207   - var context = this;
  206 + var self = this;
208 207 if (this.config.channels) {
209 208 Object.keys(this.config.channels).forEach(function(channel) {
210   - var newChan = context.channels.registered[context.normalizeName(channel)] = new Channel(channel, context);
211   - newChan.topic = context.config.channels[channel].topic;
  209 + var newChannel = self.channels.registered[self.normalizeName(channel)] = new Channel(channel, self);
  210 + newChannel.topic = self.config.channels[channel].topic;
212 211 });
213 212 }
214 213 },
@@ -239,19 +238,18 @@ Server.prototype = {
239 238 },
240 239
241 240 start: function(callback) {
242   - var server = this;
  241 + var server = this, key, cert, options;
243 242
244 243 if (this.config.key && this.config.cert) {
245 244 try {
246   - var key = fs.readFileSync(this.config.key);
247   - var cert = fs.readFileSync(this.config.cert);
  245 + key = fs.readFileSync(this.config.key);
  246 + cert = fs.readFileSync(this.config.cert);
248 247 } catch (exception) {
249 248 winston.error('Fatal error:', exception);
250 249 }
251   - var options = { key: key, cert: cert };
  250 + options = { key: key, cert: cert };
252 251 this.server = tls.createServer(options, handleStream);
253   - }
254   - else {
  252 + } else {
255 253 this.server = net.createServer(handleStream);
256 254 }
257 255
10 package.json
... ... @@ -1,7 +1,7 @@
1 1 {
2 2 "name": "ircdjs",
3 3 "description": "An IRCD for Node",
4   - "version": "0.0.14",
  4 + "version": "0.0.15",
5 5 "homepage": "https://github.com/alexyoung/ircd.js",
6 6 "author": "Alex R. Young (http://alexyoung.org)",
7 7 "directories": {
@@ -19,12 +19,12 @@
19 19 "dependencies": {
20 20 "carrier": ">= 0.0.3",
21 21 "bcrypt": ">= 0.4.0",
22   - "winston": "latest",
23   - "commander": "latest"
  22 + "winston": "0.6.1",
  23 + "commander": "git://github.com/visionmedia/commander.js.git"
24 24 },
25 25 "devDependencies": {
26   - "nodeunit": "0.6.2",
27   - "irc": "latest"
  26 + "mocha": "1.2.2",
  27 + "irc": "0.3.4"
28 28 },
29 29 "bin": {
30 30 "ircdjs-pwgen": "bin/pwgen.js",
295 test/channels.test.js
... ... @@ -1,169 +1,182 @@
1   -var assert = require('assert'),
2   - helpers = require('./helpers'),
3   - createClient = helpers.createClient,
4   - testCase = require('nodeunit').testCase;
  1 +var assert = require('assert')
  2 + , helpers = require('./helpers')
  3 + ;
5 4
6 5 module.exports = {
7   - setUp: function(done) {
8   - helpers.createServer(done);
9   - },
10   -
11   - tearDown: function(done) {
12   - helpers.close(done);
13   - },
14   -
15   - 'test bad join (#22)': function(test) {
16   - // Create two clients
17   - createClient({ nick: 'testbot1', channel: '#test' }, function(testbot1) {
18   - createClient({ nick: 'testbot2', channel: '#test' }, function(testbot2) {
19   -
20   - testbot1.on('error', function(message) {
21   - if (message.command === 'err_needmoreparams') {
22   - testbot1.disconnect();
23   - testbot2.disconnect();
24   - test.done();
25   - }
26   - });
  6 + 'Channels': {
  7 + beforeEach: function(done) {
  8 + this.server = new helpers.MockServer(done, false, 6660);
  9 + },
27 10
28   - testbot1.on('raw', function(data) {
29   - if (data.command === 'JOIN') {
30   - testbot1.send('join');
31   - }
  11 + afterEach: function(done) {
  12 + this.server.close(done);
  13 + },
  14 +
  15 + 'test bad join (#22)': function(done) {
  16 + // Create two clients
  17 + var createClient = this.server.createClient.bind(this.server);
  18 +
  19 + createClient({ nick: 'testbot1', channel: '#test' }, function(testbot1) {
  20 + createClient({ nick: 'testbot2', channel: '#test' }, function(testbot2) {
  21 +
  22 + testbot1.on('error', function(message) {
  23 + if (message.command === 'err_needmoreparams') {
  24 + testbot1.disconnect();
  25 + testbot2.disconnect();
  26 + done();
  27 + }
  28 + });
  29 +
  30 + testbot1.on('raw', function(data) {
  31 + if (data.command === 'JOIN') {
  32 + testbot1.send('join');
  33 + }
  34 + });
32 35 });
33 36 });
34   - });
35   - },
36   -
37   - 'test messaging a non-existent channel (#26)': function(test) {
38   - // Create two clients
39   - createClient({ nick: 'testbot1', channel: '#test' }, function(testbot1) {
40   - createClient({ nick: 'testbot2', channel: '#test' }, function(testbot2) {
41   - testbot1.on('error', function(message) {
42   - if (message.command === 'err_nosuchnick') {
43   - testbot1.disconnect();
44   - testbot2.disconnect();
45   - test.done();
46   - }
  37 + },
  38 +
  39 + 'test messaging a non-existent channel (#26)': function(done) {
  40 + var createClient = this.server.createClient.bind(this.server);
  41 +
  42 + // Create two clients
  43 + createClient({ nick: 'testbot1', channel: '#test' }, function(testbot1) {
  44 + createClient({ nick: 'testbot2', channel: '#test' }, function(testbot2) {
  45 + testbot1.on('error', function(message) {
  46 + if (message.command === 'err_nosuchnick') {
  47 + testbot1.disconnect();
  48 + testbot2.disconnect();
  49 + done();
  50 + }
  51 + });
  52 +
  53 + testbot1.say('#error', 'Hello');
47 54 });
48   -
49   - testbot1.say('#error', 'Hello');
50 55 });
51   - });
52   - },
53   -
54   - 'remove channels when the last person leaves (#25)': function(test) {
55   - // Create two clients
56   - createClient({ nick: 'testbot1', channel: '#test' }, function(testbot1) {
57   - function done() {
58   - testbot1.disconnect();
59   - test.done();
60   - }
  56 + },
61 57
62   - var seenList = false;
  58 + 'remove channels when the last person leaves (#25)': function(done) {
  59 + var createClient = this.server.createClient.bind(this.server);
63 60
64   - testbot1.on('raw', function(data) {
65   - // Double equal, because this is returned as a string but could easily
66   - // be returned as an integer if the IRC client library changes
67   - if (data.rawCommand == 322) {
68   - if (seenList) {
69   - assert.fail('Channels should be deleted');
70   - } else {
71   - assert.equal(data.args[1], '#test', 'The #test channel should be returned by LIST');
72   -
73   - // Now part the channel
74   - testbot1.part('#test');
75   - }
76   - } else if (data.rawCommand == 323 && !seenList) {
77   - seenList = true;
78   - } else if (data.rawCommand == 323 && seenList) {
  61 + // Create two clients
  62 + createClient({ nick: 'testbot1', channel: '#test' }, function(testbot1) {
  63 + function teardown() {
  64 + testbot1.disconnect();
79 65 done();
80   - } else if (data.command === 'PART') {
81   - testbot1.send('LIST');
82 66 }
83   - });
84 67
85   - // Send a list command
86   - testbot1.send('LIST');
87   - });
88   - },
  68 + var seenList = false;
89 69
90   - 'simultaneous user simulation': function(test) {
91   - var nicks = [], i;
92   - for (i = 1; i <= 100; i++) {
93   - nicks.push('user_' + i);
94   - }
95   -
96   - function assertReceive(bots, assertion, fn) {
97   - bots[0].say(bots[1].nick, assertion);
  70 + testbot1.on('raw', function(data) {
  71 + // Double equal, because this is returned as a string but could easily
  72 + // be returned as an integer if the IRC client library changes
  73 + if (data.rawCommand == 322) {
  74 + if (seenList) {
  75 + assert.fail('Channels should be deleted');
  76 + } else {
  77 + assert.equal(data.args[1], '#test', 'The #test channel should be returned by LIST');
  78 +
  79 + // Now part the channel
  80 + testbot1.part('#test');
  81 + }
  82 + } else if (data.rawCommand == 323 && !seenList) {
  83 + seenList = true;
  84 + } else if (data.rawCommand == 323 && seenList) {
  85 + teardown();
  86 + } else if (data.command === 'PART') {
  87 + testbot1.send('LIST');
  88 + }
  89 + });
98 90
99   - var callback = function(from, to, message) {
100   - assert.equal(assertion, message);
101   - bots[1].removeListener('message', callback);
102   - fn();
103   - };
  91 + // Send a list command
  92 + testbot1.send('LIST');
  93 + });
  94 + },
104 95
105   - bots[1].on('message', callback);
106   - }
  96 + 'simultaneous user simulation': function(done) {
  97 + var nicks = [], i;
107 98
108   - helpers.createClients(nicks, '#test', function(bots) {
109   - function done() {
110   - bots.forEach(function(bot) {
111   - bot.disconnect();
112   - });
113   - test.done();
  99 + for (i = 1; i <= 100; i++) {
  100 + nicks.push('user_' + i);
114 101 }
115 102
116   - var tested = 0, max = bots.length - 1;
117   - for (var i = 0; i < max; i++) {
118   - assertReceive([bots[i], bots[i + 1]], 'Message ' + Math.random(), function() {
119   - tested++;
120   - if (tested === max) {
121   - done();
122   - }
123   - });
  103 + function assertReceive(bots, assertion, fn) {
  104 + bots[0].say(bots[1].nick, assertion);
  105 +
  106 + var callback = function(from, to, message) {
  107 + assert.equal(assertion, message);
  108 + bots[1].removeListener('message', callback);
  109 + fn();
  110 + };
  111 +
  112 + bots[1].on('message', callback);
124 113 }
125   - });
126   - },
127   -
128   - 'test join with invalid key': function(test) {
129   - createClient({ nick: 'testbot1', channel: '#test' }, function(testbot1) {
130   - // Set the channel key to 'test'
131   - testbot1.send('MODE #test +k test');
132   - testbot1.on('raw', function(data) {
133   - if (data.rawCommand === '324') {
134   - createClient({ nick: 'testbot2', channel: '#test2' }, function(testbot2) {
135   - testbot2.on('error', function(message) {
136   - assert.equal(message.rawCommand, '475', 'Should receive a bad channel key');
137   - testbot1.disconnect();
138   - testbot2.disconnect();
139   - test.done();
140   - });
141   -
142   - // Join without the correct key
143   - testbot2.send('JOIN #test');
  114 +
  115 + this.server.createClients(nicks, '#test', function(bots) {
  116 + function teardown() {
  117 + bots.forEach(function(bot) {
  118 + bot.disconnect();
  119 + });
  120 + done();
  121 + }
  122 +
  123 + var tested = 0, max = bots.length - 1;
  124 + for (var i = 0; i < max; i++) {
  125 + assertReceive([bots[i], bots[i + 1]], 'Message ' + Math.random(), function() {
  126 + tested++;
  127 + if (tested === max) {
  128 + teardown();
  129 + }
144 130 });
145 131 }
146 132 });
147   - });
148   - },
149   -
150   - 'test join with valid key': function(test) {
151   - createClient({ nick: 'testbot1', channel: '#test' }, function(testbot1) {
152   - // Set the channel key to 'password'
153   - testbot1.send('MODE #test +k password');
154   - testbot1.on('raw', function(data) {
155   - if (data.rawCommand === '324') {
156   - createClient({ nick: 'testbot2', channel: '#test password' }, function(testbot2) {
157   - testbot2.on('raw', function(data) {
158   - if (data.rawCommand === '324') {
  133 + },
  134 +
  135 + 'test join with invalid key': function(done) {
  136 + var createClient = this.server.createClient.bind(this.server);
  137 +
  138 + createClient({ nick: 'testbot1', channel: '#test' }, function(testbot1) {
  139 + // Set the channel key to 'test'
  140 + testbot1.send('MODE #test +k test');
  141 + testbot1.on('raw', function(data) {
  142 + if (data.rawCommand === '324') {
  143 + createClient({ nick: 'testbot2', channel: '#test2' }, function(testbot2) {
  144 + testbot2.on('error', function(message) {
  145 + assert.equal(message.rawCommand, '475', 'Should receive a bad channel key');
159 146 testbot1.disconnect();
160 147 testbot2.disconnect();
161   - test.done();
162   - }
  148 +
  149 + done();
  150 + });
  151 +
  152 + // Join without the correct key
  153 + testbot2.send('JOIN #test');
163 154 });
164   - });
165   - }
  155 + }
  156 + });
  157 + });
  158 + },
  159 +
  160 + 'test join with valid key': function(done) {
  161 + var createClient = this.server.createClient.bind(this.server);
  162 +
  163 + createClient({ nick: 'testbotv', channel: '#test' }, function(testbot1) {
  164 + // Set the channel key to 'password'
  165 + testbot1.send('MODE #test +k password');
  166 + testbot1.on('raw', function(data) {
  167 + if (data.rawCommand === '324') {
  168 + createClient({ nick: 'testbot2', channel: '#test password' }, function(testbot2) {
  169 + testbot2.on('raw', function(data) {
  170 + if (data.rawCommand === '324') {
  171 + testbot1.disconnect();
  172 + testbot2.disconnect();
  173 + done();
  174 + }
  175 + });
  176 + });
  177 + }
  178 + });
166 179 });
167   - });
  180 + }
168 181 }
169 182 };
319 test/clients.test.js
... ... @@ -1,173 +1,190 @@
1   -var assert = require('assert'),
2   - helpers = require('./helpers.js'),
3   - createClient = helpers.createClient,
4   - testCase = require('nodeunit').testCase;
  1 +var assert = require('assert')
  2 + , helpers = require('./helpers.js')
  3 + ;
5 4
6 5 module.exports = {
7   - setUp: function(done) {
8   - helpers.createServer(done);
9   - },
10   -
11   - tearDown: function(done) {
12   - helpers.close(done);
13   - },
14   -
15   - 'test valid WHOIS': function(test) {
16   - createClient({ nick: 'testbot1', channel: '#test' }, function(testbot1) {
17   - createClient({ nick: 'testbot2', channel: '#test' }, function(testbot2) {
18   - testbot1.on('raw', function(data) {
19   - if (data.command === 'JOIN') {
20   - testbot1.send('WHOIS', 'testbot2');
21   - } else if (data.command === 'rpl_whoisuser') {
22   - assert.equal('testbot2', data.args[1]);
23   - testbot1.disconnect();
24   - testbot2.disconnect();
25   - test.done();
26   - }
  6 + 'Clients': {
  7 + beforeEach: function(done) {
  8 + this.server = new helpers.MockServer(done, false, 6661);
  9 + },
  10 +
  11 + afterEach: function(done) {
  12 + this.server.close(done);
  13 + },
  14 +
  15 + 'test valid WHOIS': function(done) {
  16 + var createClient = this.server.createClient.bind(this.server);
  17 +
  18 + createClient({ nick: 'testbot1', channel: '#test' }, function(testbot1) {
  19 + createClient({ nick: 'testbot2', channel: '#test' }, function(testbot2) {
  20 + testbot1.on('raw', function(data) {
  21 + if (data.command === 'JOIN') {
  22 + testbot1.send('WHOIS', 'testbot2');
  23 + } else if (data.command === 'rpl_whoisuser') {
  24 + assert.equal('testbot2', data.args[1]);
  25 + testbot1.disconnect();
  26 + testbot2.disconnect();
  27 + done();
  28 + }
  29 + });
27 30 });
28 31 });
29   - });
30   - },
31   -
32   - 'valid WHO': function(test) {
33   - createClient({ nick: 'testbot1', channel: '#test' }, function(testbot1) {
34   - createClient({ nick: 'testbot2', channel: '#test' }, function(testbot2) {
35   - testbot1.on('raw', function(data) {
36   - if (data.command === 'rpl_endofwho') {
37   - assert.equal('#test', data.args[1]);
38   - testbot1.disconnect();
39   - testbot2.disconnect();
40   - test.done();
41   - }
  32 + },
  33 +
  34 + 'valid WHO': function(done) {
  35 + var createClient = this.server.createClient.bind(this.server);
  36 +
  37 + createClient({ nick: 'testbot1', channel: '#test' }, function(testbot1) {
  38 + createClient({ nick: 'testbot2', channel: '#test' }, function(testbot2) {
  39 + testbot1.on('raw', function(data) {
  40 + if (data.command === 'rpl_endofwho') {
  41 + assert.equal('#test', data.args[1]);
  42 + testbot1.disconnect();
  43 + testbot2.disconnect();
  44 + done();
  45 + }
  46 + });
  47 + testbot1.send('WHO', '#test');
  48 + });
  49 + });
  50 + },
  51 +
  52 + 'invalid WHO (bug #9)': function(done) {
  53 + var createClient = this.server.createClient.bind(this.server);
  54 +
  55 + createClient({ nick: 'testbot1', channel: '#test' }, function(testbot1) {
  56 + createClient({ nick: 'testbot2', channel: '#test' }, function(testbot2) {
  57 + testbot1.addListener('error', function(message) {
  58 + if (message.command === 'err_nosuchchannel') {
  59 + testbot1.disconnect();
  60 + testbot2.disconnect();
  61 + done();
  62 + }
  63 + });
  64 +
  65 + testbot1.send('WHO', '#argh');
42 66 });
43   - testbot1.send('WHO', '#test');
44 67 });
45   - });
46   - },
47   -
48   - 'invalid WHO (bug #9)': function(test) {
49   - createClient({ nick: 'testbot1', channel: '#test' }, function(testbot1) {
50   - createClient({ nick: 'testbot2', channel: '#test' }, function(testbot2) {
51   - testbot1.addListener('error', function(message) {
52   - if (message.command === 'err_nosuchchannel') {
  68 + },
  69 +
  70 + 'socket error handling (bug #10)': function(done) {
  71 + var createClient = this.server.createClient.bind(this.server)
  72 + , server = this.server.server;
  73 +
  74 + createClient({ nick: 'testbot1', channel: '#test' }, function(testbot1) {
  75 + createClient({ nick: 'testbot2', channel: '#test' }, function(testbot2) {
  76 + var user = server.users.registered.filter(function(user) { return user.nick == testbot2.nick; })[0];
  77 +
  78 + // Simulate a socket issue by causing user.send to raise an exception
  79 + user.stream = 'bad';
  80 + testbot2.send('WHO', '#test');
  81 +
  82 + setTimeout(function() {
  83 + // There should now be one user instead of two in the channel
  84 + assert.equal(1, server.channels.registered['#test'].users.length);
53 85 testbot1.disconnect();
54 86 testbot2.disconnect();
55   - test.done();
56   - }
  87 + done();
  88 + }, 10);
57 89 });
58   -
59   - testbot1.send('WHO', '#argh');
60 90 });
61   - });
62   - },
63   -
64   - 'socket error handling (bug #10)': function(test) {
65   - createClient({ nick: 'testbot1', channel: '#test' }, function(testbot1) {
66   - createClient({ nick: 'testbot2', channel: '#test' }, function(testbot2) {
67   - var user = helpers.server().users.registered.filter(function(user) { return user.nick == testbot2.nick; })[0];
68   -
69   - // Simulate a socket issue by causing user.send to raise an exception
70   - user.stream = 'bad';
71   - testbot2.send('WHO', '#test');
72   -
73   - setTimeout(function() {
74   - // There should now be one user instead of two in the channel
75   - assert.equal(1, helpers.server().channels.registered['#test'].users.length);
76   - testbot1.disconnect();
77   - testbot2.disconnect();
78   - test.done();
79   - }, 10);
80   - });
81   - });
82   - },
83   -
84   - "users shouldn't be able to join channel twice (bug #12)": function(test) {
85   - createClient({ nick: 'testbot1', channel: '#test' }, function(testbot1) {
86   - testbot1.join('#test', function() {
87   - setTimeout(function() {
88   - assert.equal(helpers.server().channels.registered['#test'].users.length, 1);
89   - testbot1.disconnect();
90   - test.done();
91   - }, 10);
92   - });
93   - });
94   - },
95   -
96   - 'invalid ban mask (bug #19)': function(test) {
97   - createClient({ nick: 'huey', channel: '#aff' }, function(huey) {
98   - huey.send('MODE', '#aff', '+b');
99   - huey.on('error', function(data) {
100   - if (data.command === 'err_needmoreparams') {
101   - createClient({ nick: 'dewey', channel: '#aff' }, function(dewey) {
102   - huey.disconnect();
103   - dewey.disconnect();
104   - test.done();
105   - });
106   - }
  91 + },
  92 +
  93 + "users shouldn't be able to join channel twice (bug #12)": function(done) {
  94 + var createClient = this.server.createClient.bind(this.server)
  95 + , server = this.server.server;
  96 +
  97 + createClient({ nick: 'testbot1', channel: '#test' }, function(testbot1) {
  98 + testbot1.join('#test', function() {
  99 + setTimeout(function() {
  100 + assert.equal(server.channels.registered['#test'].users.length, 1);
  101 + testbot1.disconnect();
  102 + done();
  103 + }, 10);
  104 + });
107 105 });
108   - });
109   - },
110   -
111   - 'invalid away status (bug #18)': function(test) {
112   - createClient({ nick: 'huey', channel: '#aff' }, function(huey) {
113   - var dewey;
114   -
115   - huey.send('AWAY');
116   - huey.on('message', function(from, to, message) {
117   - assert.equal('dewey', from);
118   - assert.equal('huey', to);
119   - assert.equal('Hello', message);
120   - huey.disconnect();
121   - dewey.disconnect();
122   - test.done();
  106 + },
  107 +
  108 + 'invalid ban mask (bug #19)': function(done) {
  109 + var createClient = this.server.createClient.bind(this.server);
  110 +
  111 + createClient({ nick: 'huey', channel: '#aff' }, function(huey) {
  112 + huey.send('MODE', '#aff', '+b');
  113 + huey.on('error', function(data) {
  114 + if (data.command === 'err_needmoreparams') {
  115 + createClient({ nick: 'dewey', channel: '#aff' }, function(dewey) {
  116 + huey.disconnect();
  117 + dewey.disconnect();
  118 + done();
  119 + });
  120 + }
  121 + });
123 122 });
  123 + },
  124 +
  125 + 'invalid away status (bug #18)': function(done) {
  126 + var createClient = this.server.createClient.bind(this.server);
  127 +
  128 + createClient({ nick: 'huey', channel: '#aff' }, function(huey) {
  129 + var dewey;
  130 +
  131 + huey.send('AWAY');
  132 + huey.on('message', function(from, to, message) {
  133 + assert.equal('dewey', from);
  134 + assert.equal('huey', to);
  135 + assert.equal('Hello', message);
  136 + huey.disconnect();
  137 + dewey.disconnect();
  138 + done();
  139 + });
124 140
125   - huey.on('error', function(data) {
126   - if (data.command === 'err_needmoreparams') {
127   - createClient({ nick: 'dewey', channel: '#aff' }, function(client) {
128   - dewey = client;
129   - dewey.say('huey', 'Hello');
130   - });
131   - }
  141 + huey.on('error', function(data) {
  142 + if (data.command === 'err_needmoreparams') {
  143 + createClient({ nick: 'dewey', channel: '#aff' }, function(client) {
  144 + dewey = client;
  145 + dewey.say('huey', 'Hello');
  146 + });
  147 + }
  148 + });
132 149 });
133   - });
134   - },
  150 + },
135 151
136   - 'simultaneous user simulation': function(test) {
137   - var nicks = [], i;
138   - for (i = 1; i <= 10; i++) {
139   - nicks.push('user_' + i);
140   - }
  152 + 'simultaneous user simulation': function(done) {
  153 + var nicks = [], i;
  154 + for (i = 1; i <= 10; i++) {
  155 + nicks.push('user_' + i);
  156 + }
141 157
142   - function assertReceive(bots, assertion, fn) {
143   - bots[0].say(bots[1].nick, assertion);
  158 + function assertReceive(bots, assertion, fn) {
  159 + bots[0].say(bots[1].nick, assertion);
144 160
145   - var callback = function(from, to, message) {
146   - assert.equal(assertion, message);
147   - bots[1].removeListener('message', callback);
148   - fn();
149   - };
  161 + var callback = function(from, to, message) {
  162 + assert.equal(assertion, message);
  163 + bots[1].removeListener('message', callback);
  164 + fn();
  165 + };
150 166
151   - bots[1].addListener('message', callback);
152   - }
153   -
154   - helpers.createClients(nicks, '#test', function(bots) {
155   - function done() {
156   - bots.forEach(function(bot) {
157   - bot.disconnect();
158   - });
159   - test.done();
  167 + bots[1].addListener('message', callback);
160 168 }
161 169
162   - var tested = 0, max = bots.length - 1;
163   - for (var i = 0; i < max; i++) {
164   - assertReceive([bots[i], bots[i + 1]], 'Message ' + Math.random(), function() {
165   - tested++;
166   - if (tested === max) {
167   - done();
168   - }
169   - });
170   - }
171   - });
  170 + this.server.createClients(nicks, '#test', function(bots) {
  171 + function teardown() {
  172 + bots.forEach(function(bot) {
  173 + bot.disconnect();
  174 + });
  175 + done();
  176 + }
  177 +
  178 + var i, tested = 0, max = bots.length - 1;
  179 + for (i = 0; i < max; i++) {
  180 + assertReceive([bots[i], bots[i + 1]], 'Message ' + Math.random(), function() {
  181 + tested++;
  182 + if (tested === max) {
  183 + teardown();
  184 + }
  185 + });
  186 + }
  187 + });
  188 + }
172 189 }
173 190 };
94 test/helpers.js
... ... @@ -1,16 +1,15 @@
1   -var path = require('path'),
2   - Server = require(path.join(__dirname, '..', 'lib', 'server')).Server,
3   - irc = require('irc'),
4   - winston = require('winston'),
5   - port = 6711,
6   - server;
  1 +var path = require('path')
  2 + , Server = require(path.join(__dirname, '..', 'lib', 'server')).Server
  3 + , irc = require('irc')
  4 + , winston = require('winston')
  5 + ;
7 6
8 7 winston.remove(winston.transports.Console);
9 8
10   -function createServer(test, usepass) {
11   - server = new Server();
12   - server.showLog = false;
13   - server.config = {
  9 +function MockServer(done, usepass, port) {
  10 + this.server = new Server();
  11 + this.server.showLog = false;
  12 + this.server.config = {
14 13 'network': 'ircn',
15 14 'hostname': 'localhost',
16 15 'serverDescription': 'A Node IRC daemon',
@@ -24,51 +23,54 @@ function createServer(test, usepass) {
24 23 };
25 24
26 25 if (usepass) {
27   - server.config.serverPassword = '$2a$10$T1UJYlinVUGHqfInKSZQz./CHrYIVVqbDO3N1fRNEUvFvSEcshNdC';
  26 + this.server.config.serverPassword = '$2a$10$T1UJYlinVUGHqfInKSZQz./CHrYIVVqbDO3N1fRNEUvFvSEcshNdC';
28 27 }
29 28
30   - server.start(test);
  29 + this.server.start(done);
31 30 }
32 31
33   -function createClient(options, fn) {
34   - var ranCallback = false,
35   - client = new irc.Client('localhost', options.nick, {
36   - channels: [options.channel],
37   - port: port,
38   - debug: false,
39   - password: options.password
40   - });
  32 +MockServer.prototype = {
  33 + close: function(done) {
  34 + this.server.close(done);
  35 + },
41 36
42   - client.addListener('join', function() {
43   - if (!ranCallback) {
44   - fn(client);
45   - ranCallback = true;
46   - }
47   - });
48   -}
  37 + createClient: function(options, fn) {
  38 + options.port = this.server.config.port;
49 39
50   -function createClients(nicks, channel, fn) {
51   - var connected = [];
  40 + var ranCallback = false
  41 + , client = new irc.Client('localhost', options.nick, {
  42 + channels: [options.channel]
  43 + , port: options.port
  44 + , debug: false
  45 + , password: options.password
  46 + });
52 47
53   - nicks.forEach(function(nick) {
54   - createClient({ nick: nick, channel: channel }, function(bot) {
55   - connected.push(bot);
56   - if (connected.length == nicks.length) {
57   - fn(connected);
  48 + client.addListener('join', function() {
  49 + if (!ranCallback) {
  50 + fn(client);
  51 + ranCallback = true;
58 52 }
59 53 });
60   - });
61   -}
  54 + },
  55 +
  56 + createClients: function(nicks, channel, fn) {
  57 + var connected = []
  58 + , createClient = this.createClient.bind(this);
  59 +
  60 + nicks.forEach(function(nick) {
  61 + createClient({ nick: nick, channel: channel }, function(bot) {
  62 + connected.push(bot);
  63 + if (connected.length == nicks.length) {
  64 + fn(connected);
  65 + }
  66 + });
  67 + });
  68 + }
  69 +};
62 70
63 71 module.exports = {
64   - createServer: createServer,
65   - createClient: createClient,
66   - createClients: createClients,
67   - close: function(fn) {
68   - server.close(fn);
69   - },
70   - server: function() {
71   - return server;
72   - },
73   - port: port
  72 + MockServer: MockServer
  73 +, createServer: function(usepass, port, fn) {
  74 + var server = new MockServer(function() { fn(server); }, usepass, port);
  75 + }
74 76 };
118 test/protocol.test.js
... ... @@ -1,67 +1,71 @@
1   -var path = require('path'),
2   - assert = require('assert'),
3   - protocol = require(path.join(__dirname, '..', 'lib', 'protocol')),
4   - invalidNick = protocol.validations.invalidNick,
5   - invalidChannel = protocol.validations.invalidChannel,
6   - invalidChannelKey = protocol.validations.invalidChannelKey;
  1 +var path = require('path')
  2 + , assert = require('assert')
  3 + , protocol = require(path.join(__dirname, '..', 'lib', 'protocol'))
  4 + , invalidNick = protocol.validations.invalidNick
  5 + , invalidChannel = protocol.validations.invalidChannel
  6 + , invalidChannelKey = protocol.validations.invalidChannelKey
  7 + ;
7 8
8   -exports['nickname validation'] = function(test) {
9   - // Valid nicknames
10   - test.strictEqual('alexyoung'.match(invalidNick), null);
11   - test.strictEqual('AbC123'.match(invalidNick), null);
12   - test.strictEqual('a{b[}]'.match(invalidNick), null);
  9 +module.exports = {
  10 + 'Protocol': {
  11 + 'test nickname validation': function(done) {
  12 + // Valid nicknames
  13 + assert.strictEqual('alexyoung'.match(invalidNick), null);
  14 + assert.strictEqual('AbC123'.match(invalidNick), null);
  15 + assert.strictEqual('a{b[}]'.match(invalidNick), null);
13 16
14   - // Invalid nicknames
15   - // Nicknames shall not contain some special characters
16   - test.notStrictEqual('abc#'.match(invalidNick), null);
17   - test.notStrictEqual('abc*defg'.match(invalidNick), null);
18   - test.notStrictEqual('abc~'.match(invalidNick), null);
19   - test.notStrictEqual('a\0a'.match(invalidNick), null, 'NULL');
20   - test.notStrictEqual('abc\ndefg'.match(invalidNick), null, 'LF');
21   - test.notStrictEqual('abc\7xyz'.match(invalidNick), null, 'BELL');
  17 + // Invalid nicknames
  18 + // Nicknames shall not contain some special characters
  19 + assert.notStrictEqual('abc#'.match(invalidNick), null);
  20 + assert.notStrictEqual('abc*defg'.match(invalidNick), null);
  21 + assert.notStrictEqual('abc~'.match(invalidNick), null);
  22 + assert.notStrictEqual('a\0a'.match(invalidNick), null, 'NULL');
  23 + assert.notStrictEqual('abc\ndefg'.match(invalidNick), null, 'LF');
  24 + assert.notStrictEqual('abc\7xyz'.match(invalidNick), null, 'BELL');
22 25
23   - // Invalid nicknames
24   - // RFC1459 says nicks must start with a letter
25   - // https://github.com/alexyoung/ircd.js/blob/5d7443847311d4d6d1ff7371fa1fdee021315b0f/doc/rfc1459.txt#L492
26   - test.notStrictEqual('9abc'.match(protocol.validations.invalidNick), null, 'starting with a digit');
27   - test.notStrictEqual('^abc123'.match(protocol.validations.invalidNick), null, 'starting with a special character');
  26 + // Invalid nicknames
  27 + // RFC1459 says nicks must start with a letter
  28 + // https://github.com/alexyoung/ircd.js/blob/5d7443847311d4d6d1ff7371fa1fdee021315b0f/doc/rfc1459.txt#L492
  29 + assert.notStrictEqual('9abc'.match(protocol.validations.invalidNick), null, 'starting with a digit');
  30 + assert.notStrictEqual('^abc123'.match(protocol.validations.invalidNick), null, 'starting with a special character');
28 31
29   - test.done();
30   -};
31   -
32   -exports['channelname validation'] = function(test) {
33   - // Valid channelnames
34   - test.strictEqual('node.js'.match(invalidChannel), null);
35   - test.strictEqual('#9'.match(invalidChannel), null);
36   - test.strictEqual('bla\u01D2'.match(invalidChannel), null, 'random 8 bit character');
  32 + done();
  33 + },
37 34
38   - // Invalid channelnames
39   - // https://github.com/alexyoung/ircd.js/blob/5d7443847311d4d6d1ff7371fa1fdee021315b0f/doc/rfc1459.txt#L494
40   - test.notStrictEqual('word1 word2'.match(invalidChannel), null, 'SPACE');
41   - test.notStrictEqual('ring\x07'.match(invalidChannel), null, 'BELL');
42   - test.notStrictEqual('zero\x00'.match(invalidChannel), null, 'NUL');
43   - test.notStrictEqual('word\rword'.match(invalidChannel), null, 'CR');
44   - test.notStrictEqual('word\nword'.match(invalidChannel), null, 'LF');
45   - test.notStrictEqual('first,secound,third'.match(invalidChannel), null, 'Comma (,)');
  35 + 'test channelname validation': function(done) {
  36 + // Valid channelnames
  37 + assert.strictEqual('node.js'.match(invalidChannel), null);
  38 + assert.strictEqual('#9'.match(invalidChannel), null);
  39 + assert.strictEqual('bla\u01D2'.match(invalidChannel), null, 'random 8 bit character');
46 40
47   - test.done();
48   -}
  41 + // Invalid channelnames
  42 + // https://github.com/alexyoung/ircd.js/blob/5d7443847311d4d6d1ff7371fa1fdee021315b0f/doc/rfc1459.txt#L494
  43 + assert.notStrictEqual('word1 word2'.match(invalidChannel), null, 'SPACE');
  44 + assert.notStrictEqual('ring\x07'.match(invalidChannel), null, 'BELL');
  45 + assert.notStrictEqual('zero\x00'.match(invalidChannel), null, 'NUL');
  46 + assert.notStrictEqual('word\rword'.match(invalidChannel), null, 'CR');
  47 + assert.notStrictEqual('word\nword'.match(invalidChannel), null, 'LF');
  48 + assert.notStrictEqual('first,secound,third'.match(invalidChannel), null, 'Comma (,)');
49 49
50   -exports['channelkey validation'] = function(test) {
51   - // Valid channelkeys
52   - test.strictEqual('key'.match(invalidChannelKey), null);
53   - test.strictEqual('key*'.match(invalidChannelKey), null);
  50 + done();
  51 + },
54 52
55   - // Invalid channelkeys
56   - // any 7-bit US_ASCII character is valid, except NUL, CR, LF, FF, h/v TABs, and " "
57   - test.notStrictEqual('bla\u01D2'.match(invalidChannelKey), null, 'random 8 bit character');
58   - test.notStrictEqual('zero\x00'.match(invalidChannelKey), null, 'NUL');
59   - test.notStrictEqual('word\rword'.match(invalidChannelKey), null, 'CR');
60   - test.notStrictEqual('word\nword'.match(invalidChannelKey), null, 'LF');
61   - test.notStrictEqual('word\x0C'.match(invalidChannelKey), null, 'FF');
62   - test.notStrictEqual('horizontal\x09vertical\x0B'.match(invalidChannelKey), null, 'tabs');
63   - test.notStrictEqual('space s'.match(invalidChannelKey), null, 'SPACE')
  53 + 'test channelkey validation': function(done) {
  54 + // Valid channelkeys
  55 + assert.strictEqual('key'.match(invalidChannelKey), null);
  56 + assert.strictEqual('key*'.match(invalidChannelKey), null);
64 57
65   - test.done();
66   -}
  58 + // Invalid channelkeys
  59 + // any 7-bit US_ASCII character is valid, except NUL, CR, LF, FF, h/v TABs, and " "
  60 + assert.notStrictEqual('bla\u01D2'.match(invalidChannelKey), null, 'random 8 bit character');
  61 + assert.notStrictEqual('zero\x00'.match(invalidChannelKey), null, 'NUL');
  62 + assert.notStrictEqual('word\rword'.match(invalidChannelKey), null, 'CR');
  63 + assert.notStrictEqual('word\nword'.match(invalidChannelKey), null, 'LF');
  64 + assert.notStrictEqual('word\x0C'.match(invalidChannelKey), null, 'FF');
  65 + assert.notStrictEqual('horizontal\x09vertical\x0B'.match(invalidChannelKey), null, 'tabs');
  66 + assert.notStrictEqual('space s'.match(invalidChannelKey), null, 'SPACE')
67 67
  68 + done();
  69 + }
  70 + }
  71 +};
38 test/server.test.js
... ... @@ -1,25 +1,27 @@
1   -var assert = require('assert'),
2   - helpers = require('./helpers.js'),
3   - createClient = helpers.createClient,
4   - testCase = require('nodeunit').testCase;
  1 +var assert = require('assert')
  2 + , helpers = require('./helpers.js')
  3 + ;
5 4
6 5 module.exports = {
7   - setUp: function(done) {
8   - helpers.createServer(done, true);
9   - },
  6 + 'Server': {
  7 + beforeEach: function(done) {
  8 + this.server = new helpers.MockServer(done, true, 6662);
  9 + },
10 10
11   - tearDown: function(done) {
12   - helpers.close(done);
13   - },
  11 + afterEach: function(done) {
  12 + this.server.close(done);
  13 + },
14 14
15   - 'test connection passwords': function(test) {
16   - createClient({ nick: 'testbot1', channel: '#test', password: 'test' }, function(testbot1) {
17   - testbot1.on('raw', function(data) {
18   - if (data.command === 'rpl_channelmodeis') {
19   - testbot1.disconnect();
20   - test.done();
21   - }
  15 + 'test connection passwords': function(done) {
  16 + var createClient = this.server.createClient.bind(this.server);
  17 + createClient({ nick: 'testbot1', channel: '#test', password: 'test' }, function(testbot1) {
  18 + testbot1.on('raw', function(data) {
  19 + if (data.command === 'rpl_channelmodeis') {
  20 + testbot1.disconnect();
  21 + done();
  22 + }
  23 + });
22 24 });
23   - });
  25 + }
24 26 }
25 27 };
57 test/sockets.test.js
... ... @@ -1,32 +1,37 @@
1   -var assert = require('assert'),
2   - net = require('net'),
3   - helpers = require('./helpers'),
4   - testCase = require('nodeunit').testCase;
  1 +var assert = require('assert')
  2 + , net = require('net')
  3 + , helpers = require('./helpers')
  4 + ;
5 5
6 6 module.exports = {
7   - setUp: function(done) {
8   - helpers.createServer(done);
9   - },
  7 + 'Sockets': {
  8 + beforeEach: function(done) {
  9 + this.server = new helpers.MockServer(done, false, 6663);
  10 + },
10 11
11   - tearDown: function(done) {
12   - helpers.close(done);
13   - },
  12 + afterEach: function(done) {
  13 + this.server.close(done);
  14 + },
14 15
15   - 'test destroy a socket': function(test) {
16   - var bob = net.createConnection(helpers.server().config.port, helpers.server().config.hostname);
17   - bob.write('garbage');
18   - process.nextTick(function() {
19   - bob.destroy();
20   - test.done();
21   - });
22   - },
23   -
24   - 'test send garbage': function(test) {
25   - var alice = net.createConnection(helpers.server().config.port, helpers.server().config.hostname);
26   - alice.write('NICK alice\n\x00\x07abc\r\uAAAA', 'ascii', function() {
27   - alice.end();
28   - test.done();
29   - });
  16 + 'test destroy a socket': function(done) {
  17 + var server = this.server.server
  18 + , bob = net.createConnection(server.config.port, server.config.hostname);
  19 +
  20 + bob.write('garbage');
  21 + process.nextTick(function() {
  22 + bob.destroy();
  23 + done();
  24 + });
  25 + },
  26 +
  27 + 'test send garbage': function(done) {
  28 + var server = this.server.server
  29 + , alice = net.createConnection(server.config.port, server.config.hostname);
  30 +
  31 + alice.write('NICK alice\n\x00\x07abc\r\uAAAA', 'ascii', function() {
  32 + alice.end();
  33 + done();
  34 + });
  35 + }
30 36 }
31 37 };
32   -
28 test/user.test.js
... ... @@ -1,19 +1,21 @@
1   -var assert = require('assert'),
2   - path = require('path'),
3   - User = require(path.join(__dirname, '..', 'lib', 'user')).User,
4   - testCase = require('nodeunit').testCase;
  1 +var assert = require('assert')
  2 + , path = require('path')
  3 + , User = require(path.join(__dirname, '..', 'lib', 'user')).User
  4 + ;
5 5
6 6 module.exports = {
7   - 'test timeout calculation': function(test) {
8   - var server = {
9   - config: { idleTimeout: 60 }
10   - }
11   - , user = new User(null, server);
  7 + 'User': {
  8 + 'test timeout calculation': function(done) {
  9 + var server = {
  10 + config: { idleTimeout: 60 }
  11 + }
  12 + , user = new User(null, server);
12 13
13   - assert.ok(!user.hasTimedOut());
14   - user.lastPing = (Date.now() - 60001);
15   - assert.ok(user.hasTimedOut());
  14 + assert.ok(!user.hasTimedOut());
  15 + user.lastPing = (Date.now() - 60001);
  16 + assert.ok(user.hasTimedOut());
16 17
17   - test.done();
  18 + done();
  19 + }
18 20 }
19 21 };

No commit comments for this range

Something went wrong with that request. Please try again.