Commit 454b216a authored by Cédric Moreau's avatar Cédric Moreau

[fix] Lock certification scenarios with tests

parent 3911f6c0
......@@ -91,14 +91,21 @@ export class CIndexDAL extends AbstractIndex<CindexEntry> {
}
findExpired(medianTime:number) {
return this.query('SELECT * FROM ' + this.table + ' c1 WHERE expires_on <= ? ' +
return this.query('SELECT * FROM ' + this.table + ' c1 WHERE c1.expires_on <= ? ' +
'AND NOT EXISTS (' +
' SELECT * FROM c_index c2' +
' WHERE c1.issuer = c2.issuer' +
' AND c1.receiver = c2.receiver' +
' AND c2.op = ?' +
' AND c1.expired_on IS NOT NULL' +
')', [medianTime, CommonConstants.IDX_UPDATE])
' AND c2.writtenOn > c1.writtenOn' +
' AND c2.expired_on IS NOT NULL' +
') ' +
'AND NOT EXISTS (' +
' SELECT * FROM c_index c3' +
' WHERE c1.issuer = c3.issuer' +
' AND c1.receiver = c3.receiver' +
' AND c3.writtenOn > c1.writtenOn' +
' AND c3.replayable_on IS NOT NULL' +
')', [medianTime])
}
async findByIssuer(issuer:string) {
......
......@@ -19,7 +19,7 @@ import {CommonConstants} from "../../../app/lib/common-libs/constants"
describe('Certification replay', () => writeBasicTestWithConfAnd2Users({
sigReplay: 3,
sigPeriod: 0,
sigValidity: 10,
sigValidity: 5,
}, (test) => {
before(() => {
......@@ -39,37 +39,90 @@ describe('Certification replay', () => writeBasicTestWithConfAnd2Users({
await s1.commit({ time: now })
})
test('should exist only 1 valid link from cat replyable at t + 3', async (s1, cat) => {
const reducableFromCat = await s1.dal.cindexDAL.reducablesFrom(cat.pub)
assertEqual(reducableFromCat.length, 1)
assertEqual(reduce(reducableFromCat).chainable_on, now) // No delay between certifications
assertEqual(reduce(reducableFromCat).replayable_on, now + 3) // Replay of a particular certification <-- What we want to test
assertEqual(reduce(reducableFromCat).expires_on, now + 10) // The expiration date of the certification **INITIALLY**
test('should exist only 1 valid link from cat replyable at t + 3', async (s1, cat, tac) => {
const reducableFromTac = await s1.dal.cindexDAL.reducablesFrom(tac.pub)
assertEqual(reducableFromTac.length, 1)
assertEqual(reduce(reducableFromTac).chainable_on, now) // No delay between certifications
assertEqual(reduce(reducableFromTac).replayable_on, now + 3) // Replay of a particular certification <-- What we want to test
assertEqual(reduce(reducableFromTac).expires_on, now + 5) // The expiration date of the certification **INITIALLY**
})
test('should reject a replay from cat', async (s1, cat, tac) => {
await assertThrows(cat.cert(tac), '{\n "ucode": 1004,\n "message": "Already up-to-date"\n}')
test('should reject a replay from tac', async (s1, cat, tac) => {
await assertThrows(tac.cert(cat), '{\n "ucode": 1004,\n "message": "Already up-to-date"\n}')
})
test('should accept replay if time has passed enought', async (s1, cat, tac) => {
await s1.commit({ time: now + 4 })
await s1.commit({ time: now + 4 })
await cat.cert(tac)
await tac.cert(cat)
const b = await s1.commit({ time: now + 4 })
assertEqual(b.certifications.length, 1)
})
test('should exist only 2 CINDEX entries from cat', async (s1, cat) => {
const validLinksFromCat = await s1.dal.cindexDAL.findByIssuer(cat.pub)
assertEqual(validLinksFromCat.length, 2)
test('should exist only 2 CINDEX entries from cat', async (s1, cat, tac) => {
const validLinksFromTac = await s1.dal.cindexDAL.findByIssuer(tac.pub)
assertEqual(validLinksFromTac.length, 2)
})
test('should exist only 1 valid link from cat', async (s1, cat) => {
const reducableFromCat = await s1.dal.cindexDAL.getValidLinksFrom(cat.pub)
assertEqual(reducableFromCat.length, 1)
assertEqual(reduce(reducableFromCat).chainable_on, now + 4)
assertEqual(reduce(reducableFromCat).replayable_on, now + 4 + 3) // Replayable date should have changed!
assertEqual(reduce(reducableFromCat).expires_on, now + 4 + 10) // The expiration date should have changed! (this is the interest of a replay)
test('should exist only 1 valid link from cat', async (s1, cat, tac) => {
const reducableFromTac = await s1.dal.cindexDAL.getValidLinksFrom(tac.pub)
assertEqual(reducableFromTac.length, 1)
assertEqual(reduce(reducableFromTac).chainable_on, now + 4)
assertEqual(reduce(reducableFromTac).replayable_on, now + 4 + 3) // Replayable date should have changed!
assertEqual(reduce(reducableFromTac).expires_on, now + 4 + 5) // The expiration date should have changed! (this is the interest of a replay)
})
test('should kick a member who lost its last certification', async (s1) => {
const b5 = await s1.commit({ time: now + 5 })
const b6 = await s1.commit({ time: now + 5 })
const b7 = await s1.commit({ time: now + 5 })
assertEqual(b5.excluded.length, 0)
assertEqual(b6.excluded.length, 0)
assertEqual(b7.excluded.length, 1)
})
test('should allow comeback if a new cert is received', async (s1, cat, tac) => {
await cat.cert(tac)
await tac.join()
const b8 = await s1.commit({ time: now + 8 })
const b9 = await s1.commit({ time: now + 8 })
const b10 = await s1.commit({ time: now + 8 })
assertEqual(b8.joiners.length, 1)
assertEqual(b8.excluded.length, 0)
assertEqual(b9.excluded.length, 0)
assertEqual(b10.excluded.length, 0)
})
test('should allow to maintain again (2 certification replay)', async (s1, cat, tac) => {
await tac.cert(cat)
const b11 = await s1.commit({ time: now + 9 })
const b12 = await s1.commit({ time: now + 9 })
assertEqual(b11.certifications.length, 1)
assertEqual(b11.excluded.length, 0)
assertEqual(b12.excluded.length, 0)
const reducableFromTac = await s1.dal.cindexDAL.reducablesFrom(tac.pub)
assertEqual(reduce(reducableFromTac).expires_on, now + 4 + 5 + 4) // The expiration date should have changed! (this is the interest of a replay)
})
test('should kick tac again for lack of certifications', async (s1) => {
const b13 = await s1.commit({ time: now + 10 })
const b14 = await s1.commit({ time: now + 10 })
const b15 = await s1.commit({ time: now + 10 })
assertEqual(b13.excluded.length, 0)
assertEqual(b14.excluded.length, 0)
assertEqual(b15.excluded.length, 1)
})
test('should allow comeback again, and again.', async (s1, cat, tac) => {
await cat.cert(tac)
await tac.join()
const b16 = await s1.commit({ time: now + 10 })
const b17 = await s1.commit({ time: now + 10 })
const b18 = await s1.commit({ time: now + 10 })
assertEqual(b16.joiners.length, 1)
assertEqual(b16.excluded.length, 0)
assertEqual(b17.excluded.length, 0)
assertEqual(b18.excluded.length, 0)
})
after(() => {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment