Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for OwnedResolver #280

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions contracts/resolvers/OwnedResolver.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//SPDX-License-Identifier: MIT
pragma solidity >=0.8.4;

import "@openzeppelin/contracts/access/Ownable.sol";
import "./profiles/ABIResolver.sol";
import "./profiles/AddrResolver.sol";
Expand Down
213 changes: 213 additions & 0 deletions test/resolvers/TestOwnedResolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
const ENS = artifacts.require('./registry/ENSRegistry.sol')
const OwnedResolver = artifacts.require('OwnedResolver.sol')
const NameWrapper = artifacts.require('DummyNameWrapper.sol')
const { deploy } = require('../test-utils/contracts')
const { labelhash } = require('../test-utils/ens')
const { EMPTY_BYTES32: ROOT_NODE } = require('../test-utils/constants')

const namehash = require('eth-ens-namehash')
const sha3 = require('web3-utils').sha3

const {
exceptions,
dns: { nameToHex },
} = require('../test-utils')

contract('OwnedResolver', function (accounts) {
let node
let ens, resolver, nameWrapper
let account
let signers

beforeEach(async () => {
signers = await ethers.getSigners()
account = await signers[0].getAddress()
node = namehash.hash('eth')
ens = await ENS.new()
nameWrapper = await NameWrapper.new()

//setup reverse registrar

const ReverseRegistrar = await deploy('ReverseRegistrar', ens.address)

await ens.setSubnodeOwner(ROOT_NODE, labelhash('reverse'), account)
await ens.setSubnodeOwner(
namehash.hash('reverse'),
labelhash('addr'),
ReverseRegistrar.address,
)

resolver = await OwnedResolver.new(
ens.address,
nameWrapper.address,
accounts[9], // trusted contract
ReverseRegistrar.address, //ReverseRegistrar.address,
)

await ReverseRegistrar.setDefaultResolver(resolver.address)

await ens.setSubnodeOwner('0x0', sha3('eth'), accounts[0], {
from: accounts[0],
})
})

describe('dns', async () => {
const basicSetDNSRecords = async () => {
// a.eth. 3600 IN A 1.2.3.4
const arec = '016103657468000001000100000e10000401020304'
// b.eth. 3600 IN A 2.3.4.5
const b1rec = '016203657468000001000100000e10000402030405'
// b.eth. 3600 IN A 3.4.5.6
const b2rec = '016203657468000001000100000e10000403040506'
// eth. 86400 IN SOA ns1.ethdns.xyz. hostmaster.test.eth. 2018061501 15620 1800 1814400 14400
const soarec =
'03657468000006000100015180003a036e733106657468646e730378797a000a686f73746d6173746572057465737431036574680078492cbd00003d0400000708001baf8000003840'
const rec = '0x' + arec + b1rec + b2rec + soarec

await resolver.setDNSRecords(node, rec, { from: accounts[0] })

assert.equal(
await resolver.dnsRecord(node, sha3(nameToHex('a.eth.')), 1),
'0x016103657468000001000100000e10000401020304',
)
assert.equal(
await resolver.dnsRecord(node, sha3(nameToHex('b.eth.')), 1),
'0x016203657468000001000100000e10000402030405016203657468000001000100000e10000403040506',
)
assert.equal(
await resolver.dnsRecord(node, sha3(nameToHex('eth.')), 6),
'0x03657468000006000100015180003a036e733106657468646e730378797a000a686f73746d6173746572057465737431036574680078492cbd00003d0400000708001baf8000003840',
)
}
it('permits setting name by owner', basicSetDNSRecords)

it('should keep track of entries', async () => {
// c.eth. 3600 IN A 1.2.3.4
const crec = '016303657468000001000100000e10000401020304'
const rec = '0x' + crec

await resolver.setDNSRecords(node, rec, { from: accounts[0] })

// Initial check
let hasEntries = await resolver.hasDNSRecords(
node,
sha3(nameToHex('c.eth.')),
)
assert.equal(hasEntries, true)
hasEntries = await resolver.hasDNSRecords(node, sha3(nameToHex('d.eth.')))
assert.equal(hasEntries, false)

// Update with no new data makes no difference
await resolver.setDNSRecords(node, rec, { from: accounts[0] })
hasEntries = await resolver.hasDNSRecords(node, sha3(nameToHex('c.eth.')))
assert.equal(hasEntries, true)

// c.eth. 3600 IN A
const crec2 = '016303657468000001000100000e100000'
const rec2 = '0x' + crec2

await resolver.setDNSRecords(node, rec2, { from: accounts[0] })

// Removal returns to 0
hasEntries = await resolver.hasDNSRecords(node, sha3(nameToHex('c.eth.')))
assert.equal(hasEntries, false)
})

it('forbids setting DNS records by non-owners', async () => {
// f.eth. 3600 IN A 1.2.3.4
const frec = '016603657468000001000100000e10000401020304'
const rec = '0x' + frec
await exceptions.expectFailure(
resolver.setDNSRecords(node, rec, { from: accounts[1] }),
)
})

const basicSetZonehash = async () => {
await resolver.setZonehash(
node,
'0x0000000000000000000000000000000000000000000000000000000000000001',
{ from: accounts[0] },
)
assert.equal(
await resolver.zonehash(node),
'0x0000000000000000000000000000000000000000000000000000000000000001',
)
}

it('permits setting zonehash by owner', basicSetZonehash)

it('can overwrite previously set zonehash', async () => {
await resolver.setZonehash(
node,
'0x0000000000000000000000000000000000000000000000000000000000000001',
{ from: accounts[0] },
)
assert.equal(
await resolver.zonehash(node),
'0x0000000000000000000000000000000000000000000000000000000000000001',
)

await resolver.setZonehash(
node,
'0x0000000000000000000000000000000000000000000000000000000000000002',
{ from: accounts[0] },
)
assert.equal(
await resolver.zonehash(node),
'0x0000000000000000000000000000000000000000000000000000000000000002',
)
})

it('can overwrite to same zonehash', async () => {
await resolver.setZonehash(
node,
'0x0000000000000000000000000000000000000000000000000000000000000001',
{ from: accounts[0] },
)
assert.equal(
await resolver.zonehash(node),
'0x0000000000000000000000000000000000000000000000000000000000000001',
)

await resolver.setZonehash(
node,
'0x0000000000000000000000000000000000000000000000000000000000000002',
{ from: accounts[0] },
)
assert.equal(
await resolver.zonehash(node),
'0x0000000000000000000000000000000000000000000000000000000000000002',
)
})

it('forbids setting zonehash by non-owners', async () => {
await exceptions.expectFailure(
resolver.setZonehash(
node,
'0x0000000000000000000000000000000000000000000000000000000000000001',
{ from: accounts[1] },
),
)
})

it('forbids writing same zonehash by non-owners', async () => {
await resolver.setZonehash(
node,
'0x0000000000000000000000000000000000000000000000000000000000000001',
{ from: accounts[0] },
)

await exceptions.expectFailure(
resolver.setZonehash(
node,
'0x0000000000000000000000000000000000000000000000000000000000000001',
{ from: accounts[1] },
),
)
})

it('returns empty when fetching nonexistent zonehash', async () => {
assert.equal(await resolver.zonehash(node), null)
})
})
})
57 changes: 17 additions & 40 deletions test/resolvers/TestPublicResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ const { expect } = require('chai')
const namehash = require('eth-ens-namehash')
const sha3 = require('web3-utils').sha3

const { exceptions } = require('../test-utils')
const {
exceptions,
dns: { nameToHex },
} = require('../test-utils')

contract('PublicResolver', function (accounts) {
let node
Expand Down Expand Up @@ -750,15 +753,15 @@ contract('PublicResolver', function (accounts) {
await resolver.setDNSRecords(node, rec, { from: accounts[0] })

assert.equal(
await resolver.dnsRecord(node, sha3(dnsName('a.eth.')), 1),
await resolver.dnsRecord(node, sha3(nameToHex('a.eth.')), 1),
'0x016103657468000001000100000e10000401020304',
)
assert.equal(
await resolver.dnsRecord(node, sha3(dnsName('b.eth.')), 1),
await resolver.dnsRecord(node, sha3(nameToHex('b.eth.')), 1),
'0x016203657468000001000100000e10000402030405016203657468000001000100000e10000403040506',
)
assert.equal(
await resolver.dnsRecord(node, sha3(dnsName('eth.')), 6),
await resolver.dnsRecord(node, sha3(nameToHex('eth.')), 6),
'0x03657468000006000100015180003a036e733106657468646e730378797a000a686f73746d6173746572057465737431036574680078492cbd00003d0400000708001baf8000003840',
)
}
Expand All @@ -775,11 +778,11 @@ contract('PublicResolver', function (accounts) {
await resolver.setDNSRecords(node, rec, { from: accounts[0] })

assert.equal(
await resolver.dnsRecord(node, sha3(dnsName('a.eth.')), 1),
await resolver.dnsRecord(node, sha3(nameToHex('a.eth.')), 1),
'0x016103657468000001000100000e10000404050607',
)
assert.equal(
await resolver.dnsRecord(node, sha3(dnsName('eth.')), 6),
await resolver.dnsRecord(node, sha3(nameToHex('eth.')), 6),
'0x03657468000006000100015180003a036e733106657468646e730378797a000a686f73746d6173746572057465737431036574680078492cbe00003d0400000708001baf8000003840',
)
})
Expand All @@ -794,15 +797,15 @@ contract('PublicResolver', function (accounts) {
// Initial check
var hasEntries = await resolver.hasDNSRecords(
node,
sha3(dnsName('c.eth.')),
sha3(nameToHex('c.eth.')),
)
assert.equal(hasEntries, true)
hasEntries = await resolver.hasDNSRecords(node, sha3(dnsName('d.eth.')))
hasEntries = await resolver.hasDNSRecords(node, sha3(nameToHex('d.eth.')))
assert.equal(hasEntries, false)

// Update with no new data makes no difference
await resolver.setDNSRecords(node, rec, { from: accounts[0] })
hasEntries = await resolver.hasDNSRecords(node, sha3(dnsName('c.eth.')))
hasEntries = await resolver.hasDNSRecords(node, sha3(nameToHex('c.eth.')))
assert.equal(hasEntries, true)

// c.eth. 3600 IN A
Expand All @@ -812,7 +815,7 @@ contract('PublicResolver', function (accounts) {
await resolver.setDNSRecords(node, rec2, { from: accounts[0] })

// Removal returns to 0
hasEntries = await resolver.hasDNSRecords(node, sha3(dnsName('c.eth.')))
hasEntries = await resolver.hasDNSRecords(node, sha3(nameToHex('c.eth.')))
assert.equal(hasEntries, false)
})

Expand All @@ -824,7 +827,7 @@ contract('PublicResolver', function (accounts) {
await resolver.setDNSRecords(node, rec, { from: accounts[0] })

assert.equal(
await resolver.dnsRecord(node, sha3(dnsName('e.eth.')), 1),
await resolver.dnsRecord(node, sha3(nameToHex('e.eth.')), 1),
'0x016503657468000001000100000e10000401020304',
)
})
Expand Down Expand Up @@ -980,15 +983,15 @@ contract('PublicResolver', function (accounts) {
await basicSetDNSRecords()
await resolver.clearRecords(node)
assert.equal(
await resolver.dnsRecord(node, sha3(dnsName('a.eth.')), 1),
await resolver.dnsRecord(node, sha3(nameToHex('a.eth.')), 1),
null,
)
assert.equal(
await resolver.dnsRecord(node, sha3(dnsName('b.eth.')), 1),
await resolver.dnsRecord(node, sha3(nameToHex('b.eth.')), 1),
null,
)
assert.equal(
await resolver.dnsRecord(node, sha3(dnsName('eth.')), 6),
await resolver.dnsRecord(node, sha3(nameToHex('eth.')), 6),
null,
)
})
Expand Down Expand Up @@ -1332,29 +1335,3 @@ contract('PublicResolver', function (accounts) {
})
})
})

function dnsName(name) {
// strip leading and trailing .
const n = name.replace(/^\.|\.$/gm, '')

var bufLen = n === '' ? 1 : n.length + 2
var buf = Buffer.allocUnsafe(bufLen)

offset = 0
if (n.length) {
const list = n.split('.')
for (let i = 0; i < list.length; i++) {
const len = buf.write(list[i], offset + 1)
buf[offset] = len
offset += len + 1
}
}
buf[offset++] = 0
return (
'0x' +
buf.reduce(
(output, elem) => output + ('0' + elem.toString(16)).slice(-2),
'',
)
)
}
28 changes: 28 additions & 0 deletions test/test-utils/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,36 @@ function hexEncodeName(name) {
function hexEncodeTXT(keys) {
return '0x' + packet.answer.encode(keys).toString('hex')
}
// resolves to a hexadecimal address from a name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of rewriting this instead of using dnsName?

Copy link
Author

@subashcs subashcs Nov 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the question @Arachnid. Actually, the "dnsName" function is renamed to the "nameToHex" function. Also, I have moved this "nameToHex" function to the utils section for sharing between the newly created TestOwnedResolver and the old TestPublicResolver.


function nameToHex(name) {
// strip leading and trailing .
const n = name.replace(/^\.|\.$/gm, '')

var bufLen = n === '' ? 1 : n.length + 2
var buf = Buffer.allocUnsafe(bufLen)

offset = 0
if (n.length) {
const list = n.split('.')
for (let i = 0; i < list.length; i++) {
const len = buf.write(list[i], offset + 1)
buf[offset] = len
offset += len + 1
}
}
buf[offset++] = 0
return (
'0x' +
buf.reduce(
(output, elem) => output + ('0' + elem.toString(16)).slice(-2),
'',
)
)
}

module.exports = {
hexEncodeName: hexEncodeName,
hexEncodeTXT: hexEncodeTXT,
nameToHex: nameToHex,
}