-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from /issues/65-infinite-stacking
Fix infinite stacking of the same cached certificates
- Loading branch information
Showing
7 changed files
with
366 additions
and
3 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
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
84 changes: 84 additions & 0 deletions
84
library/src/test/java/com/wultra/android/sslpinning/model/CachedDataTest.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,84 @@ | ||
/* | ||
* Copyright 2023 Wultra s.r.o. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
*/ | ||
|
||
package com.wultra.android.sslpinning.model | ||
|
||
import android.util.Base64 | ||
import com.wultra.android.sslpinning.CertStore | ||
import io.mockk.every | ||
import io.mockk.mockkStatic | ||
import io.mockk.unmockkAll | ||
import org.junit.After | ||
import org.junit.Assert | ||
import org.junit.Before | ||
import org.junit.Test | ||
import java.util.Date | ||
|
||
/** | ||
* | ||
*/ | ||
internal class CachedDataTest { | ||
@Before | ||
fun setUp() { | ||
mockkStatic(Base64::class) | ||
every { Base64.decode(any<String>(), any()) } answers { | ||
java.util.Base64.getDecoder().decode(it.invocation.args[0] as String) | ||
} | ||
} | ||
|
||
@After | ||
fun tearDown() { | ||
unmockkAll() | ||
} | ||
|
||
@Test | ||
fun testEntries() { | ||
// the 1st item has different signature, otherwise the data are the same | ||
val jsonData = listOf("""{"fingerprints": [ | ||
{ | ||
"name" : "github.com", | ||
"fingerprint" : "kqN/vV4hpTqVxxbhFE9EL1grlND6/Gc+tnF6TrUaiKc=", | ||
"expires" : 1710460799, | ||
"signature" : "MEQCIElYrNRc/RnIJTFM9Or90Op+5YfEc+OA0JCOzEdewx07AiAm/xAKMkhu9k9mXNFNyUSB/A1FbnqKEegpEpsugY5Z/Q==" | ||
} | ||
]}""", """{"fingerprints": [ | ||
{ | ||
"name" : "github.com", | ||
"fingerprint" : "kqN/vV4hpTqVxxbhFE9EL1grlND6/Gc+tnF6TrUaiKc=", | ||
"expires" : 1710460799, | ||
"signature" : "MEUCIQDGSbss+QVvF5juP3y7/DkUPYIWopabdHrZETGqYMctLgIgX7aKQ8+22AIlmuWczXZKze4w20ycsKzaps4reobjikA=" | ||
} | ||
]}""") | ||
|
||
val responses = jsonData.map { | ||
CertStore.GSON.fromJson(it, GetFingerprintResponse::class.java) | ||
} | ||
responses.forEach { | ||
Assert.assertEquals(1, it.fingerprints.size) | ||
} | ||
|
||
val date = Date() | ||
val cachedDatas = responses.map { | ||
val certInfos = it.fingerprints.map { entry -> CertificateInfo(entry) }.toTypedArray() | ||
Assert.assertEquals(1, certInfos.size) | ||
CachedData(certInfos, date) | ||
} | ||
|
||
Assert.assertEquals(2, cachedDatas.size) | ||
Assert.assertEquals(cachedDatas[0], cachedDatas[1]) | ||
} | ||
|
||
} |
88 changes: 88 additions & 0 deletions
88
library/src/test/java/com/wultra/android/sslpinning/model/CertificateInfoTest.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,88 @@ | ||
/* | ||
* Copyright 2023 Wultra s.r.o. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions | ||
* and limitations under the License. | ||
*/ | ||
|
||
package com.wultra.android.sslpinning.model | ||
|
||
import android.util.Base64 | ||
import com.wultra.android.sslpinning.CertStore | ||
import io.mockk.every | ||
import io.mockk.mockkStatic | ||
import io.mockk.unmockkAll | ||
import junit.framework.TestCase.assertTrue | ||
import org.junit.After | ||
import org.junit.Assert | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
|
||
/** | ||
* | ||
*/ | ||
internal class CertificateInfoTest { | ||
|
||
@Before | ||
fun setUp() { | ||
mockkStatic(Base64::class) | ||
every { Base64.decode(any<String>(), any()) } answers { | ||
java.util.Base64.getDecoder().decode(it.invocation.args[0] as String) | ||
} | ||
} | ||
|
||
@After | ||
fun tearDown() { | ||
unmockkAll() | ||
} | ||
|
||
@Test | ||
fun testIndexOf() { | ||
val certList = mutableListOf<CertificateInfo>() | ||
|
||
// different signatures of the same fingerprints | ||
// simulates updates with different data | ||
val jsonData = listOf("""{"fingerprints": [ | ||
{ | ||
"name" : "github.com", | ||
"fingerprint" : "kqN/vV4hpTqVxxbhFE9EL1grlND6/Gc+tnF6TrUaiKc=", | ||
"expires" : 1710460799, | ||
"signature" : "MEQCIElYrNRc/RnIJTFM9Or90Op+5YfEc+OA0JCOzEdewx07AiAm/xAKMkhu9k9mXNFNyUSB/A1FbnqKEegpEpsugY5Z/Q==" | ||
} | ||
]}""", """{"fingerprints": [ | ||
{ | ||
"name" : "github.com", | ||
"fingerprint" : "kqN/vV4hpTqVxxbhFE9EL1grlND6/Gc+tnF6TrUaiKc=", | ||
"expires" : 1710460799, | ||
"signature" : "MEUCIQDGSbss+QVvF5juP3y7/DkUPYIWopabdHrZETGqYMctLgIgX7aKQ8+22AIlmuWczXZKze4w20ycsKzaps4reobjikA=" | ||
} | ||
]}""") | ||
|
||
// we add the first one | ||
val response = CertStore.GSON.fromJson(jsonData[0], GetFingerprintResponse::class.java) | ||
Assert.assertEquals(1, response.fingerprints.size) | ||
for (entry in response.fingerprints) { | ||
certList.add(CertificateInfo(entry)) | ||
} | ||
|
||
for (json in jsonData) { | ||
val resp = CertStore.GSON.fromJson(json, GetFingerprintResponse::class.java) | ||
Assert.assertEquals(1, resp.fingerprints.size) | ||
val ci = CertificateInfo(resp.fingerprints[0]) | ||
// the data should pre already present | ||
val idx = certList.indexOf(ci) | ||
assertTrue(idx != -1) | ||
assertTrue(certList.contains(ci)) | ||
} | ||
} | ||
} |
Oops, something went wrong.