Skip to content

Commit

Permalink
created new parameterized unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
astrapisixtynine committed Jul 15, 2024
1 parent 47b0876 commit d95df7d
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,26 @@
import io.github.astrapi69.crypt.data.model.CryptObjectDecorator;
import io.github.astrapi69.file.read.ReadFileExtensions;

/**
* Utility class for decorating and undecorating cryptographic objects.
*/
public final class CryptObjectDecoratorExtensions
{
private CryptObjectDecoratorExtensions()
{
}

/**
* Decorates the content of the given file using the specified decorator.
*
* @param toEncrypt
* the file to be encrypted
* @param decorator
* the decorator to be used
* @return the decorated string
* @throws IOException
* if an I/O error occurs
*/
public static String decorateFile(final File toEncrypt,
final CryptObjectDecorator<String> decorator) throws IOException
{
Expand All @@ -50,6 +64,17 @@ public static String decorateFile(final File toEncrypt,
return decorateWithStringDecorator(ReadFileExtensions.fromFile(toEncrypt), decorator);
}

/**
* Decorates the given string using a byte array decorator.
*
* @param toEncrypt
* the string to be encrypted
* @param decorator
* the decorator to be used
* @param charset
* the charset to be used for encoding
* @return the decorated string
*/
public static String decorateWithBytearrayDecorator(final String toEncrypt,
final CryptObjectDecorator<byte[]> decorator, final Charset charset)
{
Expand All @@ -58,13 +83,20 @@ public static String decorateWithBytearrayDecorator(final String toEncrypt,
Objects.requireNonNull(charset);
StringBuilder sb = new StringBuilder();
sb.append(new String(decorator.getPrefix(), charset));

sb.append(toEncrypt);

sb.append(new String(decorator.getSuffix(), charset));
return sb.toString();
}

/**
* Decorates the given string using a character decorator.
*
* @param toEncrypt
* the string to be encrypted
* @param decorator
* the decorator to be used
* @return the decorated string
*/
public static String decorateWithCharacterDecorator(final String toEncrypt,
final CryptObjectDecorator<Character> decorator)
{
Expand All @@ -77,6 +109,15 @@ public static String decorateWithCharacterDecorator(final String toEncrypt,
return sb.toString();
}

/**
* Decorates the given string using a string decorator.
*
* @param toEncrypt
* the string to be encrypted
* @param decorator
* the decorator to be used
* @return the decorated string
*/
public static String decorateWithStringDecorator(final String toEncrypt,
final CryptObjectDecorator<String> decorator)
{
Expand Down Expand Up @@ -141,6 +182,17 @@ private static boolean startsWith(final byte[] array, byte[] prefix)
return true;
}

/**
* Undecorates the content of the given file using the specified decorator.
*
* @param decrypted
* the file to be decrypted
* @param decorator
* the decorator to be used
* @return the undecorated string
* @throws IOException
* if an I/O error occurs
*/
public static String undecorateFile(final File decrypted,
final CryptObjectDecorator<String> decorator) throws IOException
{
Expand All @@ -149,6 +201,15 @@ public static String undecorateFile(final File decrypted,
return undecorateWithStringDecorator(ReadFileExtensions.fromFile(decrypted), decorator);
}

/**
* Undecorates the given string using a byte array decorator.
*
* @param toEncrypt
* the string to be decrypted
* @param decorator
* the decorator to be used
* @return the undecorated string
*/
public static String undecorateWithBytearrayDecorator(final String toEncrypt,
final CryptObjectDecorator<byte[]> decorator)
{
Expand All @@ -171,6 +232,15 @@ public static String undecorateWithBytearrayDecorator(final String toEncrypt,
return new String(result);
}

/**
* Undecorates the given string using a character decorator.
*
* @param toEncrypt
* the string to be decrypted
* @param decorator
* the decorator to be used
* @return the undecorated string
*/
public static String undecorateWithCharacterDecorator(final String toEncrypt,
final CryptObjectDecorator<Character> decorator)
{
Expand All @@ -193,11 +263,19 @@ public static String undecorateWithCharacterDecorator(final String toEncrypt,
{
sb.deleteCharAt(toEncrypt.length() - 1);
}

}
return sb.toString();
}

/**
* Undecorates the given string using a string decorator.
*
* @param decrypted
* the string to be decrypted
* @param decorator
* the decorator to be used
* @return the undecorated string
*/
public static String undecorateWithStringDecorator(final String decrypted,
final CryptObjectDecorator<String> decorator)
{
Expand All @@ -211,7 +289,6 @@ public static String undecorateWithStringDecorator(final String decrypted,
if (decrypted.endsWith(decorator.getSuffix()))
{
result = StringUtils.removeEnd(result, decorator.getSuffix());

}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
* This package provides classes for decorate crypt objects
* This package provides classes for decorating and undecorating cryptographic objects.
*/
package io.github.astrapi69.mystic.crypt.decorator;
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* This package provides a utility class that can sign urls for google maps with the private key.
This class is inspired
from this
<a href= "https://github.com/googlemaps/url-signing/blob/gh-pages/UrlSigner.java">class</a>
* This package provides a utility class that can sign urls for google maps with the private key.
* This class is inspired from this
* <a href= "https://github.com/googlemaps/url-signing/blob/gh-pages/UrlSigner.java">class</a>
*/
package io.github.astrapi69.mystic.crypt.gm;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* This package provides classes that obfuscate text. The disentangle process is still experimental. <br>
Note: use the disentangle process at your own risk.
* This package provides classes that obfuscate text. The disentangle process is still experimental.
* <br>
* Note: use the disentangle process at your own risk.
*/
package io.github.astrapi69.mystic.crypt.obfuscation.character;
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/**
* This package provides a class with factory methods for create TrustManager array objects and KeyManager array objects.
* This package provides a class with factory methods for create TrustManager array objects and
* KeyManager array objects.
*/
package io.github.astrapi69.mystic.crypt.ssl;
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;

import io.github.astrapi69.crypt.data.model.CryptObjectDecorator;

Expand All @@ -38,6 +41,115 @@
public class CryptObjectDecoratorExtensionsTest
{

/**
* Test method for
* {@link CryptObjectDecoratorExtensions#decorateWithStringDecorator(String, CryptObjectDecorator)}.
*
* @param input
* the input string to be decorated
* @param prefix
* the prefix for decoration
* @param suffix
* the suffix for decoration
* @param expected
* the expected result after decoration
*/
@ParameterizedTest
@CsvFileSource(resources = "/decorateStringTest.csv", numLinesToSkip = 1)
void testDecorateWithStringDecorator(String input, String prefix, String suffix,
String expected)
{
CryptObjectDecorator<String> decorator;

decorator = CryptObjectDecorator.<String> builder().prefix(prefix).suffix(suffix).build();
String result = CryptObjectDecoratorExtensions.decorateWithStringDecorator(input,
decorator);
assertEquals(expected, result);
}

/**
* Test method for
* {@link CryptObjectDecoratorExtensions#undecorateWithStringDecorator(String, CryptObjectDecorator)}.
*
* @param input
* the input string to be undecorated
* @param prefix
* the prefix for undecoration
* @param suffix
* the suffix for undecoration
* @param expected
* the expected result after undecoration
*/
@ParameterizedTest
@CsvFileSource(resources = "/undecorateStringTest.csv", numLinesToSkip = 1)
void testUndecorateWithStringDecorator(String input, String prefix, String suffix,
String expected)
{
CryptObjectDecorator<String> decorator;

decorator = CryptObjectDecorator.<String> builder().prefix(prefix).suffix(suffix).build();
String result = CryptObjectDecoratorExtensions.undecorateWithStringDecorator(input,
decorator);
assertEquals(expected, result);
}

/**
* Test method for
* {@link CryptObjectDecoratorExtensions#decorateWithBytearrayDecorator(String, CryptObjectDecorator, Charset)}.
*
* @param input
* the input string to be decorated
* @param prefix
* the prefix for decoration
* @param suffix
* the suffix for decoration
* @param expected
* the expected result after decoration
*/
@ParameterizedTest
@CsvFileSource(resources = "/decorateBytearrayTest.csv", numLinesToSkip = 1)
void testDecorateWithBytearrayDecorator(String input, String prefix, String suffix,
String expected)
{

CryptObjectDecorator<byte[]> decorator;

decorator = CryptObjectDecorator.<byte[]> builder()
.prefix(prefix.getBytes(StandardCharsets.UTF_8))
.suffix(suffix.getBytes(StandardCharsets.UTF_8)).build();
String result = CryptObjectDecoratorExtensions.decorateWithBytearrayDecorator(input,
decorator, StandardCharsets.UTF_8);
assertEquals(expected, result);
}

/**
* Test method for
* {@link CryptObjectDecoratorExtensions#undecorateWithBytearrayDecorator(String, CryptObjectDecorator)}.
*
* @param input
* the input string to be undecorated
* @param prefix
* the prefix for undecoration
* @param suffix
* the suffix for undecoration
* @param expected
* the expected result after undecoration
*/
@ParameterizedTest
@CsvFileSource(resources = "/undecorateBytearrayTest.csv", numLinesToSkip = 1)
void testUndecorateWithBytearrayDecorator(String input, String prefix, String suffix,
String expected)
{
CryptObjectDecorator<byte[]> decorator;

decorator = CryptObjectDecorator.<byte[]> builder()
.prefix(prefix.getBytes(StandardCharsets.UTF_8))
.suffix(suffix.getBytes(StandardCharsets.UTF_8)).build();
String result = CryptObjectDecoratorExtensions.undecorateWithBytearrayDecorator(input,
decorator);
assertEquals(expected, result);
}

/**
* Test method for the decoration of an crypt object with {@link CryptObjectDecorator} with byte
* array as prefix and suffix
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/decorateBytearrayTest.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
input,prefix,suffix,expected
Hello,<<,>>,<<Hello>>
World,[[,]],[[World]]
3 changes: 3 additions & 0 deletions src/test/resources/decorateStringTest.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
input,prefix,suffix,expected
Hello,<<,>>,<<Hello>>
World,[[,]],[[World]]
3 changes: 3 additions & 0 deletions src/test/resources/undecorateBytearrayTest.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
input,prefix,suffix,expected
<<Hello>>,<<,>>,Hello
[[World]],[[,]],World
3 changes: 3 additions & 0 deletions src/test/resources/undecorateStringTest.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
input,prefix,suffix,expected
<<Hello>>,<<,>>,Hello
[[World]],[[,]],World

0 comments on commit d95df7d

Please sign in to comment.