2018-05-13 16:50:16 -04:00
|
|
|
/* eslint-env mocha */
|
2015-10-01 20:02:54 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
const mc = require('../')
|
|
|
|
const os = require('os')
|
|
|
|
const path = require('path')
|
|
|
|
const assert = require('power-assert')
|
|
|
|
const SURVIVE_TIME = 10000
|
|
|
|
const MC_SERVER_PATH = path.join(__dirname, 'server')
|
2015-10-01 20:02:54 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
const Wrap = require('minecraft-wrap').Wrap
|
2017-05-09 20:49:13 -04:00
|
|
|
|
2018-09-24 16:07:34 -04:00
|
|
|
const { firstVersion, lastVersion } = require('./common/parallel')
|
2015-10-01 20:02:54 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
const download = require('minecraft-wrap').download
|
2015-10-01 20:02:54 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
mc.supportedVersions.forEach(function (supportedVersion, i) {
|
|
|
|
if (!(i >= firstVersion && i <= lastVersion)) { return }
|
2015-10-01 20:02:54 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
const PORT = Math.round(30000 + Math.random() * 20000)
|
|
|
|
const mcData = require('minecraft-data')(supportedVersion)
|
|
|
|
const version = mcData.version
|
|
|
|
const MC_SERVER_JAR_DIR = process.env.MC_SERVER_JAR_DIR || os.tmpdir()
|
|
|
|
const MC_SERVER_JAR = MC_SERVER_JAR_DIR + '/minecraft_server.' + version.minecraftVersion + '.jar'
|
2018-08-25 20:39:52 -04:00
|
|
|
const wrap = new Wrap(MC_SERVER_JAR, MC_SERVER_PATH + '_' + supportedVersion, {
|
|
|
|
minMem: 1024,
|
|
|
|
maxMem: 1024
|
|
|
|
})
|
2018-05-13 16:50:16 -04:00
|
|
|
wrap.on('line', function (line) {
|
|
|
|
console.log(line)
|
|
|
|
})
|
2015-10-01 20:02:54 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
describe('client ' + version.minecraftVersion, function () {
|
|
|
|
this.timeout(10 * 60 * 1000)
|
2015-10-01 20:02:54 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
before(download.bind(null, version.minecraftVersion, MC_SERVER_JAR))
|
2015-10-01 20:02:54 -04:00
|
|
|
|
2017-06-19 09:28:24 -04:00
|
|
|
after(function (done) {
|
|
|
|
wrap.deleteServerData(function (err) {
|
2018-05-13 16:50:16 -04:00
|
|
|
if (err) { console.log(err) }
|
|
|
|
done(err)
|
|
|
|
})
|
|
|
|
})
|
2017-06-19 09:28:24 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
describe('offline', function () {
|
2017-06-19 09:28:24 -04:00
|
|
|
before(function (done) {
|
2018-08-25 20:39:52 -04:00
|
|
|
console.log(new Date() + 'starting server ' + version.minecraftVersion)
|
2017-06-19 09:28:24 -04:00
|
|
|
wrap.startServer({
|
|
|
|
'online-mode': 'false',
|
|
|
|
'server-port': PORT,
|
2019-08-03 19:29:14 -04:00
|
|
|
motd: 'test1234',
|
2017-06-19 09:28:24 -04:00
|
|
|
'max-players': 120
|
|
|
|
}, function (err) {
|
2018-05-13 16:50:16 -04:00
|
|
|
if (err) { console.log(err) }
|
2018-08-25 20:39:52 -04:00
|
|
|
console.log(new Date() + 'started server ' + version.minecraftVersion)
|
2018-05-13 16:50:16 -04:00
|
|
|
done(err)
|
|
|
|
})
|
|
|
|
})
|
2017-06-19 09:28:24 -04:00
|
|
|
|
|
|
|
after(function (done) {
|
2018-08-25 20:39:52 -04:00
|
|
|
console.log(new Date() + 'stopping server' + version.minecraftVersion)
|
2017-06-19 09:28:24 -04:00
|
|
|
wrap.stopServer(function (err) {
|
2018-05-13 16:50:16 -04:00
|
|
|
if (err) { console.log(err) }
|
2018-08-25 20:39:52 -04:00
|
|
|
console.log(new Date() + 'stopped server ' + version.minecraftVersion)
|
2018-05-13 16:50:16 -04:00
|
|
|
done(err)
|
|
|
|
})
|
|
|
|
})
|
2017-06-19 09:28:24 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
it('pings the server', function (done) {
|
2015-10-01 20:02:54 -04:00
|
|
|
mc.ping({
|
2016-02-23 13:37:47 -05:00
|
|
|
version: version.minecraftVersion,
|
2017-06-19 09:28:24 -04:00
|
|
|
port: PORT
|
|
|
|
}, function (err, results) {
|
2018-05-13 16:50:16 -04:00
|
|
|
if (err) return done(err)
|
|
|
|
assert.ok(results.latency >= 0)
|
|
|
|
assert.ok(results.latency <= 1000)
|
|
|
|
delete results.latency
|
|
|
|
delete results.favicon // too lazy to figure it out
|
2015-10-01 20:02:54 -04:00
|
|
|
/* assert.deepEqual(results, {
|
|
|
|
version: {
|
|
|
|
name: '1.7.4',
|
|
|
|
protocol: 4
|
|
|
|
},
|
|
|
|
description: { text: "test1234" }
|
2018-05-13 16:50:16 -04:00
|
|
|
}); */
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
2017-06-19 09:28:24 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
it('connects successfully - offline mode', function (done) {
|
2017-07-13 08:03:52 -04:00
|
|
|
const client = mc.createClient({
|
2015-10-01 20:02:54 -04:00
|
|
|
username: 'Player',
|
2016-02-23 13:37:47 -05:00
|
|
|
version: version.minecraftVersion,
|
2017-06-19 09:28:24 -04:00
|
|
|
port: PORT
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
2018-08-25 20:39:52 -04:00
|
|
|
client.on('error', err => done(err))
|
2017-07-13 08:03:52 -04:00
|
|
|
const lineListener = function (line) {
|
2018-05-13 16:50:16 -04:00
|
|
|
const match = line.match(/\[Server thread\/INFO\]: <(.+?)> (.+)/)
|
|
|
|
if (!match) return
|
|
|
|
assert.strictEqual(match[1], 'Player')
|
|
|
|
assert.strictEqual(match[2], 'hello everyone; I have logged in.')
|
|
|
|
wrap.writeServer('say hello\n')
|
|
|
|
}
|
|
|
|
wrap.on('line', lineListener)
|
|
|
|
let chatCount = 0
|
2017-06-19 09:28:24 -04:00
|
|
|
client.on('login', function (packet) {
|
2018-05-13 16:50:16 -04:00
|
|
|
assert.strictEqual(packet.gameMode, 0)
|
2015-10-01 20:02:54 -04:00
|
|
|
client.write('chat', {
|
2018-05-13 16:50:16 -04:00
|
|
|
message: 'hello everyone; I have logged in.'
|
|
|
|
})
|
|
|
|
})
|
2017-06-19 09:28:24 -04:00
|
|
|
client.on('chat', function (packet) {
|
2018-05-13 16:50:16 -04:00
|
|
|
chatCount += 1
|
|
|
|
assert.ok(chatCount <= 2)
|
|
|
|
const message = JSON.parse(packet.message)
|
2017-06-19 09:28:24 -04:00
|
|
|
if (chatCount === 1) {
|
2018-05-13 16:50:16 -04:00
|
|
|
assert.strictEqual(message.translate, 'chat.type.text')
|
2019-08-20 18:14:37 -04:00
|
|
|
assert.deepEqual(message.with[0].clickEvent, {
|
2018-05-13 16:50:16 -04:00
|
|
|
action: 'suggest_command',
|
|
|
|
value: mcData.version.version > 340 ? '/tell Player ' : '/msg Player '
|
|
|
|
})
|
2019-08-20 18:14:37 -04:00
|
|
|
assert.deepEqual(message.with[0].text, 'Player')
|
|
|
|
assert.strictEqual(message.with[1], 'hello everyone; I have logged in.')
|
2017-06-19 09:28:24 -04:00
|
|
|
} else if (chatCount === 2) {
|
2018-05-13 16:50:16 -04:00
|
|
|
assert.strictEqual(message.translate, 'chat.type.announcement')
|
2019-08-20 18:14:37 -04:00
|
|
|
assert.strictEqual(message.with[0].text ? message.with[0].text : message.with[0], 'Server')
|
2020-10-24 09:44:33 -04:00
|
|
|
assert.deepEqual(message.with[1].extra
|
|
|
|
? (message.with[1].extra[0].text
|
|
|
|
? message.with[1].extra[0].text
|
|
|
|
: message.with[1].extra[0])
|
|
|
|
: message.with[1].text, 'hello')
|
2018-05-13 16:50:16 -04:00
|
|
|
wrap.removeListener('line', lineListener)
|
|
|
|
client.end()
|
|
|
|
done()
|
2015-10-01 20:02:54 -04:00
|
|
|
}
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
|
|
|
})
|
2017-06-19 09:28:24 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
it('does not crash for ' + SURVIVE_TIME + 'ms', function (done) {
|
2017-07-13 08:03:52 -04:00
|
|
|
const client = mc.createClient({
|
2015-10-01 20:02:54 -04:00
|
|
|
username: 'Player',
|
2016-02-23 13:37:47 -05:00
|
|
|
version: version.minecraftVersion,
|
2017-06-19 09:28:24 -04:00
|
|
|
port: PORT
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
2018-08-25 20:39:52 -04:00
|
|
|
client.on('error', err => done(err))
|
|
|
|
client.on('login', function () {
|
2018-05-13 16:50:16 -04:00
|
|
|
client.write('chat', {
|
|
|
|
message: 'hello everyone; I have logged in.'
|
|
|
|
})
|
2017-06-19 09:28:24 -04:00
|
|
|
setTimeout(function () {
|
2018-05-13 16:50:16 -04:00
|
|
|
client.end()
|
|
|
|
done()
|
|
|
|
}, SURVIVE_TIME)
|
|
|
|
})
|
|
|
|
})
|
2017-03-12 07:23:13 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
it('produce a decent error when connecting with the wrong version', function (done) {
|
2017-07-13 08:03:52 -04:00
|
|
|
const client = mc.createClient({
|
2017-03-12 07:23:13 -04:00
|
|
|
username: 'Player',
|
2018-05-13 16:50:16 -04:00
|
|
|
version: version.minecraftVersion === '1.8.8' ? '1.11.2' : '1.8.8',
|
2017-06-19 09:28:24 -04:00
|
|
|
port: PORT
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
|
|
|
client.once('error', function (err) {
|
|
|
|
if (err.message.startsWith('This server is version')) {
|
2018-08-25 20:39:52 -04:00
|
|
|
console.log(new Date() + 'Correctly got an error for wrong version : ' + err.message)
|
2018-08-25 20:23:39 -04:00
|
|
|
client.end()
|
2018-05-13 16:50:16 -04:00
|
|
|
done()
|
|
|
|
} else {
|
2018-08-25 20:23:39 -04:00
|
|
|
client.end()
|
2018-05-13 16:50:16 -04:00
|
|
|
done(err)
|
2017-03-12 07:23:13 -04:00
|
|
|
}
|
2018-08-25 20:39:52 -04:00
|
|
|
client.on('error', err => {
|
|
|
|
if (err.message.indexOf('ECONNRESET') === -1) {
|
|
|
|
done(err)
|
|
|
|
}
|
|
|
|
})
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2017-06-19 09:28:24 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
describe('online', function () {
|
2017-06-19 09:28:24 -04:00
|
|
|
before(function (done) {
|
2018-08-25 20:39:52 -04:00
|
|
|
console.log(new Date() + 'starting server ' + version.minecraftVersion)
|
2017-06-19 09:28:24 -04:00
|
|
|
wrap.startServer({
|
|
|
|
'online-mode': 'true',
|
|
|
|
'server-port': PORT
|
|
|
|
}, function (err) {
|
2018-05-13 16:50:16 -04:00
|
|
|
if (err) { console.log(err) }
|
2018-08-25 20:39:52 -04:00
|
|
|
console.log(new Date() + 'started server ' + version.minecraftVersion)
|
2018-05-13 16:50:16 -04:00
|
|
|
done(err)
|
|
|
|
})
|
|
|
|
})
|
2017-06-19 09:28:24 -04:00
|
|
|
|
|
|
|
after(function (done) {
|
2018-08-25 20:39:52 -04:00
|
|
|
console.log(new Date() + 'stopping server ' + version.minecraftVersion)
|
2017-06-19 09:28:24 -04:00
|
|
|
wrap.stopServer(function (err) {
|
2018-05-13 16:50:16 -04:00
|
|
|
if (err) { console.log(err) }
|
2018-08-25 20:39:52 -04:00
|
|
|
console.log(new Date() + 'stopped server ' + version.minecraftVersion)
|
2018-05-13 16:50:16 -04:00
|
|
|
done(err)
|
|
|
|
})
|
|
|
|
})
|
2017-06-19 09:28:24 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
it.skip('connects successfully - online mode', function (done) {
|
2017-07-13 08:03:52 -04:00
|
|
|
const client = mc.createClient({
|
2017-06-19 09:28:24 -04:00
|
|
|
username: process.env.MC_USERNAME,
|
|
|
|
password: process.env.MC_PASSWORD,
|
|
|
|
version: version.minecraftVersion,
|
|
|
|
port: PORT
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
2018-08-25 20:39:52 -04:00
|
|
|
client.on('error', err => done(err))
|
2017-07-13 08:03:52 -04:00
|
|
|
const lineListener = function (line) {
|
2018-05-13 16:50:16 -04:00
|
|
|
const match = line.match(/\[Server thread\/INFO\]: <(.+?)> (.+)/)
|
|
|
|
if (!match) return
|
|
|
|
assert.strictEqual(match[1], client.username)
|
|
|
|
assert.strictEqual(match[2], 'hello everyone; I have logged in.')
|
|
|
|
wrap.writeServer('say hello\n')
|
|
|
|
}
|
|
|
|
wrap.on('line', lineListener)
|
2017-06-19 09:28:24 -04:00
|
|
|
client.on('login', function (packet) {
|
2018-05-13 16:50:16 -04:00
|
|
|
assert.strictEqual(packet.levelType, 'default')
|
|
|
|
assert.strictEqual(packet.difficulty, 1)
|
|
|
|
assert.strictEqual(packet.dimension, 0)
|
|
|
|
assert.strictEqual(packet.gameMode, 0)
|
2017-06-19 09:28:24 -04:00
|
|
|
client.write('chat', {
|
2018-05-13 16:50:16 -04:00
|
|
|
message: 'hello everyone; I have logged in.'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
let chatCount = 0
|
2017-06-19 09:28:24 -04:00
|
|
|
client.on('chat', function (packet) {
|
2018-05-13 16:50:16 -04:00
|
|
|
chatCount += 1
|
|
|
|
assert.ok(chatCount <= 2)
|
2017-07-13 08:03:52 -04:00
|
|
|
if (chatCount === 2) {
|
2018-05-13 16:50:16 -04:00
|
|
|
client.removeAllListeners('chat')
|
|
|
|
wrap.removeListener('line', lineListener)
|
|
|
|
client.end()
|
|
|
|
done()
|
2017-06-19 09:28:24 -04:00
|
|
|
}
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
|
|
|
})
|
2017-06-19 09:28:24 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
it('gets kicked when no credentials supplied in online mode', function (done) {
|
2017-07-13 08:03:52 -04:00
|
|
|
const client = mc.createClient({
|
2017-06-19 09:28:24 -04:00
|
|
|
username: 'Player',
|
|
|
|
version: version.minecraftVersion,
|
|
|
|
port: PORT
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
2018-08-25 20:39:52 -04:00
|
|
|
client.on('error', err => done(err))
|
2018-05-13 16:50:16 -04:00
|
|
|
let gotKicked = false
|
2017-06-19 09:28:24 -04:00
|
|
|
client.on('disconnect', function (packet) {
|
2018-05-13 16:50:16 -04:00
|
|
|
assert.ok(packet.reason.indexOf('"Failed to verify username!"') !== -1 || packet.reason.indexOf('multiplayer.disconnect.unverified_username') !== -1)
|
|
|
|
gotKicked = true
|
|
|
|
})
|
2017-06-19 09:28:24 -04:00
|
|
|
client.on('end', function () {
|
2018-05-13 16:50:16 -04:00
|
|
|
assert.ok(gotKicked)
|
2018-08-25 20:23:39 -04:00
|
|
|
client.end()
|
2018-05-13 16:50:16 -04:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|