From 5910e5551f3edc22bad8aefb5a7b158c7ceb8fd2 Mon Sep 17 00:00:00 2001
From: Jun Kimura <jun.kimura@datachain.jp>
Date: Wed, 20 Nov 2024 16:08:32 +0900
Subject: [PATCH] fix parallel test execution

Signed-off-by: Jun Kimura <jun.kimura@datachain.jp>
---
 scripts/gencert.sh         | 12 ++++-----
 test/CertificateTest.t.sol | 55 +++++++++++++++++++++-----------------
 2 files changed, 37 insertions(+), 30 deletions(-)

diff --git a/scripts/gencert.sh b/scripts/gencert.sh
index f9781f7..bae21a6 100755
--- a/scripts/gencert.sh
+++ b/scripts/gencert.sh
@@ -1,7 +1,6 @@
 #!/bin/env bash
 set -eu
 
-out_certs_dir=./test/.tmp/testcerts
 signing_rsa_bits=2048
 signing_exponent=65537
 
@@ -41,23 +40,24 @@ function gen_ecdsa_signing_cert() {
 }
 
 function usage() {
-    echo "Usage: $0 {gen_rsa_root_cert|gen_rsa_signing_cert|gen_ecdsa_root_cert|gen_ecdsa_signing_cert}"
+    echo "Usage: $0 <out_certs_dir> {gen_rsa_root_cert|gen_rsa_signing_cert|gen_ecdsa_root_cert|gen_ecdsa_signing_cert}"
     exit 1
 }
 
-if [ $# -eq 0 ]; then
+if [ $# -lt 2 ]; then
     usage
 fi
 
+out_certs_dir=$1
 mkdir -p ${out_certs_dir}
 
-case "$1" in
+case "$2" in
     "gen_rsa_root_cert")
         gen_rsa_root_cert
         ;;
     "gen_rsa_signing_cert")
-        signing_rsa_bits=$2
-        signing_exponent=$3
+        signing_rsa_bits=$3
+        signing_exponent=$4
         gen_rsa_signing_cert
         ;;
     "gen_ecdsa_root_cert")
diff --git a/test/CertificateTest.t.sol b/test/CertificateTest.t.sol
index f880615..ac88e95 100644
--- a/test/CertificateTest.t.sol
+++ b/test/CertificateTest.t.sol
@@ -71,7 +71,7 @@ contract CertificateTest is BasicTest {
         );
     }
 
-    string internal constant testCertsDir = "./test/.tmp/testcerts";
+    string internal constant testCertsBaseDir = "./test/.tmp/testcerts";
     string internal constant genCertCmd = "./scripts/gencert.sh";
 
     struct RSAParamsCase {
@@ -80,6 +80,7 @@ contract CertificateTest is BasicTest {
     }
 
     function testValidSigningCerts() public {
+        string memory testCertsDir = string(abi.encodePacked(testCertsBaseDir, "/", "valid_signing_certs"));
         RSAParamsCase[4] memory cases = [
             RSAParamsCase("2048", "65537"),
             RSAParamsCase("2048", "3"),
@@ -87,10 +88,10 @@ contract CertificateTest is BasicTest {
             RSAParamsCase("4096", "3")
         ];
         vm.warp(1795167418);
-        cleanupTestCerts();
-        genRsaRootCert();
+        cleanupTestCerts(testCertsDir);
+        genRsaRootCert(testCertsDir);
         for (uint256 i = 0; i < cases.length; i++) {
-            genRsaSigningCert(cases[i].bits, cases[i].exponent);
+            genRsaSigningCert(testCertsDir, cases[i].bits, cases[i].exponent);
             AVRValidator.RSAParams memory rootParams = AVRValidator.verifyRootCACert(
                 vm.readFileBinary(string(abi.encodePacked(testCertsDir, "/root.crt.der")))
             );
@@ -103,19 +104,21 @@ contract CertificateTest is BasicTest {
     }
 
     function testInvalidRootCerts() public {
+        string memory testCertsDir = string(abi.encodePacked(testCertsBaseDir, "/", "invalid_root_certs"));
         vm.warp(1795167418);
-        cleanupTestCerts();
-        genEcdsaRootCert();
+        cleanupTestCerts(testCertsDir);
+        genEcdsaRootCert(testCertsDir);
 
         vm.expectRevert();
         AVRValidator.verifyRootCACert(vm.readFileBinary(string(abi.encodePacked(testCertsDir, "/root.crt.der"))));
     }
 
     function testInvalidSigningCerts() public {
+        string memory testCertsDir = string(abi.encodePacked(testCertsBaseDir, "/", "invalid_signing_certs"));
         vm.warp(1795167418);
-        cleanupTestCerts();
-        genRsaRootCert();
-        genEcdsaSigningCert();
+        cleanupTestCerts(testCertsDir);
+        genRsaRootCert(testCertsDir);
+        genEcdsaSigningCert(testCertsDir);
 
         AVRValidator.RSAParams memory rootParams =
             AVRValidator.verifyRootCACert(vm.readFileBinary(string(abi.encodePacked(testCertsDir, "/root.crt.der"))));
@@ -127,7 +130,7 @@ contract CertificateTest is BasicTest {
         );
     }
 
-    function cleanupTestCerts() internal {
+    function cleanupTestCerts(string memory testCertsDir) internal {
         string[] memory cmd = new string[](3);
         cmd[0] = "rm";
         cmd[1] = "-rf";
@@ -135,33 +138,37 @@ contract CertificateTest is BasicTest {
         runCmd(cmd);
     }
 
-    function genRsaRootCert() internal {
-        string[] memory cmd = new string[](2);
+    function genRsaRootCert(string memory testCertsDir) internal {
+        string[] memory cmd = new string[](3);
         cmd[0] = genCertCmd;
-        cmd[1] = "gen_rsa_root_cert";
+        cmd[1] = testCertsDir;
+        cmd[2] = "gen_rsa_root_cert";
         runCmd(cmd);
     }
 
-    function genRsaSigningCert(string memory bits, string memory exponent) internal {
-        string[] memory cmd = new string[](4);
+    function genRsaSigningCert(string memory testCertsDir, string memory bits, string memory exponent) internal {
+        string[] memory cmd = new string[](5);
         cmd[0] = genCertCmd;
-        cmd[1] = "gen_rsa_signing_cert";
-        cmd[2] = bits;
-        cmd[3] = exponent;
+        cmd[1] = testCertsDir;
+        cmd[2] = "gen_rsa_signing_cert";
+        cmd[3] = bits;
+        cmd[4] = exponent;
         runCmd(cmd);
     }
 
-    function genEcdsaRootCert() internal {
-        string[] memory cmd = new string[](2);
+    function genEcdsaRootCert(string memory testCertsDir) internal {
+        string[] memory cmd = new string[](3);
         cmd[0] = genCertCmd;
-        cmd[1] = "gen_ecdsa_root_cert";
+        cmd[1] = testCertsDir;
+        cmd[2] = "gen_ecdsa_root_cert";
         runCmd(cmd);
     }
 
-    function genEcdsaSigningCert() internal {
-        string[] memory cmd = new string[](2);
+    function genEcdsaSigningCert(string memory testCertsDir) internal {
+        string[] memory cmd = new string[](3);
         cmd[0] = genCertCmd;
-        cmd[1] = "gen_ecdsa_signing_cert";
+        cmd[1] = testCertsDir;
+        cmd[2] = "gen_ecdsa_signing_cert";
         runCmd(cmd);
     }