diff options
author | Patrick Paysant <patrick.paysant@linagora.com> | 2016-12-07 11:43:44 +0100 |
---|---|---|
committer | Lukas Reschke <lukas@statuscode.ch> | 2016-12-19 17:29:20 +0100 |
commit | d4c088cb796f22545f79379c27145ef6285d2d5e (patch) | |
tree | 18342dca9cd85cc1da274043b3db1df46c625e29 /core/js | |
parent | ec4bca619d7cd57271262928b1fe06d5f8dc3211 (diff) | |
download | nextcloud-server-d4c088cb796f22545f79379c27145ef6285d2d5e.tar.gz nextcloud-server-d4c088cb796f22545f79379c27145ef6285d2d5e.zip |
Verify input, add more unit tests
Signed-off-by: Lukas Reschke <lukas@statuscode.ch>
Diffstat (limited to 'core/js')
-rw-r--r-- | core/js/js.js | 22 | ||||
-rw-r--r-- | core/js/tests/specs/coreSpec.js | 13 |
2 files changed, 23 insertions, 12 deletions
diff --git a/core/js/js.js b/core/js/js.js index f2cdf7c93ef..3651635541a 100644 --- a/core/js/js.js +++ b/core/js/js.js @@ -1670,21 +1670,26 @@ OC.Util = { /** * Returns a file size in bytes from a humanly readable string + * Makes 2kB to 2048. + * Inspired by computerFileSize in helper.php * @param {string} string file size in human readable format * @return {number} or null if string could not be parsed * - * Makes 2kB to 2048. * - * Inspired by computerFileSize in helper.php */ computerFileSize: function (string) { + if (typeof string != 'string') { + return null; + } + var s = string.toLowerCase(); + var bytes = parseFloat(s) - if (!isNaN(parseFloat(s)) && isFinite(s)) { - return parseFloat(s); + if (!isNaN(bytes) && isFinite(s)) { + return bytes; } - var bytes_array = { + var bytesArray = { 'b' : 1, 'k' : 1024, 'kb': 1024, @@ -1698,13 +1703,10 @@ OC.Util = { 'p' : 1024 * 1024 * 1024 * 1024 * 1024 }; - var bytes = parseFloat(s); - var matches = s.match(/([kmgtp]?b?)$/i); if (matches[1]) { - bytes = bytes * bytes_array[matches[1]]; - } - else { + bytes = bytes * bytesArray[matches[1]]; + } else { return null; } diff --git a/core/js/tests/specs/coreSpec.js b/core/js/tests/specs/coreSpec.js index 39060f4e473..370ebc6ba2d 100644 --- a/core/js/tests/specs/coreSpec.js +++ b/core/js/tests/specs/coreSpec.js @@ -593,6 +593,8 @@ describe('Core base tests', function() { describe('computerFileSize', function() { it('correctly parses file sizes from a human readable formated string', function() { var data = [ + ['125', 125], + ['125.25', 125.25], ['0 B', 0], ['125 B', 125], ['125b', 125], @@ -603,13 +605,20 @@ describe('Core base tests', function() { ['119.2 GB', 127990025421], ['119.2gb', 127990025421], ['116.4 TB', 127983153473126], - ['116.4tb', 127983153473126], - ['foobar', null] + ['116.4tb', 127983153473126] ]; for (var i = 0; i < data.length; i++) { expect(OC.Util.computerFileSize(data[i][0])).toEqual(data[i][1]); } }); + it('returns null if the parameter is not a string', function() { + expect(OC.Util.computerFileSize(NaN)).toEqual(null); + expect(OC.Util.computerFileSize(125)).toEqual(null); + }); + it('returns null if the string is unparsable', function() { + expect(OC.Util.computerFileSize('')).toEqual(null); + expect(OC.Util.computerFileSize('foobar')).toEqual(null); + }); }); describe('stripTime', function() { it('strips time from dates', function() { |