-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RUMM-2651 SR - skip new lines and spaces when obfuscating texts
- Loading branch information
Showing
6 changed files
with
200 additions
and
20 deletions.
There are no files selected for viewing
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
26 changes: 26 additions & 0 deletions
26
...lay/src/main/kotlin/com/datadog/android/sessionreplay/recorder/mapper/StringObfuscator.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 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
|
||
package com.datadog.android.sessionreplay.recorder.mapper | ||
|
||
internal class StringObfuscator { | ||
fun obfuscate(stringValue: String): String { | ||
return String( | ||
CharArray(stringValue.length) { | ||
val character = stringValue[it] | ||
if (character.isWhitespace()) { | ||
character | ||
} else { | ||
CHARACTER_MASK | ||
} | ||
} | ||
) | ||
} | ||
|
||
companion object { | ||
private const val CHARACTER_MASK = 'x' | ||
} | ||
} |
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
145 changes: 145 additions & 0 deletions
145
...src/test/kotlin/com/datadog/android/sessionreplay/recorder/mapper/StringObfuscatorTest.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,145 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
|
||
package com.datadog.android.sessionreplay.recorder.mapper | ||
|
||
import com.datadog.android.sessionreplay.utils.ForgeConfigurator | ||
import com.datadog.tools.unit.extensions.ApiLevelExtension | ||
import fr.xgouchet.elmyr.Forge | ||
import fr.xgouchet.elmyr.junit5.ForgeConfiguration | ||
import fr.xgouchet.elmyr.junit5.ForgeExtension | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.junit.jupiter.api.extension.Extensions | ||
import org.mockito.junit.jupiter.MockitoExtension | ||
import org.mockito.junit.jupiter.MockitoSettings | ||
import org.mockito.quality.Strictness | ||
|
||
@Extensions( | ||
ExtendWith(MockitoExtension::class), | ||
ExtendWith(ForgeExtension::class), | ||
ExtendWith(ApiLevelExtension::class) | ||
) | ||
@MockitoSettings(strictness = Strictness.LENIENT) | ||
@ForgeConfiguration(ForgeConfigurator::class) | ||
internal class StringObfuscatorTest { | ||
|
||
lateinit var testedObfuscator: StringObfuscator | ||
|
||
@BeforeEach | ||
fun `set up`() { | ||
testedObfuscator = StringObfuscator() | ||
} | ||
|
||
@Test | ||
fun `M mask String W maskString(){string with newline}`( | ||
forge: Forge | ||
) { | ||
// Given | ||
val fakeExpectedChunk1 = forge.aString(size = forge.anInt(1, max = 10)) { '\n' } | ||
val fakeExpectedChunk2 = forge.aString { 'x' } | ||
val fakeExpectedChunk3 = forge.aString(size = forge.anInt(1, max = 10)) { '\n' } | ||
val fakeExpectedChunk4 = forge.aString { 'x' } | ||
val fakeExpectedChunk5 = forge.aString(size = forge.anInt(1, max = 10)) { '\n' } | ||
val n = forge.anInt(min = 1, max = 10) | ||
val fakeExpectedText = ( | ||
fakeExpectedChunk1 + | ||
fakeExpectedChunk2 + | ||
fakeExpectedChunk3 + | ||
fakeExpectedChunk4 + | ||
fakeExpectedChunk5 | ||
).times(n) | ||
val fakeText = ( | ||
fakeExpectedChunk1 + | ||
forge.aString(fakeExpectedChunk2.length) { forge.anAlphaNumericalChar() } + | ||
fakeExpectedChunk3 + | ||
forge.aString(fakeExpectedChunk4.length) { forge.anAlphaNumericalChar() } + | ||
fakeExpectedChunk5 | ||
).times(n) | ||
|
||
// When | ||
val obfuscatedText = testedObfuscator.obfuscate(fakeText) | ||
|
||
// Then | ||
assertThat(obfuscatedText).isEqualTo(fakeExpectedText) | ||
} | ||
|
||
@Test | ||
fun `M mask String W maskString(){string with carriage return character}`( | ||
forge: Forge | ||
) { | ||
// Given | ||
val fakeExpectedChunk1 = forge.aString(size = forge.anInt(1, max = 10)) { '\r' } | ||
val fakeExpectedChunk2 = forge.aString { 'x' } | ||
val fakeExpectedChunk3 = forge.aString(size = forge.anInt(1, max = 10)) { '\r' } | ||
val fakeExpectedChunk4 = forge.aString { 'x' } | ||
val fakeExpectedChunk5 = forge.aString(size = forge.anInt(1, max = 10)) { '\r' } | ||
val n = forge.anInt(min = 1, max = 10) | ||
val fakeExpectedText = ( | ||
fakeExpectedChunk1 + | ||
fakeExpectedChunk2 + | ||
fakeExpectedChunk3 + | ||
fakeExpectedChunk4 + | ||
fakeExpectedChunk5 | ||
).times(n) | ||
val fakeText = ( | ||
fakeExpectedChunk1 + | ||
forge.aString(fakeExpectedChunk2.length) { forge.anAlphaNumericalChar() } + | ||
fakeExpectedChunk3 + | ||
forge.aString(fakeExpectedChunk4.length) { forge.anAlphaNumericalChar() } + | ||
fakeExpectedChunk5 | ||
).times(n) | ||
|
||
// When | ||
val obfuscatedText = testedObfuscator.obfuscate(fakeText) | ||
|
||
// Then | ||
assertThat(obfuscatedText).isEqualTo(fakeExpectedText) | ||
} | ||
|
||
@Test | ||
fun `M mask String W maskString(){string with whitespace character}`( | ||
forge: Forge | ||
) { | ||
// Given | ||
val fakeExpectedChunk1 = forge.aWhitespaceString() | ||
val fakeExpectedChunk2 = forge.aString { 'x' } | ||
val fakeExpectedChunk3 = forge.aWhitespaceString() | ||
val fakeExpectedChunk4 = forge.aString { 'x' } | ||
val fakeExpectedChunk5 = forge.aWhitespaceString() | ||
val n = forge.anInt(min = 1, max = 10) | ||
val fakeExpectedText = ( | ||
fakeExpectedChunk1 + | ||
fakeExpectedChunk2 + | ||
fakeExpectedChunk3 + | ||
fakeExpectedChunk4 + | ||
fakeExpectedChunk5 | ||
).times(n) | ||
val fakeText = ( | ||
fakeExpectedChunk1 + | ||
forge.aString(fakeExpectedChunk2.length) { forge.anAlphaNumericalChar() } + | ||
fakeExpectedChunk3 + | ||
forge.aString(fakeExpectedChunk4.length) { forge.anAlphaNumericalChar() } + | ||
fakeExpectedChunk5 | ||
).times(n) | ||
|
||
// When | ||
val obfuscatedText = testedObfuscator.obfuscate(fakeText) | ||
|
||
// Then | ||
assertThat(obfuscatedText).isEqualTo(fakeExpectedText) | ||
} | ||
|
||
private fun String.times(n: Int): String { | ||
val builder = StringBuilder(n * length) | ||
repeat(n) { | ||
builder.append(this) | ||
} | ||
return builder.toString() | ||
} | ||
} |