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')
|
2023-12-27 18:48:10 -05:00
|
|
|
const fs = require('fs')
|
2018-05-13 16:50:16 -04:00
|
|
|
const path = require('path')
|
|
|
|
const assert = require('power-assert')
|
2021-06-28 18:27:37 -04:00
|
|
|
const util = require('util')
|
2022-08-15 18:57:26 -04:00
|
|
|
const applyClientHelpers = require('./common/clientHelpers')
|
|
|
|
const download = util.promisify(require('minecraft-wrap').download)
|
|
|
|
const { getPort } = require('./common/util')
|
|
|
|
|
|
|
|
const SURVIVE_TIME = 10000
|
2018-05-13 16:50:16 -04:00
|
|
|
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
|
|
|
|
2021-06-12 22:54:16 -04:00
|
|
|
for (const supportedVersion of mc.supportedVersions) {
|
2021-06-28 18:27:37 -04:00
|
|
|
let PORT = null
|
2018-05-13 16:50:16 -04:00
|
|
|
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'
|
2023-12-27 18:48:10 -05:00
|
|
|
const MC_SERVER_DIR = MC_SERVER_PATH + '_' + supportedVersion
|
|
|
|
const wrap = new Wrap(MC_SERVER_JAR, MC_SERVER_DIR, {
|
2018-08-25 20:39:52 -04:00
|
|
|
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
|
|
|
|
2023-12-27 18:12:23 -05:00
|
|
|
describe('client ' + supportedVersion + 'v', function () {
|
2018-05-13 16:50:16 -04:00
|
|
|
this.timeout(10 * 60 * 1000)
|
2015-10-01 20:02:54 -04:00
|
|
|
|
2021-06-28 18:27:37 -04:00
|
|
|
before(async function () {
|
|
|
|
this.timeout(30 * 1000)
|
|
|
|
await download(version.minecraftVersion, MC_SERVER_JAR)
|
|
|
|
PORT = await getPort()
|
|
|
|
console.log(`Port chosen: ${PORT}`)
|
|
|
|
})
|
2015-10-01 20:02:54 -04:00
|
|
|
|
2021-06-28 18:27:37 -04:00
|
|
|
after(async () => {
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
wrap.deleteServerData(err => {
|
|
|
|
if (err) reject(err)
|
|
|
|
resolve()
|
|
|
|
})
|
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('offline', function () {
|
2021-12-12 13:07:50 -05:00
|
|
|
this.timeout(240 * 1000)
|
2021-06-28 18:27:37 -04:00
|
|
|
before(async () => {
|
2018-08-25 20:39:52 -04:00
|
|
|
console.log(new Date() + 'starting server ' + version.minecraftVersion)
|
2021-06-28 18:27:37 -04:00
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
wrap.startServer({
|
|
|
|
'online-mode': 'false',
|
|
|
|
'server-port': PORT,
|
|
|
|
motd: 'test1234',
|
|
|
|
'max-players': 120,
|
|
|
|
'use-native-transport': 'false' // java 16 throws errors without this, https://www.spigotmc.org/threads/unable-to-access-address-of-buffer.311602
|
|
|
|
}, (err) => {
|
|
|
|
if (err) reject(err)
|
|
|
|
resolve()
|
|
|
|
})
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
2021-06-28 18:27:37 -04:00
|
|
|
console.log(new Date() + 'started server ' + version.minecraftVersion)
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
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) {
|
2022-08-15 18:57:26 -04:00
|
|
|
const client = applyClientHelpers(mc.createClient({
|
2015-10-01 20:02:54 -04:00
|
|
|
username: 'Player',
|
2016-02-23 13:37:47 -05:00
|
|
|
version: version.minecraftVersion,
|
2022-07-29 12:03:20 -04:00
|
|
|
port: PORT,
|
|
|
|
auth: 'offline'
|
2022-08-15 18:57:26 -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) {
|
2023-01-21 14:31:17 -05:00
|
|
|
const match = line.match(/\[Server thread\/INFO\]: (?:\[Not Secure\] )?<(.+?)> (.+)/)
|
2018-05-13 16:50:16 -04:00
|
|
|
if (!match) return
|
|
|
|
assert.strictEqual(match[1], 'Player')
|
|
|
|
assert.strictEqual(match[2], 'hello everyone; I have logged in.')
|
|
|
|
wrap.writeServer('say hello\n')
|
2022-08-15 18:57:26 -04:00
|
|
|
wrap.off('line', lineListener)
|
2018-05-13 16:50:16 -04:00
|
|
|
}
|
|
|
|
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)
|
2022-08-15 18:57:26 -04:00
|
|
|
client.chat('hello everyone; I have logged in.')
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
2023-12-27 18:48:10 -05:00
|
|
|
// Dump some data for easier debugging
|
|
|
|
client.on('raw.registry_data', (buffer) => {
|
|
|
|
fs.writeFileSync(MC_SERVER_DIR + '_registry_data.bin', buffer)
|
|
|
|
})
|
|
|
|
client.on('registry_data', (json) => {
|
|
|
|
fs.writeFileSync(MC_SERVER_DIR + '_registry_data.json', JSON.stringify(json))
|
|
|
|
})
|
|
|
|
client.on('login', (packet) => {
|
|
|
|
fs.writeFileSync(MC_SERVER_DIR + '_login.json', JSON.stringify(packet))
|
|
|
|
if (fs.existsSync(MC_SERVER_DIR + '_registry_data.json')) {
|
|
|
|
// generate a loginPacket.json for minecraft-data
|
|
|
|
fs.writeFileSync(MC_SERVER_DIR + '_loginPacket.json', JSON.stringify({
|
|
|
|
...packet,
|
|
|
|
dimensionCodec: JSON.parse(fs.readFileSync(MC_SERVER_DIR + '_registry_data.json')).codec
|
|
|
|
}, null, 2))
|
|
|
|
}
|
|
|
|
})
|
2023-01-21 14:31:17 -05:00
|
|
|
client.on('playerChat', function (data) {
|
2018-05-13 16:50:16 -04:00
|
|
|
chatCount += 1
|
|
|
|
assert.ok(chatCount <= 2)
|
2023-01-21 14:31:17 -05:00
|
|
|
|
|
|
|
if (!mcData.supportFeature('clientsideChatFormatting')) {
|
|
|
|
const message = JSON.parse(data.formattedMessage)
|
|
|
|
if (chatCount === 1) {
|
|
|
|
assert.strictEqual(message.translate, 'chat.type.text')
|
|
|
|
assert.deepEqual(message.with[0].clickEvent, {
|
|
|
|
action: 'suggest_command',
|
|
|
|
value: mcData.version.version > 340 ? '/tell Player ' : '/msg Player '
|
|
|
|
})
|
|
|
|
assert.deepEqual(message.with[0].text, 'Player')
|
|
|
|
assert.strictEqual(message.with[1], 'hello everyone; I have logged in.')
|
|
|
|
} else if (chatCount === 2) {
|
|
|
|
assert.strictEqual(message.translate, 'chat.type.announcement')
|
|
|
|
assert.strictEqual(message.with[0].text ? message.with[0].text : message.with[0], 'Server')
|
|
|
|
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')
|
|
|
|
wrap.removeListener('line', lineListener)
|
|
|
|
client.end()
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// 1.19+
|
|
|
|
|
|
|
|
const message = JSON.parse(data.formattedMessage || JSON.stringify({ text: data.plainMessage }))
|
|
|
|
|
|
|
|
if (chatCount === 1) {
|
|
|
|
assert.strictEqual(message.text, 'hello everyone; I have logged in.')
|
|
|
|
const sender = JSON.parse(data.senderName)
|
|
|
|
assert.deepEqual(sender.clickEvent, {
|
|
|
|
action: 'suggest_command',
|
|
|
|
value: '/tell Player '
|
|
|
|
})
|
|
|
|
assert.strictEqual(sender.text, 'Player')
|
|
|
|
} else if (chatCount === 2) {
|
|
|
|
assert.strictEqual(message.text, 'hello')
|
|
|
|
const sender = JSON.parse(data.senderName)
|
|
|
|
assert.strictEqual(sender.text, 'Server')
|
|
|
|
wrap.removeListener('line', lineListener)
|
|
|
|
client.end()
|
|
|
|
done()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
client.on('systemChat', function (data) {
|
|
|
|
// For 1.7.10
|
|
|
|
chatCount += 1
|
|
|
|
assert.ok(chatCount <= 2)
|
|
|
|
|
|
|
|
const message = JSON.parse(data.formattedMessage)
|
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) {
|
2022-08-15 18:57:26 -04:00
|
|
|
const client = applyClientHelpers(mc.createClient({
|
2015-10-01 20:02:54 -04:00
|
|
|
username: 'Player',
|
2016-02-23 13:37:47 -05:00
|
|
|
version: version.minecraftVersion,
|
2022-07-29 12:03:20 -04:00
|
|
|
port: PORT,
|
|
|
|
auth: 'offline'
|
2022-08-15 18:57:26 -04:00
|
|
|
}))
|
2018-08-25 20:39:52 -04:00
|
|
|
client.on('error', err => done(err))
|
|
|
|
client.on('login', function () {
|
2022-08-15 18:57:26 -04:00
|
|
|
client.chat('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) {
|
2022-08-15 18:57:26 -04:00
|
|
|
if (process.platform === 'win32') return 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',
|
2022-07-29 12:03:20 -04:00
|
|
|
port: PORT,
|
|
|
|
auth: 'offline'
|
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
|
|
|
|
2021-06-28 18:27:37 -04:00
|
|
|
describe.skip('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',
|
2021-06-28 18:27:37 -04:00
|
|
|
'server-port': PORT,
|
|
|
|
'use-native-transport': 'false' // java 16 throws errors without this, https://www.spigotmc.org/threads/unable-to-access-address-of-buffer.311602
|
2017-06-19 09:28:24 -04:00
|
|
|
}, 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
|
|
|
|
2021-06-28 18:27:37 -04:00
|
|
|
it('connects successfully - online mode', function (done) {
|
2022-08-15 18:57:26 -04:00
|
|
|
const client = applyClientHelpers(mc.createClient({
|
2017-06-19 09:28:24 -04:00
|
|
|
username: process.env.MC_USERNAME,
|
|
|
|
password: process.env.MC_PASSWORD,
|
|
|
|
version: version.minecraftVersion,
|
2022-08-15 18:57:26 -04:00
|
|
|
port: PORT
|
|
|
|
}))
|
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)
|
2022-08-15 18:57:26 -04:00
|
|
|
client.chat('hello everyone; I have logged in.')
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
|
|
|
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,
|
2022-07-29 12:03:20 -04:00
|
|
|
port: PORT,
|
|
|
|
auth: 'offline'
|
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()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2021-06-12 22:54:16 -04:00
|
|
|
}
|