michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: "use strict"; michael@0: michael@0: const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; michael@0: michael@0: michael@0: let subscriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"] michael@0: .getService(Ci.mozIJSSubScriptLoader); michael@0: michael@0: /** michael@0: * Test whether specified function throws exception with expected michael@0: * result. michael@0: * michael@0: * @param func michael@0: * Function to be tested. michael@0: * @param exception michael@0: * Expected class name of thrown exception. Use null for no throws. michael@0: * @param stack michael@0: * Optional stack object to be printed. null for Components#stack#caller. michael@0: */ michael@0: function do_check_throws(func, result, stack) michael@0: { michael@0: if (!stack) michael@0: stack = Components.stack.caller; michael@0: michael@0: try { michael@0: func(); michael@0: } catch (ex) { michael@0: if (ex.name == result) { michael@0: return; michael@0: } michael@0: do_throw("expected result " + result + ", caught " + ex, stack); michael@0: } michael@0: michael@0: if (result) { michael@0: do_throw("expected result " + result + ", none thrown", stack); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Internal test function for comparing results. michael@0: * michael@0: * @param func michael@0: * A function under test. It should accept an arguement and return the michael@0: * result. michael@0: * @param data michael@0: * Input data for `func`. michael@0: * @param expect michael@0: * Expected result. michael@0: */ michael@0: function wsp_test_func(func, data, expect) { michael@0: let result_str = JSON.stringify(func(data)); michael@0: let expect_str = JSON.stringify(expect); michael@0: do_check_eq(result_str, expect_str); michael@0: } michael@0: michael@0: /** michael@0: * Test customized WSP PDU decoding. michael@0: * michael@0: * @param func michael@0: * Decoding func under test. It should return a decoded value if invoked. michael@0: * @param input michael@0: * Array of octets as test data. michael@0: * @param expect michael@0: * Expected decoded value, use null if expecting errors instead. michael@0: * @param exception michael@0: * Expected class name of thrown exception. Use null for no throws. michael@0: */ michael@0: function wsp_decode_test_ex(func, input, expect, exception) { michael@0: let data = {array: input, offset: 0}; michael@0: do_check_throws(wsp_test_func.bind(null, func, data, expect), exception); michael@0: } michael@0: michael@0: /** michael@0: * Test default WSP PDU decoding. michael@0: * michael@0: * @param target michael@0: * Target decoding object, ie. TextValue. michael@0: * @param input michael@0: * Array of octets as test data. michael@0: * @param expect michael@0: * Expected decoded value, use null if expecting errors instead. michael@0: * @param exception michael@0: * Expected class name of thrown exception. Use null for no throws. michael@0: */ michael@0: function wsp_decode_test(target, input, expect, exception) { michael@0: let func = function decode_func(data) { michael@0: return target.decode(data); michael@0: }; michael@0: michael@0: wsp_decode_test_ex(func, input, expect, exception); michael@0: } michael@0: michael@0: /** michael@0: * Test customized WSP PDU encoding. michael@0: * michael@0: * @param func michael@0: * Encoding func under test. It should return an encoded octet array if michael@0: * invoked. michael@0: * @param input michael@0: * An object to be encoded. michael@0: * @param expect michael@0: * Expected encoded octet array, use null if expecting errors instead. michael@0: * @param exception michael@0: * Expected class name of thrown exception. Use null for no throws. michael@0: */ michael@0: function wsp_encode_test_ex(func, input, expect, exception) { michael@0: let data = {array: [], offset: 0}; michael@0: do_check_throws(wsp_test_func.bind(null, func.bind(null, data), input, michael@0: expect), exception); michael@0: } michael@0: michael@0: /** michael@0: * Test default WSP PDU encoding. michael@0: * michael@0: * @param target michael@0: * Target decoding object, ie. TextValue. michael@0: * @param input michael@0: * An object to be encoded. michael@0: * @param expect michael@0: * Expected encoded octet array, use null if expecting errors instead. michael@0: * @param exception michael@0: * Expected class name of thrown exception. Use null for no throws. michael@0: */ michael@0: function wsp_encode_test(target, input, expect, exception) { michael@0: let func = function encode_func(data, input) { michael@0: target.encode(data, input); michael@0: michael@0: // Remove extra space consumed during encoding. michael@0: while (data.array.length > data.offset) { michael@0: data.array.pop(); michael@0: } michael@0: michael@0: return data.array; michael@0: } michael@0: michael@0: wsp_encode_test_ex(func, input, expect, exception); michael@0: } michael@0: michael@0: /** michael@0: * @param str michael@0: * A string. michael@0: * @param noAppendNull michael@0: * True to omit terminating NUL octet. Default false. michael@0: * michael@0: * @return A number array of char codes of characters in `str`. michael@0: */ michael@0: function strToCharCodeArray(str, noAppendNull) { michael@0: let result = []; michael@0: michael@0: for (let i = 0; i < str.length; i++) { michael@0: result.push(str.charCodeAt(i)); michael@0: } michael@0: if (!noAppendNull) { michael@0: result.push(0); michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: