Bug 879861 - Part 4: Adding SEUtils.jsm. r=allstars.chh
authorKrzysztof Mioduszewski <kmioduszewski@gmail.com>
Wed, 11 Feb 2015 03:58:00 -0500
changeset 256102 374b013116c244d5c1a7195b2af69e95d8b5553b
parent 256101 1d8a6e33379efcc470146e60b38123f258dca1cf
child 256103 93f03475e1ad67535ba8daece719f1911687962a
push id4610
push userjlund@mozilla.com
push dateMon, 30 Mar 2015 18:32:55 +0000
treeherdermozilla-beta@4df54044d9ef [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersallstars
bugs879861
milestone38.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 879861 - Part 4: Adding SEUtils.jsm. r=allstars.chh
dom/secureelement/SEUtils.jsm
dom/secureelement/tests/unit/test_SEUtils.js
dom/secureelement/tests/unit/xpcshell.ini
new file mode 100644
--- /dev/null
+++ b/dom/secureelement/SEUtils.jsm
@@ -0,0 +1,55 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/* Copyright © 2015, Deutsche Telekom, Inc. */
+
+"use strict";
+
+this.SEUtils = {
+  byteArrayToHexString: function byteArrayToHexString(array) {
+    let hexStr = "";
+
+    let len = array ? array.length : 0;
+    for (let i = 0; i < len; i++) {
+      let hex = (array[i] & 0xff).toString(16);
+      hex = (hex.length === 1) ? "0" + hex : hex;
+      hexStr += hex;
+    }
+
+    return hexStr.toUpperCase();
+  },
+
+  hexStringToByteArray: function hexStringToByteArray(hexStr) {
+    if (typeof hexStr !== "string" || hexStr.length % 2 !== 0) {
+      return [];
+    }
+
+    let array = [];
+    for (let i = 0, len = hexStr.length; i < len; i += 2) {
+      array.push(parseInt(hexStr.substr(i, 2), 16));
+    }
+
+    return array;
+  },
+
+  arraysEqual: function arraysEqual(a1, a2) {
+    if (!a1 || !a2) {
+      return false;
+    }
+
+    if (a1.length !== a2.length) {
+      return false;
+    }
+
+    for (let i = 0, len = a1.length; i < len; i++) {
+      if (a1[i] !== a2[i]) {
+        return false;
+      }
+    }
+
+    return true;
+  },
+};
+
+this.EXPORTED_SYMBOLS = ["SEUtils"];
new file mode 100644
--- /dev/null
+++ b/dom/secureelement/tests/unit/test_SEUtils.js
@@ -0,0 +1,68 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+/* globals run_next_test, add_test, ok, Components, SEUtils */
+/* exported run_test */
+
+Components.utils.import("resource://gre/modules/SEUtils.jsm");
+
+const VALID_HEX_STR = "0123456789ABCDEF";
+const VALID_BYTE_ARR = [0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF];
+
+function run_test() {
+  ok(!!SEUtils, "SEUtils should be available");
+  run_next_test();
+}
+
+add_test(function test_byteArrayToHexString() {
+  let hexStr = SEUtils.byteArrayToHexString(VALID_BYTE_ARR);
+  ok(hexStr === VALID_HEX_STR,
+     "should convert byte Array to uppercased hex string");
+
+  [[], null, undefined].forEach((input) => {
+    hexStr = SEUtils.byteArrayToHexString(input);
+    ok(hexStr === "", "invalid arg:" + input + " should return empty string");
+  });
+
+  run_next_test();
+});
+
+add_test(function test_hexStringToByteArray() {
+  let byteArr = SEUtils.hexStringToByteArray(VALID_HEX_STR);
+  ok(SEUtils.arraysEqual(byteArr, VALID_BYTE_ARR),
+     "should convert uppercased string to byte Array");
+
+  byteArr = SEUtils.hexStringToByteArray(VALID_HEX_STR.toLowerCase());
+  ok(SEUtils.arraysEqual(byteArr, VALID_BYTE_ARR),
+     "should convert lowercased string to byte Array");
+
+  ["", null, undefined, "123"].forEach((input) => {
+    byteArr = SEUtils.hexStringToByteArray(input);
+    ok(Array.isArray(byteArr) && byteArr.length === 0,
+       "invalid arg: " + input + " should be empty Array");
+  });
+
+  run_next_test();
+});
+
+add_test(function test_arraysEqual() {
+  ok(SEUtils.arraysEqual([1, 2, 3], [1, 2, 3]),
+     "should return true on equal Arrays");
+
+  [[1], [1, 2, 4], [3, 2, 1]].forEach((input) => {
+    ok(!SEUtils.arraysEqual([1, 2, 3], input),
+       "should return false when Arrays not equal");
+  });
+
+  [null, undefined].forEach((input) => {
+    ok(!SEUtils.arraysEqual([1, 2, 3], input),
+       "should return false when comparing Array with invalid argument");
+
+    ok(!SEUtils.arraysEqual(input, input),
+       "should return false when both args are invalid");
+  });
+
+  run_next_test();
+});
new file mode 100644
--- /dev/null
+++ b/dom/secureelement/tests/unit/xpcshell.ini
@@ -0,0 +1,5 @@
+[DEFAULT]
+head =
+tail =
+
+[test_SEUtils.js]
\ No newline at end of file