From 2339c5e71024626ec6faa148736fa6cf5ae69a70 Mon Sep 17 00:00:00 2001 From: Tim Mickel Date: Tue, 7 Jun 2016 12:01:47 -0400 Subject: [PATCH] Additional tests for invalid `adapter` inputs --- src/engine/adapter.js | 7 +++++++ test/fixtures/events.json | 18 ++++++++++++++++++ test/unit/adapter.js | 24 ++++++++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/src/engine/adapter.js b/src/engine/adapter.js index 7f891da34..48613ec5d 100644 --- a/src/engine/adapter.js +++ b/src/engine/adapter.js @@ -32,6 +32,9 @@ function domToBlocks (blocksDOM) { var blocks = {}; for (var i = 0; i < blocksDOM.length; i++) { var block = blocksDOM[i]; + if (!block.name || !block.attribs) { + continue; + } var tagName = block.name.toLowerCase(); if (tagName == 'block' || tagName == 'shadow') { domToBlock(block, blocks, true); @@ -114,6 +117,10 @@ function domToBlock (blockDOM, blocks, isTopBlock) { }; break; case 'next': + if (!childBlockNode || !childBlockNode.attribs) { + // Invalid child block. + continue; + } // Recursively generate block structure for next block. domToBlock(childBlockNode, blocks, false); // Link next block to this block. diff --git a/test/fixtures/events.json b/test/fixtures/events.json index f6f4cb796..6d94fc62a 100644 --- a/test/fixtures/events.json +++ b/test/fixtures/events.json @@ -35,5 +35,23 @@ "xml": { "outerHTML": "" } + }, + "createinvalid": { + "name": "whatever", + "xml": { + "outerHTML": "" + } + }, + "createinvalidgrandchild": { + "name": "block", + "xml": { + "outerHTML": "xxx" + } + }, + "createbadxml": { + "name": "whatever", + "xml": { + "outerHTML": ">" + } } } diff --git a/test/unit/adapter.js b/test/unit/adapter.js index 72be10d88..838815626 100644 --- a/test/unit/adapter.js +++ b/test/unit/adapter.js @@ -141,3 +141,27 @@ test('create with next connection', function (t) { t.end(); }); + +test('create with invalid block xml', function (t) { + // Entirely invalid block XML + var result = adapter(events.createinvalid); + t.ok(Array.isArray(result)); + t.equal(result.length, 0); + + // Invalid grandchild tag + var result2 = adapter(events.createinvalidgrandchild); + t.ok(Array.isArray(result2)); + t.equal(result2.length, 1); + t.type(result2[0].id, 'string'); + t.equal(Object.keys(result2[0].inputs).length, 0); + t.equal(Object.keys(result2[0].fields).length, 0); + + t.end(); +}); + +test('create with invalid xml', function (t) { + var result = adapter(events.createbadxml); + t.ok(Array.isArray(result)); + t.equal(result.length, 0); + t.end(); +});