From 15cbce87704b9f3f1b2aaddb4136d5e76a46a4ce Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Wed, 1 Feb 2017 21:49:32 +0100 Subject: return_bytes(): Throw exception on unhandled unit type Signed-off-by: Florian Pritz --- application/helpers/filebin_helper.php | 6 +++++- application/test/tests/test_filebin_helper.php | 10 ++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/application/helpers/filebin_helper.php b/application/helpers/filebin_helper.php index 05a136eb3..55fdf773d 100644 --- a/application/helpers/filebin_helper.php +++ b/application/helpers/filebin_helper.php @@ -375,7 +375,11 @@ function return_bytes($size_str) case 'K': case 'k': return (int)$size_str * 1024; case 'M': case 'm': return (int)$size_str * 1048576; case 'G': case 'g': return (int)$size_str * 1073741824; - default: return (int)$size_str; + default: + if (strlen($size_str) === strlen(intval($size_str))) { + return (int)$size_str; + } + throw new \exceptions\ApiException('filebin-helper/invalid-input-unit', "Input has invalid unit"); } } diff --git a/application/test/tests/test_filebin_helper.php b/application/test/tests/test_filebin_helper.php index c505dfe8a..2069f2953 100644 --- a/application/test/tests/test_filebin_helper.php +++ b/application/test/tests/test_filebin_helper.php @@ -65,7 +65,13 @@ class test_filebin_helper extends \test\Test { $this->t->is(return_bytes("1k"), 1*1024, "1k"); $this->t->is(return_bytes("1M"), 1*1024*1024, "1M"); $this->t->is(return_bytes("1G"), 1*1024*1024*1024, "1G"); - $this->t->is(return_bytes("1P"), "1P", "unhandled text: 1P"); - $this->t->ok(return_bytes("106954752") === 106954752, "value without unit is returned as int"); + + try { + return_bytes("1P"); + } catch (\exceptions\ApiException $e) { + $this->t->is($e->get_error_id(), 'filebin-helper/invalid-input-unit', "unhandled text: 1P"); + } + + $this->t->is(return_bytes("106954752"), 106954752, "value without unit is returned as int"); } } -- cgit v1.2.3-24-g4f1b