[tauri] TauriPlatforms: The shell around the built game, the Android privacy patch, the shell build of the game, and the device check
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
/**
|
||||
* The Android privacy patch (tauri-platforms.md D6), on Tauri 2.12.5's stock manifest exactly as
|
||||
* `tauri android init` wrote it (fixtures/AndroidManifest.stock.xml), and on the committed rule files.
|
||||
*/
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
APPLICATION_ATTRS,
|
||||
DOMAINS,
|
||||
manifestProblems,
|
||||
META_DATA,
|
||||
patchManifest,
|
||||
RULE_FILES,
|
||||
RULE_SECTIONS,
|
||||
ruleProblems
|
||||
} from '../src/android-privacy.js';
|
||||
|
||||
const HERE = dirname(fileURLToPath(import.meta.url));
|
||||
const STOCK = readFileSync(join(HERE, 'fixtures', 'AndroidManifest.stock.xml'), 'utf8').replaceAll('\r\n', '\n');
|
||||
const rules = (f: string): string => readFileSync(join(HERE, '..', 'android', 'res', 'xml', f), 'utf8');
|
||||
|
||||
describe('patchManifest', () => {
|
||||
const patched = patchManifest(STOCK);
|
||||
|
||||
it("removes Tauri's INTERNET permission: the kernel then refuses the app any socket", () => {
|
||||
expect(STOCK).toContain('android.permission.INTERNET');
|
||||
expect(patched).not.toContain('android.permission.INTERNET');
|
||||
});
|
||||
|
||||
it('turns backup and device transfer off on <application>', () => {
|
||||
const tag = /<application\b[^>]*>/.exec(patched)?.[0] ?? '';
|
||||
for (const [name, value] of Object.entries(APPLICATION_ATTRS)) expect(tag).toContain(`${name}="${value}"`);
|
||||
// What Tauri set is kept.
|
||||
expect(tag).toContain('android:icon="@mipmap/ic_launcher"');
|
||||
expect(tag).toContain('android:usesCleartextTraffic="${usesCleartextTraffic}"');
|
||||
});
|
||||
|
||||
it("switches the WebView's metrics and Safe Browsing off, inside <application>", () => {
|
||||
const inside = /<application\b[\s\S]*<\/application>/.exec(patched)?.[0] ?? '';
|
||||
for (const [name, value] of Object.entries(META_DATA)) {
|
||||
expect(inside).toContain(`<meta-data android:name="${name}" android:value="${value}" />`);
|
||||
}
|
||||
});
|
||||
|
||||
it('leaves the rest of the manifest as it was', () => {
|
||||
for (const keep of ['android:name=".MainActivity"', 'android.intent.category.LAUNCHER', 'androidx.core.content.FileProvider']) {
|
||||
expect(patched).toContain(keep);
|
||||
}
|
||||
});
|
||||
|
||||
it('changes nothing on a second run', () => {
|
||||
expect(patchManifest(patched)).toBe(patched);
|
||||
});
|
||||
|
||||
it('keeps Windows line endings when the file has them', () => {
|
||||
const crlf = patchManifest(STOCK.replaceAll('\n', '\r\n'));
|
||||
expect(crlf).toBe(patched.replaceAll('\n', '\r\n'));
|
||||
});
|
||||
|
||||
it('overwrites an allowBackup="true" rather than adding a second one', () => {
|
||||
const on = STOCK.replace('<application', '<application android:allowBackup="true"');
|
||||
const out = patchManifest(on);
|
||||
expect(out).toContain('android:allowBackup="false"');
|
||||
expect(out).not.toContain('android:allowBackup="true"');
|
||||
expect(out.match(/android:allowBackup=/g)).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('refuses a manifest whose shape it does not know, rather than guessing', () => {
|
||||
expect(() => patchManifest(STOCK.replace('</application>', '</application><application></application>'))).toThrow(/application/);
|
||||
expect(() => patchManifest(STOCK.replace(/<application[\s\S]*<\/application>/, '<application />'))).toThrow();
|
||||
// INTERNET asked for in a form the line pattern does not match.
|
||||
const odd = STOCK.replace(
|
||||
'<uses-permission android:name="android.permission.INTERNET" />',
|
||||
'<uses-permission android:name="android.permission.INTERNET" android:maxSdkVersion="99" />'
|
||||
);
|
||||
expect(() => patchManifest(odd)).toThrow(/INTERNET/);
|
||||
});
|
||||
|
||||
it('removes INTERNET written over two lines too', () => {
|
||||
const split = STOCK.replace(
|
||||
'<uses-permission android:name="android.permission.INTERNET" />',
|
||||
'<uses-permission\n android:name="android.permission.INTERNET" />'
|
||||
);
|
||||
expect(patchManifest(split)).not.toContain('INTERNET');
|
||||
});
|
||||
});
|
||||
|
||||
describe('manifestProblems', () => {
|
||||
it("names everything missing from Tauri's stock manifest", () => {
|
||||
const problems = manifestProblems(STOCK);
|
||||
expect(problems).toContain('android.permission.INTERNET is requested');
|
||||
for (const name of Object.keys(APPLICATION_ATTRS)) expect(problems.join('\n')).toContain(name);
|
||||
for (const name of Object.keys(META_DATA)) expect(problems.join('\n')).toContain(name);
|
||||
expect(problems).toHaveLength(1 + Object.keys(APPLICATION_ATTRS).length + Object.keys(META_DATA).length);
|
||||
});
|
||||
|
||||
it('finds nothing missing once patched', () => {
|
||||
expect(manifestProblems(patchManifest(STOCK))).toEqual([]);
|
||||
});
|
||||
|
||||
it('notices a single switch turned back on', () => {
|
||||
const patched = patchManifest(STOCK);
|
||||
expect(manifestProblems(patched.replace('android:allowBackup="false"', 'android:allowBackup="true"'))).toEqual([
|
||||
'<application> lacks android:allowBackup="false"'
|
||||
]);
|
||||
expect(manifestProblems(patched.replace('android:value="true"', 'android:value="false"'))).toEqual([
|
||||
'no meta-data android.webkit.WebView.MetricsOptOut=true'
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('the committed rule files', () => {
|
||||
for (const f of RULE_FILES) {
|
||||
it(`${f} excludes every domain, in every section, and includes nothing`, () => {
|
||||
expect(ruleProblems(rules(f), RULE_SECTIONS[f])).toEqual([]);
|
||||
});
|
||||
}
|
||||
|
||||
it('a file that forgets one domain is caught, by name', () => {
|
||||
const f = 'rosea_data_extraction_rules.xml' as const;
|
||||
const text = rules(f);
|
||||
const firstDb = text.indexOf('<exclude domain="database" path="." />');
|
||||
const broken = text.slice(0, firstDb) + text.slice(firstDb + '<exclude domain="database" path="." />'.length);
|
||||
expect(ruleProblems(broken, RULE_SECTIONS[f])).toEqual(['cloud-backup: database is not excluded']);
|
||||
});
|
||||
|
||||
it('a file that includes something is caught', () => {
|
||||
const f = 'rosea_backup_rules.xml' as const;
|
||||
const broken = rules(f).replace('</full-backup-content>', '<include domain="file" path="." />\n</full-backup-content>');
|
||||
expect(ruleProblems(broken, RULE_SECTIONS[f])).toEqual(['includes something']);
|
||||
});
|
||||
|
||||
it('knows all nine storage domains', () => {
|
||||
expect(DOMAINS).toHaveLength(9);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user