diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2018-08-01 02:47:34 +0530 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-08-26 11:04:01 -0600 |
commit | c38f6c471d640a73e9c512b64c22001cdec3ba65 (patch) | |
tree | 404fc99e6684998f39a6466b4581decbba3c4f4b /app/src/main/java/com | |
parent | 85462de254050cc693f6f51cc549899fd656913a (diff) |
Extract error messages to string resources
Useful for validation errors and localisation later on
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main/java/com')
6 files changed, 35 insertions, 15 deletions
diff --git a/app/src/main/java/com/wireguard/android/model/TunnelManager.java b/app/src/main/java/com/wireguard/android/model/TunnelManager.java index 0032724e..dc868010 100644 --- a/app/src/main/java/com/wireguard/android/model/TunnelManager.java +++ b/app/src/main/java/com/wireguard/android/model/TunnelManager.java @@ -15,6 +15,7 @@ import android.support.annotation.Nullable; import com.wireguard.android.Application; import com.wireguard.android.BR; +import com.wireguard.android.R; import com.wireguard.android.configStore.ConfigStore; import com.wireguard.android.model.Tunnel.State; import com.wireguard.android.model.Tunnel.Statistics; @@ -46,6 +47,7 @@ public final class TunnelManager extends BaseObservable { private static final String KEY_RUNNING_TUNNELS = "enabled_configs"; private final ConfigStore configStore; + private final Context context = Application.get(); private final CompletableFuture<ObservableSortedKeyedList<String, Tunnel>> completableTunnels = new CompletableFuture<>(); private final ObservableSortedKeyedList<String, Tunnel> tunnels = new ObservableSortedKeyedArrayList<>(COMPARATOR); @Nullable private Tunnel lastUsedTunnel; @@ -64,9 +66,9 @@ public final class TunnelManager extends BaseObservable { public CompletionStage<Tunnel> create(final String name, @Nullable final Config config) { if (Tunnel.isNameInvalid(name)) - return CompletableFuture.failedFuture(new IllegalArgumentException("Invalid name")); + return CompletableFuture.failedFuture(new IllegalArgumentException(context.getString(R.string.tunnel_error_invalid_name))); if (tunnels.containsKey(name)) { - final String message = "Tunnel " + name + " already exists"; + final String message = context.getString(R.string.tunnel_error_already_exists, name); return CompletableFuture.failedFuture(new IllegalArgumentException(message)); } return Application.getAsyncWorker().supplyAsync(() -> configStore.create(name, config)) @@ -212,9 +214,9 @@ public final class TunnelManager extends BaseObservable { CompletionStage<String> setTunnelName(final Tunnel tunnel, final String name) { if (Tunnel.isNameInvalid(name)) - return CompletableFuture.failedFuture(new IllegalArgumentException("Invalid name")); + return CompletableFuture.failedFuture(new IllegalArgumentException(context.getString(R.string.tunnel_error_invalid_name))); if (tunnels.containsKey(name)) { - final String message = "Tunnel " + name + " already exists"; + final String message = context.getString(R.string.tunnel_error_already_exists, name); return CompletableFuture.failedFuture(new IllegalArgumentException(message)); } final State originalState = tunnel.getState(); diff --git a/app/src/main/java/com/wireguard/config/Config.java b/app/src/main/java/com/wireguard/config/Config.java index db8c2fc7..fbfc35fc 100644 --- a/app/src/main/java/com/wireguard/config/Config.java +++ b/app/src/main/java/com/wireguard/config/Config.java @@ -6,6 +6,7 @@ package com.wireguard.config; +import android.content.Context; import android.databinding.BaseObservable; import android.databinding.Bindable; import android.databinding.ObservableArrayList; @@ -15,6 +16,8 @@ import android.os.Parcelable; import android.support.annotation.Nullable; import com.android.databinding.library.baseAdapters.BR; +import com.wireguard.android.Application; +import com.wireguard.android.R; import java.io.BufferedReader; import java.io.IOException; @@ -43,6 +46,7 @@ public class Config { public static Config from(final BufferedReader reader) throws IOException { final Config config = new Config(); + final Context context = Application.get(); Peer currentPeer = null; String line; boolean inInterfaceSection = false; @@ -65,11 +69,11 @@ public class Config { } else if (currentPeer != null) { currentPeer.parse(line); } else { - throw new IllegalArgumentException("Invalid configuration line: " + line); + throw new IllegalArgumentException(context.getString(R.string.tunnel_error_invalid_config_line, line)); } } if (!inInterfaceSection && currentPeer == null) { - throw new IllegalArgumentException("Could not find any config information"); + throw new IllegalArgumentException(context.getString(R.string.tunnel_error_no_config_information)); } return config; } diff --git a/app/src/main/java/com/wireguard/config/InetAddresses.java b/app/src/main/java/com/wireguard/config/InetAddresses.java index b7304cce..2cb43343 100644 --- a/app/src/main/java/com/wireguard/config/InetAddresses.java +++ b/app/src/main/java/com/wireguard/config/InetAddresses.java @@ -8,6 +8,9 @@ package com.wireguard.config; import android.support.annotation.Nullable; +import com.wireguard.android.Application; +import com.wireguard.android.R; + import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.InetAddress; @@ -30,7 +33,7 @@ public final class InetAddresses { public static InetAddress parse(@Nullable final String address) { if (address == null || address.isEmpty()) - throw new IllegalArgumentException("Empty address"); + throw new IllegalArgumentException(Application.get().getString(R.string.tunnel_error_empty_inetaddress)); try { return (InetAddress) PARSER_METHOD.invoke(null, address); } catch (final IllegalAccessException | InvocationTargetException e) { diff --git a/app/src/main/java/com/wireguard/config/Interface.java b/app/src/main/java/com/wireguard/config/Interface.java index eea1d18b..c43c2c5b 100644 --- a/app/src/main/java/com/wireguard/config/Interface.java +++ b/app/src/main/java/com/wireguard/config/Interface.java @@ -6,13 +6,16 @@ package com.wireguard.config; +import android.content.Context; import android.databinding.BaseObservable; import android.databinding.Bindable; import android.os.Parcel; import android.os.Parcelable; import android.support.annotation.Nullable; +import com.wireguard.android.Application; import com.wireguard.android.BR; +import com.wireguard.android.R; import com.wireguard.crypto.Keypair; import java.net.InetAddress; @@ -31,6 +34,7 @@ public class Interface { @Nullable private Keypair keypair; private int listenPort; private int mtu; + private final Context context = Application.get(); public Interface() { addressList = new ArrayList<>(); @@ -42,7 +46,7 @@ public class Interface { if (addresses != null && addresses.length > 0) { for (final String addr : addresses) { if (addr.isEmpty()) - throw new IllegalArgumentException("Address is empty"); + throw new IllegalArgumentException(context.getString(R.string.tunnel_error_empty_interface_address)); addressList.add(new InetNetwork(addr)); } } @@ -141,7 +145,7 @@ public class Interface { public void parse(final String line) { final Attribute key = Attribute.match(line); if (key == null) - throw new IllegalArgumentException(String.format("Unable to parse line: \"%s\"", line)); + throw new IllegalArgumentException(String.format(context.getString(R.string.tunnel_error_interface_parse_failed), line)); switch (key) { case ADDRESS: addAddresses(key.parseList(line)); diff --git a/app/src/main/java/com/wireguard/config/Peer.java b/app/src/main/java/com/wireguard/config/Peer.java index b65f52cb..1ed615e6 100644 --- a/app/src/main/java/com/wireguard/config/Peer.java +++ b/app/src/main/java/com/wireguard/config/Peer.java @@ -6,6 +6,7 @@ package com.wireguard.config; +import android.content.Context; import android.databinding.BaseObservable; import android.databinding.Bindable; import android.os.Parcel; @@ -13,6 +14,8 @@ import android.os.Parcelable; import android.support.annotation.Nullable; import com.android.databinding.library.baseAdapters.BR; +import com.wireguard.android.Application; +import com.wireguard.android.R; import com.wireguard.crypto.KeyEncoding; import java.net.Inet6Address; @@ -38,6 +41,7 @@ public class Peer { private int persistentKeepalive; @Nullable private String preSharedKey; @Nullable private String publicKey; + private final Context context = Application.get(); public Peer() { allowedIPsList = new ArrayList<>(); @@ -117,7 +121,7 @@ public class Peer { public void parse(final String line) { final Attribute key = Attribute.match(line); if (key == null) - throw new IllegalArgumentException(String.format("Unable to parse line: \"%s\"", line)); + throw new IllegalArgumentException(context.getString(R.string.tunnel_error_interface_parse_failed, line)); switch (key) { case ALLOWED_IPS: addAllowedIPs(key.parseList(line)); @@ -152,7 +156,7 @@ public class Peer { if (endpoint != null && !endpoint.isEmpty()) { final InetSocketAddress constructedEndpoint; if (endpoint.indexOf('/') != -1 || endpoint.indexOf('?') != -1 || endpoint.indexOf('#') != -1) - throw new IllegalArgumentException("Forbidden characters in endpoint"); + throw new IllegalArgumentException(context.getString(R.string.tunnel_error_forbidden_endpoint_chars)); final URI uri; try { uri = new URI("wg://" + endpoint); @@ -253,7 +257,7 @@ public class Peer { parent.setPreSharedKey(preSharedKey); parent.setPublicKey(publicKey); if (parent.getPublicKey() == null) - throw new IllegalArgumentException("Peer public key may not be empty"); + throw new IllegalArgumentException(Application.get().getString(R.string.tunnel_error_empty_peer_public_key)); loadData(parent); notifyChange(); } diff --git a/app/src/main/java/com/wireguard/crypto/KeyEncoding.java b/app/src/main/java/com/wireguard/crypto/KeyEncoding.java index 09d27f2d..3e221caa 100644 --- a/app/src/main/java/com/wireguard/crypto/KeyEncoding.java +++ b/app/src/main/java/com/wireguard/crypto/KeyEncoding.java @@ -5,6 +5,9 @@ package com.wireguard.crypto; +import com.wireguard.android.Application; +import com.wireguard.android.R; + /** * This is a specialized constant-time base64 and hex implementation that resists side-channel attacks. */ @@ -15,11 +18,11 @@ public final class KeyEncoding { public static final int KEY_LENGTH_BASE64 = 44; public static final int KEY_LENGTH_HEX = 64; private static final String KEY_LENGTH_BASE64_EXCEPTION_MESSAGE = - "WireGuard base64 keys must be 44 characters encoding 32 bytes"; + Application.get().getString(R.string.key_length_base64_exception_message); private static final String KEY_LENGTH_EXCEPTION_MESSAGE = - "WireGuard keys must be 32 bytes"; + Application.get().getString(R.string.key_length_exception_message); private static final String KEY_LENGTH_HEX_EXCEPTION_MESSAGE = - "WireGuard hex keys must be 64 characters encoding 32 bytes"; + Application.get().getString(R.string.key_length_hex_exception_message); private KeyEncoding() { // Prevent instantiation. |