Skip to content

Commit

Permalink
update testhexutils
Browse files Browse the repository at this point in the history
  • Loading branch information
mdtanrikulu committed Mar 27, 2024
1 parent 2be5088 commit e9894f6
Showing 1 changed file with 46 additions and 11 deletions.
57 changes: 46 additions & 11 deletions test/utils/TestHexUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,46 @@ describe('HexUtils', () => {
HexUtils = await HexUtilsFactory.deploy()
})

describe('hexToBytes()', () => {
it('Converts a hex string to bytes', async () => {
let [bytes32, valid] = await HexUtils.hexToBytes(
toUtf8Bytes(
'5cee339e13375638553bdf5a6e36ba80fb9f6a4f0783680884d92b558aa471da',
),
0,
64,
)
expect(valid).to.equal(true)
expect(bytes32).to.equal(
'0x5cee339e13375638553bdf5a6e36ba80fb9f6a4f0783680884d92b558aa471da',
)
})

it('Handles short strings', async () => {
let [bytes32, valid] = await HexUtils.hexToBytes(
toUtf8Bytes('5cee'),
0,
4,
)
expect(valid).to.equal(true)
expect(bytes32).to.equal('0x5cee')
})

it('Handles long strings', async () => {
let [bytes32, valid] = await HexUtils.hexToBytes(
toUtf8Bytes(
'5cee339e13375638553bdf5a6e36ba80fb9f6a4f0783680884d92b558aa471da010203',
),
0,
70,
)
expect(valid).to.equal(true)
expect(bytes32).to.equal(
'0x5cee339e13375638553bdf5a6e36ba80fb9f6a4f0783680884d92b558aa471da010203',
)
})
})

describe('hexStringToBytes32()', () => {
it('Converts a hex string to bytes32', async () => {
let [bytes32, valid] = await HexUtils.hexStringToBytes32(
Expand All @@ -25,10 +65,10 @@ describe('HexUtils', () => {
0,
64,
)
expect(valid).to.equal(true)
expect(bytes32).to.equal(
'0x5cee339e13375638553bdf5a6e36ba80fb9f6a4f0783680884d92b558aa471da',
)
expect(valid).to.equal(true)
})
it('Uses the correct index to read from', async () => {
let [bytes32, valid] = await HexUtils.hexStringToBytes32(
Expand All @@ -38,21 +78,21 @@ describe('HexUtils', () => {
7,
71,
)
expect(valid).to.equal(true)
expect(bytes32).to.equal(
'0x5cee339e13375638553bdf5a6e36ba80fb9f6a4f0783680884d92b558aa471da',
)
expect(valid).to.equal(true)
})
it('Correctly parses all hex characters', async () => {
let [bytes32, valid] = await HexUtils.hexStringToBytes32(
toUtf8Bytes('0123456789abcdefABCDEF0123456789abcdefABCD'),
0,
40,
)
expect(valid).to.equal(true)
expect(bytes32).to.equal(
'0x0000000000000000000000000123456789abcdefabcdef0123456789abcdefab',
)
expect(valid).to.equal(true)
})
it('Returns invalid when the string contains non-hex characters', async () => {
const [bytes32, valid] = await HexUtils.hexStringToBytes32(
Expand All @@ -62,8 +102,8 @@ describe('HexUtils', () => {
0,
64,
)
expect(bytes32).to.equal(NULL_HASH)
expect(valid).to.equal(false)
expect(bytes32).to.equal(NULL_HASH)
})
it('Reverts when the string is too short', async () => {
await expect(
Expand All @@ -87,8 +127,8 @@ describe('HexUtils', () => {
0,
40,
)
expect(address).to.equal('0x5ceE339e13375638553bdF5a6e36BA80fB9f6a4F')
expect(valid).to.equal(true)
expect(address).to.equal('0x5ceE339e13375638553bdF5a6e36BA80fB9f6a4F')
})
it('Does not allow sizes smaller than 40 characters', async () => {
let [address, valid] = await HexUtils.hexToAddress(
Expand All @@ -98,8 +138,8 @@ describe('HexUtils', () => {
0,
39,
)
expect(address).to.equal('0x0000000000000000000000000000000000000000')
expect(valid).to.equal(false)
expect(address).to.equal('0x0000000000000000000000000000000000000000')
})
})

Expand All @@ -117,11 +157,6 @@ describe('HexUtils', () => {
).to.be.reverted
})

it('not enough length', async () => {
await expect(HexUtils.hexStringToBytes32(toUtf8Bytes(hex32Bytes), 0, 2))
.to.be.reverted
})

it('exceed length', async () => {
await expect(
HexUtils.hexStringToBytes32(
Expand Down

0 comments on commit e9894f6

Please sign in to comment.