mega-mock
mega-mock mocks MEGA API servers. It's intended to be used to test MEGA clients.
Supported features
- Logging into (version 1) MEGA accounts
- Getting basic account info
- Listing files from accounts
- Listing files from shared folders
- Uploading files
- Downloading files
- Creating and sharing folders
- Updating attributes
- Deleting files
Usage
CLI
- Install it using
npm install mega-mock
(or npm install -g mega-mock
) - Run it using
npx mega-mock
(or mega-mock
) - Configure MEGA to use
http://localhost:3000/
as gateway - Log in into
mock@test
with password mock
- Use MEGA as usual
- Stop server by using
Ctrl+C
It will create a "mega-mock-data" folder. Uploaded files will be stored named as their handlers. Server state is stored in "state.json" when it stops. Temporary files, used on upload, are also stored in this folder and may remain if some error happens. Those files can be used during tests in order to check if clients tested are working as expected.
Module
Install it using npm install mega-mock
then run the following:
const megamock = require('mega-mock')
const server = megamock({
dataFolder: 'path to the data folder',
visualize: true,
state: {}
})
server.listen(3000, '0.0.0.0')
server.state.users
server.state.shares
server.state.loginData
server.state.uploadStates
To make implementation simpler "uh" is used as the internal user identifier. Initial server state is normalized by casting data of each property using new Map
.
When using the module the data folder isn't created automatically. Also no users are registered: you can register users by following the "new account registration" instructions below.
Account registration
The account login flow uses RSA encryption, so to simplify implementation the server use pre-generated data. This data can be generated by opening MEGA website and running this code in the console:
;(async function () {
let email = 'mock@test'
let password = 'mock'
const derivedKey = prepare_key_pw(password)
const derivedAes = new sjcl.cipher.aes(derivedKey)
const uh = stringhash(email, derivedAes)
const accountKey = [109, 111, 99, 107]
const u_k_aes = new sjcl.cipher.aes(accountKey)
const testSid = (uh + '_megamock'.repeat(4)).substr(0, 43)
const rsakey = await new Promise(resolve => {
const w = new Worker('/keygen.js')
w.onmessage = function (e) {
w.terminate()
resolve(e.data)
}
const workerSeed = new Uint8Array(256)
asmCrypto.getRandomValues(workerSeed)
w.postMessage([2048, 257, workerSeed])
})
console.log('Run server.state.loginData.set(%s, %s)', JSON.stringify(uh),
JSON.stringify({
csid: base64urlencode(crypto_rsaencrypt(base64urldecode(testSid), rsakey)),
privk: a32_to_base64(encrypt_key(u_k_aes, str_to_a32(crypto_encodeprivkey(rsakey)))),
k: a32_to_base64(encrypt_key(derivedAes, accountKey))
}))
}())
By now only version 1 accounts are supported, that's why the code above uses prepare_key_pw
.