Skip to content

Commit

Permalink
Added test to check whether the translation keys are complete
Browse files Browse the repository at this point in the history
  • Loading branch information
sarpsahinalp committed Oct 15, 2024
1 parent f552b9e commit ec8a350
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static de.tum.cit.ase.ares.api.aop.java.instrumentation.advice.JavaInstrumentationAdviceToolbox.localize;
//</editor-fold>

/**
Expand Down Expand Up @@ -79,23 +81,14 @@ private static String generateAdviceSettingValue(@Nonnull String dataType, @Nonn

return switch (dataType) {
case "String" -> {
// TODO: Refactor the error messages here
if (!(value instanceof String)) {
throw new SecurityException(String.format(
"Ares Security Error (Reason: Ares-Code; Stage: Creation): "
+ "Invalid value type for String advice setting: "
+ "%s but should be String.",
value.getClass()));
throw new SecurityException(localize("security.advice.settings.data.type.mismatch.string", value.getClass()));
}
yield String.format("private static String %s = \"%s\";%n", adviceSetting, value);
}
case "String[]" -> {
if (!(value instanceof List<?>)) {
throw new SecurityException(String.format(
"Ares Security Error (Reason: Ares-Code; Stage: Creation): "
+ "Invalid value type for String[] advice setting: "
+ "%s but should be List<String>.",
value.getClass()));
throw new SecurityException(localize("security.advice.settings.data.type.mismatch.string[]", value.getClass()));
}
String stringArrayValue = ((List<?>) value).stream()
.map(Object::toString)
Expand All @@ -105,11 +98,7 @@ private static String generateAdviceSettingValue(@Nonnull String dataType, @Nonn
}
case "String[][]" -> {
if (!(value instanceof List<?>)) {
throw new SecurityException(String.format(
"Ares Security Error (Reason: Ares-Code; Stage: Creation): "
+ "Invalid value type for String[][] advice setting: "
+ "%s but should be List<List<String>>.",
value.getClass()));
throw new SecurityException(localize("security.advice.settings.data.type.mismatch.string[][]", value.getClass()));
}
String stringArrayArrayValue = ((List<?>) value).stream()
.filter(e -> e instanceof List<?>)
Expand All @@ -124,38 +113,17 @@ private static String generateAdviceSettingValue(@Nonnull String dataType, @Nonn
}
case "int[]" -> {
if (!(value instanceof List<?>)) {
throw new SecurityException(String.format(
"Ares Security Error (Reason: Ares-Code; Stage: Creation): "
+ "Invalid value type for int[] advice setting: "
+ "%s but should be List<Integer>.",
value.getClass()));
throw new SecurityException(localize("security.advice.settings.data.type.mismatch.int[]", value.getClass()));
}
String intArrayValue = ((List<?>) value).stream()
.map(Object::toString)
.collect(Collectors.joining(", "));
yield String.format("private static int[] %s = new int[] {%s};%n", adviceSetting, intArrayValue);
}
default -> throw new SecurityException(
"Ares Security Error (Reason: Ares-Code; Stage: Creation): "
+ "Unknown data type while creating the value "
+ value
+ " for the advice settings "
+ dataType
+ " "
+ adviceSetting
);
default -> throw new SecurityException(localize("security.advice.settings.data.type.unknown", value, dataType, adviceSetting));
};
} catch (IllegalFormatException e) {
throw new SecurityException(
"Ares Security Error (Reason: Ares-Code; Stage: Creation): "
+ "Format error while creating the value "
+ value
+ " for the advice settings "
+ dataType
+ " "
+ adviceSetting,
e
);
throw new SecurityException(localize("security.advice.invalid.format", value, dataType, adviceSetting));
}

}
Expand All @@ -180,53 +148,31 @@ public static void setJavaAdviceSettingValue(@Nonnull String adviceSetting, @Nul
field.setAccessible(false);
} catch (LinkageError e) {
throw new SecurityException(
"Ares Security Error (Reason: Ares-Code; Stage: Creation):"
+ "Linkage error while accessing field '"
+ adviceSetting
+ "' in AdviceSettings",
localize("security.advice.linkage.exception", adviceSetting),
e);
} catch (ClassNotFoundException e) {
throw new SecurityException(
"Ares Security Error (Reason: Ares-Code; Stage: Creation):"
+ "Could not find 'JavaSecurityTestCaseSettings' class to access field '"
+ adviceSetting
+ "'",
localize("security.advice.class.not.found.exception", adviceSetting),
e);
} catch (NoSuchFieldException e) {
throw new SecurityException(
"Ares Security Error (Reason: Ares-Code; Stage: Creation):"
+ "Field '"
+ adviceSetting
+ "' not found in AdviceSettings",
localize("security.advice.no.such.field.exception", adviceSetting),
e);
} catch (NullPointerException e) {
throw new SecurityException(
"Ares Security Error (Reason: Ares-Code; Stage: Creation):"
+ "Null pointer exception while accessing field '"
+ adviceSetting
+ "' in AdviceSettings",
localize("security.advice.null.pointer.exception", adviceSetting),
e);
} catch (IllegalAccessException e) {
throw new SecurityException(
"Ares Security Error (Reason: Ares-Code; Stage: Creation):"
+ "Field '"
+ adviceSetting
+ "' is not accessible in AdviceSettings",
localize("security.advice.illegal.access.exception", adviceSetting),
e);
} catch (IllegalArgumentException e) {
throw new SecurityException(
"Ares Security Error (Reason: Ares-Code; Stage: Creation):"
+ "Illegal argument while setting field '"
+ adviceSetting
+ "' in AdviceSettings with value "
+ value,
localize("security.advice.illegal.argument.exception", adviceSetting, value),
e);
} catch (InaccessibleObjectException e) {
throw new SecurityException(
"Ares Security Error (Reason: Ares-Code; Stage: Creation):"
+ "Field '"
+ adviceSetting
+ "' is inaccessible in AdviceSettings",
localize("security.advice.inaccessible.object.exception", adviceSetting),
e);
}
}
Expand Down Expand Up @@ -266,7 +212,7 @@ private List<String> getPermittedFilePaths(@Nonnull String filePermission) {
case "execute" -> FilePermission::executeAllFiles;
case "delete" -> FilePermission::deleteAllFiles;
default ->
throw new IllegalArgumentException("Ares Security Error (Reason: Ares-Code; Stage: Creation): Invalid file permission: " + filePermission);
throw new IllegalArgumentException(localize("security.advice.settings.invalid.file.permission", filePermission));
};
return resourceAccesses.regardingFileSystemInteractions()
.stream()
Expand Down Expand Up @@ -329,7 +275,7 @@ private List<String> getPermittedNetworkHosts(@Nonnull String networkPermission)
case "send" -> NetworkPermission::sendData;
case "receive" -> NetworkPermission::receiveData;
default ->
throw new IllegalArgumentException("Ares Security Error (Reason: Ares-Code; Stage: Creation): Invalid network permission: " + networkPermission);
throw new IllegalArgumentException(localize("security.advice.settings.invalid.network.permission", networkPermission));
};
return resourceAccesses.regardingNetworkConnections()
.stream()
Expand All @@ -351,7 +297,7 @@ private List<Integer> getPermittedNetworkPorts(@Nonnull String networkPermission
case "send" -> NetworkPermission::sendData;
case "receive" -> NetworkPermission::receiveData;
default ->
throw new IllegalArgumentException("Ares Security Error (Reason: Ares-Code; Stage: Creation): Invalid network permission: " + networkPermission);
throw new IllegalArgumentException(localize("security.advice.settings.invalid.network.permission", networkPermission));
};
return resourceAccesses.regardingNetworkConnections()
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ public static String localize(String key, Object... args) {
throw new IllegalStateException("Method does not return a String");
}
} catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
// TODO: Add a correct implementation of the localization
throw new RuntimeException(e.getMessage());
// Fallback: Return the key if localization fails
return key;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ security.advice.class.not.found.exception=Ares Security Error (Reason: Ares-Code
security.advice.no.such.field.exception=Ares Security Error (Reason: Ares-Code; Stage: Execution): Field '%s' not found in AdviceSettings
security.advice.null.pointer.exception=Ares Security Error (Reason: Ares-Code; Stage: Execution): Null pointer exception while accessing field '%s' in AdviceSettings
security.advice.illegal.access.exception=Ares Security Error (Reason: Ares-Code; Stage: Execution): Field '%s' is not accessible in AdviceSettings
security.advice.illegal.argument.exception=Ares Security Error (Reason: Ares-Code; Stage: Execution): Illegal argument while setting field '%s' in AdviceSettings with value %s
security.advice.inaccessible.object.exception=Ares Security Error (Reason: Ares-Code; Stage: Execution): Field '%s' is inaccessible in AdviceSettings
security.advice.transform.path.exception=Cannot transform to path
security.instrumentation.inaccessible.object.exception=Ares Security Error (Reason: Ares-Code; Stage: Execution): Unable to make field '%s' in class '%s' accessible due to JVM security restrictions.
Expand Down Expand Up @@ -92,6 +93,14 @@ security.policy.java.architecture.mode.null=Ares Security Error (Reason: Ares-Co
security.policy.java.aop.mode.null=Ares Security Error (Reason: Ares-Code; Stage: Creation): The AOP mode cannot be null.
security.policy.java.not.correct.set=Ares Security Error (Reason: Ares-Code; Stage: Creation): The x cannot be null.
security.file-tools.read.content.failure=Ares Security Error (Stage: Creation): Failed to read content from source file.
security.advice.settings.data.type.mismatch.string=Ares Security Error (Reason: Ares-Code; Stage: Creation): Invalid value type for String advice setting: %s but should be String.
security.advice.settings.data.type.mismatch.string[]=Ares Security Error (Reason: Ares-Code; Stage: Creation): Invalid value type for String[] advice setting: %s but should be List<String>.
security.advice.settings.data.type.mismatch.string[][]=Ares Security Error (Reason: Ares-Code; Stage: Creation): Invalid value type for String[][] advice setting: %s but should be List<List<String>>.
security.advice.settings.data.type.mismatch.int[]=Ares Security Error (Reason: Ares-Code; Stage: Creation): Invalid value type for int[] advice setting: %s but should be List<Integer>.
security.advice.settings.data.type.unknown=Ares Security Error (Reason: Ares-Code; Stage: Creation): Unknown data type while creating the value %s for the advice settings %s %s
security.advice.invalid.format=Ares Security Error (Reason: Ares-Code; Stage: Creation): Format error while creating the value %s for the advice settings %s %s
security.advice.settings.invalid.file.permission=Ares Security Error (Reason: Ares-Code; Stage: Creation): Invalid file permission: %s
security.advice.settings.invalid.network.permission=Ares Security Error (Reason: Ares-Code; Stage: Creation): Invalid network permission: %s

# Policy reader and configurer
security.policy.reader.path.blank=The policy file path is not specified.
Expand Down
Loading

0 comments on commit ec8a350

Please sign in to comment.