1+ /*
2+ * Copyright (c) 2015-2020, Virgil Security, Inc.
3+ *
4+ * Lead Maintainer: Virgil Security Inc. <support@virgilsecurity.com>
5+ *
6+ * All rights reserved.
7+ *
8+ * Redistribution and use in source and binary forms, with or without
9+ * modification, are permitted provided that the following conditions are met:
10+ *
11+ * (1) Redistributions of source code must retain the above copyright notice, this
12+ * list of conditions and the following disclaimer.
13+ *
14+ * (2) Redistributions in binary form must reproduce the above copyright notice,
15+ * this list of conditions and the following disclaimer in the documentation
16+ * and/or other materials provided with the distribution.
17+ *
18+ * (3) Neither the name of virgil nor the names of its
19+ * contributors may be used to endorse or promote products derived from
20+ * this software without specific prior written permission.
21+ *
22+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32+ */
33+
34+ package com.virgilsecurity.android.common.snippet
35+
36+ import androidx.test.ext.junit.runners.AndroidJUnit4
37+ import com.virgilsecurity.android.common.callback.OnGetTokenCallback
38+ import com.virgilsecurity.android.common.utils.TestConfig
39+ import com.virgilsecurity.android.common.utils.TestUtils
40+ import com.virgilsecurity.android.ethree.interaction.EThree
41+ import com.virgilsecurity.common.model.Data
42+ import com.virgilsecurity.sdk.crypto.VirgilCrypto
43+ import org.junit.Assert.assertArrayEquals
44+ import org.junit.Assert.assertNotNull
45+ import org.junit.Before
46+ import org.junit.Test
47+ import org.junit.runner.RunWith
48+ import java.io.ByteArrayInputStream
49+ import java.io.ByteArrayOutputStream
50+ import java.util.*
51+
52+ /* *
53+ * This test covers snippets that used in documentation.
54+ */
55+ @RunWith(AndroidJUnit4 ::class )
56+ class SnippetsTest {
57+
58+ private lateinit var aliceIdentity: String
59+ private lateinit var bobIdentity: String
60+ private lateinit var crypto: VirgilCrypto
61+ private lateinit var aliceEthree: EThree
62+ private lateinit var bobEthree: EThree
63+
64+ @Before
65+ fun setup () {
66+ this .crypto = VirgilCrypto ()
67+ this .aliceIdentity = UUID .randomUUID().toString()
68+ this .aliceEthree = EThree (aliceIdentity,
69+ object : OnGetTokenCallback {
70+ override fun onGetToken (): String {
71+ return TestUtils .generateTokenString(aliceIdentity)
72+ }
73+ },
74+ TestConfig .context)
75+ assertNotNull(this .aliceEthree)
76+ this .aliceEthree.register().execute()
77+
78+ this .bobIdentity = UUID .randomUUID().toString()
79+ this .bobEthree = EThree (bobIdentity,
80+ object : OnGetTokenCallback {
81+ override fun onGetToken (): String {
82+ return TestUtils .generateTokenString(bobIdentity)
83+ }
84+ },
85+ TestConfig .context)
86+ assertNotNull(this .bobEthree)
87+ this .bobEthree.register().execute()
88+ }
89+
90+ @Test
91+ fun encryptShared_complex () {
92+ val triple = encryptShared()
93+ val p2pEncryptedStreamKeyData = triple.first
94+ val groupEncryptedStreamKeyData = triple.second
95+ val encryptedData = triple.third
96+
97+ val decryptedData = decryptShared(p2pEncryptedStreamKeyData, groupEncryptedStreamKeyData, encryptedData)
98+
99+ assertArrayEquals(" Hello" .toByteArray(), decryptedData)
100+ }
101+
102+ private fun encryptShared (): Triple <ByteArray , ByteArray , ByteArray > {
103+ // encryptShared >>
104+
105+ // 1. Prepare streams.
106+ val plaintext = " Hello"
107+ val data = plaintext.toByteArray()
108+ val inputStream = ByteArrayInputStream (data)
109+ val inputStreamSize = data.size
110+ val encryptedOutputStream = ByteArrayOutputStream ()
111+
112+ // 2. Encrypt stream.
113+ val streamKeyData = aliceEthree.encryptShared(inputStream, inputStreamSize, encryptedOutputStream)
114+
115+ // 3. Upload data from `encryptedOutputStream` to a remote storage.
116+
117+ /* *
118+ * Application specific code.
119+ */
120+
121+ // 4.1 Encrypt `streamKeyData` to a specific user (peer-to-peer).
122+ val bobCard = aliceEthree.findUser(bobIdentity).get()
123+ val p2pEncryptedStreamKeyData = aliceEthree.authEncrypt(Data (streamKeyData), bobCard)
124+
125+ // 4.2 Encrypt `streamKeyData` to a group.
126+ val groupId = " group-chat-1"
127+ val bobUsersResult = aliceEthree.findUsers(arrayListOf (bobIdentity)).get()
128+ val aliceGroup = aliceEthree.createGroup(groupId, bobUsersResult).get()
129+ val groupEncryptedStreamKeyData = aliceGroup.encrypt(streamKeyData)
130+
131+ // 5. Send encrypted `streamKeyData` (p2pEncryptedStreamKeyData, or groupEncryptedStreamKeyData) to destination device.
132+
133+ /* *
134+ * Application specific code.
135+ */
136+
137+ // << encryptShared
138+
139+ return Triple (p2pEncryptedStreamKeyData.value, groupEncryptedStreamKeyData, encryptedOutputStream.toByteArray())
140+ }
141+
142+ private fun decryptShared (p2pEncryptedStreamKeyData : ByteArray , groupEncryptedStreamKeyData : ByteArray , encryptedData : ByteArray ): ByteArray? {
143+ // decryptShared >>
144+
145+ // 1. Receive `encryptedStreamKeyData` and download data from the remote storage.
146+ /* *
147+ * Application specific code.
148+ */
149+
150+ // 2. Prepare streams.
151+ val encryptedInputStream = ByteArrayInputStream (encryptedData)
152+ val decryptedOutputStream = ByteArrayOutputStream ()
153+
154+ // 3. Find initiator's Card.
155+ val aliceCard = bobEthree.findUser(aliceIdentity).get()
156+
157+ // 4.1 Decrypt `encryptedStreamKeyData` received peer-to-peer.
158+ val p2pDecryptedStreamKeyData = bobEthree.authDecrypt(Data (p2pEncryptedStreamKeyData), aliceCard).value
159+
160+ // 4.2 Decrypt `encryptedStreamKeyData` received to the group.
161+ val groupId = " group-chat-1"
162+ val bobGroup = bobEthree.loadGroup(groupId, aliceCard).get() // load correspond group
163+ val groupDecryptedStreamKeyData = bobGroup.decrypt(groupEncryptedStreamKeyData, aliceCard) // decrypt key
164+
165+ // 5. Decrypt stream.
166+ val decryptedStreamKeyData = p2pDecryptedStreamKeyData ? : groupDecryptedStreamKeyData
167+
168+ bobEthree.decryptShared(encryptedInputStream, decryptedOutputStream, decryptedStreamKeyData, aliceCard)
169+
170+ // << decryptShared
171+
172+ return decryptedOutputStream.toByteArray()
173+ }
174+ }
0 commit comments