diff --git a/services_app/src/androidTest/java/org/opendatakit/services/preferences/fragments/GeneralAdminConfigurationTest.java b/services_app/src/androidTest/java/org/opendatakit/services/preferences/fragments/GeneralAdminConfigurationTest.java index add79e7db..a2e021767 100644 --- a/services_app/src/androidTest/java/org/opendatakit/services/preferences/fragments/GeneralAdminConfigurationTest.java +++ b/services_app/src/androidTest/java/org/opendatakit/services/preferences/fragments/GeneralAdminConfigurationTest.java @@ -17,21 +17,14 @@ import androidx.test.espresso.action.ViewActions; import androidx.test.espresso.contrib.RecyclerViewActions; -import androidx.test.espresso.intent.Intents; import org.junit.After; import org.junit.Test; import org.opendatakit.BaseUITest; import org.opendatakit.consts.IntentConsts; -import org.opendatakit.properties.CommonToolProperties; import org.opendatakit.properties.PropertiesSingleton; import org.opendatakit.services.R; import org.opendatakit.services.preferences.activities.AppPropertiesActivity; -import org.opendatakit.services.preferences.activities.ClearAppPropertiesActivity; -import org.opendatakit.utilities.LocalizationUtils; -import org.opendatakit.utilities.ODKFileUtils; - -import java.io.File; public class GeneralAdminConfigurationTest extends BaseUITest { diff --git a/services_app/src/androidTest/java/org/opendatakit/services/sync/service/logic/AggregateSynchronizerTest.java b/services_app/src/androidTest/java/org/opendatakit/services/sync/service/logic/AggregateSynchronizerTest.java index 7c3f561c5..95082af48 100644 --- a/services_app/src/androidTest/java/org/opendatakit/services/sync/service/logic/AggregateSynchronizerTest.java +++ b/services_app/src/androidTest/java/org/opendatakit/services/sync/service/logic/AggregateSynchronizerTest.java @@ -1,10 +1,14 @@ package org.opendatakit.services.sync.service.logic; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + import android.Manifest; import android.app.Application; import android.content.Context; -import androidx.test.InstrumentationRegistry; +import androidx.test.core.app.ApplicationProvider; import androidx.test.filters.Suppress; import androidx.test.rule.GrantPermissionRule; @@ -57,10 +61,6 @@ import java.util.Map; import java.util.UUID; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; - /** * This set of tests relies on a propery configured test cloud endpoint. * Do not adorn this with RunWith unless that server is configured. @@ -101,7 +101,6 @@ public class AggregateSynchronizerTest { @Before public void setUp() throws Exception { - agg_url = "https://test.appspot.com"; absolutePathOfTestFiles = "testfiles/test/"; batchSize = 1000; @@ -115,9 +114,8 @@ public void setUp() throws Exception { boolean beganUninitialized = !initialized; if (beganUninitialized) { initialized = true; - application = InstrumentationRegistry.getInstrumentation().newApplication(this.getClass() - .getClassLoader(), "org.opendatakit.services.application.Services", - InstrumentationRegistry.getTargetContext()); + Context context = ApplicationProvider.getApplicationContext(); + application = ApplicationProvider.getApplicationContext(); // Used to ensure that the singleton has been initialized properly AndroidConnectFactory.configure(); } @@ -164,7 +162,8 @@ public void clearVerificationNotification(String appName, String title, String t } private SyncExecutionContext getSyncExecutionContext() { - Context context = InstrumentationRegistry.getTargetContext(); + //Context context = InstrumentationRegistry.getTargetContext(); + Context context = ApplicationProvider.getApplicationContext(); SyncProgressTracker syncProg = new SyncProgressTracker(context, new GlobalSyncNotificationManagerStub(), appName); SyncOverallResult syncRes = new SyncOverallResult(); diff --git a/services_app/src/androidTest/java/org/opendatakit/webkitserver/service/OdkWebserverServiceTest.java b/services_app/src/androidTest/java/org/opendatakit/webkitserver/service/OdkWebserverServiceTest.java index d3ea766d4..4833b7c30 100644 --- a/services_app/src/androidTest/java/org/opendatakit/webkitserver/service/OdkWebserverServiceTest.java +++ b/services_app/src/androidTest/java/org/opendatakit/webkitserver/service/OdkWebserverServiceTest.java @@ -1,15 +1,21 @@ package org.opendatakit.webkitserver.service; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import android.Manifest; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; +import android.net.Uri; import android.os.Build; import android.os.IBinder; import android.os.RemoteException; import androidx.annotation.NonNull; +import androidx.documentfile.provider.DocumentFile; import androidx.test.platform.app.InstrumentationRegistry; import androidx.test.rule.GrantPermissionRule; import androidx.test.rule.ServiceTestRule; @@ -31,13 +37,12 @@ import java.io.File; import java.io.IOException; import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; -import java.nio.charset.StandardCharsets; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import java.nio.file.Files; /** * @author mitchellsundt@gmail.com @@ -48,10 +53,28 @@ public class OdkWebserverServiceTest { private static final String TAG = "OdkWebserverServiceTest"; + private static final String TEST_DIR = "testfiles"; + private static final String TEST_FILE_NAME_HELLO = "Hello.html"; + private static final String TEST_FILE_NAME_EMPTY = "EmptyFile.html"; + private static final String TEST_FILE_NAME_LARGE = "LargeFile.txt"; + private static final String TEST_FILE_NAME_SPECIAL = "Special@File#Name.txt"; + private static final String TEST_FILE_NAME_MULTIPLE_1 = "MultipleFiles1.html"; + private static final String TEST_FILE_NAME_MULTIPLE_2 = "MultipleFiles2.html"; + private static final String TEST_FILE_NAME_SPACES = "File with Spaces.txt"; + private static final String TEST_FILE_NAME_UNICODE = "FileWithUnicodeContent.txt"; private static final String HELLO_WORLD_HTML_TXT = "Hello World!!!"; - private static final String TEST_FILE_NAME = "Hello.html"; - private static final String TEST_DIR = "testfiles"; + private static final String EMPTY_HTML_TXT = ""; + private static final String LARGE_FILE_CONTENT = generateLargeFileContent(); + private static final String SPECIAL_CHARACTERS_CONTENT = "Special characters in file name"; + private static final String UNICODE_CONTENT = "Unicode characters: 你好, مرحبا, こんにちは"; + private static final String TEST_FILE_NAME_INVALID_EXTENSION = "InvalidFile.xyz"; + private static final String TEST_FILE_NAME_UPPERCASE_EXTENSION = "FileWithUpperCaseExtension.TXT"; + private static final String TEST_FILE_NAME_BINARY = "BinaryFile.bin"; + private static final String TEST_FILE_NAME_MULTIPLE_DOTS = "File.With.Multiple.Dots.txt"; + private static final String TEST_FILE_NAME_NO_EXTENSION = "FileWithNoExtension"; + private static final String TEST_FILE_NAME_DIFFERENT_EXTENSION = "FileWithDifferentExtension.docx"; + @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule(); @@ -186,18 +209,6 @@ public void testBindingNRestart() { } } - - @Test - public void testServingHelloWorldHtml() { - // Arrange - File directoryLocation = createTestDirectory(); - File fileLocation = createTestFile(directoryLocation); - // Act - IWebkitServerInterface serviceInterface = getIWebkitServerInterface(); - restartService(serviceInterface); - // Assert - assertResponseMatchesHelloWorldHtml(fileLocation); - } private File createTestDirectory() { File directoryLocation = new File(ODKFileUtils.getConfigFolder(TestConsts.APPNAME), TEST_DIR); if (!directoryLocation.isDirectory()) { @@ -206,21 +217,6 @@ private File createTestDirectory() { return directoryLocation; } - private File createTestFile(File directoryLocation) { - File fileLocation = new File(directoryLocation, TEST_FILE_NAME); - - try (PrintWriter writer = new PrintWriter(fileLocation, "UTF-8")) { - writer.println(HELLO_WORLD_HTML_TXT); - writer.flush(); - writer.close(); - } catch (Exception e) { - e.printStackTrace(); - fail("Failed to create the test file: " + e.getMessage()); - } - - return fileLocation; - } - private void restartService(IWebkitServerInterface serviceInterface) { try { serviceInterface.restart(); @@ -230,12 +226,34 @@ private void restartService(IWebkitServerInterface serviceInterface) { } } + private String buildTestUrl(File fileLocation) { + Uri baseUrl = Uri.parse("http://" + WebkitServerConsts.HOSTNAME + ":" + + Integer.toString(WebkitServerConsts.PORT) + "/" + TestConsts.APPNAME + "/"); + return baseUrl.buildUpon() + .appendPath(ODKFileUtils.asUriFragment(TestConsts.APPNAME, fileLocation)) + .build() + .toString(); + } + + @Test + public void testServingHelloWorldHtml() { + // Arrange + File directoryLocation = createTestDirectory(); + File fileLocation = createTestFile(directoryLocation, TEST_FILE_NAME_HELLO, HELLO_WORLD_HTML_TXT); + + // Act + IWebkitServerInterface serviceInterface = getIWebkitServerInterface(); + restartService(serviceInterface); + + // Assert + assertResponseMatchesHelloWorldHtml(fileLocation); + } + private void assertResponseMatchesHelloWorldHtml(File fileLocation) { - try { - String urlStr = buildTestUrl(fileLocation); - URL url = new URL(urlStr); + try { + Uri uri = Uri.parse(buildTestUrl(fileLocation)); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + HttpURLConnection connection = (HttpURLConnection) new URL(uri.toString()).openConnection(); if (connection.getResponseCode() != HttpStatus.SC_OK) { fail("Response code was NOT HTTP_OK"); } @@ -257,16 +275,11 @@ private void assertResponseMatchesHelloWorldHtml(File fileLocation) { } } - private String buildTestUrl(File fileLocation) { - return "http://" + WebkitServerConsts.HOSTNAME + ":" + - Integer.toString(WebkitServerConsts.PORT) + "/" + TestConsts.APPNAME + "/" + - ODKFileUtils.asUriFragment(TestConsts.APPNAME, fileLocation); - } @Test public void testServingEmptyFile() { - // Setup + // Arrange File directoryLocation = createTestDirectory(); - File fileLocation = createEmptyTestFile(directoryLocation); + File fileLocation = createTestFile(directoryLocation, TEST_FILE_NAME_EMPTY, EMPTY_HTML_TXT); // Act IWebkitServerInterface serviceInterface = getIWebkitServerInterface(); @@ -276,29 +289,14 @@ public void testServingEmptyFile() { assertResponseIsNotEmpty(fileLocation); } - private File createEmptyTestFile(File directoryLocation) { - File fileLocation = new File(directoryLocation, TEST_FILE_NAME); - - // Create an empty file - try { - fileLocation.createNewFile(); - } catch (IOException e) { - e.printStackTrace(); - fail("Failed to create the empty test file: " + e.getMessage()); - } - - return fileLocation; - } - private void assertResponseIsNotEmpty(File fileLocation) { try { - String urlStr = buildTestUrl(fileLocation); - URL url = new URL(urlStr); + Uri uri = Uri.parse(buildTestUrl(fileLocation)); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + HttpURLConnection connection = (HttpURLConnection) new URL(uri.toString()).openConnection(); if (connection.getResponseCode() != HttpStatus.SC_OK) { fail("Response code was NOT HTTP_OK"); - } + } try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) { String responseStr = br.readLine(); @@ -307,7 +305,6 @@ private void assertResponseIsNotEmpty(File fileLocation) { } } } catch (IOException e) { - e.printStackTrace(); fail("Got an IOException when trying to use the web server: " + e.getMessage()); } catch (Exception e) { @@ -315,4 +312,459 @@ private void assertResponseIsNotEmpty(File fileLocation) { fail("Got an Exception when trying to use the web server: " + e.getMessage()); } } + + @Test + public void testServingNonexistentFile() { + // Act + IWebkitServerInterface serviceInterface = getIWebkitServerInterface(); + restartService(serviceInterface); + + // Assert + assertNonexistentFileResponse(getNonexistentFileLocation()); + } + + private File getNonexistentFileLocation() { + return new File(ODKFileUtils.getConfigFolder(TestConsts.APPNAME), "NonexistentFile.txt"); + } + + private void assertNonexistentFileResponse(File fileLocation) { + try { + Uri uri = Uri.parse(buildTestUrl(fileLocation)); + + HttpURLConnection connection = (HttpURLConnection) new URL(uri.toString()).openConnection(); + int responseCode = connection.getResponseCode(); + + assertEquals("Response code should be HTTP_NOT_FOUND", HttpStatus.SC_NOT_FOUND, responseCode); + } catch (IOException e) { + e.printStackTrace(); + fail("Got an IOException when trying to use the web server: " + e.getMessage()); + } catch (Exception e) { + e.printStackTrace(); + fail("Got an Exception when trying to use the web server: " + e.getMessage()); + } + } + + @Test + public void testServingLargeFile() { + // Arrange + File directoryLocation = createTestDirectory(); + File largeFileLocation = createTestFile(directoryLocation, TEST_FILE_NAME_LARGE, LARGE_FILE_CONTENT); + + // Act + IWebkitServerInterface serviceInterface = getIWebkitServerInterface(); + restartService(serviceInterface); + + // Assert + assertLargeFileResponse(largeFileLocation); + } + + private void assertLargeFileResponse(File fileLocation) { + try { + Uri uri = Uri.parse(buildTestUrl(fileLocation)); + + HttpURLConnection connection = (HttpURLConnection) new URL(uri.toString()).openConnection(); + int responseCode = connection.getResponseCode(); + + assertEquals("Response code should be HTTP_OK", HttpStatus.SC_OK, responseCode); + + // Additional assertions for large file content, if needed + } catch (IOException e) { + e.printStackTrace(); + fail("Got an IOException when trying to use the web server: " + e.getMessage()); + } catch (Exception e) { + e.printStackTrace(); + fail("Got an Exception when trying to use the web server: " + e.getMessage()); + } + } + + @Test + public void testServingFileWithSpecialCharactersInName() { + // Arrange + File directoryLocation = createTestDirectory(); + File fileLocation = createTestFile(directoryLocation, TEST_FILE_NAME_SPECIAL, SPECIAL_CHARACTERS_CONTENT); + + // Act + IWebkitServerInterface serviceInterface = getIWebkitServerInterface(); + restartService(serviceInterface); + + // Assert + assertResponseContainsSpecialCharacters(fileLocation); + } + + private void assertResponseContainsSpecialCharacters(File fileLocation) { + try { + Uri uri = Uri.parse(buildTestUrl(fileLocation)); + + HttpURLConnection connection = (HttpURLConnection) new URL(uri.toString()).openConnection(); + int responseCode = connection.getResponseCode(); + + assertEquals("Response code should be HTTP_OK", HttpStatus.SC_OK, responseCode); + + // Additional assertions for special characters in the file content, if needed + } catch (IOException e) { + e.printStackTrace(); + fail("Got an IOException when trying to use the web server: " + e.getMessage()); + } catch (Exception e) { + e.printStackTrace(); + fail("Got an Exception when trying to use the web server: " + e.getMessage()); + } + } + + @Test + public void testServingMultipleFilesWithSameNameInDifferentDirectories() { + // Arrange + File directory1 = createTestDirectory(); + File file1 = createTestFile(directory1, TEST_FILE_NAME_MULTIPLE_1, HELLO_WORLD_HTML_TXT); + + File directory2 = createTestDirectory(); + File file2 = createTestFile(directory2, TEST_FILE_NAME_MULTIPLE_2, HELLO_WORLD_HTML_TXT); + + // Act + IWebkitServerInterface serviceInterface = getIWebkitServerInterface(); + restartService(serviceInterface); + + // Assert + assertResponseForMultipleFiles(file1, file2); + } + + private void assertResponseForMultipleFiles(File file1, File file2) { + try { + Uri uri1 = Uri.parse(buildTestUrl(file1)); + Uri uri2 = Uri.parse(buildTestUrl(file2)); + + HttpURLConnection connection1 = (HttpURLConnection) new URL(uri1.toString()).openConnection(); + int responseCode1 = connection1.getResponseCode(); + assertEquals("Response code should be HTTP_OK", HttpStatus.SC_OK, responseCode1); + + HttpURLConnection connection2 = (HttpURLConnection) new URL(uri2.toString()).openConnection(); + int responseCode2 = connection2.getResponseCode(); + assertEquals("Response code should be HTTP_OK", HttpStatus.SC_OK, responseCode2); + } catch (IOException e) { + e.printStackTrace(); + fail("Got an IOException when trying to use the web server: " + e.getMessage()); + } catch (Exception e) { + e.printStackTrace(); + fail("Got an Exception when trying to use the web server: " + e.getMessage()); + } + } + + @Test + public void testServingFileWithSpacesInName() { + // Arrange + File directoryLocation = createTestDirectory(); + File fileLocation = createTestFile(directoryLocation, TEST_FILE_NAME_SPACES, "File with spaces in name"); + + // Act + IWebkitServerInterface serviceInterface = getIWebkitServerInterface(); + restartService(serviceInterface); + + // Assert + assertResponseContainsSpaces(fileLocation); + } + + private void assertResponseContainsSpaces(File fileLocation) { + try { + Uri uri = Uri.parse(buildTestUrl(fileLocation)); + + HttpURLConnection connection = (HttpURLConnection) new URL(uri.toString()).openConnection(); + int responseCode = connection.getResponseCode(); + + assertEquals("Response code should be HTTP_OK", HttpStatus.SC_OK, responseCode); + + // Additional assertions for spaces in the file content, if needed + } catch (IOException e) { + e.printStackTrace(); + fail("Got an IOException when trying to use the web server: " + e.getMessage()); + } catch (Exception e) { + e.printStackTrace(); + fail("Got an Exception when trying to use the web server: " + e.getMessage()); + } + } + + @Test + public void testServingFileWithUnicodeCharactersInContent() { + // Arrange + File directoryLocation = createTestDirectory(); + File fileLocation = createTestFile(directoryLocation, TEST_FILE_NAME_UNICODE, UNICODE_CONTENT); + + // Act + IWebkitServerInterface serviceInterface = getIWebkitServerInterface(); + restartService(serviceInterface); + + // Assert + assertResponseContainsUnicodeContent(fileLocation); + } + + private void assertResponseContainsUnicodeContent(File fileLocation) { + try { + Uri uri = Uri.parse(buildTestUrl(fileLocation)); + + HttpURLConnection connection = (HttpURLConnection) new URL(uri.toString()).openConnection(); + int responseCode = connection.getResponseCode(); + + assertEquals("Response code should be HTTP_OK", HttpStatus.SC_OK, responseCode); + + // Additional assertions for Unicode characters in the file content, if needed + } catch (IOException e) { + e.printStackTrace(); + fail("Got an IOException when trying to use the web server: " + e.getMessage()); + } catch (Exception e) { + e.printStackTrace(); + fail("Got an Exception when trying to use the web server: " + e.getMessage()); + } + } + + @Test + public void testServingFileWithInvalidExtension() { + // Arrange + File directoryLocation = createTestDirectory(); + File fileLocation = createTestFile(directoryLocation, TEST_FILE_NAME_INVALID_EXTENSION, "Content with an invalid extension"); + + // Act + IWebkitServerInterface serviceInterface = getIWebkitServerInterface(); + restartService(serviceInterface); + + // Assert + assertResponseForInvalidExtension(fileLocation); + } + + private void assertResponseForInvalidExtension(File fileLocation) { + try { + Uri uri = Uri.parse(buildTestUrl(fileLocation)); + + HttpURLConnection connection = (HttpURLConnection) new URL(uri.toString()).openConnection(); + int responseCode = connection.getResponseCode(); + + assertEquals("Response code should be HTTP_OK", HttpStatus.SC_OK, responseCode); + + // Additional assertions for handling an invalid extension, if needed + } catch (IOException e) { + e.printStackTrace(); + fail("Got an IOException when trying to use the web server: " + e.getMessage()); + } catch (Exception e) { + e.printStackTrace(); + fail("Got an Exception when trying to use the web server: " + e.getMessage()); + } + } + + @Test + public void testServingFileWithUpperCaseExtension() { + // Arrange + File directoryLocation = createTestDirectory(); + File fileLocation = createTestFile(directoryLocation, TEST_FILE_NAME_UPPERCASE_EXTENSION, "Content with an uppercase extension"); + + // Act + IWebkitServerInterface serviceInterface = getIWebkitServerInterface(); + restartService(serviceInterface); + + // Assert + assertResponseForUpperCaseExtension(fileLocation); + } + + private void assertResponseForUpperCaseExtension(File fileLocation) { + try { + Uri uri = Uri.parse(buildTestUrl(fileLocation)); + + HttpURLConnection connection = (HttpURLConnection) new URL(uri.toString()).openConnection(); + int responseCode = connection.getResponseCode(); + + assertEquals("Response code should be HTTP_OK", HttpStatus.SC_OK, responseCode); + + // Additional assertions for handling an uppercase extension, if needed + } catch (IOException e) { + e.printStackTrace(); + fail("Got an IOException when trying to use the web server: " + e.getMessage()); + } catch (Exception e) { + e.printStackTrace(); + fail("Got an Exception when trying to use the web server: " + e.getMessage()); + } + } + + @Test + public void testServingBinaryFile() { + // Arrange + File directoryLocation = createTestDirectory(); + File binaryFileLocation = createBinaryFile(directoryLocation, TEST_FILE_NAME_BINARY); + + // Act + IWebkitServerInterface serviceInterface = getIWebkitServerInterface(); + restartService(serviceInterface); + + // Assert + assertBinaryFileResponse(binaryFileLocation); + } + + private void assertBinaryFileResponse(File binaryFileLocation) { + try { + Uri uri = Uri.parse(buildTestUrl(binaryFileLocation)); + + HttpURLConnection connection = (HttpURLConnection) new URL(uri.toString()).openConnection(); + int responseCode = connection.getResponseCode(); + + assertEquals("Response code should be HTTP_OK", HttpStatus.SC_OK, responseCode); + + // Additional assertions for handling binary file content, if needed + } catch (IOException e) { + e.printStackTrace(); + fail("Got an IOException when trying to use the web server: " + e.getMessage()); + } catch (Exception e) { + e.printStackTrace(); + fail("Got an Exception when trying to use the web server: " + e.getMessage()); + } + } + + @Test + public void testServingFileWithMultipleDotsInFilename() { + // Arrange + File directoryLocation = createTestDirectory(); + File fileLocation = createTestFile(directoryLocation, TEST_FILE_NAME_MULTIPLE_DOTS, "Content with multiple dots in filename"); + + // Act + IWebkitServerInterface serviceInterface = getIWebkitServerInterface(); + restartService(serviceInterface); + + // Assert + assertResponseForMultipleDotsInFilename(fileLocation); + } + + private void assertResponseForMultipleDotsInFilename(File fileLocation) { + try { + Uri uri = Uri.parse(buildTestUrl(fileLocation)); + + HttpURLConnection connection = (HttpURLConnection) new URL(uri.toString()).openConnection(); + int responseCode = connection.getResponseCode(); + + assertEquals("Response code should be HTTP_OK", HttpStatus.SC_OK, responseCode); + + // Additional assertions for handling filenames with multiple dots, if needed + } catch (IOException e) { + e.printStackTrace(); + fail("Got an IOException when trying to use the web server: " + e.getMessage()); + } catch (Exception e) { + e.printStackTrace(); + fail("Got an Exception when trying to use the web server: " + e.getMessage()); + } + } + + @Test + public void testServingFileWithNoExtension() { + // Arrange + File directoryLocation = createTestDirectory(); + File fileLocation = createTestFile(directoryLocation, TEST_FILE_NAME_NO_EXTENSION, "Content with no extension"); + + // Act + IWebkitServerInterface serviceInterface = getIWebkitServerInterface(); + restartService(serviceInterface); + + // Assert + assertResponseForNoExtension(fileLocation); + } + + private void assertResponseForNoExtension(File fileLocation) { + try { + Uri uri = Uri.parse(buildTestUrl(fileLocation)); + + HttpURLConnection connection = (HttpURLConnection) new URL(uri.toString()).openConnection(); + int responseCode = connection.getResponseCode(); + + assertEquals("Response code should be HTTP_OK", HttpStatus.SC_OK, responseCode); + + // Additional assertions for handling files with no extension, if needed + } catch (IOException e) { + e.printStackTrace(); + fail("Got an IOException when trying to use the web server: " + e.getMessage()); + } catch (Exception e) { + e.printStackTrace(); + fail("Got an Exception when trying to use the web server: " + e.getMessage()); + } + } + + @Test + public void testServingFileWithDifferentExtension() { + // Arrange + File directoryLocation = createTestDirectory(); + File fileLocation = createTestFile(directoryLocation, TEST_FILE_NAME_DIFFERENT_EXTENSION, "Content with a different extension"); + + // Act + IWebkitServerInterface serviceInterface = getIWebkitServerInterface(); + restartService(serviceInterface); + + // Assert + assertResponseForDifferentExtension(fileLocation); + } + + private void assertResponseForDifferentExtension(File fileLocation) { + try { + Uri uri = Uri.parse(buildTestUrl(fileLocation)); + + HttpURLConnection connection = (HttpURLConnection) new URL(uri.toString()).openConnection(); + int responseCode = connection.getResponseCode(); + + assertEquals("Response code should be HTTP_OK", HttpStatus.SC_OK, responseCode); + + // Additional assertions for handling files with different extensions, if needed + } catch (IOException e) { + e.printStackTrace(); + fail("Got an IOException when trying to use the web server: " + e.getMessage()); + } catch (Exception e) { + e.printStackTrace(); + fail("Got an Exception when trying to use the web server: " + e.getMessage()); + } + } + + private File createTestFile(File directoryLocation, String fileName, String fileContent) { + File file = new File(directoryLocation, fileName); + + try { + DocumentFile docFile = DocumentFile.fromFile(file); + if (docFile != null) { + docFile.delete(); + } + docFile = DocumentFile.fromFile(directoryLocation); + DocumentFile newFile = docFile.createFile(null, fileName); + if (newFile != null) { + OutputStream outputStream = InstrumentationRegistry.getInstrumentation().getContext().getContentResolver().openOutputStream(newFile.getUri()); + PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream)); + writer.println(fileContent); + writer.close(); + } + } catch (IOException e) { + e.printStackTrace(); + fail("Failed to create test file: " + e.getMessage()); + } + + return file; + } + + private static String generateLargeFileContent() { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < 1000; i++) { + sb.append("This is a line in the large file.\n"); + } + return sb.toString(); + } + + + private File createBinaryFile(File directoryLocation, String fileName) { + Context context = InstrumentationRegistry.getInstrumentation().getContext(); + + Uri dirUri = Uri.fromFile(directoryLocation); + Uri fileUri = Uri.withAppendedPath(dirUri, fileName); + DocumentFile documentFile = DocumentFile.fromSingleUri(context, fileUri); + + try { + OutputStream outputStream = context.getContentResolver().openOutputStream(documentFile.getUri()); + if (outputStream != null) { + // Create a binary file with random content + byte[] binaryContent = new byte[]{0x12, 0x34, 0x56, 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0xF0}; + outputStream.write(binaryContent); + outputStream.close(); + } + } catch (IOException e) { + e.printStackTrace(); + fail("Failed to create the binary test file: " + e.getMessage()); + } + + return new File(directoryLocation, fileName); + } }