Signed-off-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com>
92 lines
2.1 KiB
Go
92 lines
2.1 KiB
Go
package ar
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"io"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
fileContent = []byte("google is content")
|
|
|
|
originalDate, _ = time.Parse(time.RFC3339Nano, "2025-03-11T23:52:08.000Z")
|
|
controlFile, _ = base64.StdEncoding.DecodeString("ITxhcmNoPgp0ZXN0LnR4dC8gICAgICAgMTc0MTczNzEyOCAgMTAwMCAgMTAwMCAgMTAwNjQ0ICAxNyAgICAgICAgYApnb29nbGUgaXMgY29udGVudAo=")
|
|
)
|
|
|
|
func TestWriter(t *testing.T) {
|
|
// Buffer to write to
|
|
buff := &bytes.Buffer{}
|
|
ar, err := NewWriter(buff)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
_, err = ar.WriteFile(fileContent, &Header{
|
|
Filename: "test.txt/",
|
|
Owner: 1000,
|
|
Group: 1000,
|
|
Mode: 100644,
|
|
Size: int64(len(fileContent)),
|
|
ModeTime: originalDate,
|
|
})
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
if !bytes.Equal(controlFile, buff.Bytes()) {
|
|
t.Errorf("file content mismatch\nexpected: \t%q\nactual: \t%q", controlFile, buff.Bytes())
|
|
return
|
|
}
|
|
}
|
|
|
|
func TestReader(t *testing.T) {
|
|
// Buffer to read from
|
|
buff := bytes.NewBuffer(controlFile)
|
|
ar, err := NewReader(buff)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
header, err := ar.Next()
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
if header.Filename != "test.txt" {
|
|
t.Errorf("filename mismatch\nexpected: \t%q\nactual: \t%q", "test.txt", header.Filename)
|
|
return
|
|
}
|
|
if header.Owner != 1000 {
|
|
t.Errorf("owner mismatch\nexpected: \t%d\nactual: \t%d", 1000, header.Owner)
|
|
return
|
|
}
|
|
if header.Group != 1000 {
|
|
t.Errorf("group mismatch\nexpected: \t%d\nactual: \t%d", 1000, header.Group)
|
|
return
|
|
}
|
|
if header.Mode != 100644 {
|
|
t.Errorf("mode mismatch\nexpected: \t%d\nactual: \t%d", 100644, header.Mode)
|
|
return
|
|
}
|
|
if header.Size != int64(len(fileContent)) {
|
|
t.Errorf("size mismatch\nexpected: \t%d\nactual: \t%d", len(fileContent), header.Size)
|
|
return
|
|
}
|
|
if header.ModeTime.Local() != originalDate.Local() {
|
|
t.Errorf("time mismatch\nexpected: \t%v\nactual: \t%v", originalDate, header.ModeTime)
|
|
return
|
|
}
|
|
|
|
content, err := io.ReadAll(ar)
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
} else if !bytes.Equal(content, fileContent) {
|
|
t.Errorf("file content mismatch\nexpected: \t%q\nactual: \t%q", fileContent, content)
|
|
return
|
|
}
|
|
}
|