-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6367 from seadowg/v2024.2.2-3-4
Merge changes from v2024.2/3/4
- Loading branch information
Showing
18 changed files
with
242 additions
and
355 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
androidshared/src/main/java/org/odk/collect/androidshared/utils/PathUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.odk.collect.androidshared.utils | ||
|
||
import timber.log.Timber | ||
import java.io.File | ||
|
||
object PathUtils { | ||
@JvmStatic | ||
fun getAbsoluteFilePath(dirPath: String, filePath: String): String { | ||
val absoluteFilePath = | ||
if (filePath.startsWith(dirPath)) filePath else dirPath + File.separator + filePath | ||
|
||
val canonicalAbsoluteFilePath = File(absoluteFilePath).canonicalPath | ||
val canonicalDirPath = File(dirPath).canonicalPath | ||
if (!canonicalAbsoluteFilePath.startsWith(canonicalDirPath)) { | ||
Timber.e( | ||
"Attempt to access file outside of Collect directory:\n" + | ||
"dirPath: $dirPath\n" + | ||
"filePath: $filePath\n" + | ||
"absoluteFilePath: $absoluteFilePath\n" + | ||
"canonicalAbsoluteFilePath: $canonicalAbsoluteFilePath\n" + | ||
"canonicalDirPath: $canonicalDirPath" | ||
) | ||
} | ||
return absoluteFilePath | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
androidshared/src/test/java/org/odk/collect/androidshared/utils/PathUtilsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.odk.collect.androidshared.utils | ||
|
||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.hamcrest.Matchers.equalTo | ||
import org.junit.Test | ||
import org.odk.collect.shared.TempFiles | ||
import java.io.File | ||
|
||
class PathUtilsTest { | ||
@Test | ||
fun `getAbsoluteFilePath() returns filePath prepended with dirPath`() { | ||
val path = PathUtils.getAbsoluteFilePath("/anotherRoot/anotherDir", "root/dir/file") | ||
assertThat(path, equalTo("/anotherRoot/anotherDir/root/dir/file")) | ||
} | ||
|
||
@Test | ||
fun `getAbsoluteFilePath() returns valid path when filePath does not start with seperator`() { | ||
val path = PathUtils.getAbsoluteFilePath("/root/dir", "file") | ||
assertThat(path, equalTo("/root/dir/file")) | ||
} | ||
|
||
@Test | ||
fun `getAbsoluteFilePath() returns filePath when it starts with dirPath`() { | ||
val path = PathUtils.getAbsoluteFilePath("/root/dir", "/root/dir/file") | ||
assertThat(path, equalTo("/root/dir/file")) | ||
} | ||
|
||
@Test | ||
fun `getAbsoluteFilePath() works when dirPath is not canonical`() { | ||
val tempDir = TempFiles.createTempDir() | ||
val nonCanonicalPath = | ||
tempDir.canonicalPath + File.separator + ".." + File.separator + tempDir.name | ||
assertThat(File(nonCanonicalPath).canonicalPath, equalTo(tempDir.canonicalPath)) | ||
|
||
val path = PathUtils.getAbsoluteFilePath(nonCanonicalPath, "file") | ||
assertThat(path, equalTo(nonCanonicalPath + File.separator + "file")) | ||
} | ||
} |
54 changes: 41 additions & 13 deletions
54
...src/androidTest/java/org/odk/collect/android/feature/external/InstanceUploadActionTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,68 @@ | ||
package org.odk.collect.android.feature.external | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.provider.BaseColumns._ID | ||
import androidx.test.core.app.ApplicationProvider | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.hamcrest.Matchers.equalTo | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.rules.RuleChain | ||
import org.junit.runner.RunWith | ||
import org.odk.collect.android.external.InstancesContract | ||
import org.odk.collect.android.instancemanagement.send.InstanceUploaderActivity | ||
import org.odk.collect.android.support.TestDependencies | ||
import org.odk.collect.android.support.pages.FormEntryPage | ||
import org.odk.collect.android.support.pages.OkDialog | ||
import org.odk.collect.android.support.rules.CollectTestRule | ||
import org.odk.collect.android.support.rules.TestRuleChain | ||
import org.odk.collect.android.utilities.ApplicationConstants | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class InstanceUploadActionTest { | ||
|
||
val collectTestRule = CollectTestRule() | ||
private val rule = CollectTestRule() | ||
private val context = ApplicationProvider.getApplicationContext<Context>() | ||
private val testDependencies = TestDependencies() | ||
|
||
@get:Rule | ||
val rule: RuleChain = TestRuleChain.chain() | ||
.around(collectTestRule) | ||
val chain: RuleChain = TestRuleChain.chain(testDependencies) | ||
.around(rule) | ||
|
||
@Test | ||
fun whenInstanceDoesNotExist_showsError() { | ||
val instanceIds = longArrayOf(11) | ||
instanceUploadAction(instanceIds) | ||
fun whenIntentIncludesURLExtra_instancesAreUploadedToThatURL() { | ||
rule.startAtMainMenu() | ||
.copyForm("one-question.xml") | ||
.startBlankForm("One Question") | ||
.fillOutAndFinalize(FormEntryPage.QuestionAndAnswer("what is your age", "34")) | ||
|
||
OkDialog() | ||
.assertOnPage() | ||
.assertText(org.odk.collect.strings.R.string.no_forms_uploaded) | ||
val instanceId = | ||
context.contentResolver.query(InstancesContract.getUri("DEMO"), null, null, null, null) | ||
.use { | ||
it!!.moveToFirst() | ||
it.getLong(it.getColumnIndex(_ID)) | ||
} | ||
|
||
val intent = Intent("org.odk.collect.android.INSTANCE_UPLOAD") | ||
intent.type = InstancesContract.CONTENT_TYPE | ||
intent.putExtra(ApplicationConstants.BundleKeys.URL, testDependencies.server.url) | ||
intent.putExtra("instances", longArrayOf(instanceId)) | ||
|
||
rule.launch(intent, OkDialog()) | ||
.assertTextInDialog("One Question - Success") | ||
assertThat(testDependencies.server.submissions.size, equalTo(1)) | ||
} | ||
|
||
private fun instanceUploadAction(instanceIds: LongArray) { | ||
@Test | ||
fun whenInstanceDoesNotExist_showsError() { | ||
rule.startAtMainMenu() | ||
|
||
val intent = Intent("org.odk.collect.android.INSTANCE_UPLOAD") | ||
intent.type = InstancesContract.CONTENT_TYPE | ||
intent.putExtra("instances", instanceIds) | ||
collectTestRule.launch<InstanceUploaderActivity>(intent) | ||
intent.putExtra("instances", longArrayOf(11)) | ||
|
||
rule.launch(intent, OkDialog()) | ||
.assertText(org.odk.collect.strings.R.string.no_forms_uploaded) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.