<commit_before>"use strict";
/* eslint-disable no-undef, no-console */

var expect = require("chai").expect;
var net = require("net");
var TcpServer = require("./../../servers/servertcp");

describe("Modbus TCP Server", function() {
    var serverTCP; // eslint-disable-line no-unused-vars

    before(function() {
        var vector = {
            getInputRegister: function(addr, callback) {
                setTimeout(function() {
                    callback(addr);
                }, 50);
            },
            getHoldingRegister: function(addr, callback) {
                setTimeout(function() {
                    callback(addr + 8000);
                }, 50);
            },
            getCoil: function(addr, callback) {
                setTimeout(function() {
                    callback((addr % 2) === 0);
                }, 50);
            },
            setRegister: function(addr, value, unit, callback) {
                setTimeout(function() {
                    console.log("set register", addr, value);
                    callback();
                }, 50);
            },
            setCoil: function(addr, value, unit, callback) {
                setTimeout(function() {console.log("set coil", addr, value);
                    callback();
                }, 50);
            }
        };
        serverTCP = new TcpServer(vector, { host: "0.0.0.0", port: 8512, debug: true, unitID: 1 });
    });

    describe("function code handler", function() {
        it("should receive a valid Modbus TCP message", function(done) {
            const client = net.connect({ host: "0.0.0.0", port: 8512 }, function() {
                // FC05 - force single coil, to on 0xff00
                client.write(new Buffer("00010000000601050005ff00", "hex"));
            });

            client.once("data", function(data) {
                // FC05 - valid responce
                expect(data.toString("hex")).to.equal("00010000000601050005ff00");
                done();
            });
        });

        // TODO: FC1 - FCX tests
    });

    describe("modbus exception handler", function() {
        it("should receive a valid Modbus TCP message", function(done) {
            const client = net.connect({ host: "0.0.0.0", port: 8512 }, function() {
                // FC07 - unhandled function
                client.write(new Buffer("000100000006010700000000", "hex"));
            });

            client.once("data", function(data) {
                // A valid error message, code 0x01 - Illegal fanction
                expect(data.toString("hex")).to.equal("000100000003018701");
                done();
            });
        });

        // TODO: exceptions
    });

    describe("socket connection error", function() {
        it("should receive an error event on socket closed by client", function(done) {
            var client = net.connect({ host: "0.0.0.0", port: 8512 }, function() {
                client.destroy();

                serverTCP.emit("socketError");
            });

            serverTCP.on("socketError", function() {
                // Error handled correctly
                done();
            });<commit_msg>Changed server port.<commit_after>"use strict";
/* eslint-disable no-undef, no-console */

var expect = require("chai").expect;
var net = require("net");
var TcpServer = require("./../../servers/servertcp");

describe("Modbus TCP Server Callbacks", function() {
    var serverTCP; // eslint-disable-line no-unused-vars

    before(function() {
        var vector = {
            getInputRegister: function(addr, callback) {
                setTimeout(function() {
                    callback(addr);
                }, 50);
            },
            getHoldingRegister: function(addr, callback) {
                setTimeout(function() {
                    callback(addr + 8000);
                }, 50);
            },
            getCoil: function(addr, callback) {
                setTimeout(function() {
                    callback((addr % 2) === 0);
                }, 50);
            },
            setRegister: function(addr, value, unit, callback) {
                setTimeout(function() {
                    console.log("set register", addr, value);
                    callback();
                }, 50);
            },
            setCoil: function(addr, value, unit, callback) {
                setTimeout(function() {console.log("set coil", addr, value);
                    callback();
                }, 50);
            }
        };
        serverTCP = new TcpServer(vector, { host: "0.0.0.0", port: 8513, debug: true, unitID: 1 });
    });

    describe("function code handler", function() {
        it("should receive a valid Modbus TCP message", function(done) {
            const client = net.connect({ host: "0.0.0.0", port: 8513 }, function() {
                // FC05 - force single coil, to on 0xff00
                client.write(new Buffer("00010000000601050005ff00", "hex"));
            });

            client.once("data", function(data) {
                // FC05 - valid responce
                expect(data.toString("hex")).to.equal("00010000000601050005ff00");
                done();
            });
        });

        // TODO: FC1 - FCX tests
    });

    describe("modbus exception handler", function() {
        it("should receive a valid Modbus TCP message", function(done) {
            const client = net.connect({ host: "0.0.0.0", port: 8513 }, function() {
                // FC07 - unhandled function
                client.write(new Buffer("000100000006010700000000", "hex"));
            });

            client.once("data", function(data) {
                // A valid error message, code 0x01 - Illegal fanction
                expect(data.toString("hex")).to.equal("000100000003018701");
                done();
            });
        });

        // TODO: exceptions
    });

    describe("socket connection error", function() {
        it("should receive an error event on socket closed by client", function(done) {
            var client = net.connect({ host: "0.0.0.0", port: 8513 }, function() {
                client.destroy();

                serverTCP.emit("socketError");
            });

            serverTCP.on("socketError", function() {
                // Error handled correctly
                done();
            });
{'fraction_non_alphanumeric': 0.13894571203776554, 'fraction_numerical': 0.04657749803304485, 'mean_word_length': 1.5764085934333198, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 8, 'substrings_counts': 10, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5882486', 'n_tokens_mistral': 1849, 'n_tokens_neox': 1584, 'n_words': 389}