[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [flashproxy/js] Add parse_addr_spec and tests.



commit 627cd6044c22a3e27c709f55b137caacaf47736f
Author: David Fifield <david@xxxxxxxxxxxxxxx>
Date:   Tue Mar 13 10:22:39 2012 -0700

    Add parse_addr_spec and tests.
---
 flashproxy-test.js |   57 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 flashproxy.js      |   32 +++++++++++++++++++++++++++++
 2 files changed, 89 insertions(+), 0 deletions(-)

diff --git a/flashproxy-test.js b/flashproxy-test.js
index ca68f85..2c786ee 100755
--- a/flashproxy-test.js
+++ b/flashproxy-test.js
@@ -123,7 +123,64 @@ function test_parse_query_string()
     }
 }
 
+function test_parse_addr_spec()
+{
+    var TESTS = [
+        { spec: "",
+          expected: null },
+        { spec: "3.3.3.3:4444",
+          expected: { host: "3.3.3.3", port: 4444 } },
+        { spec: "3.3.3.3",
+          expected: null },
+        { spec: "3.3.3.3:0x1111",
+          expected: null },
+        { spec: "3.3.3.3:-4444",
+          expected: null },
+        { spec: "3.3.3.3:65536",
+          expected: null },
+    ];
+
+    for (var i = 0; i < TESTS.length; i++) {
+        var test = TESTS[i];
+        var actual;
+
+        actual = parse_addr_spec(test.spec);
+        if (objects_equal(actual, test.expected))
+            pass(test.spec);
+        else
+            fail(test.spec, test.expected, actual);
+    }
+}
+
+function test_get_query_param_addr()
+{
+    var DEFAULT = { host: "1.1.1.1", port: 2222 };
+    var TESTS = [
+        { query: { },
+          expected: DEFAULT },
+        { query: { addr: "3.3.3.3:4444" },
+          expected: { host: "3.3.3.3", port: 4444 } },
+        { query: { x: "3.3.3.3:4444" },
+          expected: DEFAULT },
+        { query: { addr: "---" },
+          expected: null },
+    ];
+
+    for (var i = 0; i < TESTS.length; i++) {
+        var test = TESTS[i];
+        var actual;
+
+        actual = get_query_param_addr(test.query, "addr", DEFAULT);
+        if (objects_equal(actual, test.expected))
+            pass(test.query);
+        else
+            fail(test.query, test.expected, actual);
+    }
+}
+
 test_parse_query_string();
+test_parse_addr_spec();
+test_get_query_param_addr();
 
 if (num_failed == 0)
     quit(0);
diff --git a/flashproxy.js b/flashproxy.js
index b68da1f..f45fd1d 100644
--- a/flashproxy.js
+++ b/flashproxy.js
@@ -43,6 +43,38 @@ function parse_query_string(qs)
     return result;
 }
 
+/* Get a query string parameter and parse it as an address spec. Returns
+   default_val if param is not defined in the query string. Returns null on a
+   parsing error. */
+function get_query_param_addr(query, param, default_val)
+{
+    var val;
+
+    val = query[param];
+    if (val === undefined)
+        return default_val;
+    else
+        return parse_addr_spec(val);
+}
+
+/* Parse an address in the form "host:port". Returns an Object with
+   keys "host" (String) and "port" (int). Returns null on error. */
+function parse_addr_spec(spec)
+{
+    var groups;
+    var host, port;
+
+    groups = spec.match(/^([^:]+):(\d+)$/);
+    if (!groups)
+        return null;
+    host = groups[1];
+    port = parseInt(groups[2], 10);
+    if (isNaN(port) || port < 0 || port > 65535)
+        return null;
+
+    return { host: host, port: port }
+}
+
 function format_addr(addr)
 {
     return addr.host + ":" + addr.port;



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits