Skip to content
Snippets Groups Projects
Verified Commit 094d6a61 authored by Sebastian's avatar Sebastian
Browse files

Add MockTorPlugin that boots quickly for testing

parent 2149035b
No related branches found
No related tags found
No related merge requests found
Pipeline #10861 passed
......@@ -73,7 +73,7 @@ internal class AndroidTorModule {
backoff: Backoff,
lifecycleManager: LifecycleManager,
eventBus: EventBus,
): TorPlugin = AndroidTorPlugin(
): TorPlugin = MockTorPlugin(
ioExecutor,
app,
settingsManager,
......
/*
* Briar Mailbox
* Copyright (C) 2021-2022 The Briar Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
package org.briarproject.mailbox.core.tor;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import org.briarproject.mailbox.core.db.DbException;
import org.briarproject.mailbox.core.event.Event;
import org.briarproject.mailbox.core.settings.Settings;
import org.briarproject.mailbox.core.settings.SettingsManager;
import org.briarproject.mailbox.core.system.AndroidWakeLock;
import org.briarproject.mailbox.core.system.AndroidWakeLockManager;
import org.briarproject.mailbox.core.system.Clock;
import org.briarproject.mailbox.core.system.LocationUtils;
import org.briarproject.mailbox.core.system.ResourceProvider;
import org.slf4j.Logger;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.Executor;
import javax.annotation.Nullable;
import static org.briarproject.mailbox.core.tor.TorConstants.HS_ADDRESS_V3;
import static org.briarproject.mailbox.core.tor.TorConstants.SETTINGS_NAMESPACE;
import static org.briarproject.mailbox.core.util.LogUtils.info;
import static org.briarproject.mailbox.core.util.LogUtils.logException;
import static org.briarproject.mailbox.core.util.PrivacyUtils.scrubOnion;
import static org.slf4j.LoggerFactory.getLogger;
public class MockTorPlugin extends TorPlugin {
private static final Logger LOG = getLogger(MockTorPlugin.class);
private final Context ctx;
private final AndroidWakeLock wakeLock;
private final SettingsManager settingsManager;
MockTorPlugin(Executor ioExecutor,
Context ctx,
SettingsManager settingsManager,
NetworkManager networkManager,
LocationUtils locationUtils,
Clock clock,
ResourceProvider resourceProvider,
CircumventionProvider circumventionProvider,
AndroidWakeLockManager wakeLockManager,
Backoff backoff,
@Nullable String architecture,
File torDirectory) {
super(ioExecutor, settingsManager, networkManager, locationUtils, clock,
resourceProvider, circumventionProvider, backoff, architecture,
torDirectory);
this.ctx = ctx;
wakeLock = wakeLockManager.createWakeLock("TorPlugin");
this.settingsManager = settingsManager;
}
@Override
protected int getProcessId() {
return android.os.Process.myPid();
}
@Override
protected long getLastUpdateTime() {
try {
PackageManager pm = ctx.getPackageManager();
PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(), 0);
return pi.lastUpdateTime;
} catch (NameNotFoundException e) {
throw new AssertionError(e);
}
}
@Override
protected void enableNetwork(boolean enable) throws IOException {
if (enable) wakeLock.acquire();
super.enableNetwork(enable);
if (!enable) wakeLock.release();
}
@Override
public void startService() {
state.setStarted();
state.enableNetwork(true);
state.getAndSetCircuitBuilt();
state.setBootstrapPercent(100);
state.onServiceDescriptorUploaded();
state.onServiceDescriptorUploaded();
state.onServiceDescriptorUploaded();
Settings s = new Settings();
String onion3 =
"rxyuze4lx2xzanneyu2wzcfxodkhxcix76nc57higz7cn2g6bt76jiqd";
s.put(HS_ADDRESS_V3, onion3);
info(LOG, () -> "V3 hidden service " + scrubOnion(onion3));
try {
settingsManager.mergeSettings(s, SETTINGS_NAMESPACE);
} catch (DbException e) {
logException(LOG, e, "Error while merging settings");
}
}
@Override
public void stopService() {
wakeLock.release();
}
@Override
public void orConnStatus(String status, String orName) {
// No-Op
}
@Override
public void eventOccurred(Event e) {
// No-Op
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment