- Deleted old deb822_encode.go and deb822_types.go files. - Introduced new deb822_encode.go with improved Marshall function and Writer struct for encoding. - Added deb822_decode.go for decoding functionality with enhanced error handling. - Created deb822_rawdata.go to define RawData type for handling raw deb822 values. - Implemented Description type in deb822/datatype/description.go for structured description handling. - Updated Package struct in dpkg/header.go to use new Description type. - Refactored UnmarshalBinary method in dpkg/header.go to utilize new deb822 decoding logic. - Added comprehensive tests for encoding and decoding in deb822_encode_test.go and deb822_decode_test.go. - Removed internal scanner package as it was no longer needed. Signed-off-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com>
87 lines
2.1 KiB
Go
87 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 := new(bytes.Buffer)
|
|
_, err := NewWriter(buff).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
|
|
}
|
|
}
|