Browse Source

Added custom validation for websites.

A website must contain one of the following files in its root directory to be considered valid:

index.html
index.htm
default.html
default.htm
home.html
home.htm

This is the first page that is loaded when loading a Qortal-hosted website.
qdn
CalDescent 3 years ago
parent
commit
b592aa6a02
  1. 4
      src/main/java/org/qortal/arbitrary/ArbitraryDataRenderer.java
  2. 18
      src/main/java/org/qortal/arbitrary/misc/Service.java
  3. 2
      src/test/java/org/qortal/test/arbitrary/ArbitraryDataStorageCapacityTests.java
  4. 2
      src/test/java/org/qortal/test/arbitrary/ArbitraryDataStoragePolicyTests.java
  5. 14
      src/test/java/org/qortal/test/arbitrary/ArbitraryDataTests.java
  6. 61
      src/test/java/org/qortal/test/arbitrary/ArbitraryServiceTests.java
  7. 2
      src/test/java/org/qortal/test/arbitrary/ArbitraryTransactionMetadataTests.java

4
src/main/java/org/qortal/arbitrary/ArbitraryDataRenderer.java

@ -134,7 +134,7 @@ public class ArbitraryDataRenderer {
private String getFilename(String directory, String userPath) {
if (userPath == null || userPath.endsWith("/") || userPath.equals("")) {
// Locate index file
List<String> indexFiles = this.indexFiles();
List<String> indexFiles = ArbitraryDataRenderer.indexFiles();
for (String indexFile : indexFiles) {
String filePath = directory + File.separator + indexFile;
if (Files.exists(Paths.get(filePath))) {
@ -173,7 +173,7 @@ public class ArbitraryDataRenderer {
return response;
}
private List<String> indexFiles() {
public static List<String> indexFiles() {
List<String> indexFiles = new ArrayList<>();
indexFiles.add("index.html");
indexFiles.add("index.htm");

18
src/main/java/org/qortal/arbitrary/misc/Service.java

@ -18,7 +18,23 @@ import static java.util.stream.Collectors.toMap;
public enum Service {
AUTO_UPDATE(1, false, null, null),
ARBITRARY_DATA(100, false, null, null),
WEBSITE(200, false, null, null),
WEBSITE(200, true, null, null) {
@Override
public ValidationResult validate(Path path) {
// Custom validation function to require an index HTML file in the root directory
List<String> fileNames = ArbitraryDataRenderer.indexFiles();
String[] files = path.toFile().list();
if (files != null) {
for (String file : files) {
Path fileName = Paths.get(file).getFileName();
if (fileName != null && fileNames.contains(fileName.toString())) {
return ValidationResult.OK;
}
}
}
return ValidationResult.MISSING_INDEX_FILE;
}
},
GIT_REPOSITORY(300, false, null, null),
IMAGE(400, true, 10*1024*1024L, null),
THUMBNAIL(410, true, 500*1024L, null),

2
src/test/java/org/qortal/test/arbitrary/ArbitraryDataStorageCapacityTests.java

@ -137,7 +137,7 @@ public class ArbitraryDataStorageCapacityTests extends Common {
public void testDeleteRandomFilesForName() throws DataException, IOException, InterruptedException {
try (final Repository repository = RepositoryManager.getRepository()) {
String identifier = null; // Not used for this test
Service service = Service.WEBSITE; // Can be anything for this test
Service service = Service.ARBITRARY_DATA;
int chunkSize = 100;
int dataLength = 900; // Actual data length will be longer due to encryption

2
src/test/java/org/qortal/test/arbitrary/ArbitraryDataStoragePolicyTests.java

@ -234,7 +234,7 @@ public class ArbitraryDataStoragePolicyTests extends Common {
Path path = Paths.get("src/test/resources/arbitrary/demo1");
ArbitraryDataTransactionBuilder txnBuilder = new ArbitraryDataTransactionBuilder(
repository, publicKey58, path, name, Method.PUT, Service.WEBSITE, null);
repository, publicKey58, path, name, Method.PUT, Service.ARBITRARY_DATA, null);
txnBuilder.build();
ArbitraryTransactionData transactionData = txnBuilder.getArbitraryTransactionData();

14
src/test/java/org/qortal/test/arbitrary/ArbitraryDataTests.java

@ -48,7 +48,7 @@ public class ArbitraryDataTests extends Common {
String publicKey58 = Base58.encode(alice.getPublicKey());
String name = "TEST"; // Can be anything for this test
String identifier = null; // Not used for this test
Service service = Service.WEBSITE; // Can be anything for this test
Service service = Service.ARBITRARY_DATA;
// Register the name to Alice
RegisterNameTransactionData transactionData = new RegisterNameTransactionData(TestTransaction.generateBase(alice), name, "");
@ -96,7 +96,7 @@ public class ArbitraryDataTests extends Common {
String publicKey58 = Base58.encode(alice.getPublicKey());
String name = "TEST"; // Can be anything for this test
String identifier = null; // Not used for this test
Service service = Service.WEBSITE; // Can be anything for this test
Service service = Service.ARBITRARY_DATA;
// Create PATCH transaction, ensuring that an exception is thrown
try {
@ -119,7 +119,7 @@ public class ArbitraryDataTests extends Common {
String publicKey58 = Base58.encode(alice.getPublicKey());
String name = "TEST"; // Can be anything for this test
String identifier = null; // Not used for this test
Service service = Service.WEBSITE; // Can be anything for this test
Service service = Service.ARBITRARY_DATA;
// Ensure the name doesn't exist
assertNull(repository.getNameRepository().fromName(name));
@ -142,7 +142,7 @@ public class ArbitraryDataTests extends Common {
PrivateKeyAccount alice = Common.getTestAccount(repository, "alice");
String name = "TEST"; // Can be anything for this test
String identifier = null; // Not used for this test
Service service = Service.WEBSITE; // Can be anything for this test
Service service = Service.ARBITRARY_DATA;
// Register the name to Alice
RegisterNameTransactionData transactionData = new RegisterNameTransactionData(TestTransaction.generateBase(alice), name, "");
@ -174,7 +174,7 @@ public class ArbitraryDataTests extends Common {
String publicKey58 = Base58.encode(alice.getPublicKey());
String name = "TEST"; // Can be anything for this test
String identifier = null; // Not used for this test
Service service = Service.WEBSITE; // Can be anything for this test
Service service = Service.ARBITRARY_DATA;
// Register the name to Alice
RegisterNameTransactionData transactionData = new RegisterNameTransactionData(TestTransaction.generateBase(alice), name, "");
@ -219,7 +219,7 @@ public class ArbitraryDataTests extends Common {
String publicKey58 = Base58.encode(alice.getPublicKey());
String name = "TEST"; // Can be anything for this test
String identifier = "test_identifier";
Service service = Service.WEBSITE; // Can be anything for this test
Service service = Service.ARBITRARY_DATA;
// Register the name to Alice
RegisterNameTransactionData transactionData = new RegisterNameTransactionData(TestTransaction.generateBase(alice), name, "");
@ -287,7 +287,7 @@ public class ArbitraryDataTests extends Common {
String publicKey58 = Base58.encode(alice.getPublicKey());
String name = "TEST"; // Can be anything for this test
String identifier = ""; // Blank, not null
Service service = Service.WEBSITE; // Can be anything for this test
Service service = Service.ARBITRARY_DATA;
// Register the name to Alice
RegisterNameTransactionData transactionData = new RegisterNameTransactionData(TestTransaction.generateBase(alice), name, "");

61
src/test/java/org/qortal/test/arbitrary/ArbitraryServiceTests.java

@ -40,6 +40,67 @@ public class ArbitraryServiceTests extends Common {
assertEquals(ValidationResult.OK, service.validate(path));
}
@Test
public void testValidateWebsite() throws IOException {
// Generate some random data
byte[] data = new byte[1024];
new Random().nextBytes(data);
// Write the data to several files in a temp path
Path path = Files.createTempDirectory("testValidateWebsite");
path.toFile().deleteOnExit();
Files.write(Paths.get(path.toString(), "index.html"), data, StandardOpenOption.CREATE);
Files.write(Paths.get(path.toString(), "data2"), data, StandardOpenOption.CREATE);
Files.write(Paths.get(path.toString(), "data3"), data, StandardOpenOption.CREATE);
Service service = Service.WEBSITE;
assertTrue(service.isValidationRequired());
// There is an index file in the root
assertEquals(ValidationResult.OK, service.validate(path));
}
@Test
public void testValidateWebsiteWithoutIndexFile() throws IOException {
// Generate some random data
byte[] data = new byte[1024];
new Random().nextBytes(data);
// Write the data to several files in a temp path
Path path = Files.createTempDirectory("testValidateWebsiteWithoutIndexFile");
path.toFile().deleteOnExit();
Files.write(Paths.get(path.toString(), "data1.html"), data, StandardOpenOption.CREATE);
Files.write(Paths.get(path.toString(), "data2"), data, StandardOpenOption.CREATE);
Files.write(Paths.get(path.toString(), "data3"), data, StandardOpenOption.CREATE);
Service service = Service.WEBSITE;
assertTrue(service.isValidationRequired());
// There is no index file in the root
assertEquals(ValidationResult.MISSING_INDEX_FILE, service.validate(path));
}
@Test
public void testValidateWebsiteWithoutIndexFileInRoot() throws IOException {
// Generate some random data
byte[] data = new byte[1024];
new Random().nextBytes(data);
// Write the data to several files in a temp path
Path path = Files.createTempDirectory("testValidateWebsiteWithoutIndexFileInRoot");
path.toFile().deleteOnExit();
Files.createDirectories(Paths.get(path.toString(), "directory"));
Files.write(Paths.get(path.toString(), "directory", "index.html"), data, StandardOpenOption.CREATE);
Files.write(Paths.get(path.toString(), "data2"), data, StandardOpenOption.CREATE);
Files.write(Paths.get(path.toString(), "data3"), data, StandardOpenOption.CREATE);
Service service = Service.WEBSITE;
assertTrue(service.isValidationRequired());
// There is no index file in the root
assertEquals(ValidationResult.MISSING_INDEX_FILE, service.validate(path));
}
@Test
public void testValidQortalMetadata() throws IOException {
// Metadata is to describe an arbitrary resource (title, description, tags, etc)

2
src/test/java/org/qortal/test/arbitrary/ArbitraryTransactionMetadataTests.java

@ -48,7 +48,7 @@ public class ArbitraryTransactionMetadataTests extends Common {
String publicKey58 = Base58.encode(alice.getPublicKey());
String name = "TEST"; // Can be anything for this test
String identifier = null; // Not used for this test
Service service = Service.WEBSITE; // Can be anything for this test
Service service = Service.ARBITRARY_DATA;
int chunkSize = 100;
int dataLength = 900; // Actual data length will be longer due to encryption

Loading…
Cancel
Save