aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/androidTest/java/com/zeapo/pwdstore/DecryptTest.kt
blob: 448edc00f93e3ba76a86b9803a4c30be67c418e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.zeapo.pwdstore

import android.annotation.SuppressLint
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.os.SystemClock
import android.util.Log
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.ActivityTestRule
import com.zeapo.pwdstore.crypto.PgpActivity
import kotlinx.android.synthetic.main.decrypt_layout.crypto_extra_show
import kotlinx.android.synthetic.main.decrypt_layout.crypto_password_category_decrypt
import kotlinx.android.synthetic.main.decrypt_layout.crypto_password_file
import kotlinx.android.synthetic.main.decrypt_layout.crypto_password_show
import kotlinx.android.synthetic.main.decrypt_layout.crypto_username_show
import org.apache.commons.io.FileUtils
import org.apache.commons.io.IOUtils
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.nio.charset.StandardCharsets


@RunWith(AndroidJUnit4::class)
@LargeTest
class DecryptTest {
    private lateinit var targetContext: Context
    private lateinit var testContext: Context
    lateinit var activity: PgpActivity

    private val name = "sub"
    private val parentPath = "/category/"
    lateinit var path: String
    lateinit var repoPath: String

    @Rule @JvmField
    var mActivityRule: ActivityTestRule<PgpActivity> = ActivityTestRule<PgpActivity>(PgpActivity::class.java, true, false)

    private fun init() {
        targetContext = InstrumentationRegistry.getInstrumentation().targetContext
        testContext = InstrumentationRegistry.getInstrumentation().context
        copyAssets("encrypted-store", File(targetContext.filesDir, "test-store").absolutePath)
        repoPath = File(targetContext.filesDir, "test-store").absolutePath
        path = "$repoPath/$parentPath/$name.gpg".replace("//", "/")

        val intent = Intent(targetContext, PgpActivity::class.java)
        intent.putExtra("OPERATION", "DECRYPT")
        intent.putExtra("FILE_PATH", path)
        intent.putExtra("REPO_PATH", repoPath)

        activity = mActivityRule.launchActivity(intent)
    }

    @Test
    fun pathShouldDecompose() {
        val pathOne = "/fake/path/cat1/n1.gpg"
        val pathTwo = "/fake/path/n2.gpg"

        assertEquals("/cat1/n1.gpg", PgpActivity.getRelativePath(pathOne, "/fake/path"))
        assertEquals("/cat1/", PgpActivity.getParentPath(pathOne, "/fake/path"))
        assertEquals("n1", PgpActivity.getName("$pathOne/fake/path"))
        // test that even if we append a `/` it still works
        assertEquals("n1", PgpActivity.getName("$pathOne/fake/path/"))

        assertEquals("/n2.gpg", PgpActivity.getRelativePath(pathTwo, "/fake/path"))
        assertEquals("/", PgpActivity.getParentPath(pathTwo, "/fake/path"))
        assertEquals("n2", PgpActivity.getName("$pathTwo/fake/path"))
        assertEquals("n2", PgpActivity.getName("$pathTwo/fake/path/"))
    }

    @Test
    fun activityShouldShowName() {
        init()

        val categoryView = activity.crypto_password_category_decrypt
        assertNotNull(categoryView)
        assertEquals(parentPath, categoryView.text)

        val nameView = activity.crypto_password_file
        assertNotNull(nameView)
        assertEquals(name, nameView.text)
    }

    @SuppressLint("ApplySharedPref") // we need the preferences right away
    @Test
    fun shouldDecrypt() {
        init()
        val clearPass = IOUtils.toString(testContext.assets.open("clear-store/category/sub"), StandardCharsets.UTF_8)
        val passEntry = PasswordEntry(clearPass)

        // Setup the timer to 1 second
        // first remember the previous timer to set it back later
        val showTime = try {
            Integer.parseInt(activity.settings.getString("general_show_time", "45") ?: "45")
        } catch (e: NumberFormatException) {
            45
        }
        // second set the new timer
        activity.settings.edit().putString("general_show_time", "2").commit()

        activity.onBound(null)

        // have we decrypted things correctly?
        assertEquals(passEntry.password, activity.crypto_password_show.text)
        assertEquals(passEntry.username, activity.crypto_username_show.text.toString())
        assertEquals(passEntry.extraContent, activity.crypto_extra_show.text.toString())

        // did we copy the password?
        val clipboard: ClipboardManager = activity.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        assertEquals(passEntry.password, clipboard.primaryClip.getItemAt(0).text)

        // wait until the clipboard is cleared
        SystemClock.sleep(4000)

        // The clipboard should be cleared!!
        for(i in 0..clipboard.primaryClip.itemCount) {
            assertEquals("", clipboard.primaryClip.getItemAt(i).text)
        }

        // set back the timer
        activity.settings.edit().putString("general_show_time", showTime.toString()).commit()
    }

    companion object {
        fun copyAssets(source: String, destination: String) {
            FileUtils.forceMkdir(File(destination))
            FileUtils.cleanDirectory(File(destination))

            val testContext = InstrumentationRegistry.getInstrumentation().context
            val assetManager = testContext.assets
            val files: Array<String>? = assetManager.list(source)

            files?.map { filename ->
                val destPath = "$destination/$filename"
                val sourcePath = "$source/$filename"

                if (assetManager.list(sourcePath).isNotEmpty()) {
                    FileUtils.forceMkdir(File(destination, filename))
                    copyAssets("$source/$filename", destPath)
                } else {
                    try {
                        val input = assetManager.open(sourcePath)
                        val outFile = File(destination, filename)
                        val output = FileOutputStream(outFile)
                        IOUtils.copy(input, output)
                        input.close()
                        output.flush()
                        output.close()
                    } catch (e: IOException) {
                        Log.e("tag", "Failed to copy asset file: $filename", e)
                    }
                }
            }
        }
    }

}