Skip to content

Commit

Permalink
fix(tests): apply more test cases for unsafe addresses and eth addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
atticusofsparta committed Dec 4, 2024
1 parent 94c0ed6 commit 75621c4
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 206 deletions.
231 changes: 99 additions & 132 deletions test/balances.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
AO_LOADER_HANDLER_ENV,
DEFAULT_HANDLE_OPTIONS,
STUB_ADDRESS,
STUB_ETH_ADDRESS,
} from '../tools/constants.mjs';

describe('aos Balances', async () => {
Expand Down Expand Up @@ -44,16 +45,105 @@ describe('aos Balances', async () => {
return result.Messages[0].Data;
}

it('should fetch the owner balance', async () => {
const result = await handle({
Tags: [
{ name: 'Action', value: 'Balance' },
{ name: 'Recipient', value: STUB_ADDRESS },
],
const STUB_RECIPIENT = 'recipient-'.padEnd(43, '1');

for (const [target_address, allowUnsafe, shouldPass] of [
[STUB_RECIPIENT, undefined, true],
[STUB_RECIPIENT, true, true],
[STUB_RECIPIENT, false, true],
[STUB_ETH_ADDRESS, undefined, true],
[STUB_ETH_ADDRESS, true, true],
[STUB_ETH_ADDRESS, false, true],
['invalid-address', true, true],
['invalid-address', false, false],
['invalid-address', false, false],
]) {
it(`should ${shouldPass ? '' : 'not'} fetch the target balance`, async () => {
const result = await handle({
Tags: [
{ name: 'Action', value: 'Balance' },
{ name: 'Recipient', value: target_address },
{ name: 'Allow-Unsafe-Addresses', value: allowUnsafe },
],
});

if (shouldPass === true) {
const targetBalance = result.Messages[0].Data;
assert.equal(typeof targetBalance === 'number', shouldPass);
} else {
assert.strictEqual(
result.Messages[0].Tags.find((t) => t.name === 'Error')?.value,
'Balance-Error',
);
}
});
const ownerBalance = result.Messages[0].Data;
assert(ownerBalance === 1);
});

it(`should ${allowUnsafe ? '' : 'not'} transfer the ANT`, async () => {
const transferResult = await handle({
Tags: [
{ name: 'Action', value: 'Transfer' },
{ name: 'Recipient', value: target_address },
{ name: 'Allow-Unsafe-Addresses', value: allowUnsafe },
],
});

if (shouldPass === true) {
const balancesResult = await handle(
{
Tags: [{ name: 'Action', value: 'Balances' }],
},
transferResult.Memory,
);
const balances = JSON.parse(balancesResult.Messages[0].Data);
assert.equal(balances[target_address] === 1, shouldPass);
} else {
assert.strictEqual(
transferResult.Messages[0].Tags.find((t) => t.name === 'Error')
?.value,
'Transfer-Error',
);
}
});

it(`should ${shouldPass ? '' : 'not'} send credit and debit notice on transfer`, async () => {
const transferResult = await handle({
Tags: [
{ name: 'Action', value: 'Transfer' },
{ name: 'Recipient', value: target_address },
{ name: 'Allow-Unsafe-Addresses', value: allowUnsafe },
],
});

if (shouldPass === true) {
const creditNotice = transferResult.Messages.find((msg) =>
msg.Tags.find(
(tag) => tag.name === 'Action' && tag.value === 'Credit-Notice',
),
);
const sender = creditNotice.Tags.find(
(tag) => tag.name === 'Sender' && tag.value === STUB_ADDRESS,
).value;
assert.equal(sender === STUB_ADDRESS, shouldPass);
const debitNotice = transferResult.Messages.find((msg) =>
msg.Tags.find(
(tag) => tag.name === 'Action' && tag.value === 'Debit-Notice',
),
);
const recipient = debitNotice.Tags.find(
(tag) => tag.name === 'Recipient' && tag.value === target_address,
).value;
assert.equal(recipient === target_address, shouldPass);
} else {
assert.strictEqual(
transferResult.Messages[0].Tags.find((t) => t.name === 'Error')
?.value,
'Transfer-Error',
);
}
});
// for end
}

it('should fetch the balances of the ANT', async () => {
const result = await handle({
Tags: [{ name: 'Action', value: 'Balances' }],
Expand All @@ -68,59 +158,6 @@ describe('aos Balances', async () => {
assert(ownerBalance === 1);
});

it('should transfer the ANT', async () => {
const recipient = ''.padEnd(43, '2');
const transferResult = await handle({
Tags: [
{ name: 'Action', value: 'Transfer' },
{ name: 'Recipient', value: recipient },
],
});

const balancesResult = await handle(
{
Tags: [{ name: 'Action', value: 'Balances' }],
},
transferResult.Memory,
);

const balances = JSON.parse(balancesResult.Messages[0].Data);
assert(balances[recipient] === 1);
});

it('should send credit and debit notice on transfer', async () => {
const recipient = ''.padEnd(43, '2');
const transferResult = await handle({
Tags: [
{ name: 'Action', value: 'Transfer' },
{ name: 'Recipient', value: recipient },
],
});

const creditNotice = transferResult.Messages.find((msg) =>
msg.Tags.find(
(tag) => tag.name === 'Action' && tag.value === 'Credit-Notice',
),
);
assert(creditNotice);
assert(
creditNotice.Tags.find(
(tag) => tag.name === 'Sender' && tag.value === STUB_ADDRESS,
),
);
const debitNotice = transferResult.Messages.find((msg) =>
msg.Tags.find(
(tag) => tag.name === 'Action' && tag.value === 'Debit-Notice',
),
);
assert(debitNotice);
assert(
debitNotice.Tags.find(
(tag) => tag.name === 'Recipient' && tag.value === recipient,
),
);
});

it('should set the logo of the ant', async () => {
const logo = 'my-logo-'.padEnd(43, '0');
const result = await handle({
Expand All @@ -137,74 +174,4 @@ describe('aos Balances', async () => {
const res = await getTotalSupply();
assert.strictEqual(res, 1, 'total supply should be equal to 1');
});

for (const allowUnsafeAddresses of [false, undefined]) {
it(`should transfer when an invalid address is provided and \`Allow-Unsafe-Addresses\` is ${allowUnsafeAddresses}`, async () => {
const recipient = 'invalid-address';
const senderBalance = await handle({
Tags: [{ name: 'Action', value: 'Balance' }],
});
const senderBalanceData = JSON.parse(senderBalance.Messages[0].Data);
const transferResult = await handle({
Tags: [
{ name: 'Action', value: 'Transfer' },
{ name: 'Recipient', value: recipient },
{ name: 'Cast', value: true },
{ name: 'Allow-Unsafe-Addresses', value: allowUnsafeAddresses },
],
});

// assert the error tag
const errorTag = transferResult.Messages?.[0]?.Tags?.find(
(tag) => tag.name === 'Error',
);
assert.ok(errorTag, 'Error tag should be present');

const result = await handle(
{
Tags: [{ name: 'Action', value: 'Balances' }],
},
transferResult.Memory,
);
const balances = JSON.parse(result.Messages[0].Data);

assert.equal(balances[recipient], undefined);
assert.equal(balances[STUB_ADDRESS], 1);
});
}

/**
* We allow transfers to addresses that may appear invalid if the `Allow-Unsafe-Addresses` tag is true for several reasons:
* 1. To support future address formats and signature schemes that may emerge
* 2. To maintain compatibility with new blockchain networks that could be integrated
* 3. To avoid breaking changes if address validation rules need to be updated
* 4. To give users flexibility in how they structure their addresses
* 5. To reduce protocol-level restrictions that could limit innovation
*/
it('should transfer when an invalid address is provided and `Allow-Unsafe-Addresses` is true', async () => {
const recipient = 'invalid-address';
const senderBalance = await handle({
Tags: [{ name: 'Action', value: 'Balance' }],
});
const senderBalanceData = JSON.parse(senderBalance.Messages[0].Data);
const transferResult = await handle({
Tags: [
{ name: 'Action', value: 'Transfer' },
{ name: 'Recipient', value: recipient },
{ name: 'Cast', value: true },
{ name: 'Allow-Unsafe-Addresses', value: true },
],
});

// get balances
const result = await handle(
{
Tags: [{ name: 'Action', value: 'Balances' }],
},
transferResult.Memory,
);
const balances = JSON.parse(result.Messages[0].Data);
assert.strictEqual(balances[recipient], 1);
assert.strictEqual(balances[STUB_ADDRESS], undefined);
});
});
106 changes: 76 additions & 30 deletions test/controllers.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
AO_LOADER_HANDLER_ENV,
DEFAULT_HANDLE_OPTIONS,
STUB_ADDRESS,
STUB_ETH_ADDRESS,
} from '../tools/constants.mjs';

describe('aos Controllers', async () => {
Expand All @@ -22,6 +23,15 @@ describe('aos Controllers', async () => {
);
}

async function getControllers(mem = startMemory) {
return handle(
{
Tags: [{ name: 'Action', value: 'Controllers' }],
},
mem,
);
}

it('should get the controllers', async () => {
const result = await handle({
Tags: [{ name: 'Action', value: 'Controllers' }],
Expand All @@ -31,40 +41,76 @@ describe('aos Controllers', async () => {
assert(controllers);
assert(controllers.includes(STUB_ADDRESS));
});
const stubController = ''.padEnd(43, 'controller');
for (const [target_address, allowUnsafe, shouldPass] of [
[stubController, undefined, true],
[stubController, true, true],
[stubController, false, true],
[STUB_ETH_ADDRESS, undefined, true],
[STUB_ETH_ADDRESS, true, true],
[STUB_ETH_ADDRESS, false, true],
['invalid-address', true, true],
['invalid-address', false, false],
['invalid-address', false, false],
]) {
it(`should ${shouldPass ? 'add' : 'not add'} the controller ${target_address}`, async () => {
const result = await handle({
Tags: [
{ name: 'Action', value: 'Add-Controller' },
{ name: 'Controller', value: target_address },
{ name: 'Allow-Unsafe-Addresses', value: allowUnsafe },
],
});

it('should add the controller', async () => {
const controller = ''.padEnd(43, '2');
const result = await handle({
Tags: [
{ name: 'Action', value: 'Add-Controller' },
{ name: 'Controller', value: controller },
],
});

assert(JSON.parse(result.Messages[0].Data).includes(controller), true);
});

it('should remove the controller', async () => {
const result = await handle({
Tags: [
{ name: 'Action', value: 'Remove-Controller' },
{ name: 'Controller', value: STUB_ADDRESS },
],
if (shouldPass === true) {
assert(
JSON.parse(result.Messages[0].Data).includes(target_address),
shouldPass,
);
} else {
assert.strictEqual(
result.Messages[0].Tags.find((t) => t.name === 'Error')?.value,
'Add-Controller-Error',
);
}
});
assert(result);

const addControllerResult = await handle(
{
it(`should ${shouldPass ? 'remove' : 'not remove'} the controller ${target_address}`, async () => {
const addControllerResult = await handle({
Tags: [
{ name: 'Action', value: 'Add-Controller' },
{ name: 'Controller', value: STUB_ADDRESS },
{ name: 'Controller', value: target_address },
{ name: 'Allow-Unsafe-Addresses', value: allowUnsafe },
],
},
result.Memory,
);
assert.equal(
JSON.parse(addControllerResult.Messages[0].Data).includes(STUB_ADDRESS),
true,
);
});
});

const removeControllerResult = await handle(
{
Tags: [
{ name: 'Action', value: 'Remove-Controller' },
{ name: 'Controller', value: target_address },
{ name: 'Allow-Unsafe-Addresses', value: allowUnsafe },
],
},
addControllerResult.Memory,
);

if (shouldPass === true) {
const controllersRes = await getControllers(
removeControllerResult.Memory,
);
assert.strictEqual(
!JSON.parse(controllersRes.Messages[0].Data).includes(target_address),
shouldPass,
);
} else {
assert.strictEqual(
removeControllerResult.Messages[0].Tags.find(
(t) => t.name === 'Error',
)?.value,
'Remove-Controller-Error',
);
}
});
}
});
Loading

0 comments on commit 75621c4

Please sign in to comment.