/** * 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 ', () => { const tag = /]*>/.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 ", () => { const inside = //.exec(patched)?.[0] ?? ''; for (const [name, value] of Object.entries(META_DATA)) { expect(inside).toContain(``); } }); 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(' { expect(() => patchManifest(STOCK.replace('', ''))).toThrow(/application/); expect(() => patchManifest(STOCK.replace(//, ''))).toThrow(); // INTERNET asked for in a form the line pattern does not match. const odd = STOCK.replace( '', '' ); expect(() => patchManifest(odd)).toThrow(/INTERNET/); }); it('removes INTERNET written over two lines too', () => { const split = STOCK.replace( '', '' ); 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([ ' 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(''); const broken = text.slice(0, firstDb) + text.slice(firstDb + ''.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('', '\n'); expect(ruleProblems(broken, RULE_SECTIONS[f])).toEqual(['includes something']); }); it('knows all nine storage domains', () => { expect(DOMAINS).toHaveLength(9); }); });