-
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
196 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
33 changes: 33 additions & 0 deletions
33
...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,33 @@ | ||
/* | ||
* 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] | ||
// Using the Character.isWhitespace() function we will support also the UNICODE | ||
// characters according with the docs. Also have in mind that in JAVA the | ||
// `char` primitive is a 16bits long number which covers all the UNICODE characters: | ||
// https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html | ||
// Given that we replace each printable character with `x` for 2 chars expressions | ||
// as emojis we will have 2 `x` instead of 1 but this will not be a problem as the | ||
// obfuscation will still be applied. | ||
if (Character.isWhitespace(character.code)) { | ||
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
134 changes: 134 additions & 0 deletions
134
...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,134 @@ | ||
/* | ||
* 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 fakeExpectedText = ( | ||
fakeExpectedChunk1 + | ||
fakeExpectedChunk2 + | ||
fakeExpectedChunk3 + | ||
fakeExpectedChunk4 + | ||
fakeExpectedChunk5 | ||
) | ||
val fakeText = ( | ||
fakeExpectedChunk1 + | ||
forge.aString(fakeExpectedChunk2.length) { forge.anAlphaNumericalChar() } + | ||
fakeExpectedChunk3 + | ||
forge.aString(fakeExpectedChunk4.length) { forge.anAlphaNumericalChar() } + | ||
fakeExpectedChunk5 | ||
) | ||
|
||
// 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 fakeExpectedText = ( | ||
fakeExpectedChunk1 + | ||
fakeExpectedChunk2 + | ||
fakeExpectedChunk3 + | ||
fakeExpectedChunk4 + | ||
fakeExpectedChunk5 | ||
) | ||
val fakeText = ( | ||
fakeExpectedChunk1 + | ||
forge.aString(fakeExpectedChunk2.length) { forge.anAlphaNumericalChar() } + | ||
fakeExpectedChunk3 + | ||
forge.aString(fakeExpectedChunk4.length) { forge.anAlphaNumericalChar() } + | ||
fakeExpectedChunk5 | ||
) | ||
|
||
// 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 fakeExpectedText = ( | ||
fakeExpectedChunk1 + | ||
fakeExpectedChunk2 + | ||
fakeExpectedChunk3 + | ||
fakeExpectedChunk4 + | ||
fakeExpectedChunk5 | ||
) | ||
val fakeText = ( | ||
fakeExpectedChunk1 + | ||
forge.aString(fakeExpectedChunk2.length) { forge.anAlphaNumericalChar() } + | ||
fakeExpectedChunk3 + | ||
forge.aString(fakeExpectedChunk4.length) { forge.anAlphaNumericalChar() } + | ||
fakeExpectedChunk5 | ||
) | ||
|
||
// When | ||
val obfuscatedText = testedObfuscator.obfuscate(fakeText) | ||
|
||
// Then | ||
assertThat(obfuscatedText).isEqualTo(fakeExpectedText) | ||
} | ||
} |