DebugLogFileBackendTest.kt 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package ch.threema.logging.backend
  2. import android.util.Log
  3. import androidx.test.ext.junit.runners.AndroidJUnit4
  4. import androidx.test.rule.GrantPermissionRule
  5. import ch.threema.app.BuildConfig
  6. import ch.threema.app.DangerousTest
  7. import ch.threema.app.ThreemaApplication
  8. import ch.threema.app.getReadWriteExternalStoragePermissionRule
  9. import ch.threema.logging.LogLevel
  10. import java.util.concurrent.TimeUnit
  11. import kotlin.test.BeforeTest
  12. import kotlin.test.Test
  13. import kotlin.test.assertFalse
  14. import kotlin.test.assertTrue
  15. import org.junit.Assume
  16. import org.junit.BeforeClass
  17. import org.junit.Rule
  18. import org.junit.runner.RunWith
  19. @RunWith(AndroidJUnit4::class)
  20. @DangerousTest(reason = "Deletes logfile")
  21. class DebugLogFileBackendTest {
  22. @JvmField
  23. @Rule
  24. val permissionRule: GrantPermissionRule = getReadWriteExternalStoragePermissionRule()
  25. @BeforeTest
  26. fun disableLogfile() {
  27. DebugLogFileBackend.setEnabled(false)
  28. }
  29. /**
  30. * Make sure that logging into the debug log file actually creates the debug log file.
  31. * Also test that the file is only created when enabled.
  32. */
  33. @Test
  34. fun testEnable() {
  35. val logFilePath = DebugLogFileBackend.getLogFilePath()
  36. // Log with the debug log file disabled
  37. val backend = DebugLogFileBackend(Log.INFO)
  38. backend.printSomething(level = Log.WARN)
  39. // Enabling the debug log file won't create the log file just yet
  40. assertFalse(logFilePath.exists())
  41. DebugLogFileBackend.setEnabled(true)
  42. assertFalse(logFilePath.exists())
  43. // Logs below the min log level are filtered
  44. backend.printSomething(level = Log.DEBUG)
  45. assertFalse(logFilePath.exists())
  46. // Log with the debug log file enabled
  47. backend.printSomething(level = Log.WARN)
  48. assertTrue(logFilePath.exists())
  49. // Verify that the fallback file is not created when not needed
  50. assertFalse(DebugLogFileBackend.getFallbackLogFilePath().exists())
  51. }
  52. /**
  53. * Make sure that the fallback log file is deleted when the default log file can be created successfully.
  54. */
  55. @Test
  56. fun testFallbackFileIsDeletedIfDefaultFileCanBeCreated() {
  57. // Create the fallback log file
  58. val fallbackLogFilePath = DebugLogFileBackend.getFallbackLogFilePath()
  59. assertTrue(fallbackLogFilePath.createNewFile(), "Could not create fallback logfile")
  60. // Enable logging and write a log message
  61. DebugLogFileBackend.setEnabled(true)
  62. val backend = DebugLogFileBackend(Log.INFO)
  63. backend.printSomething(level = Log.WARN)
  64. // Verify that the fallback file is now deleted, as it is not needed
  65. assertFalse(fallbackLogFilePath.exists())
  66. }
  67. /**
  68. * Make sure that disabling the debug log actually deletes the debug log file.
  69. */
  70. @Test
  71. fun testDisableRemovesFile() {
  72. val logFilePath = DebugLogFileBackend.getLogFilePath()
  73. assertFalse(logFilePath.exists())
  74. assertTrue(logFilePath.createNewFile(), "Could not create logfile")
  75. assertTrue(logFilePath.exists())
  76. DebugLogFileBackend.setEnabled(false)
  77. assertFalse(logFilePath.exists())
  78. }
  79. /**
  80. * Make sure that disabling the debug log actually deletes the fallback debug log file.
  81. */
  82. @Test
  83. fun testDisableRemovesFallbackFile() {
  84. val fallbackLogFilePath = DebugLogFileBackend.getFallbackLogFilePath()
  85. assertFalse(fallbackLogFilePath.exists())
  86. assertTrue(fallbackLogFilePath.createNewFile(), "Could not create fallback logfile")
  87. assertTrue(fallbackLogFilePath.exists())
  88. DebugLogFileBackend.setEnabled(false)
  89. assertFalse(fallbackLogFilePath.exists())
  90. }
  91. private fun DebugLogFileBackend.printSomething(@LogLevel level: Int) {
  92. printAsync(level, BuildConfig.LOG_TAG, null, "hi").get(500, TimeUnit.MILLISECONDS)
  93. }
  94. companion object {
  95. /**
  96. * On one of our CI devices, the access to the external storage directory is inexplicably broken, which leads these tests to fail.
  97. * Since the external storage is only used for the debug log file, and a fallback is already in place to write the debug log into
  98. * a different location if writing to the external storage fails, it is acceptable for the time being to simply skip these tests
  99. * based on the precondition that the external storage directory exists.
  100. */
  101. @BeforeClass
  102. @JvmStatic
  103. fun assumeDeviceHasAccessToExternalStorage() {
  104. Assume.assumeTrue(
  105. try {
  106. ThreemaApplication.getAppContext().getExternalFilesDir(null)?.exists() == true
  107. } catch (_: Exception) {
  108. false
  109. },
  110. )
  111. }
  112. }
  113. }