-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: disallow non alphanumeric symbols (#945)
* refactor: disallow non alphanumeric symbols * feat: allow empty spaces in "isAlphanumeric" refactor: alphabetical ordering refactor: return different string test: thorough concrete tests for "isAlphanumeric" * refactor: simplify logical conditions test: fuzz tests for "isAlphanumeric" test: shared test contract for NFT descriptor --------- Co-authored-by: Paul Razvan Berg <[email protected]>
- Loading branch information
1 parent
0a80c28
commit 902c2d3
Showing
13 changed files
with
221 additions
and
17 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
4 changes: 2 additions & 2 deletions
4
test/integration/concrete/nft-descriptor/generateAccentColor.t.sol
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
100 changes: 100 additions & 0 deletions
100
test/integration/concrete/nft-descriptor/is-alphanumeric/isAlphanumeric.t.sol
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,100 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity >=0.8.22 <0.9.0; | ||
|
||
import { NFTDescriptor_Integration_Shared_Test } from "../../../shared/nft-descriptor/NFTDescriptor.t.sol"; | ||
|
||
contract IsAlphanumeric_Integration_Concrete_Test is NFTDescriptor_Integration_Shared_Test { | ||
function test_IsAlphanumeric_EmptyString() external view { | ||
string memory symbol = ""; | ||
bool result = nftDescriptorMock.isAlphanumeric_(symbol); | ||
assertTrue(result, "isAlphanumeric"); | ||
} | ||
|
||
modifier whenNotEmptyString() { | ||
_; | ||
} | ||
|
||
function test_IsAlphanumeric_ContainsNonAlphanumericCharacters() external view whenNotEmptyString { | ||
string memory symbol = "<foo/>"; | ||
bool result = nftDescriptorMock.isAlphanumeric_(symbol); | ||
assertFalse(result, "isAlphanumeric"); | ||
|
||
symbol = "foo/"; | ||
result = nftDescriptorMock.isAlphanumeric_(symbol); | ||
assertFalse(result, "isAlphanumeric"); | ||
|
||
symbol = "foo\\"; | ||
result = nftDescriptorMock.isAlphanumeric_(symbol); | ||
assertFalse(result, "isAlphanumeric"); | ||
symbol = "foo%"; | ||
result = nftDescriptorMock.isAlphanumeric_(symbol); | ||
assertFalse(result, "isAlphanumeric"); | ||
symbol = "foo&"; | ||
result = nftDescriptorMock.isAlphanumeric_(symbol); | ||
assertFalse(result, "isAlphanumeric"); | ||
symbol = "foo("; | ||
result = nftDescriptorMock.isAlphanumeric_(symbol); | ||
assertFalse(result, "isAlphanumeric"); | ||
symbol = "foo)"; | ||
result = nftDescriptorMock.isAlphanumeric_(symbol); | ||
assertFalse(result, "isAlphanumeric"); | ||
symbol = "foo\""; | ||
result = nftDescriptorMock.isAlphanumeric_(symbol); | ||
assertFalse(result, "isAlphanumeric"); | ||
symbol = "foo'"; | ||
result = nftDescriptorMock.isAlphanumeric_(symbol); | ||
assertFalse(result, "isAlphanumeric"); | ||
symbol = "foo`"; | ||
result = nftDescriptorMock.isAlphanumeric_(symbol); | ||
assertFalse(result, "isAlphanumeric"); | ||
symbol = "foo;"; | ||
result = nftDescriptorMock.isAlphanumeric_(symbol); | ||
assertFalse(result, "isAlphanumeric"); | ||
symbol = "foo%20"; // URL-encoded empty space | ||
result = nftDescriptorMock.isAlphanumeric_(symbol); | ||
assertFalse(result, "isAlphanumeric"); | ||
} | ||
modifier whenOnlyAlphanumericCharacters() { | ||
_; | ||
} | ||
function test_IsAlphanumeric() external view whenNotEmptyString whenOnlyAlphanumericCharacters { | ||
string memory symbol = "foo"; | ||
bool result = nftDescriptorMock.isAlphanumeric_(symbol); | ||
assertTrue(result, "isAlphanumeric"); | ||
symbol = "Foo"; | ||
result = nftDescriptorMock.isAlphanumeric_(symbol); | ||
assertTrue(result, "isAlphanumeric"); | ||
symbol = "Foo "; | ||
result = nftDescriptorMock.isAlphanumeric_(symbol); | ||
assertTrue(result, "isAlphanumeric"); | ||
symbol = "Foo Bar"; | ||
result = nftDescriptorMock.isAlphanumeric_(symbol); | ||
assertTrue(result, "isAlphanumeric"); | ||
symbol = " "; | ||
result = nftDescriptorMock.isAlphanumeric_(symbol); | ||
assertTrue(result, "isAlphanumeric"); | ||
symbol = "foo01234"; | ||
result = nftDescriptorMock.isAlphanumeric_(symbol); | ||
assertTrue(result, "isAlphanumeric"); | ||
symbol = "123456789"; | ||
result = nftDescriptorMock.isAlphanumeric_(symbol); | ||
assertTrue(result, "isAlphanumeric"); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
test/integration/concrete/nft-descriptor/is-alphanumeric/isAlphanumeric.tree
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,8 @@ | ||
isAlphanumeric.t.sol | ||
├── when the symbol is an empty string | ||
│ └── it should return true | ||
└── when the symbol is not an empty string | ||
├── given the symbol does not contain only alphanumeric characters | ||
│ └── it should return false | ||
└── given the symbol contains only alphanumeric characters | ||
└── it should return true |
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
4 changes: 2 additions & 2 deletions
4
test/integration/concrete/nft-descriptor/safe-asset-decimals/safeAssetDecimals.t.sol
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,46 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity >=0.8.22 <0.9.0; | ||
|
||
import { NFTDescriptor_Integration_Shared_Test } from "../../shared/nft-descriptor/NFTDescriptor.t.sol"; | ||
|
||
contract IsAlphanumeric_Integration_Fuzz_Test is NFTDescriptor_Integration_Shared_Test { | ||
bytes1 internal constant SPACE = 0x20; // ASCII 32 | ||
bytes1 internal constant ZERO = 0x30; // ASCII 48 | ||
bytes1 internal constant NINE = 0x39; // ASCII 57 | ||
bytes1 internal constant A = 0x41; // ASCII 65 | ||
bytes1 internal constant Z = 0x5A; // ASCII 90 | ||
bytes1 internal constant a = 0x61; // ASCII 97 | ||
bytes1 internal constant z = 0x7A; // ASCII 122 | ||
|
||
modifier whenNotEmptyString() { | ||
_; | ||
} | ||
|
||
/// @dev Given enough fuzz runs, all the following scenarios will be fuzzed: | ||
/// | ||
/// - String with only alphanumerical characters | ||
/// - String with only non-alphanumerical characters | ||
/// - String with both alphanumerical and non-alphanumerical characters | ||
function testFuzz_IsAlphanumeric(string memory symbol) external view whenNotEmptyString { | ||
bytes memory b = bytes(symbol); | ||
uint256 length = b.length; | ||
bool expectedResult = true; | ||
for (uint256 i = 0; i < length; ++i) { | ||
bytes1 char = b[i]; | ||
if (!isAlphanumericChar(char)) { | ||
expectedResult = false; | ||
break; | ||
} | ||
} | ||
bool actualResult = nftDescriptorMock.isAlphanumeric_(symbol); | ||
assertEq(actualResult, expectedResult, "isAlphanumeric"); | ||
} | ||
|
||
function isAlphanumericChar(bytes1 char) internal pure returns (bool) { | ||
bool isSpace = char == SPACE; | ||
bool isDigit = char >= ZERO && char <= NINE; | ||
bool isUppercaseLetter = char >= A && char <= Z; | ||
bool isLowercaseLetter = char >= a && char <= z; | ||
return isSpace || isDigit || isUppercaseLetter || isLowercaseLetter; | ||
} | ||
} |
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