/*
 * Goal of this test suite is to test XModem transfer
 * using virtual serialport sockets created via socat
 * this suite only runs when the socat utility is available
 */
var commandExistsSync = require('command-exists').sync;

if(commandExistsSync('socat')) {
  describe('XMODEM Serialport sending - receiving', function() {
    const xmodemSend = require('../lib/index');
    const xmodemRecv = require('../lib/index');
    const serialport = require('serialport');
    var child;
    
    var sendPort = "";
    var sPort;
    var recvPort = "";
    var rPort;
    
    // Setting up virtual ports 
    
    it('setup pair of virtual serialports', function(done) {
      const spawn = require('child_process').spawn;
    
      child = spawn('socat', ['-d', '-d', 'pty,raw,echo=0', 'pty,raw,echo=0']);
      
      child.stderr.on('data', (data) => {
        // Socat returns the allocated PTY terminal references to stderr
        if(data.indexOf("N PTY is ") >= 0) {
          if(sendPort === "") {
            sendPort = String(data).substring(data.indexOf("N PTY is ") + 9, data.length - 1);
          }
          else if(recvPort === "") {
            recvPort = String(data).substring(data.indexOf("N PTY is ") + 9, data.length - 1);
            assert.include(sendPort, '/dev/');
            assert.include(recvPort, '/dev/');
            done();
          }
        }
      });
    });
    
    it('sending port device node should exist', function(done) {
      assert.equal(true, fs.existsSync(sendPort));
      done();
    });
    
    it('receiving port device node should exist', function(done) {
      assert.equal(true, fs.existsSync(sendPort));
      done();
    });
    
    it('should be possible to open serialports', function(done) {
      sPort = new serialport(sendPort, { baudRate: 9600 });
      rPort = new serialport(recvPort, { baudRate: 9600 });
      
      sPort.once('open', function() {
        sPort.write('We only see what we know', function(err) {
          if (err) {
            return console.log('Error on write: ', err.message);
          }
        });
      });
      
      rPort.once('data', function (data) {
        assert.equal('We only see what we know', data);
        done();
      });
      
    });
    
    // Transfer (CRC)
    
    it('transfer should succeed - crc', function(done) {
      var buffer = fs.readFileSync(sendFile);
      xmodemSend.send(sPort, buffer);
      
      xmodemRecv.receive(rPort, receiveFile);
      
      rPort.once('close', function(err) {
        assert.equal(null, err);
        done();
      });
    });
    
    it('receive file should exist', function(done) {
      setTimeout(function() {
        assert.equal(true, fs.existsSync(receiveFile));
        done();
      }, 100);
    });
  
    it('send and receive files should be identical', function(done) {
      const md5File = require('md5-file');
      assert.equal(md5File.sync(sendFile), md5File.sync(receiveFile));
      done();
    });
  
    it('receiveFile rm should return undefined', function(done) {
      assert.equal(undefined, fs.unlinkSync(receiveFile));
      done();
    });
    
    // Transfer (checksum)
    
    it('transfer should succeed - checksum', function(done) {
      this.timeout(60000);
      
      // Set receiver to normal mode
      xmodemRecv.XMODEM_OP_MODE = 'normal';
      assert(xmodemRecv.XMODEM_OP_MODE, 'normal');
      // Sender should still be in CRC mode from previous (default) transfer
      assert(xmodemSend.XMODEM_OP_MODE, 'crc');
      
      var buffer = fs.readFileSync(sendFile);
      xmodemSend.send(sPort, buffer);
      
      rPort.once('open', function() {
        xmodemRecv.receive(rPort, receiveFile);
      });
      
      rPort.once('close', function(err) {
        assert.equal(null, err);
        done();
      });
      
      rPort.open();
    });
    
    it('receive file should exist', function(done) {
      setTimeout(function() {
        assert.equal(true, fs.existsSync(receiveFile));
        done();
      }, 100);
    });
  
    it('send and receive files should be identical', function(done) {
      const md5File = require('md5-file');
      assert.equal(md5File.sync(sendFile), md5File.sync(receiveFile));
      done();
    });
  
    it('receiveFile rm should return undefined', function(done) {
      assert.equal(undefined, fs.unlinkSync(receiveFile));
      done();
    });
    
    
    // Cleaning up
    
    it('socat process should exit', function(done) {
      child.once('exit', function(code) {
        assert.equal(129, code);
        done();
      });
      child.kill('SIGHUP');
    });
    
    delete require.cache[require.resolve('../lib/index.js')];
  });
} // if(commandExistsSync('socat'))

@@ -500,26 +500,53 @@
  sendPort = 
-%22%22
+process.env.HOME + '/ttyXM01'
 ;%0A    var sP
@@ -569,18 +569,45 @@
 vPort = 
-%22%22
+process.env.HOME + '/ttyXM02'
 ;%0A    va
@@ -833,35 +833,69 @@
 w,echo=0
-', 'pty,raw,echo=0'
+,link=' + sendPort, 'pty,raw,echo=0,link=' + recvPort
 %5D);%0A    
@@ -1041,395 +1041,55 @@
 Of(%22
-N PTY is %22) %3E= 0) %7B%0A          if(sendPort === %22%22) %7B%0A            sendPort = String(
+starting 
 data
-).substring(data.indexOf(%22N PTY is %22) + 9, data.length - 1);%0A          %7D%0A          else if(recvPort === %22%22) %7B%0A            recvPort = String(data).substring(data.indexOf(%22N PTY is %22) + 9, data.length - 1);%0A            assert.include(sendPort, '/dev/');%0A            assert.include(recvPort, '/dev/');%0A  
+ transfer loop with FDs%22) %3E= 0) %7B%0A
     
@@ -1102,28 +1102,16 @@
 done();%0A
-          %7D%0A
         

Use pty links instead of reading the values dynamically