Skip to content

Commit

Permalink
feat: implement ReadDataFromFile() to all file reads
Browse files Browse the repository at this point in the history
  • Loading branch information
Sashwat-K committed May 13, 2024
1 parent 3c178b8 commit d96f02a
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 245 deletions.
22 changes: 5 additions & 17 deletions attestation/attestation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package attestation

import (
"fmt"
"io"
"os"
"testing"

"github.com/stretchr/testify/assert"

gen "github.com/Sashwat-K/lib-hpcr/common/general"
)

const (
Expand All @@ -16,29 +16,17 @@ const (

// Testcase to check if HpcrGetAttestationRecords() retrieves attestation records from encrypted data
func TestHpcrGetAttestationRecords(t *testing.T) {
encChecksumPath, err := os.Open(encryptedChecksumPath)
if err != nil {
fmt.Println(err)
}
defer encChecksumPath.Close()

encChecksum, err := io.ReadAll(encChecksumPath)
if err != nil {
fmt.Println(err)
}

privateKey, err := os.Open(privateKeyPath)
encChecksum, err := gen.ReadDataFromFile(encryptedChecksumPath)
if err != nil {
fmt.Println(err)
}
defer privateKey.Close()

privateKeyData, err := io.ReadAll(privateKey)
privateKeyData, err := gen.ReadDataFromFile(privateKeyPath)
if err != nil {
fmt.Println(err)
}

result, err := HpcrGetAttestationRecords(string(encChecksum), string(privateKeyData))
result, err := HpcrGetAttestationRecords(encChecksum, privateKeyData)
if err != nil {
fmt.Println(err)
}
Expand Down
46 changes: 11 additions & 35 deletions common/decrypt/decrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package decrypt

import (
"fmt"
"io"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"

gen "github.com/Sashwat-K/lib-hpcr/common/general"
)

const (
Expand All @@ -17,31 +17,19 @@ const (

// Testcase to check if DecryptPassword() is able to decrypt password
func TestDecryptPassword(t *testing.T) {
encChecksumPath, err := os.Open(encryptedChecksumPath)
if err != nil {
fmt.Println(err)
}
defer encChecksumPath.Close()

encChecksum, err := io.ReadAll(encChecksumPath)
encChecksum, err := gen.ReadDataFromFile(encryptedChecksumPath)
if err != nil {
fmt.Println(err)
}

encodedEncryptedData := strings.Split(string(encChecksum), ".")[1]

privateKey, err := os.Open(privateKeyPath)
if err != nil {
fmt.Println(err)
}
defer privateKey.Close()
encodedEncryptedData := strings.Split(encChecksum, ".")[1]

privateKeyData, err := io.ReadAll(privateKey)
privateKeyData, err := gen.ReadDataFromFile(privateKeyPath)
if err != nil {
fmt.Println(err)
}

_, err = DecryptPassword(encodedEncryptedData, string(privateKeyData))
_, err = DecryptPassword(encodedEncryptedData, privateKeyData)
if err != nil {
fmt.Println(err)
}
Expand All @@ -51,32 +39,20 @@ func TestDecryptPassword(t *testing.T) {

// Testcase to check if DecryptWorkload() is able to decrypt workload
func TestDecryptWorkload(t *testing.T) {
encChecksumPath, err := os.Open(encryptedChecksumPath)
encChecksum, err := gen.ReadDataFromFile(encryptedChecksumPath)
if err != nil {
fmt.Println(err)
}
defer encChecksumPath.Close()

encChecksum, err := io.ReadAll(encChecksumPath)
if err != nil {
fmt.Println(err)
}

encodedEncryptedPassword := strings.Split(string(encChecksum), ".")[1]
encodedEncryptedData := strings.Split(string(encChecksum), ".")[2]

privateKey, err := os.Open(privateKeyPath)
if err != nil {
fmt.Println(err)
}
defer privateKey.Close()
encodedEncryptedPassword := strings.Split(encChecksum, ".")[1]
encodedEncryptedData := strings.Split(encChecksum, ".")[2]

privateKeyData, err := io.ReadAll(privateKey)
privateKeyData, err := gen.ReadDataFromFile(privateKeyPath)
if err != nil {
fmt.Println(err)
}

password, err := DecryptPassword(encodedEncryptedPassword, string(privateKeyData))
password, err := DecryptPassword(encodedEncryptedPassword, privateKeyData)
if err != nil {
fmt.Println(err)
}
Expand Down
116 changes: 18 additions & 98 deletions common/encrypt/encrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
_ "embed"
"encoding/json"
"fmt"
"io"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -44,34 +42,22 @@ func TestOpensslCheck(t *testing.T) {
}

func TestGeneratePublicKey(t *testing.T) {
privateKeyFile, err := os.Open(simplePrivateKeyPath)
privateKey, err := gen.ReadDataFromFile(simplePrivateKeyPath)
if err != nil {
fmt.Println(err)
}
defer privateKeyFile.Close()

privateKey, err := io.ReadAll(privateKeyFile)
publicKey, err := gen.ReadDataFromFile(simplePublicKeyPath)
if err != nil {
fmt.Println(err)
}

publicKeyFile, err := os.Open(simplePublicKeyPath)
result, err := GeneratePublicKey(privateKey)
if err != nil {
fmt.Println(err)
}
defer publicKeyFile.Close()

publicKey, err := io.ReadAll(publicKeyFile)
if err != nil {
fmt.Println(err)
}

result, err := GeneratePublicKey(string(privateKey))
if err != nil {
fmt.Println(err)
}

assert.Equal(t, result, string(publicKey))
assert.Equal(t, result, publicKey)
assert.NoError(t, err)
}

Expand Down Expand Up @@ -108,13 +94,7 @@ func TestEncryptPassword(t *testing.T) {
func TestEncryptContract(t *testing.T) {
var contractMap map[string]interface{}

file, err := os.Open(simpleContractPath)
if err != nil {
fmt.Println(err)
}
defer file.Close()

contract, err := io.ReadAll(file)
contract, err := gen.ReadDataFromFile(simpleContractPath)
if err != nil {
fmt.Println(err)
}
Expand Down Expand Up @@ -163,13 +143,7 @@ func TestEncryptString(t *testing.T) {
func TestEncryptFinalStr(t *testing.T) {
var contractMap map[string]interface{}

file, err := os.Open(simpleContractPath)
if err != nil {
fmt.Println(err)
}
defer file.Close()

contract, err := io.ReadAll(file)
contract, err := gen.ReadDataFromFile(simpleContractPath)
if err != nil {
fmt.Println(err)
}
Expand Down Expand Up @@ -208,35 +182,17 @@ func TestEncryptFinalStr(t *testing.T) {

// Testcase to check if CreateSigningCert() is able to create signing certificate with CSR parameters
func TestCreateSigningCert(t *testing.T) {
privateKeyPath, err := os.Open(samplePrivateKeyPath)
if err != nil {
fmt.Println(err)
}
defer privateKeyPath.Close()

privateKey, err := io.ReadAll(privateKeyPath)
if err != nil {
fmt.Println(err)
}

cacertPath, err := os.Open(sampleCaCertPath)
if err != nil {
fmt.Println(err)
}
defer cacertPath.Close()

cacert, err := io.ReadAll(cacertPath)
privateKey, err := gen.ReadDataFromFile(samplePrivateKeyPath)
if err != nil {
fmt.Println(err)
}

caKeyPath, err := os.Open(sampleCaKeyPath)
cacert, err := gen.ReadDataFromFile(sampleCaCertPath)
if err != nil {
fmt.Println(err)
}
defer caKeyPath.Close()

caKey, err := io.ReadAll(caKeyPath)
caKey, err := gen.ReadDataFromFile(sampleCaKeyPath)
if err != nil {
fmt.Println(err)
}
Expand All @@ -255,7 +211,7 @@ func TestCreateSigningCert(t *testing.T) {
fmt.Println(err)
}

signingCert, err := CreateSigningCert(string(privateKey), string(cacert), string(caKey), string(csrDataStr), "", sampleExpiryDays)
signingCert, err := CreateSigningCert(privateKey, cacert, caKey, string(csrDataStr), "", sampleExpiryDays)
if err != nil {
fmt.Println(err)
}
Expand All @@ -266,51 +222,27 @@ func TestCreateSigningCert(t *testing.T) {

// Testcase to check if CreateSigningCert() is able to create signing certificate using CSR file
func TestCreateSigningCertCsrFile(t *testing.T) {
privateKeyPath, err := os.Open(samplePrivateKeyPath)
if err != nil {
fmt.Println(err)
}
defer privateKeyPath.Close()

privateKey, err := io.ReadAll(privateKeyPath)
if err != nil {
fmt.Println(err)
}

cacertPath, err := os.Open(sampleCaCertPath)
if err != nil {
fmt.Println(err)
}
defer cacertPath.Close()

cacert, err := io.ReadAll(cacertPath)
privateKey, err := gen.ReadDataFromFile(samplePrivateKeyPath)
if err != nil {
fmt.Println(err)
}

caKeyPath, err := os.Open(sampleCaKeyPath)
cacert, err := gen.ReadDataFromFile(sampleCaCertPath)
if err != nil {
fmt.Println(err)
}
defer caKeyPath.Close()

caKey, err := io.ReadAll(caKeyPath)
caKey, err := gen.ReadDataFromFile(sampleCaKeyPath)
if err != nil {
fmt.Println(err)
}

csrFilePath, err := os.Open(sampleCsrFilePath)
csr, err := gen.ReadDataFromFile(sampleCsrFilePath)
if err != nil {
fmt.Println(err)
}
defer csrFilePath.Close()

csr, err := io.ReadAll(csrFilePath)
if err != nil {
fmt.Println(err)
}

signingCert, err := CreateSigningCert(string(privateKey), string(cacert), string(caKey), "", string(csr), sampleExpiryDays)
signingCert, err := CreateSigningCert(privateKey, cacert, caKey, "", csr, sampleExpiryDays)
if err != nil {
fmt.Println(err)
}
Expand All @@ -323,24 +255,12 @@ func TestCreateSigningCertCsrFile(t *testing.T) {
func TestSignContract(t *testing.T) {
var contractMap map[string]interface{}

file, err := os.Open(simpleContractPath)
if err != nil {
fmt.Println(err)
}
defer file.Close()

contract, err := io.ReadAll(file)
if err != nil {
fmt.Println(err)
}

privateKeyPath, err := os.Open(samplePrivateKeyPath)
contract, err := gen.ReadDataFromFile(simpleContractPath)
if err != nil {
fmt.Println(err)
}
defer privateKeyPath.Close()

privateKey, err := io.ReadAll(privateKeyPath)
privateKey, err := gen.ReadDataFromFile(samplePrivateKeyPath)
if err != nil {
fmt.Println(err)
}
Expand Down Expand Up @@ -378,7 +298,7 @@ func TestSignContract(t *testing.T) {

finalEnv := EncryptFinalStr(encryptedPassword, encryptedEnv)

workloadEnvSignature, err := SignContract(finalWorkload, finalEnv, string(privateKey))
workloadEnvSignature, err := SignContract(finalWorkload, finalEnv, privateKey)
if err != nil {
fmt.Println(err)
}
Expand Down
Loading

0 comments on commit d96f02a

Please sign in to comment.