feat: add snjs package

This commit is contained in:
Karol Sójko
2022-07-06 14:04:18 +02:00
parent 321a055bae
commit 0e40469e2f
296 changed files with 46109 additions and 187 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,584 @@
/* eslint-disable no-unused-expressions */
/* eslint-disable no-undef */
import * as Factory from '../lib/factory.js'
import FakeWebCrypto from '../lib/fake_web_crypto.js'
chai.use(chaiAsPromised)
const expect = chai.expect
describe('2020-01-15 web migration', () => {
beforeEach(() => {
localStorage.clear()
})
afterEach(() => {
localStorage.clear()
})
/**
* This test will pass but sync afterwards will not be successful
* as we are using a random value for the legacy session token
*/
it('2020-01-15 migration with passcode and account', async function () {
const application = await Factory.createAppWithRandNamespace()
/** Create legacy migrations value so that base migration detects old app */
await application.deviceInterface.setRawStorageValue('migrations', JSON.stringify(['anything']))
const operator003 = new SNProtocolOperator003(new FakeWebCrypto())
const identifier = 'foo'
const passcode = 'bar'
/** Create old version passcode parameters */
const passcodeKey = await operator003.createRootKey(identifier, passcode)
await application.deviceInterface.setRawStorageValue(
'offlineParams',
JSON.stringify(passcodeKey.keyParams.getPortableValue()),
)
/** Create arbitrary storage values and make sure they're migrated */
const arbitraryValues = {
foo: 'bar',
zar: 'tar',
har: 'car',
}
for (const key of Object.keys(arbitraryValues)) {
await application.deviceInterface.setRawStorageValue(key, arbitraryValues[key])
}
/** Create old version account parameters */
const password = 'tar'
const accountKey = await operator003.createRootKey(identifier, password)
/** Create legacy storage and encrypt it with passcode */
const embeddedStorage = {
mk: accountKey.masterKey,
ak: accountKey.dataAuthenticationKey,
pw: accountKey.serverPassword,
jwt: 'anything',
/** Legacy versions would store json strings inside of embedded storage */
auth_params: JSON.stringify(accountKey.keyParams.getPortableValue()),
}
const storagePayload = new DecryptedPayload({
uuid: await operator003.crypto.generateUUID(),
content_type: ContentType.EncryptedStorage,
content: {
storage: embeddedStorage,
},
})
const encryptionParams = await operator003.generateEncryptedParametersAsync(storagePayload, passcodeKey)
const persistPayload = new EncryptedPayload({ ...storagePayload, ...encryptionParams })
await application.deviceInterface.setRawStorageValue('encryptedStorage', JSON.stringify(persistPayload))
/** Create encrypted item and store it in db */
const notePayload = Factory.createNotePayload()
const noteEncryptionParams = await operator003.generateEncryptedParametersAsync(notePayload, accountKey)
const noteEncryptedPayload = new EncryptedPayload({ ...notePayload, ...noteEncryptionParams })
await application.deviceInterface.saveRawDatabasePayload(noteEncryptedPayload, application.identifier)
/** Run migration */
await application.prepareForLaunch({
receiveChallenge: async (challenge) => {
application.submitValuesForChallenge(challenge, [CreateChallengeValue(challenge.prompts[0], passcode)])
},
})
await application.launch(true)
expect(application.sessionManager.online()).to.equal(true)
expect(application.protocolService.rootKeyEncryption.keyMode).to.equal(KeyMode.RootKeyPlusWrapper)
/** Should be decrypted */
const storageMode = application.diskStorageService.domainKeyForMode(StorageValueModes.Default)
const valueStore = application.diskStorageService.values[storageMode]
expect(valueStore.content_type).to.not.be.ok
expect(await application.deviceInterface.getRawStorageValue('offlineParams')).to.not.be.ok
const keyParams = await application.diskStorageService.getValue(StorageKey.RootKeyParams, StorageValueModes.Nonwrapped)
expect(typeof keyParams).to.equal('object')
/** Embedded value should match */
const migratedKeyParams = await application.diskStorageService.getValue(
StorageKey.RootKeyParams,
StorageValueModes.Nonwrapped,
)
expect(migratedKeyParams).to.eql(JSON.parse(embeddedStorage.auth_params))
const rootKey = await application.protocolService.getRootKey()
expect(rootKey.masterKey).to.equal(accountKey.masterKey)
expect(rootKey.dataAuthenticationKey).to.equal(accountKey.dataAuthenticationKey)
/** Application should not retain server password from legacy versions */
expect(rootKey.serverPassword).to.not.be.ok
expect(rootKey.keyVersion).to.equal(ProtocolVersion.V003)
expect(application.protocolService.rootKeyEncryption.keyMode).to.equal(KeyMode.RootKeyPlusWrapper)
/** Expect note is decrypted */
expect(application.itemManager.getDisplayableNotes().length).to.equal(1)
const retrievedNote = application.itemManager.getDisplayableNotes()[0]
expect(retrievedNote.uuid).to.equal(notePayload.uuid)
expect(retrievedNote.content.text).to.equal(notePayload.content.text)
/** Ensure arbitrary values have been migrated */
for (const key of Object.keys(arbitraryValues)) {
const value = await application.diskStorageService.getValue(key)
expect(arbitraryValues[key]).to.equal(value)
}
console.warn('Expecting exception due to deiniting application while trying to renew session')
await Factory.safeDeinit(application)
}).timeout(15000)
it('2020-01-15 migration with passcode only', async function () {
const application = await Factory.createAppWithRandNamespace()
/** Create legacy migrations value so that base migration detects old app */
await application.deviceInterface.setRawStorageValue('migrations', JSON.stringify(['anything']))
const operator003 = new SNProtocolOperator003(new FakeWebCrypto())
const identifier = 'foo'
const passcode = 'bar'
/** Create old version passcode parameters */
const passcodeKey = await operator003.createRootKey(identifier, passcode)
await application.deviceInterface.setRawStorageValue(
'offlineParams',
JSON.stringify(passcodeKey.keyParams.getPortableValue()),
)
/** Create arbitrary storage values and make sure they're migrated */
const arbitraryValues = {
foo: 'bar',
zar: 'tar',
har: 'car',
}
for (const key of Object.keys(arbitraryValues)) {
await application.deviceInterface.setRawStorageValue(key, arbitraryValues[key])
}
const embeddedStorage = {
...arbitraryValues,
}
const storagePayload = new DecryptedPayload({
uuid: await operator003.crypto.generateUUID(),
content: {
storage: embeddedStorage,
},
content_type: ContentType.EncryptedStorage,
})
const encryptionParams = await operator003.generateEncryptedParametersAsync(storagePayload, passcodeKey)
const persistPayload = new EncryptedPayload({ ...storagePayload, ...encryptionParams })
await application.deviceInterface.setRawStorageValue('encryptedStorage', JSON.stringify(persistPayload))
/** Create encrypted item and store it in db */
const notePayload = Factory.createNotePayload()
const noteEncryptionParams = await operator003.generateEncryptedParametersAsync(notePayload, passcodeKey)
const noteEncryptedPayload = new EncryptedPayload({ ...notePayload, ...noteEncryptionParams })
await application.deviceInterface.saveRawDatabasePayload(noteEncryptedPayload, application.identifier)
await application.prepareForLaunch({
receiveChallenge: async (challenge) => {
application.submitValuesForChallenge(challenge, [CreateChallengeValue(challenge.prompts[0], passcode)])
},
})
await application.launch(true)
expect(application.protocolService.rootKeyEncryption.keyMode).to.equal(KeyMode.WrapperOnly)
/** Should be decrypted */
const storageMode = application.diskStorageService.domainKeyForMode(StorageValueModes.Default)
const valueStore = application.diskStorageService.values[storageMode]
expect(valueStore.content_type).to.not.be.ok
expect(await application.deviceInterface.getRawStorageValue('offlineParams')).to.not.be.ok
/** Embedded value should match */
const migratedKeyParams = await application.diskStorageService.getValue(
StorageKey.RootKeyParams,
StorageValueModes.Nonwrapped,
)
expect(migratedKeyParams).to.eql(embeddedStorage.auth_params)
const rootKey = await application.protocolService.getRootKey()
expect(rootKey.masterKey).to.equal(passcodeKey.masterKey)
expect(rootKey.dataAuthenticationKey).to.equal(passcodeKey.dataAuthenticationKey)
/** Root key is in memory with passcode only, so server password can be defined */
expect(rootKey.serverPassword).to.be.ok
expect(rootKey.keyVersion).to.equal(ProtocolVersion.V003)
expect(application.protocolService.rootKeyEncryption.keyMode).to.equal(KeyMode.WrapperOnly)
/** Expect note is decrypted */
expect(application.itemManager.getDisplayableNotes().length).to.equal(1)
const retrievedNote = application.itemManager.getDisplayableNotes()[0]
expect(retrievedNote.uuid).to.equal(notePayload.uuid)
expect(retrievedNote.content.text).to.equal(notePayload.content.text)
/** Ensure arbitrary values have been migrated */
for (const key of Object.keys(arbitraryValues)) {
const value = await application.diskStorageService.getValue(key)
expect(arbitraryValues[key]).to.equal(value)
}
await Factory.safeDeinit(application)
})
/**
* This test will pass but sync afterwards will not be successful
* as we are using a random value for the legacy session token
*/
it('2020-01-15 migration with account only', async function () {
const application = await Factory.createAppWithRandNamespace()
/** Create legacy migrations value so that base migration detects old app */
await application.deviceInterface.setRawStorageValue('migrations', JSON.stringify(['anything']))
const operator003 = new SNProtocolOperator003(new FakeWebCrypto())
const identifier = 'foo'
/** Create old version account parameters */
const password = 'tar'
const accountKey = await operator003.createRootKey(identifier, password)
/** Create arbitrary storage values and make sure they're migrated */
const storage = {
foo: 'bar',
zar: 'tar',
har: 'car',
mk: accountKey.masterKey,
ak: accountKey.dataAuthenticationKey,
pw: accountKey.serverPassword,
jwt: 'anything',
/** Legacy versions would store json strings inside of embedded storage */
auth_params: JSON.stringify(accountKey.keyParams.getPortableValue()),
}
for (const key of Object.keys(storage)) {
await application.deviceInterface.setRawStorageValue(key, storage[key])
}
/** Create encrypted item and store it in db */
const notePayload = Factory.createNotePayload()
const noteEncryptionParams = await operator003.generateEncryptedParametersAsync(notePayload, accountKey)
const noteEncryptedPayload = new EncryptedPayload({ ...notePayload, ...noteEncryptionParams })
await application.deviceInterface.saveRawDatabasePayload(noteEncryptedPayload, application.identifier)
/** Run migration */
const promptValueReply = (prompts) => {
const values = []
for (const prompt of prompts) {
if (prompt.validation === ChallengeValidation.LocalPasscode) {
values.push(CreateChallengeValue(prompt, passcode))
} else {
/** We will be prompted to reauthetnicate our session, not relevant to this test
* but pass any value to avoid exception
*/
values.push(CreateChallengeValue(prompt, 'foo'))
}
}
return values
}
const receiveChallenge = async (challenge) => {
application.addChallengeObserver(challenge, {
onInvalidValue: (value) => {
const values = promptValueReply([value.prompt])
application.submitValuesForChallenge(challenge, values)
},
})
const initialValues = promptValueReply(challenge.prompts)
application.submitValuesForChallenge(challenge, initialValues)
}
await application.prepareForLaunch({
receiveChallenge: receiveChallenge,
})
await application.launch(true)
expect(application.sessionManager.online()).to.equal(true)
expect(application.protocolService.rootKeyEncryption.keyMode).to.equal(KeyMode.RootKeyOnly)
/** Should be decrypted */
const storageMode = application.diskStorageService.domainKeyForMode(StorageValueModes.Default)
const valueStore = application.diskStorageService.values[storageMode]
expect(valueStore.content_type).to.not.be.ok
/** Embedded value should match */
const migratedKeyParams = await application.diskStorageService.getValue(
StorageKey.RootKeyParams,
StorageValueModes.Nonwrapped,
)
expect(migratedKeyParams).to.eql(accountKey.keyParams.getPortableValue())
const rootKey = await application.protocolService.getRootKey()
expect(rootKey).to.be.ok
expect(await application.deviceInterface.getRawStorageValue('migrations')).to.not.be.ok
expect(await application.deviceInterface.getRawStorageValue('auth_params')).to.not.be.ok
expect(await application.deviceInterface.getRawStorageValue('jwt')).to.not.be.ok
const keyParams = await application.diskStorageService.getValue(StorageKey.RootKeyParams, StorageValueModes.Nonwrapped)
expect(typeof keyParams).to.equal('object')
expect(rootKey.masterKey).to.equal(accountKey.masterKey)
expect(rootKey.dataAuthenticationKey).to.equal(accountKey.dataAuthenticationKey)
expect(rootKey.serverPassword).to.not.be.ok
expect(rootKey.keyVersion).to.equal(ProtocolVersion.V003)
expect(application.protocolService.rootKeyEncryption.keyMode).to.equal(KeyMode.RootKeyOnly)
/** Expect note is decrypted */
expect(application.itemManager.getDisplayableNotes().length).to.equal(1)
const retrievedNote = application.itemManager.getDisplayableNotes()[0]
expect(retrievedNote.uuid).to.equal(notePayload.uuid)
expect(retrievedNote.content.text).to.equal(notePayload.content.text)
/** Ensure arbitrary values have been migrated */
for (const key of Object.keys(storage)) {
/** Is stringified in storage, but parsed in storageService */
if (key === 'auth_params') {
continue
}
const value = await application.diskStorageService.getValue(key)
expect(storage[key]).to.equal(value)
}
console.warn('Expecting exception due to deiniting application while trying to renew session')
await Factory.safeDeinit(application)
})
it('2020-01-15 migration with no account and no passcode', async function () {
const application = await Factory.createAppWithRandNamespace()
/** Create legacy migrations value so that base migration detects old app */
await application.deviceInterface.setRawStorageValue('migrations', JSON.stringify(['anything']))
/** Create arbitrary storage values and make sure they're migrated */
const storage = {
foo: 'bar',
zar: 'tar',
har: 'car',
}
for (const key of Object.keys(storage)) {
await application.deviceInterface.setRawStorageValue(key, storage[key])
}
/** Create item and store it in db */
const notePayload = Factory.createNotePayload()
await application.deviceInterface.saveRawDatabasePayload(notePayload.ejected(), application.identifier)
/** Run migration */
await application.prepareForLaunch({
receiveChallenge: (_challenge) => {
return null
},
})
await application.launch(true)
expect(application.protocolService.rootKeyEncryption.keyMode).to.equal(KeyMode.RootKeyNone)
/** Should be decrypted */
const storageMode = application.diskStorageService.domainKeyForMode(StorageValueModes.Default)
const valueStore = application.diskStorageService.values[storageMode]
expect(valueStore.content_type).to.not.be.ok
const rootKey = await application.protocolService.getRootKey()
expect(rootKey).to.not.be.ok
expect(application.protocolService.rootKeyEncryption.keyMode).to.equal(KeyMode.RootKeyNone)
expect(await application.deviceInterface.getRawStorageValue('migrations')).to.not.be.ok
/** Expect note is decrypted */
expect(application.itemManager.getDisplayableNotes().length).to.equal(1)
const retrievedNote = application.itemManager.getDisplayableNotes()[0]
expect(retrievedNote.uuid).to.equal(notePayload.uuid)
expect(retrievedNote.content.text).to.equal(notePayload.content.text)
/** Ensure arbitrary values have been migrated */
for (const key of Object.keys(storage)) {
const value = await application.diskStorageService.getValue(key)
expect(storage[key]).to.equal(value)
}
await Factory.safeDeinit(application)
})
/**
* This test will pass but sync afterwards will not be successful
* as we are using a random value for the legacy session token
*/
it('2020-01-15 migration from app v1.0.1 with account only', async function () {
const application = await Factory.createAppWithRandNamespace()
/** Create legacy migrations value so that base migration detects old app */
await application.deviceInterface.setRawStorageValue('migrations', JSON.stringify(['anything']))
const operator001 = new SNProtocolOperator001(new FakeWebCrypto())
const identifier = 'foo'
/** Create old version account parameters */
const password = 'tar'
const accountKey = await operator001.createRootKey(identifier, password)
/** Create arbitrary storage values and make sure they're migrated */
const storage = {
mk: accountKey.masterKey,
pw: accountKey.serverPassword,
jwt: 'anything',
/** Legacy versions would store json strings inside of embedded storage */
auth_params: JSON.stringify(accountKey.keyParams.getPortableValue()),
user: JSON.stringify({ uuid: 'anything', email: 'anything' }),
}
for (const key of Object.keys(storage)) {
await application.deviceInterface.setRawStorageValue(key, storage[key])
}
/** Create encrypted item and store it in db */
const notePayload = Factory.createNotePayload()
const noteEncryptionParams = await operator001.generateEncryptedParametersAsync(notePayload, accountKey)
const noteEncryptedPayload = new EncryptedPayload({ ...notePayload, ...noteEncryptionParams })
await application.deviceInterface.saveRawDatabasePayload(noteEncryptedPayload, application.identifier)
/** Run migration */
const promptValueReply = (prompts) => {
const values = []
for (const prompt of prompts) {
/** We will be prompted to reauthetnicate our session, not relevant to this test
* but pass any value to avoid exception
*/
values.push(CreateChallengeValue(prompt, 'foo'))
}
return values
}
const receiveChallenge = async (challenge) => {
application.addChallengeObserver(challenge, {
onInvalidValue: (value) => {
const values = promptValueReply([value.prompt])
application.submitValuesForChallenge(challenge, values)
},
})
const initialValues = promptValueReply(challenge.prompts)
application.submitValuesForChallenge(challenge, initialValues)
}
await application.prepareForLaunch({
receiveChallenge: receiveChallenge,
})
await application.launch(true)
expect(application.sessionManager.online()).to.equal(true)
expect(application.sessionManager.getUser()).to.be.ok
expect(application.protocolService.rootKeyEncryption.keyMode).to.equal(KeyMode.RootKeyOnly)
/** Should be decrypted */
const storageMode = application.diskStorageService.domainKeyForMode(StorageValueModes.Default)
const valueStore = application.diskStorageService.values[storageMode]
expect(valueStore.content_type).to.not.be.ok
/** Embedded value should match */
const migratedKeyParams = await application.diskStorageService.getValue(
StorageKey.RootKeyParams,
StorageValueModes.Nonwrapped,
)
expect(migratedKeyParams).to.eql(accountKey.keyParams.getPortableValue())
const rootKey = await application.protocolService.getRootKey()
expect(rootKey).to.be.ok
expect(await application.deviceInterface.getRawStorageValue('migrations')).to.not.be.ok
expect(await application.deviceInterface.getRawStorageValue('auth_params')).to.not.be.ok
expect(await application.deviceInterface.getRawStorageValue('jwt')).to.not.be.ok
expect(await application.deviceInterface.getRawStorageValue('ak')).to.not.be.ok
expect(await application.deviceInterface.getRawStorageValue('mk')).to.not.be.ok
expect(await application.deviceInterface.getRawStorageValue('pw')).to.not.be.ok
const keyParams = await application.diskStorageService.getValue(StorageKey.RootKeyParams, StorageValueModes.Nonwrapped)
expect(typeof keyParams).to.equal('object')
expect(rootKey.masterKey).to.equal(accountKey.masterKey)
expect(rootKey.dataAuthenticationKey).to.equal(accountKey.dataAuthenticationKey)
expect(rootKey.serverPassword).to.not.be.ok
expect(rootKey.keyVersion).to.equal(ProtocolVersion.V001)
expect(application.protocolService.rootKeyEncryption.keyMode).to.equal(KeyMode.RootKeyOnly)
/** Expect note is decrypted */
expect(application.itemManager.getDisplayableNotes().length).to.equal(1)
const retrievedNote = application.itemManager.getDisplayableNotes()[0]
expect(retrievedNote.uuid).to.equal(notePayload.uuid)
expect(retrievedNote.content.text).to.equal(notePayload.content.text)
/** Ensure arbitrary values have been migrated */
for (const key of Object.keys(storage)) {
/** Is stringified in storage, but parsed in storageService */
const value = await application.diskStorageService.getValue(key)
if (key === 'auth_params') {
continue
} else if (key === 'user') {
expect(storage[key]).to.equal(JSON.stringify(value))
} else {
expect(storage[key]).to.equal(value)
}
}
await Factory.safeDeinit(application)
})
it('2020-01-15 migration from 002 app with account and passcode but missing offlineParams.version', async function () {
/**
* There was an issue where if the user had offlineParams but it was missing the version key,
* the user could not get past the passcode migration screen.
*/
const application = await Factory.createAppWithRandNamespace()
/** Create legacy migrations value so that base migration detects old app */
await application.deviceInterface.setRawStorageValue('migrations', JSON.stringify(['anything']))
const operator002 = new SNProtocolOperator002(new FakeWebCrypto())
const identifier = 'foo'
const passcode = 'bar'
/** Create old version passcode parameters */
const passcodeKey = await operator002.createRootKey(identifier, passcode)
/** The primary chaos agent */
const offlineParams = passcodeKey.keyParams.getPortableValue()
omitInPlace(offlineParams, ['version'])
await application.deviceInterface.setRawStorageValue('offlineParams', JSON.stringify(offlineParams))
/** Create old version account parameters */
const password = 'tar'
const accountKey = await operator002.createRootKey(identifier, password)
/** Create legacy storage and encrypt it with passcode */
const embeddedStorage = {
mk: accountKey.masterKey,
ak: accountKey.dataAuthenticationKey,
pw: accountKey.serverPassword,
jwt: 'anything',
/** Legacy versions would store json strings inside of embedded storage */
auth_params: JSON.stringify(accountKey.keyParams.getPortableValue()),
user: JSON.stringify({ uuid: 'anything', email: 'anything' }),
}
const storagePayload = new DecryptedPayload({
uuid: await operator002.crypto.generateUUID(),
content_type: ContentType.EncryptedStorage,
content: {
storage: embeddedStorage,
},
})
const encryptionParams = await operator002.generateEncryptedParametersAsync(storagePayload, passcodeKey)
const persistPayload = new EncryptedPayload({ ...storagePayload, ...encryptionParams })
await application.deviceInterface.setRawStorageValue('encryptedStorage', JSON.stringify(persistPayload))
/** Create encrypted item and store it in db */
const notePayload = Factory.createNotePayload()
const noteEncryptionParams = await operator002.generateEncryptedParametersAsync(notePayload, accountKey)
const noteEncryptedPayload = new EncryptedPayload({ ...notePayload, ...noteEncryptionParams })
await application.deviceInterface.saveRawDatabasePayload(noteEncryptedPayload, application.identifier)
/** Runs migration */
await application.prepareForLaunch({
receiveChallenge: async (challenge) => {
application.submitValuesForChallenge(challenge, [CreateChallengeValue(challenge.prompts[0], passcode)])
},
})
await application.launch(true)
expect(application.sessionManager.online()).to.equal(true)
expect(application.sessionManager.getUser()).to.be.ok
expect(application.protocolService.rootKeyEncryption.keyMode).to.equal(KeyMode.RootKeyPlusWrapper)
/** Should be decrypted */
const storageMode = application.diskStorageService.domainKeyForMode(StorageValueModes.Default)
const valueStore = application.diskStorageService.values[storageMode]
expect(valueStore.content_type).to.not.be.ok
/** Embedded value should match */
const migratedKeyParams = await application.diskStorageService.getValue(
StorageKey.RootKeyParams,
StorageValueModes.Nonwrapped,
)
expect(migratedKeyParams).to.eql(accountKey.keyParams.getPortableValue())
const rootKey = await application.protocolService.getRootKey()
expect(rootKey).to.be.ok
expect(await application.deviceInterface.getRawStorageValue('migrations')).to.not.be.ok
expect(await application.deviceInterface.getRawStorageValue('auth_params')).to.not.be.ok
expect(await application.deviceInterface.getRawStorageValue('jwt')).to.not.be.ok
expect(await application.deviceInterface.getRawStorageValue('ak')).to.not.be.ok
expect(await application.deviceInterface.getRawStorageValue('mk')).to.not.be.ok
expect(await application.deviceInterface.getRawStorageValue('pw')).to.not.be.ok
const keyParams = await application.diskStorageService.getValue(StorageKey.RootKeyParams, StorageValueModes.Nonwrapped)
expect(typeof keyParams).to.equal('object')
expect(rootKey.masterKey).to.equal(accountKey.masterKey)
expect(rootKey.dataAuthenticationKey).to.equal(accountKey.dataAuthenticationKey)
expect(rootKey.serverPassword).to.not.be.ok
expect(rootKey.keyVersion).to.equal(ProtocolVersion.V002)
/** Expect note is decrypted */
expect(application.itemManager.getDisplayableNotes().length).to.equal(1)
const retrievedNote = application.itemManager.getDisplayableNotes()[0]
expect(retrievedNote.uuid).to.equal(notePayload.uuid)
expect(retrievedNote.content.text).to.equal(notePayload.content.text)
await Factory.safeDeinit(application)
})
})

View File

@@ -0,0 +1,175 @@
import * as Factory from '../lib/factory.js'
chai.use(chaiAsPromised)
const expect = chai.expect
describe('migrations', () => {
const allMigrations = ['2.0.0', '2.0.15', '2.7.0', '2.20.0', '2.36.0', '2.42.0']
beforeEach(async () => {
localStorage.clear()
})
afterEach(async () => {
localStorage.clear()
})
it('version number is stored as string', async function () {
const application = await Factory.createInitAppWithFakeCrypto()
const version = await application.migrationService.getStoredSnjsVersion()
expect(typeof version).to.equal('string')
await Factory.safeDeinit(application)
})
it('should return correct required migrations if stored version is 1.0.0', async function () {
expect((await SNMigrationService.getRequiredMigrations('1.0.0')).length).to.equal(allMigrations.length)
})
it('should return correct required migrations if stored version is 2.0.0', async function () {
expect((await SNMigrationService.getRequiredMigrations('2.0.0')).length).to.equal(allMigrations.length - 1)
})
it('should return 0 required migrations if stored version is futuristic', async function () {
expect((await SNMigrationService.getRequiredMigrations('100.0.1')).length).to.equal(0)
})
it('after running base migration, legacy structure should set version as 1.0.0', async function () {
const application = await Factory.createAppWithRandNamespace()
/** Set up 1.0.0 structure with tell-tale storage key */
await application.deviceInterface.setRawStorageValue('migrations', JSON.stringify(['anything']))
await application.migrationService.runBaseMigrationPreRun()
expect(await application.migrationService.getStoredSnjsVersion()).to.equal('1.0.0')
await Factory.safeDeinit(application)
})
it('after running base migration, 2.0.0 structure set version as 2.0.0', async function () {
const application = await Factory.createAppWithRandNamespace()
/** Set up 2.0.0 structure with tell-tale storage key */
await application.deviceInterface.setRawStorageValue(
namespacedKey(application.identifier, 'last_migration_timestamp'),
'anything',
)
await application.migrationService.runBaseMigrationPreRun()
expect(await application.migrationService.getStoredSnjsVersion()).to.equal('2.0.0')
await Factory.safeDeinit(application)
})
it('after running base migration with no present storage values, should set version to current', async function () {
const application = await Factory.createAppWithRandNamespace()
await application.migrationService.runBaseMigrationPreRun()
expect(await application.migrationService.getStoredSnjsVersion()).to.equal(SnjsVersion)
await Factory.safeDeinit(application)
})
it('after running all migrations from a 1.0.0 installation, should set stored version to current', async function () {
const application = await Factory.createAppWithRandNamespace()
/** Set up 1.0.0 structure with tell-tale storage key */
await application.deviceInterface.setRawStorageValue('migrations', JSON.stringify(['anything']))
await application.prepareForLaunch({
receiveChallenge: () => {},
})
await application.launch(true)
expect(await application.migrationService.getStoredSnjsVersion()).to.equal(SnjsVersion)
await Factory.safeDeinit(application)
})
it('after running all migrations from a 2.0.0 installation, should set stored version to current', async function () {
const application = await Factory.createAppWithRandNamespace()
/** Set up 2.0.0 structure with tell-tale storage key */
await application.deviceInterface.setRawStorageValue('last_migration_timestamp', JSON.stringify(['anything']))
await application.prepareForLaunch({
receiveChallenge: () => {},
})
await application.launch(true)
expect(await application.migrationService.getStoredSnjsVersion()).to.equal(SnjsVersion)
await Factory.safeDeinit(application)
})
it('should be correct migration count coming from 1.0.0', async function () {
const application = await Factory.createAppWithRandNamespace()
await application.deviceInterface.setRawStorageValue('migrations', 'anything')
await application.migrationService.runBaseMigrationPreRun()
expect(await application.migrationService.getStoredSnjsVersion()).to.equal('1.0.0')
const pendingMigrations = await SNMigrationService.getRequiredMigrations(
await application.migrationService.getStoredSnjsVersion(),
)
expect(pendingMigrations.length).to.equal(allMigrations.length)
expect(pendingMigrations[0].version()).to.equal('2.0.0')
await application.prepareForLaunch({
receiveChallenge: () => {},
})
await application.launch(true)
expect(await application.migrationService.getStoredSnjsVersion()).to.equal(SnjsVersion)
await Factory.safeDeinit(application)
})
it('2.20.0 remove mfa migration', async function () {
const application = await Factory.createAppWithRandNamespace()
await application.prepareForLaunch({
receiveChallenge: () => {},
})
await application.launch(true)
const mfaItem = CreateDecryptedItemFromPayload(
new DecryptedPayload({
uuid: '123',
content_type: 'SF|MFA',
content: FillItemContent({
key: '123',
}),
}),
)
await application.mutator.insertItem(mfaItem)
await application.sync.sync()
expect(application.items.getItems('SF|MFA').length).to.equal(1)
expect(
(await application.diskStorageService.getAllRawPayloads()).filter((p) => p.content_type === 'SF|MFA').length,
).to.equal(1)
/** Run migration */
const migration = new Migration2_20_0(application.migrationService.services)
await migration.handleStage(ApplicationStage.LoadedDatabase_12)
expect(application.items.getItems('SF|MFA').length).to.equal(0)
expect(
(await application.diskStorageService.getAllRawPayloads()).filter((p) => p.content_type === 'SF|MFA').length,
).to.equal(0)
await Factory.safeDeinit(application)
})
it('2.42.0 remove no distraction theme', async function () {
const application = await Factory.createAppWithRandNamespace()
await application.prepareForLaunch({
receiveChallenge: () => {},
})
await application.launch(true)
const noDistractionItem = CreateDecryptedItemFromPayload(
new DecryptedPayload({
uuid: '123',
content_type: ContentType.Theme,
content: FillItemContent({
package_info: {
identifier: 'org.standardnotes.theme-no-distraction',
},
}),
}),
)
await application.mutator.insertItem(noDistractionItem)
await application.sync.sync()
expect(application.items.getItems(ContentType.Theme).length).to.equal(1)
/** Run migration */
const migration = new Migration2_42_0(application.migrationService.services)
await migration.handleStage(ApplicationStage.FullSyncCompleted_13)
await application.sync.sync()
expect(application.items.getItems(ContentType.Theme).length).to.equal(0)
await Factory.safeDeinit(application)
})
})

View File

@@ -0,0 +1,191 @@
/* eslint-disable no-unused-expressions */
/* eslint-disable no-undef */
import * as Factory from '../lib/factory.js'
chai.use(chaiAsPromised)
const expect = chai.expect
const setupRandomUuid = () => {
let currentId = 0
UuidGenerator.SetGenerator(() => String(currentId++))
}
describe('web native folders migration', () => {
beforeEach(async function () {
this.application = await Factory.createInitAppWithFakeCrypto()
setupRandomUuid()
})
afterEach(async function () {
await Factory.safeDeinit(this.application)
// TODO: cleanup uuid behind us or we'll mess other tests.
})
it('migration with flat tag folders', async function () {
const titles = ['a', 'b', 'c']
await makeTags(this.application, titles)
// Run the migration
await this.application.mutator.migrateTagsToFolders()
// Check new tags
const result = extractTagHierarchy(this.application)
expect(result).to.deep.equal({
a: { _uuid: 'a' },
b: { _uuid: 'b' },
c: { _uuid: 'c' },
})
})
it('migration with simple tag folders', async function () {
const titles = ['a.b.c', 'b', 'a.b']
await makeTags(this.application, titles)
// Run the migration
await this.application.mutator.migrateTagsToFolders()
// Check new tags
const result = extractTagHierarchy(this.application)
expect(result).to.deep.equal({
a: {
_uuid: '0',
b: {
_uuid: 'a.b',
c: { _uuid: 'a.b.c' },
},
},
b: { _uuid: 'b' },
})
})
it('migration with more complex cases', async function () {
const titles = ['a.b.c', 'b', 'a.b']
await makeTags(this.application, titles)
// Run the migration
await this.application.mutator.migrateTagsToFolders()
// Check new tags
const result = extractTagHierarchy(this.application)
expect(result).to.deep.equal({
a: {
_uuid: '0',
b: {
_uuid: 'a.b',
c: { _uuid: 'a.b.c' },
},
},
b: { _uuid: 'b' },
})
})
it('should produce a valid hierarchy cases with missing intermediate tags or unordered', async function () {
const titles = ['y.2', 'w.3', 'y']
await makeTags(this.application, titles)
// Run the migration
await this.application.mutator.migrateTagsToFolders()
// Check new tags
const result = extractTagHierarchy(this.application)
expect(result).to.deep.equal({
w: {
_uuid: '0',
3: {
_uuid: 'w.3',
},
},
y: { _uuid: 'y', 2: { _uuid: 'y.2' } },
})
})
it('skip prefixed names', async function () {
const titles = ['.something', '.something...something', 'something.a.b.c']
await makeTags(this.application, titles)
// Run the migration
await this.application.mutator.migrateTagsToFolders()
// Check new tags
const result = extractTagHierarchy(this.application)
expect(result).to.deep.equal({
'.something': { _uuid: '.something' },
'.something...something': { _uuid: '.something...something' },
something: {
_uuid: '0',
a: { _uuid: '1', b: { _uuid: '2', c: { _uuid: 'something.a.b.c' } } },
},
})
})
it('skip not-supported names', async function () {
const titles = [
'something.',
'something..',
'something..another.thing',
'a.b.c',
'a',
'something..another.thing..anyway',
]
await makeTags(this.application, titles)
// Run the migration
await this.application.mutator.migrateTagsToFolders()
// Check new tags
const result = extractTagHierarchy(this.application)
expect(result).to.deep.equal({
'something.': { _uuid: 'something.' },
'something..': { _uuid: 'something..' },
'something..another.thing': { _uuid: 'something..another.thing' },
'something..another.thing..anyway': {
_uuid: 'something..another.thing..anyway',
},
a: {
_uuid: 'a',
b: {
_uuid: '0',
c: {
_uuid: 'a.b.c',
},
},
},
})
})
})
const makeTags = async (application, titles) => {
const createTag = (title) => {
return Factory.createMappedTag(application, { title, uuid: title })
}
const tags = await Promise.all(titles.map(createTag))
return tags
}
const extractTagHierarchy = (application) => {
const result = {}
const roots = application.itemManager.getRootTags()
const constructHierarchy = (currentTag, result) => {
result[currentTag.title] = { _uuid: currentTag.uuid }
const children = application.items.getTagChildren(currentTag)
children.forEach((child) => {
constructHierarchy(child, result[currentTag.title])
})
}
roots.forEach((tag) => {
constructHierarchy(tag, result)
})
return result
}