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

[tor-commits] [snowflake/master] Bug 31203: Add tests for Parse.byteCount and Params.getByteCount.



commit abdda1c8bfd2be873bc4a04594a8ee13b85d72d2
Author: David Fifield <david@xxxxxxxxxxxxxxx>
Date:   Thu Jul 18 17:06:53 2019 -0600

    Bug 31203: Add tests for Parse.byteCount and Params.getByteCount.
---
 proxy/spec/util.spec.js | 71 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 71 insertions(+)

diff --git a/proxy/spec/util.spec.js b/proxy/spec/util.spec.js
index 9552d56..f48365d 100644
--- a/proxy/spec/util.spec.js
+++ b/proxy/spec/util.spec.js
@@ -72,6 +72,62 @@ describe('Parse', function() {
 
   });
 
+  describe('byte count', function() {
+
+    it('returns null for bad inputs', function() {
+      expect(Parse.byteCount("")).toBeNull();
+      expect(Parse.byteCount("x")).toBeNull();
+      expect(Parse.byteCount("1x")).toBeNull();
+      expect(Parse.byteCount("1.x")).toBeNull();
+      expect(Parse.byteCount("1.2x")).toBeNull();
+      expect(Parse.byteCount("toString")).toBeNull();
+      expect(Parse.byteCount("1toString")).toBeNull();
+      expect(Parse.byteCount("1.toString")).toBeNull();
+      expect(Parse.byteCount("1.2toString")).toBeNull();
+      expect(Parse.byteCount("k")).toBeNull();
+      expect(Parse.byteCount("m")).toBeNull();
+      expect(Parse.byteCount("g")).toBeNull();
+      expect(Parse.byteCount("K")).toBeNull();
+      expect(Parse.byteCount("M")).toBeNull();
+      expect(Parse.byteCount("G")).toBeNull();
+      expect(Parse.byteCount("-1")).toBeNull();
+      expect(Parse.byteCount("-1k")).toBeNull();
+      expect(Parse.byteCount("1.2.3")).toBeNull();
+      expect(Parse.byteCount("1.2.3k")).toBeNull();
+    });
+
+    it('handles numbers without a suffix', function() {
+      expect(Parse.byteCount("10")).toEqual(10);
+      expect(Parse.byteCount("10.")).toEqual(10);
+      expect(Parse.byteCount("1.5")).toEqual(1.5);
+    });
+
+    it('handles lowercase suffixes', function() {
+      expect(Parse.byteCount("10k")).toEqual(10*1024);
+      expect(Parse.byteCount("10m")).toEqual(10*1024*1024);
+      expect(Parse.byteCount("10g")).toEqual(10*1024*1024*1024);
+      expect(Parse.byteCount("10.k")).toEqual(10*1024);
+      expect(Parse.byteCount("10.m")).toEqual(10*1024*1024);
+      expect(Parse.byteCount("10.g")).toEqual(10*1024*1024*1024);
+      expect(Parse.byteCount("1.5k")).toEqual(1.5*1024);
+      expect(Parse.byteCount("1.5m")).toEqual(1.5*1024*1024);
+      expect(Parse.byteCount("1.5G")).toEqual(1.5*1024*1024*1024);
+    });
+
+    it('handles uppercase suffixes', function() {
+      expect(Parse.byteCount("10K")).toEqual(10*1024);
+      expect(Parse.byteCount("10M")).toEqual(10*1024*1024);
+      expect(Parse.byteCount("10G")).toEqual(10*1024*1024*1024);
+      expect(Parse.byteCount("10.K")).toEqual(10*1024);
+      expect(Parse.byteCount("10.M")).toEqual(10*1024*1024);
+      expect(Parse.byteCount("10.G")).toEqual(10*1024*1024*1024);
+      expect(Parse.byteCount("1.5K")).toEqual(1.5*1024);
+      expect(Parse.byteCount("1.5M")).toEqual(1.5*1024*1024);
+      expect(Parse.byteCount("1.5G")).toEqual(1.5*1024*1024*1024);
+    });
+
+  });
+
   describe('ipFromSDP', function() {
 
     var testCases = [
@@ -178,4 +234,19 @@ describe('Params', function() {
 
   });
 
+  describe('byteCount', function() {
+
+    var DEFAULT = 77;
+    var getByteCount = function(query) {
+      return Params.getByteCount(Query.parse(query), 'param', DEFAULT);
+    };
+
+    it('supports default values', function() {
+      expect(getByteCount('param=x')).toBeNull();
+      expect(getByteCount('param=10')).toEqual(10);
+      expect(getByteCount('foo=10k')).toEqual(DEFAULT);
+    });
+
+  });
+
 });



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