Compare commits

...

4 Commits

Author SHA1 Message Date
Urko 0bf26d2019 feat: fix force error after delete directory 2023-03-05 00:10:46 +01:00
Urko b049c6a779 fix lint 2023-03-05 00:07:48 +01:00
Urko 0bf7b63286 feat: test coverage more than 85% 2023-03-05 00:05:00 +01:00
Urko 89136cae59 feat: test coverage more than 80% 2023-03-05 00:04:41 +01:00
5 changed files with 139 additions and 18 deletions

View File

@ -83,6 +83,12 @@ func Test_newRootCA(t *testing.T) {
require.Greater(t, len(keyPEM), 0)
}
func Test_newRootCAError(t *testing.T) {
_, _, err := newRootCA(&ca.CaConfig{})
require.Error(t, err)
}
func Test_parseCertificate(t *testing.T) {
caPEM, _, err := newRootCA(&rootTestConfig)
require.NoError(t, err)
@ -126,3 +132,12 @@ func Test_rootCA_WithClientCert(t *testing.T) {
require.NotNil(t, clientSrv.PEM())
require.Greater(t, len(clientSrv.PEM()), 0)
}
func Test_rootCA_WithClientCertEror(t *testing.T) {
rootCert := rootCA{
caPEM: nil,
}
_, err := rootCert.WithClientCert(&clientTestConfig)
require.Error(t, err)
}

View File

@ -11,7 +11,13 @@ type writer struct {
dirPath string
}
// WriteFile writes file into `w writer` directory.
// Returns outputPath and error
func (w writer) WriteFile(filename string, data []byte) (string, error) {
if filename == "" {
return "", fmt.Errorf("filename cannot be empty")
}
if w.dirPath == "" {
return "", fmt.Errorf("export directory cannot be empty")
}
@ -27,6 +33,10 @@ func (w writer) WriteFile(filename string, data []byte) (string, error) {
}
func NewWriter(dirPath string) io.WriterIface {
return newWriter(dirPath)
}
func newWriter(dirPath string) *writer {
return &writer{
dirPath: dirPath,
}

110
internal/io/writer_test.go Normal file
View File

@ -0,0 +1,110 @@
package io
import (
"log"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
testWriterPath = "testPath"
testWriterPathError = "test Path, @:ººººº\\/.Ç*⁺´+"
testFileContent = "test data"
testFileName = "test-file.txt"
workingDir = ""
)
func deleteAllDirs() error {
return os.RemoveAll(testWriterPath)
}
func init() {
err := deleteAllDirs()
if err != nil {
log.Fatalln("deleteAllDirs: ", err)
}
workingDir, err = os.Getwd()
if err != nil {
log.Fatalln("os.Getwd: ", err)
}
}
func Test_newWriter(t *testing.T) {
w := newWriter(testWriterPath)
require.NotNil(t, w)
}
func TestNewWriter(t *testing.T) {
w := NewWriter(testWriterPath)
require.NotNil(t, w)
}
func Test_writer_WriteFile(t *testing.T) {
err := deleteAllDirs()
require.NoError(t, err)
defer func(t *testing.T) {
err := deleteAllDirs()
require.NoError(t, err)
}(t)
w := newWriter(testWriterPath)
wgot, err := w.WriteFile(testFileName, []byte(testFileContent))
require.NoError(t, err)
btsReaded, err := os.ReadFile(wgot)
require.NoError(t, err)
require.NotEmpty(t, btsReaded)
require.Equal(t, string(btsReaded), testFileContent)
}
func Test_writer_WriteFileError(t *testing.T) {
err := deleteAllDirs()
require.NoError(t, err)
defer func(t *testing.T) {
err := deleteAllDirs()
require.NoError(t, err)
}(t)
w := newWriter("")
_, err = w.WriteFile(testFileName, []byte(testFileContent))
assert.Error(t, err)
w = newWriter(testWriterPath)
_, err = w.WriteFile("", []byte(testFileContent))
require.Error(t, err)
go func(t *testing.T) {
err := deleteAllDirs()
go func(err error) {
require.NoError(t, err)
}(err)
for {
f, err := os.Stat(testWriterPath)
if err != nil {
if strings.Contains(err.Error(), "no such file or directory") {
continue
}
t.Errorf("os.Stat error: %s", err)
}
err = os.RemoveAll(testWriterPath)
require.NotNil(t, f)
log.Println("exists deleting dir")
require.NoError(t, err, "should not throw error because dir exists")
return
}
}(t)
_, err = w.WriteFile(testFileName, nil)
assert.Error(t, err)
w = newWriter(testWriterPathError)
_, err = w.WriteFile(testFileContent, []byte(testFileContent))
require.Error(t, err)
}

View File

@ -29,26 +29,10 @@ type Subject struct {
PostalCode string
}
/*
var (
subjectKeyId = []byte{1, 2, 3, 4, 6}
extKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth}
keyUsage = x509.KeyUsageDigitalSignature
)
func NewDefaultConfig() *ClientCertConfig {
return &ClientCertConfig{
Serial: big.NewInt(12321),
Subject: Subject{
Organization: "",
Country: "",
Province: "",
Locality: "",
StreetAddress: "",
PostalCode: "",
},
Duration: time.Duration(time.Hour * 24 * 365),
SubjectKeyId: subjectKeyId,
ExtKeyUsage: extKeyUsage,
KeyUsage: keyUsage,
}
}
*/

View File

@ -1,5 +1,7 @@
package io
type WriterIface interface {
// WriteFile writes file into `w writer` directory.
// Returns outputPath and error
WriteFile(filename string, data []byte) (string, error)
}