This repository has been archived by the owner on Sep 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 507
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from xavierlepretre/public_resolver
Added tests of PublicResolver.
- Loading branch information
Showing
1 changed file
with
278 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,309 @@ | ||
var assert = require('assert'); | ||
var async = require('async'); | ||
var Promise = require('bluebird'); | ||
|
||
var utils = require('./utils.js'); | ||
Promise.promisifyAll(utils); | ||
var web3 = utils.web3; | ||
Promise.promisifyAll(web3.eth); | ||
|
||
var accounts = null; | ||
|
||
before(function(done) { | ||
web3.eth.getAccounts(function(err, acct) { | ||
accounts = acct | ||
done(); | ||
}); | ||
before(function() { | ||
return web3.eth.getAccountsAsync() | ||
.then(acct => accounts = acct); | ||
}); | ||
|
||
describe('PublicResolver', function() { | ||
var resolverCode = null; | ||
var resolver = null; | ||
var ens = null; | ||
|
||
|
||
before(function() { | ||
this.timeout(10000); | ||
resolverCode = utils.compileContract(['interface.sol', 'PublicResolver.sol']).contracts['PublicResolver.sol:PublicResolver']; | ||
}); | ||
|
||
beforeEach(function(done) { | ||
async.series([ | ||
function(done) { ens = utils.deployENS(accounts[0], done); }, | ||
function(done) { | ||
resolver = web3.eth.contract(JSON.parse(resolverCode.interface)).new( | ||
ens.address, | ||
{ | ||
from: accounts[0], | ||
data: resolverCode.bytecode, | ||
gas: 4700000 | ||
}, function(err, contract) { | ||
assert.equal(err, null, err); | ||
if(contract.address != undefined) { | ||
ens.setSubnodeOwner(0, web3.sha3('eth'), accounts[0], {from: accounts[0]}, done); | ||
} | ||
}); | ||
}], | ||
done | ||
); | ||
}); | ||
beforeEach(function() { | ||
this.timeout(4000); | ||
return utils.deployENSAsync(accounts[0]) | ||
.then(_ens => { | ||
ens = _ens; | ||
return new Promise(function(resolve, reject) { | ||
web3.eth.contract(JSON.parse(resolverCode.interface)).new( | ||
ens.address, | ||
{ | ||
from: accounts[0], | ||
data: resolverCode.bytecode, | ||
gas: 4700000 | ||
}, | ||
function(err, contract) { | ||
if (err) { | ||
reject(err); | ||
} else if (typeof contract.address !== "undefined") { | ||
resolve(contract); | ||
} else { | ||
// There is to hope that reject or resolve is called | ||
} | ||
}); | ||
|
||
it('permits setting addresses', function(done) { | ||
async.series([ | ||
function(done) { resolver.supportsInterface("0x3b3b57de", function(err, result) { | ||
assert.equal(err, null, err); | ||
assert.equal(result, true); | ||
done(); | ||
}); | ||
}, | ||
function(done) { resolver.setAddr(utils.node, accounts[1], {from: accounts[0]}, function(err, txid) { | ||
assert.equal(err, null, err); | ||
done(); | ||
}); | ||
}, | ||
function(done) { resolver.addr(utils.node, function(err, address) { | ||
assert.equal(err, null, err); | ||
assert.equal(address, accounts[1]); | ||
done(); | ||
}) | ||
.then(contract => { | ||
resolver = Promise.promisifyAll(contract); | ||
return ens.setSubnodeOwnerAsync(0, web3.sha3('eth'), accounts[0], {from: accounts[0]}); | ||
}); | ||
}); | ||
|
||
describe('fallback function', function() { | ||
|
||
it('forbids calls to the fallback function with 0 value', function() { | ||
return web3.eth.sendTransactionAsync({ | ||
from: accounts[0], | ||
to: resolver.address, | ||
gas: 3000000 | ||
}) | ||
.then( | ||
tx => { throw new Error("expected to be forbidden"); }, | ||
err => assert.ok(err, err) | ||
); | ||
}); | ||
|
||
it('forbids calls to the fallback function with 1 value', function() { | ||
return web3.eth.sendTransactionAsync({ | ||
from: accounts[0], | ||
to: resolver.address, | ||
gas: 3000000, | ||
value: 1 | ||
}) | ||
.then( | ||
tx => { throw new Error("expected to be forbidden"); }, | ||
err => assert.ok(err, err) | ||
); | ||
}); | ||
|
||
}); | ||
|
||
describe('has function', function() { | ||
|
||
it('returns false when checking nonexistent addresses', function() { | ||
this.slow(400); | ||
return resolver.hasAsync(utils.node, "addr") | ||
.then(result => assert.equal(result, false)); | ||
}); | ||
|
||
it('returns true for previously set address', function() { | ||
this.slow(400); | ||
return resolver.setAddrAsync(utils.node, accounts[1], {from: accounts[0]}) | ||
.then(txid => resolver.hasAsync(utils.node, "addr")) | ||
.then(has => assert.equal(has, true)); | ||
}); | ||
|
||
it('returns false when checking nonexistent hash', function() { | ||
this.slow(400); | ||
return resolver.hasAsync(utils.node, "hash") | ||
.then(result => assert.equal(result, false)); | ||
}); | ||
|
||
it('returns true for previously set content', function() { | ||
this.slow(400); | ||
return resolver.setContentAsync(utils.node, accounts[1], {from: accounts[0]}) | ||
.then(txid => resolver.hasAsync(utils.node, "hash")) | ||
.then(has => assert.equal(has, true)); | ||
}); | ||
|
||
it('returns false for address node checked as content', function() { | ||
this.slow(400); | ||
return resolver.setAddrAsync(utils.node, accounts[1], {from: accounts[0]}) | ||
.then(txid => resolver.hasAsync(utils.node, "hash")) | ||
.then(has => assert.equal(has, false)); | ||
}); | ||
|
||
it('returns false for content node checked as address', function() { | ||
this.slow(400); | ||
return resolver.setContentAsync(utils.node, accounts[1], {from: accounts[0]}) | ||
.then(txid => resolver.hasAsync(utils.node, "addr")) | ||
.then(has => assert.equal(has, false)); | ||
}); | ||
|
||
it('returns false for address node checked as unknown kind', function() { | ||
this.slow(400); | ||
return resolver.setAddrAsync(utils.node, accounts[1], {from: accounts[0]}) | ||
.then(txid => resolver.hasAsync(utils.node, "not a kind")) | ||
.then(has => assert.equal(has, false)); | ||
}); | ||
|
||
it('returns false for content node checked as unknown kind', function() { | ||
this.slow(400); | ||
return resolver.setContentAsync(utils.node, accounts[1], {from: accounts[0]}) | ||
.then(txid => resolver.hasAsync(utils.node, "not a kind")) | ||
.then(has => assert.equal(has, false)); | ||
}); | ||
|
||
}); | ||
|
||
describe('supportsInterface function', function() { | ||
|
||
it('supports both known interfaces', function() { | ||
this.slow(250); | ||
return Promise.all([ | ||
resolver.supportsInterfaceAsync("0x3b3b57de"), | ||
resolver.supportsInterfaceAsync("0xd8389dc5") | ||
]) | ||
.then(results => { | ||
assert.equal(results[0], true); | ||
assert.equal(results[1], true); | ||
}); | ||
}], | ||
done | ||
); | ||
}); | ||
|
||
it('does not support a random interface', function() { | ||
this.slow(150); | ||
return resolver.supportsInterfaceAsync("0x3b3b57df") | ||
.then(result => assert.equal(result, false)); | ||
}); | ||
|
||
}); | ||
|
||
describe('setAddr function', function() { | ||
|
||
it('permits setting address by owner', function() { | ||
return resolver.setAddrAsync(utils.node, accounts[1], {from: accounts[0]}); | ||
}); | ||
|
||
it('can overwrite previously set address', function() { | ||
this.slow(200); | ||
return resolver.setAddrAsync(utils.node, accounts[1], {from: accounts[0]}) | ||
.then(txid => resolver.setAddrAsync(utils.node, accounts[0], {from: accounts[0]})); | ||
}); | ||
|
||
it('can overwrite to same address', function() { | ||
this.slow(200); | ||
return resolver.setAddrAsync(utils.node, accounts[1], {from: accounts[0]}) | ||
.then(txid => resolver.setAddrAsync(utils.node, accounts[1], {from: accounts[0]})); | ||
}); | ||
|
||
it('forbids setting new address by non-owners', function() { | ||
return resolver.setAddrAsync(utils.node, accounts[1], {from: accounts[1]}) | ||
.then( | ||
tx => { throw new Error("expected to be forbidden"); }, | ||
err => assert.ok(err, err) | ||
); | ||
}); | ||
|
||
it('forbids writing same address by non-owners', function() { | ||
return resolver.setAddrAsync(utils.node, accounts[1], {from: accounts[0]}) | ||
.then(txid => resolver.setAddrAsync(utils.node, accounts[1], {from: accounts[1]})) | ||
.then( | ||
tx => { throw new Error("expected to be forbidden"); }, | ||
err => assert.ok(err, err) | ||
); | ||
}); | ||
|
||
it('forbids overwriting existing address by non-owners', function() { | ||
return resolver.setAddrAsync(utils.node, accounts[1], {from: accounts[0]}) | ||
.then(txid => resolver.setAddrAsync(utils.node, accounts[0], {from: accounts[1]})) | ||
.then( | ||
tx => { throw new Error("expected to be forbidden"); }, | ||
err => assert.ok(err, err) | ||
); | ||
}); | ||
|
||
}); | ||
|
||
describe('addr function', function() { | ||
|
||
it('returns zero when fetching nonexistent addresses', function() { | ||
this.slow(200); | ||
return resolver.addrAsync(utils.node) | ||
.then(result => assert.equal(result, "0x0000000000000000000000000000000000000000")); | ||
}); | ||
|
||
it('returns previously set address', function() { | ||
this.slow(200); | ||
return resolver.setAddrAsync(utils.node, accounts[1], {from: accounts[0]}) | ||
.then(txid => resolver.addrAsync(utils.node)) | ||
.then(address => assert.equal(address, accounts[1])); | ||
}); | ||
|
||
it('returns overwritten address', function() { | ||
this.slow(300); | ||
return resolver.setAddrAsync(utils.node, accounts[1], {from: accounts[0]}) | ||
.then(txid => resolver.setAddrAsync(utils.node, accounts[0], {from: accounts[0]})) | ||
.then(txid => resolver.addrAsync(utils.node)) | ||
.then(address => assert.equal(address, accounts[0])); | ||
}); | ||
|
||
}); | ||
|
||
it('forbids setting addresses by non-owners', function(done) { | ||
resolver.setAddr(utils.node, accounts[1], {from: accounts[1]}, function(err, tx) { | ||
assert.ok(err, err); | ||
done(); | ||
describe('setContent function', function() { | ||
|
||
it('permits setting content by owner', function() { | ||
return resolver.setContentAsync(utils.node, 'hash1', {from: accounts[0]}); | ||
}); | ||
|
||
it('can overwrite previously set content', function() { | ||
this.slow(200); | ||
return resolver.setContentAsync(utils.node, 'hash1', {from: accounts[0]}) | ||
.then(txid => resolver.setContentAsync(utils.node, 'hash2', {from: accounts[0]})); | ||
}); | ||
|
||
it('can overwrite to same content', function() { | ||
this.slow(200); | ||
return resolver.setContentAsync(utils.node, 'hash1', {from: accounts[0]}) | ||
.then(txid => resolver.setContentAsync(utils.node, 'hash1', {from: accounts[0]})); | ||
}); | ||
|
||
it('forbids setting content by non-owners', function() { | ||
return resolver.setContentAsync(utils.node, 'hash1', {from: accounts[1]}) | ||
.then( | ||
tx => { throw new Error("expected to be forbidden"); }, | ||
err => assert.ok(err, err) | ||
); | ||
}); | ||
|
||
it('forbids writing same content by non-owners', function() { | ||
return resolver.setContentAsync(utils.node, 'hash1', {from: accounts[0]}) | ||
.then(txid => resolver.setContentAsync(utils.node, 'hash1', {from: accounts[1]})) | ||
.then( | ||
tx => { throw new Error("expected to be forbidden"); }, | ||
err => assert.ok(err, err) | ||
); | ||
}); | ||
|
||
it('forbids overwriting existing content by non-owners', function() { | ||
return resolver.setContentAsync(utils.node, 'hash1', {from: accounts[0]}) | ||
.then(txid => resolver.setContentAsync(utils.node, 'hash2', {from: accounts[1]})) | ||
.then( | ||
tx => { throw new Error("expected to be forbidden"); }, | ||
err => assert.ok(err, err) | ||
); | ||
}); | ||
|
||
}); | ||
|
||
it('returns zero when fetching nonexistent addresses', function(done) { | ||
resolver.addr(utils.node, function(err, result) { | ||
assert.equal(err, null, err); | ||
assert.equal(result, "0x0000000000000000000000000000000000000000"); | ||
done(); | ||
describe('content function', function() { | ||
|
||
it('returns empty when fetching nonexistent content', function() { | ||
this.slow(200); | ||
return resolver.contentAsync(utils.node) | ||
.then(result => assert.equal(result, "0x0000000000000000000000000000000000000000000000000000000000000000")); | ||
}); | ||
|
||
it('returns previously set content', function() { | ||
this.slow(200); | ||
return resolver.setContentAsync(utils.node, 'hash1', {from: accounts[0]}) | ||
.then(txid => resolver.contentAsync(utils.node)) | ||
.then(content => assert.equal(web3.toUtf8(content), 'hash1')); | ||
}); | ||
|
||
it('returns overwritten content', function() { | ||
this.slow(300); | ||
return resolver.setContentAsync(utils.node, 'hash1', {from: accounts[0]}) | ||
.then(txid => resolver.setContentAsync(utils.node, 'hash2', {from: accounts[0]})) | ||
.then(txid => resolver.contentAsync(utils.node)) | ||
.then(content => assert.equal(web3.toUtf8(content), 'hash2')); | ||
}); | ||
|
||
}); | ||
|
||
}); |