-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wakunode2 app now accepts max-num-bytes-msg-size with KiB, KB, or B u…
…nits
- Loading branch information
1 parent
189bbec
commit 4ed576c
Showing
6 changed files
with
161 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
{.used.} | ||
|
||
import | ||
testutils/unittests, | ||
stew/results | ||
import | ||
../../waku/common/utils/parse_size_units | ||
|
||
suite "Size serialization test": | ||
test "parse normal sizes": | ||
var sizeInBytesRes = parseMsgSize("15 KiB") | ||
assert sizeInBytesRes.isOk(), sizeInBytesRes.error | ||
check sizeInBytesRes.get() == 15360 | ||
|
||
sizeInBytesRes = parseMsgSize(" 1048576 B") | ||
assert sizeInBytesRes.isOk(), sizeInBytesRes.error | ||
check sizeInBytesRes.get() == 1048576 | ||
|
||
sizeInBytesRes = parseMsgSize("150 B") | ||
assert sizeInBytesRes.isOk(), sizeInBytesRes.error | ||
check sizeInBytesRes.get() == 150 | ||
|
||
sizeInBytesRes = parseMsgSize("150 b") | ||
assert sizeInBytesRes.isOk(), sizeInBytesRes.error | ||
check sizeInBytesRes.get() == 150 | ||
|
||
sizeInBytesRes = parseMsgSize("150b") | ||
assert sizeInBytesRes.isOk(), sizeInBytesRes.error | ||
check sizeInBytesRes.get() == 150 | ||
|
||
sizeInBytesRes = parseMsgSize("1024kib") | ||
assert sizeInBytesRes.isOk(), sizeInBytesRes.error | ||
check sizeInBytesRes.get() == 1048576 | ||
|
||
sizeInBytesRes = parseMsgSize("1024KiB") | ||
assert sizeInBytesRes.isOk(), sizeInBytesRes.error | ||
check sizeInBytesRes.get() == 1048576 | ||
|
||
sizeInBytesRes = parseMsgSize("1024KB") | ||
assert sizeInBytesRes.isOk(), sizeInBytesRes.error | ||
check sizeInBytesRes.get() == 1024000 | ||
|
||
sizeInBytesRes = parseMsgSize("1024kb") | ||
assert sizeInBytesRes.isOk(), sizeInBytesRes.error | ||
check sizeInBytesRes.get() == 1024000 | ||
|
||
sizeInBytesRes = parseMsgSize("1.5 kib") | ||
assert sizeInBytesRes.isOk(), sizeInBytesRes.error | ||
check sizeInBytesRes.get() == 1536 | ||
|
||
sizeInBytesRes = parseMsgSize("1,5 kb") | ||
assert sizeInBytesRes.isOk(), sizeInBytesRes.error | ||
check sizeInBytesRes.get() == 1500 | ||
|
||
sizeInBytesRes = parseMsgSize("0,5 kb") | ||
assert sizeInBytesRes.isOk(), sizeInBytesRes.error | ||
check sizeInBytesRes.get() == 500 | ||
|
||
sizeInBytesRes = parseMsgSize("1.5 kb") | ||
assert sizeInBytesRes.isOk(), sizeInBytesRes.error | ||
check sizeInBytesRes.get() == 1500 | ||
|
||
sizeInBytesRes = parseMsgSize("0.5 kb") | ||
assert sizeInBytesRes.isOk(), sizeInBytesRes.error | ||
check sizeInBytesRes.get() == 500 | ||
|
||
sizeInBytesRes = parseMsgSize(" 1.5 KB") | ||
assert sizeInBytesRes.isOk(), sizeInBytesRes.error | ||
check sizeInBytesRes.get() == 1500 | ||
|
||
sizeInBytesRes = parseMsgSize(" 0.5 kb") | ||
assert sizeInBytesRes.isOk(), sizeInBytesRes.error | ||
check sizeInBytesRes.get() == 500 | ||
|
||
sizeInBytesRes = parseMsgSize(" 1024 kib") | ||
assert sizeInBytesRes.isOk(), sizeInBytesRes.error | ||
check sizeInBytesRes.get() == (1024 * 1024) | ||
|
||
test "parse wrong sizes": | ||
var sizeInBytesRes = parseMsgSize("150K") | ||
assert sizeInBytesRes.isErr(), "The size should be considered incorrect" | ||
|
||
sizeInBytesRes = parseMsgSize("150 iB") | ||
assert sizeInBytesRes.isErr(), "The size should be considered incorrect" | ||
|
||
sizeInBytesRes = parseMsgSize("150 ib") | ||
assert sizeInBytesRes.isErr(), "The size should be considered incorrect" | ||
|
||
sizeInBytesRes = parseMsgSize("150 MB") | ||
assert sizeInBytesRes.isErr(), "The size should be considered incorrect" | ||
|
||
## notice that we don't allow MB units explicitly. If someone want to set 1MiB, the | ||
## s/he should use 1024 KiB | ||
sizeInBytesRes = parseMsgSize("150 MiB") | ||
assert sizeInBytesRes.isErr(), "The size should be considered incorrect" | ||
|
||
sizeInBytesRes = parseMsgSize("150MiB") | ||
assert sizeInBytesRes.isErr(), "The size should be considered incorrect" | ||
|
||
sizeInBytesRes = parseMsgSize("150K") | ||
assert sizeInBytesRes.isErr(), "The size should be considered incorrect" | ||
|
||
sizeInBytesRes = parseMsgSize("150 K") | ||
assert sizeInBytesRes.isErr(), "The size should be considered incorrect" | ||
|
||
sizeInBytesRes = parseMsgSize("15..0 KiB") | ||
assert sizeInBytesRes.isErr(), "The size should be considered incorrect" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
import | ||
std/strutils, | ||
stew/results, | ||
regex | ||
|
||
proc parseMsgSize*(input: string): Result[int, string] = | ||
## Parses size strings such as "1.2 KiB" or "3Kb" and returns the equivalent number of bytes | ||
## if the parse task goes well. If not, it returns an error describing the problem. | ||
|
||
const RegexDef = """\s*(\d+([\,\.]\d*)?)\s*([Kk]{0,1}[i]?[Bb]{1})""" | ||
const RegexParseSize = re2(RegexDef) | ||
|
||
var m: RegexMatch2 | ||
if input.match(RegexParseSize, m) == false: | ||
return err("error in parseSize. regex is not matching: " & RegexDef) | ||
|
||
var value: float | ||
|
||
try: | ||
value = parseFloat(input[m.captures[0]].replace(",", ".")) | ||
except ValueError: | ||
return err("invalid size in parseSize: " & getCurrentExceptionMsg() & | ||
" error parsing: " & input[m.captures[0]] & " KKK : " & $m) | ||
|
||
let units = input[m.captures[2]].toLowerAscii() # units is "kib", or "kb", or "b". | ||
|
||
var multiplier: float | ||
case units: | ||
of "kb": | ||
multiplier = 1000 | ||
of "kib": | ||
multiplier = 1024 | ||
of "ib": | ||
return err("wrong units. ib or iB aren't allowed.") | ||
else: ## bytes | ||
multiplier = 1 | ||
|
||
value = value * multiplier | ||
|
||
return ok(int(value)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters