2013-01-02 01:12:42 -05:00
|
|
|
var mc = require('../')
|
2015-05-22 21:31:47 -04:00
|
|
|
, states = mc.states
|
2013-01-07 23:36:14 -05:00
|
|
|
, Client = mc.Client
|
|
|
|
, Server = mc.Server
|
2013-01-02 01:12:42 -05:00
|
|
|
, path = require('path')
|
|
|
|
, fs = require('fs')
|
2013-01-07 23:36:14 -05:00
|
|
|
, net = require('net')
|
2015-09-11 20:33:54 -04:00
|
|
|
, assert = require('power-assert')
|
2013-01-02 01:12:42 -05:00
|
|
|
, MC_SERVER_JAR = process.env.MC_SERVER_JAR
|
|
|
|
, SURVIVE_TIME = 10000
|
|
|
|
, MC_SERVER_PATH = path.join(__dirname, 'server')
|
2015-08-24 07:49:34 -04:00
|
|
|
, getFieldInfo = require('../dist/utils').getFieldInfo
|
2015-08-24 21:10:31 -04:00
|
|
|
, getField = require('../dist/utils').getField
|
2015-05-14 16:08:49 -04:00
|
|
|
;
|
2013-01-02 01:12:42 -05:00
|
|
|
|
2015-09-14 15:30:10 -04:00
|
|
|
var Wrap = require('minecraft-wrap').Wrap;
|
|
|
|
var wrap=new Wrap(MC_SERVER_JAR,MC_SERVER_PATH);
|
2013-01-02 01:12:42 -05:00
|
|
|
|
2015-08-27 16:48:45 -04:00
|
|
|
function evalCount(count, fields) {
|
|
|
|
if(fields[count["field"]] in count["map"])
|
|
|
|
return count["map"][fields[count["field"]]];
|
|
|
|
return count["default"];
|
|
|
|
}
|
|
|
|
|
2013-01-07 23:36:14 -05:00
|
|
|
var values = {
|
2013-01-08 03:00:37 -05:00
|
|
|
'int': 123456,
|
|
|
|
'short': -123,
|
|
|
|
'ushort': 123,
|
2013-12-30 10:05:22 -05:00
|
|
|
'varint': 25992,
|
2013-01-08 03:00:37 -05:00
|
|
|
'byte': -10,
|
|
|
|
'ubyte': 8,
|
2013-08-07 13:16:05 -04:00
|
|
|
'string': "hi hi this is my client string",
|
2014-04-04 04:32:20 -04:00
|
|
|
'buffer': new Buffer(8),
|
2015-09-23 12:49:55 -04:00
|
|
|
'array': function(typeArgs, context) {
|
2015-08-27 16:48:45 -04:00
|
|
|
var count;
|
|
|
|
if (typeof typeArgs.count === "object")
|
2015-09-23 12:49:55 -04:00
|
|
|
count = evalCount(typeArgs.count, context);
|
2015-08-27 16:48:45 -04:00
|
|
|
else if (typeof typeArgs.count !== "undefined")
|
2015-09-23 12:49:55 -04:00
|
|
|
count = getField(typeArgs.count, context);
|
2015-08-27 16:48:45 -04:00
|
|
|
else if (typeof typeArgs.countType !== "undefined")
|
|
|
|
count = 1;
|
|
|
|
var arr = [];
|
|
|
|
while (count > 0) {
|
2015-09-23 12:49:55 -04:00
|
|
|
arr.push(getValue(typeArgs.type, context));
|
2015-08-27 16:48:45 -04:00
|
|
|
count--;
|
|
|
|
}
|
|
|
|
return arr;
|
2014-04-04 04:32:20 -04:00
|
|
|
},
|
2015-09-23 12:49:55 -04:00
|
|
|
'container': function(typeArgs, context) {
|
|
|
|
var results = {
|
|
|
|
"..": context;
|
|
|
|
};
|
2015-08-24 07:49:34 -04:00
|
|
|
for(var index in typeArgs) {
|
2015-09-23 12:49:55 -04:00
|
|
|
results[typeArgs[index].name] = getValue(typeArgs[index].type, results);
|
2014-04-04 04:32:20 -04:00
|
|
|
}
|
2015-09-23 12:49:55 -04:00
|
|
|
delete context[".."];
|
2014-04-04 04:32:20 -04:00
|
|
|
return results;
|
|
|
|
},
|
|
|
|
'count': 1, // TODO : might want to set this to a correct value
|
2013-01-08 03:00:37 -05:00
|
|
|
'bool': true,
|
|
|
|
'double': 99999.2222,
|
|
|
|
'float': -333.444,
|
|
|
|
'slot': {
|
2015-03-06 18:37:47 -05:00
|
|
|
blockId: 5,
|
2013-01-07 23:36:14 -05:00
|
|
|
itemCount: 56,
|
|
|
|
itemDamage: 2,
|
2015-05-14 16:08:49 -04:00
|
|
|
nbtData: {
|
|
|
|
root: "test", value: {
|
|
|
|
test1: {type: "int", value: 4},
|
|
|
|
test2: {type: "long", value: [12, 42]},
|
|
|
|
test3: {type: "byteArray", value: new Buffer(32)},
|
|
|
|
test4: {type: "string", value: "ohi"},
|
|
|
|
test5: {type: "list", value: {type: "int", value: [4]}},
|
|
|
|
test6: {type: "compound", value: {test: {type: "int", value: 4}}},
|
|
|
|
test7: {type: "intArray", value: [12, 42]}
|
|
|
|
}
|
|
|
|
}
|
2013-01-08 03:00:37 -05:00
|
|
|
},
|
|
|
|
'long': [0, 1],
|
2013-02-02 15:19:37 -05:00
|
|
|
'entityMetadata': [
|
2015-09-12 14:04:25 -04:00
|
|
|
{key: 17, value: 0, type: 0}
|
2013-02-02 15:19:37 -05:00
|
|
|
],
|
2013-01-08 03:00:37 -05:00
|
|
|
'objectData': {
|
2013-01-07 23:36:14 -05:00
|
|
|
intField: 9,
|
|
|
|
velocityX: 1,
|
|
|
|
velocityY: 2,
|
|
|
|
velocityZ: 3,
|
2013-01-08 03:00:37 -05:00
|
|
|
},
|
2015-07-18 13:48:26 -04:00
|
|
|
'UUID': "00112233-4455-6677-8899-aabbccddeeff",
|
2015-05-14 16:08:49 -04:00
|
|
|
'position': {x: 12, y: 332, z: 4382821},
|
2015-05-08 23:58:12 -04:00
|
|
|
'restBuffer': new Buffer(0),
|
2015-09-23 12:49:55 -04:00
|
|
|
'switch': function(typeArgs, context) {
|
|
|
|
var i = typeArgs.fields[getField(typeArgs.compareTo, context)];
|
2015-08-24 21:10:31 -04:00
|
|
|
if (typeof i === "undefined")
|
2015-09-23 12:49:55 -04:00
|
|
|
return getValue(typeArgs.default, context);
|
2015-08-24 21:10:31 -04:00
|
|
|
else
|
2015-09-23 12:49:55 -04:00
|
|
|
return getValue(i, context);
|
2015-08-24 21:10:31 -04:00
|
|
|
},
|
2015-09-23 12:49:55 -04:00
|
|
|
'option': function(typeArgs, context) {
|
|
|
|
return getValue(typeArgs, context);
|
2015-08-28 11:21:59 -04:00
|
|
|
}
|
2013-01-08 03:00:37 -05:00
|
|
|
};
|
2013-01-07 23:36:14 -05:00
|
|
|
|
2015-08-24 21:10:31 -04:00
|
|
|
function getValue(_type, packet) {
|
|
|
|
var fieldInfo = getFieldInfo(_type);
|
|
|
|
if (typeof values[fieldInfo.type] === "function")
|
|
|
|
return values[fieldInfo.type](fieldInfo.typeArgs, packet);
|
|
|
|
else if (values[fieldInfo.type] !== "undefined")
|
|
|
|
return values[fieldInfo.type];
|
|
|
|
else if (fieldInfo.type !== "void")
|
|
|
|
throw new Error("No value for type " + fieldInfo.type);
|
|
|
|
}
|
|
|
|
|
2013-01-07 23:36:14 -05:00
|
|
|
describe("packets", function() {
|
|
|
|
var client, server, serverClient;
|
|
|
|
before(function(done) {
|
|
|
|
server = new Server();
|
|
|
|
server.once('listening', function() {
|
|
|
|
server.once('connection', function(c) {
|
|
|
|
serverClient = c;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
client = new Client();
|
|
|
|
client.setSocket(net.connect(25565, 'localhost'));
|
|
|
|
});
|
|
|
|
server.listen(25565, 'localhost');
|
|
|
|
});
|
|
|
|
after(function(done) {
|
|
|
|
client.on('end', function() {
|
|
|
|
server.on('close', done);
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
client.end();
|
|
|
|
});
|
2015-09-19 18:41:03 -04:00
|
|
|
var packetName, packetInfo, field;
|
2015-05-22 21:31:47 -04:00
|
|
|
for(state in mc.packetFields) {
|
|
|
|
if(!mc.packetFields.hasOwnProperty(state)) continue;
|
2015-09-19 18:41:03 -04:00
|
|
|
for(packetName in mc.packetFields[state].toServer) {
|
|
|
|
if(!mc.packetFields[state].toServer.hasOwnProperty(packetName)) continue;
|
|
|
|
packetInfo = mc.get(packetName, state, true);
|
|
|
|
it(state + ",ServerBound," + packetName,
|
|
|
|
callTestPacket(packetName, packetInfo, state, true));
|
2013-12-30 10:05:22 -05:00
|
|
|
}
|
2015-09-19 18:41:03 -04:00
|
|
|
for(packetName in mc.packetFields[state].toClient) {
|
|
|
|
if(!mc.packetFields[state].toClient.hasOwnProperty(packetName)) continue;
|
|
|
|
packetInfo = mc.get(packetName, state, false);
|
|
|
|
it(state + ",ClientBound," + packetName,
|
|
|
|
callTestPacket(packetName, packetInfo, state, false));
|
2013-12-30 10:05:22 -05:00
|
|
|
}
|
2013-01-07 23:36:14 -05:00
|
|
|
}
|
2015-09-19 18:41:03 -04:00
|
|
|
function callTestPacket(packetName, packetInfo, state, toServer) {
|
2013-01-07 23:36:14 -05:00
|
|
|
return function(done) {
|
2013-12-30 10:05:22 -05:00
|
|
|
client.state = state;
|
|
|
|
serverClient.state = state;
|
2015-09-19 18:41:03 -04:00
|
|
|
testPacket(packetName, packetInfo, state, toServer, done);
|
2015-05-14 16:08:49 -04:00
|
|
|
};
|
2013-01-07 23:36:14 -05:00
|
|
|
}
|
2015-05-14 16:08:49 -04:00
|
|
|
|
2015-09-19 18:41:03 -04:00
|
|
|
function testPacket(packetName, packetInfo, state, toServer, done) {
|
2013-01-07 23:36:14 -05:00
|
|
|
// empty object uses default values
|
|
|
|
var packet = {};
|
|
|
|
packetInfo.forEach(function(field) {
|
2015-08-24 21:10:31 -04:00
|
|
|
packet[field.name] = getValue(field.type, packet);
|
2013-01-07 23:36:14 -05:00
|
|
|
});
|
2015-05-14 16:08:49 -04:00
|
|
|
if(toServer) {
|
2015-09-19 18:41:03 -04:00
|
|
|
serverClient.once(packetName, function(receivedPacket) {
|
2015-08-24 07:49:34 -04:00
|
|
|
try {
|
2013-12-30 10:05:22 -05:00
|
|
|
assertPacketsMatch(packet, receivedPacket);
|
2015-08-24 07:49:34 -04:00
|
|
|
} catch (e) {
|
|
|
|
console.log(packet, receivedPacket);
|
|
|
|
throw e;
|
|
|
|
}
|
2013-01-07 23:36:14 -05:00
|
|
|
done();
|
|
|
|
});
|
2015-09-19 18:41:03 -04:00
|
|
|
client.write(packetName, packet);
|
2013-12-30 10:05:22 -05:00
|
|
|
} else {
|
2015-09-19 18:41:03 -04:00
|
|
|
client.once(packetName, function(receivedPacket) {
|
2013-12-30 10:05:22 -05:00
|
|
|
assertPacketsMatch(packet, receivedPacket);
|
|
|
|
done();
|
|
|
|
});
|
2015-09-19 18:41:03 -04:00
|
|
|
serverClient.write(packetName, packet);
|
2013-12-30 10:05:22 -05:00
|
|
|
}
|
2013-01-07 23:36:14 -05:00
|
|
|
}
|
2015-05-14 16:08:49 -04:00
|
|
|
|
2013-01-07 23:36:14 -05:00
|
|
|
function assertPacketsMatch(p1, p2) {
|
2013-01-08 00:50:43 -05:00
|
|
|
packetInfo.forEach(function(field) {
|
2013-01-08 03:00:37 -05:00
|
|
|
assert.deepEqual(p1[field], p2[field]);
|
2013-01-08 00:50:43 -05:00
|
|
|
});
|
2013-11-19 18:06:57 -05:00
|
|
|
var field;
|
2015-05-14 16:08:49 -04:00
|
|
|
for(field in p1) {
|
2015-08-24 07:49:34 -04:00
|
|
|
if (p1[field] !== undefined)
|
|
|
|
assert.ok(field in p2, "field " + field + " missing in p2, in p1 it has value " + JSON.stringify(p1[field]));
|
2013-01-07 23:36:14 -05:00
|
|
|
}
|
2015-05-14 16:08:49 -04:00
|
|
|
for(field in p2) {
|
|
|
|
assert.ok(field in p1, "field " + field + " missing in p1, in p2 it has value " + JSON.stringify(p2[field]));
|
2013-01-07 23:36:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-01-04 19:04:06 -05:00
|
|
|
describe("client", function() {
|
2015-03-06 19:03:55 -05:00
|
|
|
this.timeout(10 * 60 * 1000);
|
2013-01-02 01:12:42 -05:00
|
|
|
|
|
|
|
afterEach(function(done) {
|
2015-09-14 15:30:10 -04:00
|
|
|
wrap.stopServer(function(err){
|
|
|
|
if(err)
|
|
|
|
console.log(err);
|
|
|
|
done(err);
|
|
|
|
});
|
2013-01-02 01:12:42 -05:00
|
|
|
});
|
|
|
|
after(function(done) {
|
2015-09-14 15:30:10 -04:00
|
|
|
wrap.deleteServerData(function(err){
|
|
|
|
if(err)
|
|
|
|
console.log(err);
|
|
|
|
done(err);
|
|
|
|
});
|
2013-01-02 01:12:42 -05:00
|
|
|
});
|
2013-01-04 21:42:31 -05:00
|
|
|
it("pings the server", function(done) {
|
2015-09-19 16:45:39 -04:00
|
|
|
wrap.on('line',function(line){
|
|
|
|
console.log(line);
|
|
|
|
});
|
2015-09-14 15:30:10 -04:00
|
|
|
wrap.startServer({
|
2013-01-04 21:42:31 -05:00
|
|
|
motd: 'test1234',
|
|
|
|
'max-players': 120,
|
2015-09-20 14:26:31 -04:00
|
|
|
}, function(err) {
|
|
|
|
if(err)
|
|
|
|
return done(err);
|
2013-01-04 21:42:31 -05:00
|
|
|
mc.ping({}, function(err, results) {
|
2015-05-14 16:08:49 -04:00
|
|
|
if(err) return done(err);
|
2013-01-28 19:24:03 -05:00
|
|
|
assert.ok(results.latency >= 0);
|
|
|
|
assert.ok(results.latency <= 1000);
|
|
|
|
delete results.latency;
|
2013-12-30 10:05:22 -05:00
|
|
|
delete results.favicon; // too lazy to figure it out
|
2015-05-14 16:08:49 -04:00
|
|
|
/* assert.deepEqual(results, {
|
|
|
|
version: {
|
|
|
|
name: '1.7.4',
|
|
|
|
protocol: 4
|
|
|
|
},
|
|
|
|
description: { text: "test1234" }
|
|
|
|
});*/
|
2013-01-04 21:42:31 -05:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-04-26 09:02:59 -04:00
|
|
|
it.skip("connects successfully - online mode (STUBBED)", function(done) {
|
2015-09-20 14:26:31 -04:00
|
|
|
wrap.startServer({'online-mode': 'true'}, function(err) {
|
|
|
|
if(err)
|
|
|
|
return done(err);
|
2013-01-02 01:12:42 -05:00
|
|
|
var client = mc.createClient({
|
|
|
|
username: process.env.MC_USERNAME,
|
|
|
|
password: process.env.MC_PASSWORD,
|
|
|
|
});
|
2015-09-14 15:30:10 -04:00
|
|
|
wrap.on('line', function(line) {
|
2013-12-30 10:05:22 -05:00
|
|
|
var match = line.match(/\[Server thread\/INFO\]: <(.+?)> (.+)/);
|
2015-05-14 16:08:49 -04:00
|
|
|
if(!match) return;
|
2013-01-02 01:12:42 -05:00
|
|
|
assert.strictEqual(match[1], client.session.username);
|
|
|
|
assert.strictEqual(match[2], "hello everyone; I have logged in.");
|
2015-09-14 15:30:10 -04:00
|
|
|
wrap.writeServer("say hello\n");
|
2013-01-02 01:12:42 -05:00
|
|
|
});
|
|
|
|
var chatCount = 0;
|
2015-02-19 20:05:15 -05:00
|
|
|
client.on('login', function(packet) {
|
2013-01-03 20:59:58 -05:00
|
|
|
assert.strictEqual(packet.levelType, 'default');
|
|
|
|
assert.strictEqual(packet.difficulty, 1);
|
|
|
|
assert.strictEqual(packet.dimension, 0);
|
|
|
|
assert.strictEqual(packet.gameMode, 0);
|
2015-02-19 20:05:15 -05:00
|
|
|
client.write('chat', {
|
2013-01-03 20:59:58 -05:00
|
|
|
message: "hello everyone; I have logged in."
|
|
|
|
});
|
|
|
|
});
|
2015-02-19 20:05:15 -05:00
|
|
|
client.on('chat', function(packet) {
|
2015-03-06 12:20:18 -05:00
|
|
|
done();
|
2013-01-02 01:12:42 -05:00
|
|
|
});
|
2015-04-26 09:02:59 -04:00
|
|
|
});
|
2015-03-06 12:20:18 -05:00
|
|
|
done();
|
2013-01-02 01:12:42 -05:00
|
|
|
});
|
2015-04-26 09:02:59 -04:00
|
|
|
it.skip("connects successfully - offline mode (STUBBED)", function(done) {
|
2015-09-20 14:26:31 -04:00
|
|
|
wrap.startServer({'online-mode': 'false'}, function(err) {
|
|
|
|
if(err)
|
|
|
|
return done(err);
|
2013-01-02 02:19:47 -05:00
|
|
|
var client = mc.createClient({
|
2013-01-30 19:56:43 -05:00
|
|
|
username: 'Player',
|
2013-01-02 02:19:47 -05:00
|
|
|
});
|
2015-09-14 15:30:10 -04:00
|
|
|
wrap.on('line', function(line) {
|
2013-12-30 10:05:22 -05:00
|
|
|
var match = line.match(/\[Server thread\/INFO\]: <(.+?)> (.+)/);
|
2015-05-14 16:08:49 -04:00
|
|
|
if(!match) return;
|
2013-01-30 19:56:43 -05:00
|
|
|
assert.strictEqual(match[1], 'Player');
|
2013-01-02 02:19:47 -05:00
|
|
|
assert.strictEqual(match[2], "hello everyone; I have logged in.");
|
2015-09-14 15:30:10 -04:00
|
|
|
wrap.writeServer("say hello\n");
|
2013-01-02 02:19:47 -05:00
|
|
|
});
|
|
|
|
var chatCount = 0;
|
2015-02-19 20:05:15 -05:00
|
|
|
client.on('login', function(packet) {
|
2015-05-14 16:08:49 -04:00
|
|
|
assert.strictEqual(packet.levelType, 'default');
|
|
|
|
assert.strictEqual(packet.difficulty, 1);
|
|
|
|
assert.strictEqual(packet.dimension, 0);
|
|
|
|
assert.strictEqual(packet.gameMode, 0);
|
2015-09-12 14:04:25 -04:00
|
|
|
client.write('chat', {
|
2015-05-14 16:08:49 -04:00
|
|
|
message: "hello everyone; I have logged in."
|
|
|
|
});
|
2013-01-03 20:59:58 -05:00
|
|
|
});
|
2015-02-19 20:05:15 -05:00
|
|
|
client.on('chat', function(packet) {
|
2013-01-03 20:59:58 -05:00
|
|
|
chatCount += 1;
|
|
|
|
assert.ok(chatCount <= 2);
|
2013-07-08 04:55:42 -04:00
|
|
|
var message = JSON.parse(packet.message);
|
2015-05-14 16:08:49 -04:00
|
|
|
if(chatCount === 1) {
|
2013-07-08 04:55:42 -04:00
|
|
|
assert.strictEqual(message.translate, "chat.type.text");
|
2013-12-30 10:05:22 -05:00
|
|
|
assert.deepEqual(message["with"][0], {
|
|
|
|
clickEvent: {
|
|
|
|
action: "suggest_command",
|
|
|
|
value: "/msg Player "
|
|
|
|
},
|
|
|
|
text: "Player"
|
|
|
|
});
|
|
|
|
assert.strictEqual(message["with"][1], "hello everyone; I have logged in.");
|
2015-05-14 16:08:49 -04:00
|
|
|
} else if(chatCount === 2) {
|
2013-07-08 04:55:42 -04:00
|
|
|
assert.strictEqual(message.translate, "chat.type.announcement");
|
2013-12-30 10:05:22 -05:00
|
|
|
assert.strictEqual(message["with"][0], "Server");
|
2015-05-14 16:08:49 -04:00
|
|
|
assert.deepEqual(message["with"][1], {
|
|
|
|
text: "",
|
2013-12-30 10:05:22 -05:00
|
|
|
extra: ["hello"]
|
|
|
|
});
|
2013-01-03 20:59:58 -05:00
|
|
|
done();
|
2013-01-02 02:19:47 -05:00
|
|
|
}
|
|
|
|
});
|
2015-04-26 09:02:59 -04:00
|
|
|
});
|
2015-03-06 12:20:18 -05:00
|
|
|
done();
|
2013-01-02 02:19:47 -05:00
|
|
|
});
|
2013-01-02 02:33:40 -05:00
|
|
|
it("gets kicked when no credentials supplied in online mode", function(done) {
|
2015-09-20 14:26:31 -04:00
|
|
|
wrap.startServer({'online-mode': 'true'}, function(err) {
|
|
|
|
if(err)
|
|
|
|
return done(err);
|
2013-01-02 02:33:40 -05:00
|
|
|
var client = mc.createClient({
|
2013-01-30 19:56:43 -05:00
|
|
|
username: 'Player',
|
2013-01-02 02:33:40 -05:00
|
|
|
});
|
|
|
|
var gotKicked = false;
|
2015-09-19 18:41:03 -04:00
|
|
|
client.on('disconnect', function(packet) {
|
2015-09-14 16:48:00 -04:00
|
|
|
assert.ok(packet.reason.indexOf('"Failed to verify username!"')!=-1);
|
2013-01-03 20:59:58 -05:00
|
|
|
gotKicked = true;
|
2013-01-02 02:33:40 -05:00
|
|
|
});
|
|
|
|
client.on('end', function() {
|
|
|
|
assert.ok(gotKicked);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-01-02 03:30:33 -05:00
|
|
|
it("does not crash for " + SURVIVE_TIME + "ms", function(done) {
|
2015-09-20 14:26:31 -04:00
|
|
|
wrap.startServer({'online-mode': 'false'}, function(err) {
|
|
|
|
if(err)
|
|
|
|
return done(err);
|
2013-01-02 03:30:33 -05:00
|
|
|
var client = mc.createClient({
|
2013-01-30 19:56:43 -05:00
|
|
|
username: 'Player',
|
2013-01-02 03:30:33 -05:00
|
|
|
});
|
2015-09-12 14:04:25 -04:00
|
|
|
client.on("login", function(packet) {
|
|
|
|
client.write("chat", {
|
2013-01-03 20:59:58 -05:00
|
|
|
message: "hello everyone; I have logged in."
|
|
|
|
});
|
|
|
|
});
|
2015-09-12 14:04:25 -04:00
|
|
|
client.on("chat", function(packet) {
|
2013-07-08 04:55:42 -04:00
|
|
|
var message = JSON.parse(packet.message);
|
|
|
|
assert.strictEqual(message.translate, "chat.type.text");
|
2015-03-06 12:20:18 -05:00
|
|
|
/*assert.deepEqual(message["with"][0], {
|
2015-05-14 16:08:49 -04:00
|
|
|
clickEvent: {
|
|
|
|
action: "suggest_command",
|
|
|
|
value: "/msg Player "
|
|
|
|
},
|
|
|
|
text: "Player"
|
|
|
|
});*/
|
2013-12-30 10:05:22 -05:00
|
|
|
assert.strictEqual(message["with"][1], "hello everyone; I have logged in.");
|
2013-01-03 20:59:58 -05:00
|
|
|
setTimeout(function() {
|
|
|
|
done();
|
|
|
|
}, SURVIVE_TIME);
|
2013-01-02 03:30:33 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-01-02 01:12:42 -05:00
|
|
|
});
|
2013-01-04 21:53:51 -05:00
|
|
|
describe("mc-server", function() {
|
2013-01-04 20:55:53 -05:00
|
|
|
it("starts listening and shuts down cleanly", function(done) {
|
2015-05-14 16:08:49 -04:00
|
|
|
var server = mc.createServer({'online-mode': false});
|
2013-01-04 20:22:19 -05:00
|
|
|
var listening = false;
|
|
|
|
server.on('listening', function() {
|
|
|
|
listening = true;
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
server.on('close', function() {
|
|
|
|
assert.ok(listening);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-01-04 20:55:53 -05:00
|
|
|
it("kicks clients that do not log in", function(done) {
|
|
|
|
var server = mc.createServer({
|
|
|
|
'online-mode': false,
|
2013-01-04 21:33:19 -05:00
|
|
|
kickTimeout: 100,
|
|
|
|
checkTimeoutInterval: 10,
|
2013-01-04 20:55:53 -05:00
|
|
|
});
|
2013-01-04 21:33:19 -05:00
|
|
|
var count = 2;
|
2013-01-04 20:55:53 -05:00
|
|
|
server.on('connection', function(client) {
|
|
|
|
client.on('end', function(reason) {
|
2015-08-08 11:55:06 -04:00
|
|
|
assert.strictEqual(reason, '{"text":"LoginTimeout"}');
|
2013-01-04 21:33:19 -05:00
|
|
|
server.close();
|
2013-01-04 20:55:53 -05:00
|
|
|
});
|
|
|
|
});
|
2013-01-04 21:33:19 -05:00
|
|
|
server.on('close', function() {
|
|
|
|
resolve();
|
|
|
|
});
|
2013-01-04 20:55:53 -05:00
|
|
|
server.on('listening', function() {
|
|
|
|
var client = new mc.Client();
|
|
|
|
client.on('end', function() {
|
2013-01-04 21:33:19 -05:00
|
|
|
resolve();
|
2013-01-04 20:55:53 -05:00
|
|
|
});
|
2015-03-06 12:20:18 -05:00
|
|
|
client.connect(25565, '127.0.0.1');
|
2013-01-04 20:55:53 -05:00
|
|
|
});
|
2013-01-04 21:33:19 -05:00
|
|
|
|
|
|
|
function resolve() {
|
|
|
|
count -= 1;
|
2015-05-14 16:08:49 -04:00
|
|
|
if(count <= 0) done();
|
2013-01-04 21:33:19 -05:00
|
|
|
}
|
|
|
|
});
|
2013-01-04 21:53:51 -05:00
|
|
|
it("kicks clients that do not send keepalive packets", function(done) {
|
|
|
|
var server = mc.createServer({
|
|
|
|
'online-mode': false,
|
|
|
|
kickTimeout: 100,
|
|
|
|
checkTimeoutInterval: 10,
|
|
|
|
});
|
|
|
|
var count = 2;
|
|
|
|
server.on('connection', function(client) {
|
|
|
|
client.on('end', function(reason) {
|
2015-08-08 11:55:06 -04:00
|
|
|
assert.strictEqual(reason, '{"text":"KeepAliveTimeout"}');
|
2013-01-04 21:53:51 -05:00
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
server.on('close', function() {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
server.on('listening', function() {
|
|
|
|
var client = mc.createClient({
|
|
|
|
username: 'superpants',
|
2015-03-06 12:20:18 -05:00
|
|
|
host: '127.0.0.1',
|
|
|
|
port: 25565,
|
2013-01-04 21:53:51 -05:00
|
|
|
keepAlive: false,
|
|
|
|
});
|
|
|
|
client.on('end', function() {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
function resolve() {
|
|
|
|
count -= 1;
|
2015-05-14 16:08:49 -04:00
|
|
|
if(count <= 0) done();
|
2013-01-04 21:53:51 -05:00
|
|
|
}
|
|
|
|
});
|
2013-01-04 21:33:19 -05:00
|
|
|
it("responds to ping requests", function(done) {
|
|
|
|
var server = mc.createServer({
|
|
|
|
'online-mode': false,
|
|
|
|
motd: 'test1234',
|
|
|
|
'max-players': 120,
|
|
|
|
});
|
|
|
|
server.on('listening', function() {
|
2015-03-06 12:20:18 -05:00
|
|
|
mc.ping({host: '127.0.0.1'}, function(err, results) {
|
2015-05-14 16:08:49 -04:00
|
|
|
if(err) return done(err);
|
2013-01-28 19:24:03 -05:00
|
|
|
assert.ok(results.latency >= 0);
|
|
|
|
assert.ok(results.latency <= 1000);
|
|
|
|
delete results.latency;
|
2013-01-04 21:33:19 -05:00
|
|
|
assert.deepEqual(results, {
|
2015-07-29 12:37:16 -04:00
|
|
|
version: {
|
|
|
|
name: mc.minecraftVersion,
|
|
|
|
protocol: mc.version
|
2013-12-30 10:05:22 -05:00
|
|
|
},
|
|
|
|
players: {
|
|
|
|
max: 120,
|
|
|
|
online: 0,
|
|
|
|
sample: []
|
|
|
|
},
|
2015-05-14 16:08:49 -04:00
|
|
|
description: {text: "test1234"}
|
2013-01-04 21:33:19 -05:00
|
|
|
});
|
2013-01-04 22:47:54 -05:00
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
server.on('close', done);
|
|
|
|
});
|
|
|
|
it("clients can log in and chat", function(done) {
|
2015-05-14 16:08:49 -04:00
|
|
|
var server = mc.createServer({'online-mode': false,});
|
2013-01-04 22:47:54 -05:00
|
|
|
var username = ['player1', 'player2'];
|
|
|
|
var index = 0;
|
|
|
|
server.on('login', function(client) {
|
|
|
|
assert.notEqual(client.id, null);
|
|
|
|
assert.strictEqual(client.username, username[index++]);
|
|
|
|
broadcast(client.username + ' joined the game.');
|
|
|
|
client.on('end', function() {
|
|
|
|
broadcast(client.username + ' left the game.', client);
|
2015-05-14 16:08:49 -04:00
|
|
|
if(client.username === 'player2') server.close();
|
2013-01-04 22:47:54 -05:00
|
|
|
});
|
2015-09-12 14:04:25 -04:00
|
|
|
client.write('login', {
|
2013-01-04 22:47:54 -05:00
|
|
|
entityId: client.id,
|
|
|
|
levelType: 'default',
|
|
|
|
gameMode: 1,
|
|
|
|
dimension: 0,
|
|
|
|
difficulty: 2,
|
2015-03-06 19:03:55 -05:00
|
|
|
maxPlayers: server.maxPlayers,
|
|
|
|
reducedDebugInfo: 0
|
2013-01-04 22:47:54 -05:00
|
|
|
});
|
2015-09-12 14:04:25 -04:00
|
|
|
client.on('chat', function(packet) {
|
2013-01-04 22:47:54 -05:00
|
|
|
var message = '<' + client.username + '>' + ' ' + packet.message;
|
|
|
|
broadcast(message);
|
2013-01-04 21:33:19 -05:00
|
|
|
});
|
|
|
|
});
|
2013-01-04 22:47:54 -05:00
|
|
|
server.on('close', done);
|
|
|
|
server.on('listening', function() {
|
2015-05-14 16:08:49 -04:00
|
|
|
var player1 = mc.createClient({username: 'player1', host: '127.0.0.1'});
|
2015-09-12 14:04:25 -04:00
|
|
|
player1.on('login', function(packet) {
|
2013-01-04 22:47:54 -05:00
|
|
|
assert.strictEqual(packet.gameMode, 1);
|
|
|
|
assert.strictEqual(packet.levelType, 'default');
|
|
|
|
assert.strictEqual(packet.dimension, 0);
|
|
|
|
assert.strictEqual(packet.difficulty, 2);
|
2015-09-12 14:04:25 -04:00
|
|
|
player1.once('chat', function(packet) {
|
2013-12-30 10:05:22 -05:00
|
|
|
assert.strictEqual(packet.message, '{"text":"player2 joined the game."}');
|
2015-09-12 14:04:25 -04:00
|
|
|
player1.once('chat', function(packet) {
|
2013-12-30 10:05:22 -05:00
|
|
|
assert.strictEqual(packet.message, '{"text":"<player2> hi"}');
|
2015-09-12 14:04:25 -04:00
|
|
|
player2.once('chat', fn);
|
2013-01-05 15:52:46 -05:00
|
|
|
function fn(packet) {
|
2015-05-14 16:08:49 -04:00
|
|
|
if(/<player2>/.test(packet.message)) {
|
2015-09-12 14:04:25 -04:00
|
|
|
player2.once('chat', fn);
|
2013-01-05 15:52:46 -05:00
|
|
|
return;
|
|
|
|
}
|
2013-12-30 10:05:22 -05:00
|
|
|
assert.strictEqual(packet.message, '{"text":"<player1> hello"}');
|
2015-09-12 14:04:25 -04:00
|
|
|
player1.once('chat', function(packet) {
|
2013-12-30 10:05:22 -05:00
|
|
|
assert.strictEqual(packet.message, '{"text":"player2 left the game."}');
|
2013-01-05 15:52:46 -05:00
|
|
|
player1.end();
|
2013-01-04 22:47:54 -05:00
|
|
|
});
|
2013-01-05 15:52:46 -05:00
|
|
|
player2.end();
|
|
|
|
}
|
2015-05-14 16:08:49 -04:00
|
|
|
|
2015-09-12 14:04:25 -04:00
|
|
|
player1.write('chat', {message: "hello"});
|
2013-01-04 22:47:54 -05:00
|
|
|
});
|
2015-09-12 14:04:25 -04:00
|
|
|
player2.write('chat', {message: "hi"});
|
2013-01-04 22:47:54 -05:00
|
|
|
});
|
2015-05-14 16:08:49 -04:00
|
|
|
var player2 = mc.createClient({username: 'player2', host: '127.0.0.1'});
|
2013-01-04 22:47:54 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function broadcast(message, exclude) {
|
|
|
|
var client;
|
2015-05-14 16:08:49 -04:00
|
|
|
for(var clientId in server.clients) {
|
|
|
|
if(!server.clients.hasOwnProperty(clientId)) continue;
|
2013-11-19 18:06:57 -05:00
|
|
|
|
2013-01-04 22:47:54 -05:00
|
|
|
client = server.clients[clientId];
|
2015-09-12 14:04:25 -04:00
|
|
|
if(client !== exclude) client.write('chat', {message: JSON.stringify({text: message}), position: 0});
|
2013-01-04 22:47:54 -05:00
|
|
|
}
|
|
|
|
}
|
2013-01-04 20:55:53 -05:00
|
|
|
});
|
2013-01-26 22:46:47 -05:00
|
|
|
it("kicks clients when invalid credentials", function(done) {
|
2015-03-06 12:20:18 -05:00
|
|
|
this.timeout(10000);
|
2013-01-26 22:46:47 -05:00
|
|
|
var server = mc.createServer();
|
2013-01-30 19:56:43 -05:00
|
|
|
var count = 4;
|
2013-01-26 22:46:47 -05:00
|
|
|
server.on('connection', function(client) {
|
|
|
|
client.on('end', function(reason) {
|
|
|
|
resolve();
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
server.on('close', function() {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
server.on('listening', function() {
|
|
|
|
resolve();
|
|
|
|
var client = mc.createClient({
|
|
|
|
username: 'lalalal',
|
2015-03-06 12:20:18 -05:00
|
|
|
host: "127.0.0.1"
|
2013-01-26 22:46:47 -05:00
|
|
|
});
|
|
|
|
client.on('end', function() {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
function resolve() {
|
|
|
|
count -= 1;
|
2015-05-14 16:08:49 -04:00
|
|
|
if(count <= 0) done();
|
2013-01-26 22:46:47 -05:00
|
|
|
}
|
|
|
|
});
|
2013-01-04 22:53:54 -05:00
|
|
|
it("gives correct reason for kicking clients when shutting down", function(done) {
|
2015-05-14 16:08:49 -04:00
|
|
|
var server = mc.createServer({'online-mode': false,});
|
2013-01-04 22:53:54 -05:00
|
|
|
var count = 2;
|
|
|
|
server.on('login', function(client) {
|
|
|
|
client.on('end', function(reason) {
|
2015-08-08 11:55:06 -04:00
|
|
|
assert.strictEqual(reason, '{"text":"ServerShutdown"}');
|
2013-01-04 22:53:54 -05:00
|
|
|
resolve();
|
|
|
|
});
|
2015-09-12 14:04:25 -04:00
|
|
|
client.write('login', {
|
2013-01-04 22:53:54 -05:00
|
|
|
entityId: client.id,
|
|
|
|
levelType: 'default',
|
|
|
|
gameMode: 1,
|
|
|
|
dimension: 0,
|
|
|
|
difficulty: 2,
|
2015-03-06 19:03:55 -05:00
|
|
|
maxPlayers: server.maxPlayers,
|
|
|
|
reducedDebugInfo: 0
|
2013-01-04 22:53:54 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
server.on('close', function() {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
server.on('listening', function() {
|
2015-05-14 16:08:49 -04:00
|
|
|
var client = mc.createClient({username: 'lalalal', host: '127.0.0.1'});
|
2015-09-12 14:04:25 -04:00
|
|
|
client.on('login', function() {
|
2013-01-04 22:53:54 -05:00
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
function resolve() {
|
|
|
|
count -= 1;
|
2015-05-14 16:08:49 -04:00
|
|
|
if(count <= 0) done();
|
2013-01-04 22:53:54 -05:00
|
|
|
}
|
|
|
|
});
|
2013-01-04 19:04:06 -05:00
|
|
|
});
|