dom/mobilemessage/tests/header_helpers.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 "use strict";
michael@0 5
michael@0 6 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
michael@0 7
michael@0 8
michael@0 9 let subscriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"]
michael@0 10 .getService(Ci.mozIJSSubScriptLoader);
michael@0 11
michael@0 12 /**
michael@0 13 * Test whether specified function throws exception with expected
michael@0 14 * result.
michael@0 15 *
michael@0 16 * @param func
michael@0 17 * Function to be tested.
michael@0 18 * @param exception
michael@0 19 * Expected class name of thrown exception. Use null for no throws.
michael@0 20 * @param stack
michael@0 21 * Optional stack object to be printed. null for Components#stack#caller.
michael@0 22 */
michael@0 23 function do_check_throws(func, result, stack)
michael@0 24 {
michael@0 25 if (!stack)
michael@0 26 stack = Components.stack.caller;
michael@0 27
michael@0 28 try {
michael@0 29 func();
michael@0 30 } catch (ex) {
michael@0 31 if (ex.name == result) {
michael@0 32 return;
michael@0 33 }
michael@0 34 do_throw("expected result " + result + ", caught " + ex, stack);
michael@0 35 }
michael@0 36
michael@0 37 if (result) {
michael@0 38 do_throw("expected result " + result + ", none thrown", stack);
michael@0 39 }
michael@0 40 }
michael@0 41
michael@0 42 /**
michael@0 43 * Internal test function for comparing results.
michael@0 44 *
michael@0 45 * @param func
michael@0 46 * A function under test. It should accept an arguement and return the
michael@0 47 * result.
michael@0 48 * @param data
michael@0 49 * Input data for `func`.
michael@0 50 * @param expect
michael@0 51 * Expected result.
michael@0 52 */
michael@0 53 function wsp_test_func(func, data, expect) {
michael@0 54 let result_str = JSON.stringify(func(data));
michael@0 55 let expect_str = JSON.stringify(expect);
michael@0 56 do_check_eq(result_str, expect_str);
michael@0 57 }
michael@0 58
michael@0 59 /**
michael@0 60 * Test customized WSP PDU decoding.
michael@0 61 *
michael@0 62 * @param func
michael@0 63 * Decoding func under test. It should return a decoded value if invoked.
michael@0 64 * @param input
michael@0 65 * Array of octets as test data.
michael@0 66 * @param expect
michael@0 67 * Expected decoded value, use null if expecting errors instead.
michael@0 68 * @param exception
michael@0 69 * Expected class name of thrown exception. Use null for no throws.
michael@0 70 */
michael@0 71 function wsp_decode_test_ex(func, input, expect, exception) {
michael@0 72 let data = {array: input, offset: 0};
michael@0 73 do_check_throws(wsp_test_func.bind(null, func, data, expect), exception);
michael@0 74 }
michael@0 75
michael@0 76 /**
michael@0 77 * Test default WSP PDU decoding.
michael@0 78 *
michael@0 79 * @param target
michael@0 80 * Target decoding object, ie. TextValue.
michael@0 81 * @param input
michael@0 82 * Array of octets as test data.
michael@0 83 * @param expect
michael@0 84 * Expected decoded value, use null if expecting errors instead.
michael@0 85 * @param exception
michael@0 86 * Expected class name of thrown exception. Use null for no throws.
michael@0 87 */
michael@0 88 function wsp_decode_test(target, input, expect, exception) {
michael@0 89 let func = function decode_func(data) {
michael@0 90 return target.decode(data);
michael@0 91 };
michael@0 92
michael@0 93 wsp_decode_test_ex(func, input, expect, exception);
michael@0 94 }
michael@0 95
michael@0 96 /**
michael@0 97 * Test customized WSP PDU encoding.
michael@0 98 *
michael@0 99 * @param func
michael@0 100 * Encoding func under test. It should return an encoded octet array if
michael@0 101 * invoked.
michael@0 102 * @param input
michael@0 103 * An object to be encoded.
michael@0 104 * @param expect
michael@0 105 * Expected encoded octet array, use null if expecting errors instead.
michael@0 106 * @param exception
michael@0 107 * Expected class name of thrown exception. Use null for no throws.
michael@0 108 */
michael@0 109 function wsp_encode_test_ex(func, input, expect, exception) {
michael@0 110 let data = {array: [], offset: 0};
michael@0 111 do_check_throws(wsp_test_func.bind(null, func.bind(null, data), input,
michael@0 112 expect), exception);
michael@0 113 }
michael@0 114
michael@0 115 /**
michael@0 116 * Test default WSP PDU encoding.
michael@0 117 *
michael@0 118 * @param target
michael@0 119 * Target decoding object, ie. TextValue.
michael@0 120 * @param input
michael@0 121 * An object to be encoded.
michael@0 122 * @param expect
michael@0 123 * Expected encoded octet array, use null if expecting errors instead.
michael@0 124 * @param exception
michael@0 125 * Expected class name of thrown exception. Use null for no throws.
michael@0 126 */
michael@0 127 function wsp_encode_test(target, input, expect, exception) {
michael@0 128 let func = function encode_func(data, input) {
michael@0 129 target.encode(data, input);
michael@0 130
michael@0 131 // Remove extra space consumed during encoding.
michael@0 132 while (data.array.length > data.offset) {
michael@0 133 data.array.pop();
michael@0 134 }
michael@0 135
michael@0 136 return data.array;
michael@0 137 }
michael@0 138
michael@0 139 wsp_encode_test_ex(func, input, expect, exception);
michael@0 140 }
michael@0 141
michael@0 142 /**
michael@0 143 * @param str
michael@0 144 * A string.
michael@0 145 * @param noAppendNull
michael@0 146 * True to omit terminating NUL octet. Default false.
michael@0 147 *
michael@0 148 * @return A number array of char codes of characters in `str`.
michael@0 149 */
michael@0 150 function strToCharCodeArray(str, noAppendNull) {
michael@0 151 let result = [];
michael@0 152
michael@0 153 for (let i = 0; i < str.length; i++) {
michael@0 154 result.push(str.charCodeAt(i));
michael@0 155 }
michael@0 156 if (!noAppendNull) {
michael@0 157 result.push(0);
michael@0 158 }
michael@0 159
michael@0 160 return result;
michael@0 161 }
michael@0 162

mercurial