From 77d72eee2d1b43ea3fc656e7d84b37100b02e30c Mon Sep 17 00:00:00 2001 From: vaguue Date: Sun, 14 Jul 2024 00:18:41 +0300 Subject: [PATCH] testing ci --- cxx/transports/pcap/Pcap.cpp | 16 ++++++++++++++-- cxx/transports/pcap/Pcap.hpp | 1 + lib/liveDevice.js | 11 ++++++++++- test/liveDevice.test.js | 33 +++++++++++++++++++-------------- 4 files changed, 44 insertions(+), 17 deletions(-) diff --git a/cxx/transports/pcap/Pcap.cpp b/cxx/transports/pcap/Pcap.cpp index e75d4ff..fcd7ff1 100644 --- a/cxx/transports/pcap/Pcap.cpp +++ b/cxx/transports/pcap/Pcap.cpp @@ -56,7 +56,15 @@ PcapDevice::PcapDevice(const Napi::CallbackInfo& info) : Napi::ObjectWrap(); if (obj.Has("iface")) { - dev = device_ptr_t{pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByName(obj.Get("iface").As().Utf8Value())->clone()}; + std::string ifaceName = obj.Get("iface").As().Utf8Value(); + + auto initDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByName(ifaceName); + if (!initDev) { + Napi::Error::New(info.Env(), "Could not find device").ThrowAsJavaScriptException(); + return; + } + dev = device_ptr_t{initDev->clone()}; + if (!dev.get()) { Napi::Error::New(info.Env(), "Could not get device").ThrowAsJavaScriptException(); return; @@ -78,6 +86,8 @@ PcapDevice::PcapDevice(const Napi::CallbackInfo& info) : Napi::ObjectWrapclose(); } - push.Release(); + if (hasPush) { + push.Release(); + } destroyed = true; } diff --git a/cxx/transports/pcap/Pcap.hpp b/cxx/transports/pcap/Pcap.hpp index 905230d..40269a6 100644 --- a/cxx/transports/pcap/Pcap.hpp +++ b/cxx/transports/pcap/Pcap.hpp @@ -59,6 +59,7 @@ namespace OverTheWire::Transports::Pcap { pcpp::PcapLiveDevice::DeviceConfiguration config; device_ptr_t dev; + bool hasPush = false; TSFN push; }; } diff --git a/lib/liveDevice.js b/lib/liveDevice.js index f3d2651..dd17d75 100644 --- a/lib/liveDevice.js +++ b/lib/liveDevice.js @@ -59,7 +59,16 @@ class LiveDevice extends Duplex { if (this.optionsChanged) { this.pcapInternal.setConfig(this.options); } - this.pcapInternal.open(); + + if (!this.pcapInternal) { + return callback(); + } + + try { + this.pcapInternal.open(); + } catch(err) { + console.log('err in construct', err); + } this.isOpen = true; if (this.options.filter) { this.pcapInternal.setFilter(this.options.filter); diff --git a/test/liveDevice.test.js b/test/liveDevice.test.js index 310f724..35e053c 100644 --- a/test/liveDevice.test.js +++ b/test/liveDevice.test.js @@ -11,23 +11,28 @@ test('LiveDevice', (t) => { return false; }) ?? []; - console.log('os.networkInterfaces()', os.networkInterfaces()); - console.log('ifaceName', ifaceName); - if (!ifaceName) return; - const dev = new LiveDevice({ iface: ifaceName }); + try { + const dev = new LiveDevice({ iface: ifaceName + 'k' }); + + dev.on('error', err => { + console.log('caught error', err); + }); - const { iface } = dev; + const { iface } = dev; - assert.ok(iface.hasOwnProperty('name')); - assert.ok(iface.hasOwnProperty('description')); - assert.ok(iface.hasOwnProperty('mac')); - assert.ok(iface.hasOwnProperty('gateway')); - assert.ok(iface.hasOwnProperty('mtu')); - assert.ok(iface.hasOwnProperty('linktype')); - assert.ok(iface.hasOwnProperty('dnsServers')); - assert.ok(iface.hasOwnProperty('addresses')); + assert.ok(iface.hasOwnProperty('name')); + assert.ok(iface.hasOwnProperty('description')); + assert.ok(iface.hasOwnProperty('mac')); + assert.ok(iface.hasOwnProperty('gateway')); + assert.ok(iface.hasOwnProperty('mtu')); + assert.ok(iface.hasOwnProperty('linktype')); + assert.ok(iface.hasOwnProperty('dnsServers')); + assert.ok(iface.hasOwnProperty('addresses')); - dev.destroy(); + dev.destroy(); + } catch(err) { + console.log('try-catch', err); + } });