Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

test(mobile): every test runs in a throwaway home, so none can touch a developer's ~/.harness

dee466bb8a93e28
diff --git a/mobile/lib/core/harness_file_store.dart b/mobile/lib/core/harness_file_store.dart
index 19be8c638..3c6ab3d74 100644
--- a/mobile/lib/core/harness_file_store.dart
+++ b/mobile/lib/core/harness_file_store.dart
@@ -69,13 +69,20 @@ class HarnessFileStore implements BatchLocalKeyValueStore {
// and neither may assume it is the only writer.
_cacheable = cacheableForTest || (isMobileHost && directory == null);
+ /// The home every default path resolves under instead of the real one — set
+ /// once for the whole test suite by `test/flutter_test_config.dart`, so that no
+ /// test, however it builds the app, can read or write a developer's own
+ /// `~/.harness`. A test that passes its own `environment` still gets its own.
+ @visibleForTesting
+ static String? homeForTest;
+
/// [name] names the sibling under `~/.harness`; it defaults to this store's own.
static String defaultDirectoryPath({
Map<String, String>? environment,
String? name,
}) {
final env = environment ?? Platform.environment;
- var home = env['HOME'];
+ var home = environment == null ? homeForTest ?? env['HOME'] : env['HOME'];
if ((home == null || home.isEmpty) && Platform.isWindows) {
home = env['USERPROFILE'];
if (home == null || home.isEmpty) {
diff --git a/mobile/test/flutter_test_config.dart b/mobile/test/flutter_test_config.dart
new file mode 100644
index 000000000..725292a0d
--- /dev/null
+++ b/mobile/test/flutter_test_config.dart
@@ -0,0 +1,22 @@
+import 'dart:async';
+import 'dart:io';
+
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness_mobile/core/harness_file_store.dart';
+
+/// Runs before every test file under `test/`.
+///
+/// ⚠️ **Every default path points into a throwaway home.** The app's stores,
+/// logs, crash log, analytics id and snapshots all resolve under
+/// `~/.harness` through [HarnessFileStore.defaultDirectoryPath], and a test
+/// that builds the app without handing each one a fake would otherwise read and
+/// write the developer's real state — sessions and E2EE keys included. See the
+/// rules in `mobile/README.md`.
+Future<void> testExecutable(FutureOr<void> Function() testMain) async {
+ final home = Directory.systemTemp.createTempSync('harness-test-home-');
+ HarnessFileStore.homeForTest = home.path;
+ tearDownAll(() {
+ if (home.existsSync()) home.deleteSync(recursive: true);
+ });
+ await testMain();
+}
diff --git a/mobile/test/test_home_guard_test.dart b/mobile/test/test_home_guard_test.dart
new file mode 100644
index 000000000..727ad0ea3
--- /dev/null
+++ b/mobile/test/test_home_guard_test.dart
@@ -0,0 +1,37 @@
+import 'dart:io';
+
+import 'package:flutter_test/flutter_test.dart';
+import 'package:harness_mobile/core/harness_file_store.dart';
+
+/// `test/flutter_test_config.dart` holds for every test: nothing a test builds
+/// with defaults can reach the developer's own `~/.harness`.
+void main() {
+ test('the shared store and every default path live in a throwaway home', () {
+ final realHome = Platform.environment['HOME'];
+ final temp = Directory.systemTemp.resolveSymbolicLinksSync();
+
+ for (final path in [
+ HarnessFileStore.shared.directory.path,
+ HarnessFileStore.defaultDirectoryPath(name: 'logs'),
+ ]) {
+ final resolved = Directory(path).absolute.path;
+ expect(resolved, isNot(startsWith('$realHome/.harness')));
+ expect(
+ resolved.startsWith(Directory.systemTemp.path) ||
+ resolved.startsWith(temp),
+ isTrue,
+ reason: '$resolved is not under the system temp directory',
+ );
+ }
+ });
+
+ test('a test that names its own environment still gets its own home', () {
+ expect(
+ HarnessFileStore.defaultDirectoryPath(
+ environment: const {'HOME': '/home/someone'},
+ name: 'logs',
+ ),
+ '/home/someone/.harness/logs',
+ );
+ });
+}