-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUpstreamTestSuite.kt
56 lines (51 loc) · 1.95 KB
/
UpstreamTestSuite.kt
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
/**
* Copyright 2022 The kage Authors. All rights reserved. Use of this source code is governed by
* either an Apache 2.0 or MIT license at your discretion, that can be found in the LICENSE-APACHE
* or LICENSE-MIT files respectively.
*/
package kage
import com.github.michaelbull.result.getError
import com.github.michaelbull.result.runCatching
import com.google.common.truth.Truth.assertThat
import java.io.ByteArrayOutputStream
import java.nio.file.Files
import java.nio.file.Paths
import java.security.MessageDigest
import kage.kage.test.utils.TestSuite
import kage.kage.utils.mapToUpstreamExpect
import kage.test.utils.Expect.Success
import kage.test.utils.PayloadHash
import kotlin.io.path.name
import org.junit.jupiter.api.DynamicTest
import org.junit.jupiter.api.TestFactory
import org.junit.jupiter.api.fail
class UpstreamTestSuite {
private val testFixtureRoot = Paths.get("src", "test", "resources", "CCTV", "age", "testdata")
@TestFactory
fun generateTests(): List<DynamicTest> {
return Files.newDirectoryStream(testFixtureRoot).map { path ->
val contents = path.toFile().readBytes()
DynamicTest.dynamicTest(path.name) {
val suite = TestSuite.parse(contents)
val expect = suite.expect
val baos = ByteArrayOutputStream()
val result = runCatching {
Age.decryptStream(suite.identities, suite.testContent.inputStream(), baos)
}
val error = result.getError()
if (error == null) {
if (expect != Success) fail("expected $expect, got success")
suite.payloadHash?.let { expectedHash ->
val md = MessageDigest.getInstance("SHA-256")
val payloadHash = PayloadHash(md.digest(baos.toByteArray()))
assertThat(payloadHash.bytes).isEqualTo(expectedHash.bytes)
}
} else {
error.printStackTrace()
val actual = mapToUpstreamExpect(error)
assertThat(actual).isEqualTo(expect)
}
}
}
}
}