dom/mobilemessage/tests/header_helpers.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial