Compare commits

..

No commits in common. "0bf26d2019a1ba766215ef77c671759bac30cef1" and "72ffe8456d5c59ffc81818852af6d51d86c2656d" have entirely different histories.

5 changed files with 18 additions and 139 deletions

View File

@ -83,12 +83,6 @@ func Test_newRootCA(t *testing.T) {
require.Greater(t, len(keyPEM), 0) 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) { func Test_parseCertificate(t *testing.T) {
caPEM, _, err := newRootCA(&rootTestConfig) caPEM, _, err := newRootCA(&rootTestConfig)
require.NoError(t, err) require.NoError(t, err)
@ -132,12 +126,3 @@ func Test_rootCA_WithClientCert(t *testing.T) {
require.NotNil(t, clientSrv.PEM()) require.NotNil(t, clientSrv.PEM())
require.Greater(t, len(clientSrv.PEM()), 0) 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,13 +11,7 @@ type writer struct {
dirPath string dirPath string
} }
// WriteFile writes file into `w writer` directory.
// Returns outputPath and error
func (w writer) WriteFile(filename string, data []byte) (string, error) { func (w writer) WriteFile(filename string, data []byte) (string, error) {
if filename == "" {
return "", fmt.Errorf("filename cannot be empty")
}
if w.dirPath == "" { if w.dirPath == "" {
return "", fmt.Errorf("export directory cannot be empty") return "", fmt.Errorf("export directory cannot be empty")
} }
@ -33,10 +27,6 @@ func (w writer) WriteFile(filename string, data []byte) (string, error) {
} }
func NewWriter(dirPath string) io.WriterIface { func NewWriter(dirPath string) io.WriterIface {
return newWriter(dirPath)
}
func newWriter(dirPath string) *writer {
return &writer{ return &writer{
dirPath: dirPath, dirPath: dirPath,
} }

View File

@ -1,110 +0,0 @@
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,10 +29,26 @@ type Subject struct {
PostalCode string PostalCode string
} }
/*
var ( var (
subjectKeyId = []byte{1, 2, 3, 4, 6} subjectKeyId = []byte{1, 2, 3, 4, 6}
extKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth} extKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth}
keyUsage = x509.KeyUsageDigitalSignature 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,7 +1,5 @@
package io package io
type WriterIface interface { type WriterIface interface {
// WriteFile writes file into `w writer` directory.
// Returns outputPath and error
WriteFile(filename string, data []byte) (string, error) WriteFile(filename string, data []byte) (string, error)
} }