diff --git a/tools/ota_analysis/jest.config.js b/tools/ota_analysis/jest.config.js index 4dbaae648..c23e006db 100644 --- a/tools/ota_analysis/jest.config.js +++ b/tools/ota_analysis/jest.config.js @@ -1,6 +1,14 @@ +const defaults = require('jest-config') + module.exports = { preset: '@vue/cli-plugin-unit-jest', transform: { '^.+\\.vue$': 'vue-jest' + }, + globals: { + ...defaults.globals, + crypto: require('crypto'), + TextEncoder: require('util').TextEncoder, + TextDecoder: require('util').TextDecoder, } -} +} \ No newline at end of file diff --git a/tools/ota_analysis/src/__tests__/map_parser.test.js b/tools/ota_analysis/src/__tests__/map_parser.test.js new file mode 100644 index 000000000..6d5243f33 --- /dev/null +++ b/tools/ota_analysis/src/__tests__/map_parser.test.js @@ -0,0 +1,63 @@ +import * as zip from '@zip.js/zip.js/dist/zip-full.min.js' +import { MapParser } from '@/services/map_parser.js' + +var targetFile = new Blob() +var mapParser +// Please refer to system_test.map for more details. +const systemMap = [ + '//system/apex/com.android.test1.apex', + '//system/apex/com.android.test2.apex', + '//system/apex/com.android.test2.apex', + '//system/apex/com.android.test2.apex', + '//system/apex/com.android.test3.apk', + '//system/apex/com.android.test1.apex', + '//system/apex/com.android.test1.apex', + '//system/apex/com.android.test1.apex', + '//system/apex/com.android.test1.apex', + 'unknown', + '//init.environ.rc' +] + +/** + * Generate a virtual Android build which only contains a .map file + */ +beforeAll(async () => { + // web worker is not supported by zip.js, turn it off + zip.configure({ + useWebWorkers: false, + }) + // Use system_test.map as a virtual map file + const fs = require("fs") + const path = require("path") + const file = path.join(__dirname, "./", "system_test.map") + const text = fs.readFileSync(file, 'utf-8', (err, data) => data) + const blobWriter = new zip.BlobWriter("application/zip") + const writer = new zip.ZipWriter(blobWriter) + await writer.add("IMAGES/system_test.map", new zip.TextReader(text)) + await writer.close() + targetFile = blobWriter.getData() + mapParser = new MapParser(targetFile) + await mapParser.init() +}) + +test('Initialize a map parser instance.', () => { + expect(mapParser.mapFiles.keys().next().value).toEqual('system_test') +}) + +test('Establish a map of system file.', async () => { + await mapParser.add('system_test', 11) + expect(mapParser.maps.get('system_test')).toEqual(systemMap) +}) + +test('Query the map of system file.', async () => { + await mapParser.add('system_test', 11) + var queryExtents = [] + for (let i = 0; i < 11; i++) + queryExtents.push(new Object({ + startBlock: i, + // The number of blocks does not matter, because we only query the + // starting block. + numBlocks: 0 + })) + expect(mapParser.query('system_test', queryExtents)).toEqual(systemMap) +}) \ No newline at end of file diff --git a/tools/ota_analysis/src/__tests__/system_test.map b/tools/ota_analysis/src/__tests__/system_test.map new file mode 100644 index 000000000..9b61b41bc --- /dev/null +++ b/tools/ota_analysis/src/__tests__/system_test.map @@ -0,0 +1,4 @@ +//init.environ.rc 10 +//system/apex/com.android.test1.apex 5-6 0 0 7-8 +//system/apex/com.android.test2.apex 1-3 +//system/apex/com.android.test3.apk 4 \ No newline at end of file