From 89e3ab33d580eeba868765d6b40b9c654d282565 Mon Sep 17 00:00:00 2001 From: KosM Date: Sat, 31 Dec 2022 14:59:48 +0200 Subject: [PATCH 1/3] catch lib crach when call rpc without args closes #63 --- index.js | 8 ++++++-- test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 76699db..ff8c848 100644 --- a/index.js +++ b/index.js @@ -335,9 +335,13 @@ function onMessage(bugout, identifier, wire, message) { } else if (packet.y == "r") { // rpc call debug("rpc", identifier, packet); var call = packet.c.toString(); - var argsstring = packet.a.toString(); try { - var args = JSON.parse(argsstring); + if (typeof packet.a !== 'undefined' && packet.a !== null) { + var argsstring = packet.a.toString(); + var args = JSON.parse(argsstring); + } else { + var args = null + } } catch(e) { var args = null; debug("Malformed args JSON: " + argsstring); diff --git a/test.js b/test.js index be3c850..ee27f42 100644 --- a/test.js +++ b/test.js @@ -143,6 +143,32 @@ test("RPC and message passing", function(t) { }); }); +test("RPC Call without passing any message/data", function(t) { + t.plan(5); + + const bs = new Bugout({dht: false, tracker: false}); + const bc = new Bugout(bs.address(), {dht: false, tracker: false}); + + bs.register("call_without_args", function(pk,args){ + console.dir(args) + t.assert(args === null , 'args should be null , when called without args from another peer') + }); + bs.register("call_with_args", function(pk,args){ + t.assert(typeof args === 'object' , 'type of args should be an object') + t.assert( args.hasOwnProperty('hello'), 'args has correct message data props') + t.assert( args.hello === 'world', 'args has correct message data') + }); + bc.on("server", function(address) { + t.equal(address,bs.address(), "server check client was rpc sender"); + bc.rpc(bs.address(), 'call_without_args') + bc.rpc(bs.address(), 'call_with_args', { hello: 'world'}) + }); + // connect the two clients together + bs.torrent.on("infoHash", function() { + bs.torrent.addPeer("127.0.0.1:" + bc.wt.address().port); + }); +}); + test("3 party incomplete graph gossip test", function(t) { t.plan(10); From 06b3751a435dc6f354b9a341a821a0ef3b359bdc Mon Sep 17 00:00:00 2001 From: KosM Date: Thu, 5 Jan 2023 15:43:11 +0200 Subject: [PATCH 2/3] shorten checks for incoming arguments - also fix typo in tests --- index.js | 8 ++------ test.js | 3 +-- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index ff8c848..acd1b69 100644 --- a/index.js +++ b/index.js @@ -336,12 +336,8 @@ function onMessage(bugout, identifier, wire, message) { debug("rpc", identifier, packet); var call = packet.c.toString(); try { - if (typeof packet.a !== 'undefined' && packet.a !== null) { - var argsstring = packet.a.toString(); - var args = JSON.parse(argsstring); - } else { - var args = null - } + var argsstring = packet["a"] ? packet.a.toString() : "null"; + var args = JSON.parse(argsstring); } catch(e) { var args = null; debug("Malformed args JSON: " + argsstring); diff --git a/test.js b/test.js index ee27f42..692f20f 100644 --- a/test.js +++ b/test.js @@ -150,8 +150,7 @@ test("RPC Call without passing any message/data", function(t) { const bc = new Bugout(bs.address(), {dht: false, tracker: false}); bs.register("call_without_args", function(pk,args){ - console.dir(args) - t.assert(args === null , 'args should be null , when called without args from another peer') + t.assert(args === null, 'args should be null, when called without args from another peer') }); bs.register("call_with_args", function(pk,args){ t.assert(typeof args === 'object' , 'type of args should be an object') From 73c41d78d11f66385856f0a1dfb3fbf4b2761979 Mon Sep 17 00:00:00 2001 From: KosM Date: Thu, 26 Jan 2023 13:00:07 +0200 Subject: [PATCH 3/3] move argstring assignment out of try block --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index acd1b69..b09c1ed 100644 --- a/index.js +++ b/index.js @@ -335,8 +335,8 @@ function onMessage(bugout, identifier, wire, message) { } else if (packet.y == "r") { // rpc call debug("rpc", identifier, packet); var call = packet.c.toString(); + var argsstring = packet["a"] ? packet.a.toString() : "null"; try { - var argsstring = packet["a"] ? packet.a.toString() : "null"; var args = JSON.parse(argsstring); } catch(e) { var args = null;