Skip to content

Commit

Permalink
testing ci
Browse files Browse the repository at this point in the history
  • Loading branch information
vaguue committed Jul 13, 2024
1 parent 1c52c37 commit 77d72ee
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 17 deletions.
16 changes: 14 additions & 2 deletions cxx/transports/pcap/Pcap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ PcapDevice::PcapDevice(const Napi::CallbackInfo& info) : Napi::ObjectWrap<PcapDe
Napi::Object obj = info[0].As<Napi::Object>();

if (obj.Has("iface")) {
dev = device_ptr_t{pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByName(obj.Get("iface").As<Napi::String>().Utf8Value())->clone()};
std::string ifaceName = obj.Get("iface").As<Napi::String>().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;
Expand All @@ -78,6 +86,8 @@ PcapDevice::PcapDevice(const Napi::CallbackInfo& info) : Napi::ObjectWrap<PcapDe
DEBUG_OUTPUT("TSFN destructor");
}
);

hasPush = true;
}

Napi::Value PcapDevice::open(const Napi::CallbackInfo& info) {
Expand Down Expand Up @@ -187,7 +197,9 @@ void PcapDevice::_destroy_impl() {
dev->close();
}

push.Release();
if (hasPush) {
push.Release();
}
destroyed = true;
}

Expand Down
1 change: 1 addition & 0 deletions cxx/transports/pcap/Pcap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ namespace OverTheWire::Transports::Pcap {

pcpp::PcapLiveDevice::DeviceConfiguration config;
device_ptr_t dev;
bool hasPush = false;
TSFN push;
};
}
11 changes: 10 additions & 1 deletion lib/liveDevice.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
33 changes: 19 additions & 14 deletions test/liveDevice.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

0 comments on commit 77d72ee

Please sign in to comment.