handle `scanf` exceptions in `test_fixture_ssz_generic_types` (#5993)
`scanf` apparently has both a `bool` return as well as raising random exceptions depending on what functions get called by the `macro`. To make this explicit, catch the `ValueError` from the generated `parseInt` call, to separate `scanf` behaviour from the actual SSZ test logic. In the end, it mostly doesn't matter as there are some `doAssert wasMatched` on the next line (not everywhere though). But it still makes the `scanf` internals explicit, so is clearer.
This commit is contained in:
parent
f4ff438aab
commit
23fa8d0c70
|
@ -5,6 +5,7 @@
|
||||||
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
||||||
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
||||||
|
|
||||||
|
{.push raises: [].}
|
||||||
{.used.}
|
{.used.}
|
||||||
|
|
||||||
import
|
import
|
||||||
|
@ -82,9 +83,11 @@ type
|
||||||
# Type specific checks
|
# Type specific checks
|
||||||
# ------------------------------------------------------------------------
|
# ------------------------------------------------------------------------
|
||||||
|
|
||||||
proc checkBasic(T: typedesc,
|
proc checkBasic(
|
||||||
dir: string,
|
T: typedesc,
|
||||||
expectedHash: SSZHashTreeRoot) =
|
dir: string,
|
||||||
|
expectedHash: SSZHashTreeRoot
|
||||||
|
) {.raises: [IOError, SerializationError, UnconsumedInput].} =
|
||||||
let fileContents = snappy.decode(readFileBytes(dir/"serialized.ssz_snappy"), MaxObjectSize)
|
let fileContents = snappy.decode(readFileBytes(dir/"serialized.ssz_snappy"), MaxObjectSize)
|
||||||
let deserialized = newClone(sszDecodeEntireInput(fileContents, T))
|
let deserialized = newClone(sszDecodeEntireInput(fileContents, T))
|
||||||
|
|
||||||
|
@ -142,16 +145,31 @@ macro testVector(typeIdent: string, size: int): untyped =
|
||||||
result = dispatcher
|
result = dispatcher
|
||||||
# echo result.toStrLit() # view the generated code
|
# echo result.toStrLit() # view the generated code
|
||||||
|
|
||||||
proc checkVector(sszSubType, dir: string, expectedHash: SSZHashTreeRoot) =
|
proc checkVector(
|
||||||
|
sszSubType, dir: string,
|
||||||
|
expectedHash: SSZHashTreeRoot
|
||||||
|
) {.raises: [
|
||||||
|
IOError, SerializationError, TestSizeError, UnconsumedInput, ValueError].} =
|
||||||
var typeIdent: string
|
var typeIdent: string
|
||||||
var size: int
|
var size: int
|
||||||
let wasMatched = scanf(sszSubType, "vec_$+_$i", typeIdent, size)
|
let wasMatched =
|
||||||
|
try:
|
||||||
|
scanf(sszSubType, "vec_$+_$i", typeIdent, size)
|
||||||
|
except ValueError:
|
||||||
|
false # Parsed `size` is out of range
|
||||||
doAssert wasMatched
|
doAssert wasMatched
|
||||||
testVector(typeIdent, size)
|
testVector(typeIdent, size)
|
||||||
|
|
||||||
proc checkBitVector(sszSubType, dir: string, expectedHash: SSZHashTreeRoot) =
|
proc checkBitVector(
|
||||||
|
sszSubType, dir: string,
|
||||||
|
expectedHash: SSZHashTreeRoot
|
||||||
|
) {.raises: [IOError, SerializationError, TestSizeError, UnconsumedInput].} =
|
||||||
var size: int
|
var size: int
|
||||||
let wasMatched = scanf(sszSubType, "bitvec_$i", size)
|
let wasMatched =
|
||||||
|
try:
|
||||||
|
scanf(sszSubType, "bitvec_$i", size)
|
||||||
|
except ValueError:
|
||||||
|
false # Parsed `size` is out of range
|
||||||
doAssert wasMatched
|
doAssert wasMatched
|
||||||
case size
|
case size
|
||||||
of 1: checkBasic(BitArray[1], dir, expectedHash)
|
of 1: checkBasic(BitArray[1], dir, expectedHash)
|
||||||
|
@ -169,9 +187,33 @@ proc checkBitVector(sszSubType, dir: string, expectedHash: SSZHashTreeRoot) =
|
||||||
else:
|
else:
|
||||||
raise newException(TestSizeError, "Unsupported BitVector of size " & $size)
|
raise newException(TestSizeError, "Unsupported BitVector of size " & $size)
|
||||||
|
|
||||||
proc checkBitList(sszSubType, dir: string, expectedHash: SSZHashTreeRoot) =
|
proc checkBitList(
|
||||||
|
sszSubType, dir: string,
|
||||||
|
expectedHash: SSZHashTreeRoot
|
||||||
|
) {.raises: [IOError, SerializationError, UnconsumedInput, ValueError].} =
|
||||||
|
if sszSubType.startsWith("bitlist_n"):
|
||||||
|
# Invalid data, ensure that it does not deserialize with different sizes
|
||||||
|
checkBasic(BitList[0], dir, expectedHash)
|
||||||
|
checkBasic(BitList[1], dir, expectedHash)
|
||||||
|
checkBasic(BitList[2], dir, expectedHash)
|
||||||
|
checkBasic(BitList[3], dir, expectedHash)
|
||||||
|
checkBasic(BitList[4], dir, expectedHash)
|
||||||
|
checkBasic(BitList[5], dir, expectedHash)
|
||||||
|
checkBasic(BitList[8], dir, expectedHash)
|
||||||
|
checkBasic(BitList[16], dir, expectedHash)
|
||||||
|
checkBasic(BitList[31], dir, expectedHash)
|
||||||
|
checkBasic(BitList[32], dir, expectedHash)
|
||||||
|
checkBasic(BitList[512], dir, expectedHash)
|
||||||
|
checkBasic(BitList[513], dir, expectedHash)
|
||||||
|
return
|
||||||
|
|
||||||
var maxLen: int
|
var maxLen: int
|
||||||
discard scanf(sszSubType, "bitlist_$i", maxLen)
|
let wasMatched =
|
||||||
|
try:
|
||||||
|
scanf(sszSubType, "bitlist_$i", maxLen)
|
||||||
|
except ValueError:
|
||||||
|
false # Parsed `size` is out of range
|
||||||
|
doAssert wasMatched
|
||||||
case maxLen
|
case maxLen
|
||||||
of 0: checkBasic(BitList[0], dir, expectedHash)
|
of 0: checkBasic(BitList[0], dir, expectedHash)
|
||||||
of 1: checkBasic(BitList[1], dir, expectedHash)
|
of 1: checkBasic(BitList[1], dir, expectedHash)
|
||||||
|
@ -191,7 +233,11 @@ proc checkBitList(sszSubType, dir: string, expectedHash: SSZHashTreeRoot) =
|
||||||
# Test dispatch for valid inputs
|
# Test dispatch for valid inputs
|
||||||
# ------------------------------------------------------------------------
|
# ------------------------------------------------------------------------
|
||||||
|
|
||||||
proc sszCheck(baseDir, sszType, sszSubType: string) =
|
proc sszCheck(
|
||||||
|
baseDir, sszType, sszSubType: string
|
||||||
|
) {.raises: [
|
||||||
|
Exception, IOError, SerializationError,
|
||||||
|
TestSizeError, UnconsumedInput, ValueError].} =
|
||||||
let dir = baseDir/sszSubType
|
let dir = baseDir/sszSubType
|
||||||
|
|
||||||
# Hash tree root
|
# Hash tree root
|
||||||
|
@ -206,7 +252,11 @@ proc sszCheck(baseDir, sszType, sszSubType: string) =
|
||||||
of "boolean": checkBasic(bool, dir, expectedHash)
|
of "boolean": checkBasic(bool, dir, expectedHash)
|
||||||
of "uints":
|
of "uints":
|
||||||
var bitsize: int
|
var bitsize: int
|
||||||
let wasMatched = scanf(sszSubType, "uint_$i", bitsize)
|
let wasMatched =
|
||||||
|
try:
|
||||||
|
scanf(sszSubType, "uint_$i", bitsize)
|
||||||
|
except ValueError:
|
||||||
|
false # Parsed `size` is out of range
|
||||||
doAssert wasMatched
|
doAssert wasMatched
|
||||||
case bitsize
|
case bitsize
|
||||||
of 8: checkBasic(uint8, dir, expectedHash)
|
of 8: checkBasic(uint8, dir, expectedHash)
|
||||||
|
|
Loading…
Reference in New Issue