Created
April 28, 2024 20:35
-
-
Save christoofar/052aab15da187f79882380026fe8abac to your computer and use it in GitHub Desktop.
Testing the variable-sized archive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package archive | |
import ( | |
"os" | |
"testing" | |
"time" | |
) | |
func TestNewArchiveRecordWithData(t *testing.T) { | |
// Create an archive record with data | |
t.Log("TestNewArchiveRecordWithData") | |
record := NewArchiveRecord() | |
record.SetFileTime(time.Now()) | |
record.SetFileName("test.txt") | |
record.SetPassValidation(true, "") | |
record.Data = []byte("This is a test file.") | |
record.SetFileSize(int64(len(record.Data))) | |
f, err := os.Create("test_archive.dat") | |
if err != nil { | |
t.Error(err) | |
} | |
// Convert the record to binary bytes | |
binaryData, err := record.ToBytes() | |
if err != nil { | |
t.Error(err) | |
} | |
// Write the binary data to the file | |
_, err = f.Write(binaryData) | |
if err != nil { | |
t.Error(err) | |
} | |
// Write the Data to the file | |
_, err = f.Write(record.Data) | |
if err != nil { | |
t.Error(err) | |
} | |
// Move the file back to the beginning | |
_, err = f.Seek(0, 0) | |
if err != nil { | |
t.Error(err) | |
} | |
// Read the first 533 bytes of the file | |
fileData := make([]byte, 533) | |
_, err = f.Read(fileData) | |
if err != nil { | |
t.Error(err) | |
} | |
// Convert the binary data to an ArchiveRecord | |
record = NewArchiveRecord() | |
err = record.FromBytes(fileData) | |
if err != nil { | |
t.Error(err) | |
} | |
// Read the data from the file of the length of the FileSize | |
data := make([]byte, record.FileSize) | |
_, err = f.Read(data) | |
if err != nil { | |
t.Error(err) | |
} | |
record.Data = data | |
// Check the record | |
if record.Marker != 0x1C { | |
t.Errorf("Marker is %v, expected 0x1C", record.Marker) | |
} | |
if record.Fnlf != 0x0A { | |
t.Errorf("Fnlf is %v, expected 0x0A", record.Fnlf) | |
} | |
if record.Fntimelf != 0x0A { | |
t.Errorf("Fntimelf is %v, expected 0x0A", record.Fntimelf) | |
} | |
if record.ReadPassValidation() != true { | |
t.Errorf("PassValidation is %v, expected 0", record.PassValidation) | |
} | |
if record.Fnvalidationlf != 0x0A { | |
t.Errorf("Fnvalidationlf is %v, expected 0x0A", record.Fnvalidationlf) | |
} | |
if record.TrailingMarker != 0x1F { | |
t.Errorf("TrailingMarker is %v, expected 0x1F", record.TrailingMarker) | |
} | |
if record.ReadFileName() != "test.txt" { | |
t.Errorf("FileName is %v, expected test.txt", record.FileName) | |
} | |
if record.ReadFileSize() != 20 { | |
t.Errorf("FileSize is %v, expected 20", record.FileSize) | |
} | |
if string(record.Data) != "This is a test file." { | |
t.Errorf("Data is %v, expected This is a test file.", string(record.Data)) | |
} | |
// Remove the test file | |
err = os.Remove("test_archive.dat") | |
if err != nil { | |
t.Error(err) | |
} | |
} | |
// TestNewArchiveRecord tests the NewArchiveRecord function and the ToBytes and FromBytes methods | |
// of the ArchiveRecord struct to make sure they can convert the struct to and from binary data, | |
// and that when written and read to a file, the data is correct. | |
func TestNewArchiveRecord(t *testing.T) { | |
// Create 10 new ArchiveRecords and store them to a file | |
t.Log("TestNewArchiveRecord") | |
records := make([]*ArchiveRecord, 10) | |
for i := 0; i < 10; i++ { | |
records[i] = NewArchiveRecord() | |
} | |
f, err := os.Create("test_archive.dat") | |
if err != nil { | |
t.Error(err) | |
} | |
// Convert the records to binary bytes | |
var data []byte | |
for _, record := range records { | |
binaryData, err := record.ToBytes() | |
if err != nil { | |
t.Error(err) | |
} | |
data = append(data, binaryData...) | |
} | |
// Write the binary data to the file | |
_, err = f.Write(data) | |
if err != nil { | |
t.Error(err) | |
} | |
// Close the file | |
defer f.Close() | |
// Read the file back in and convert the binary data to ArchiveRecords | |
f, err = os.Open("test_archive.dat") | |
if err != nil { | |
t.Error(err) | |
} | |
defer f.Close() | |
// Read the file data as one big block of bytes | |
fileInfo, err := f.Stat() | |
if err != nil { | |
t.Error(err) | |
} | |
fileSize := fileInfo.Size() | |
fileData := make([]byte, fileSize) | |
_, err = f.Read(fileData) | |
if err != nil { | |
t.Error(err) | |
} | |
// Convert the binary data to ArchiveRecords by splitting the | |
// file data into 533 byte chunks and converting each chunk to an ArchiveRecord | |
records = make([]*ArchiveRecord, 0) | |
for i := 0; i < len(fileData); i += 533 { | |
record := NewArchiveRecord() | |
err = record.FromBytes(fileData[i : i+533]) | |
if err != nil { | |
t.Error(err) | |
} | |
records = append(records, record) | |
} | |
// Check the records | |
for i, record := range records { | |
if record.Marker != 0x1C { | |
t.Errorf("record %d: Marker is %v, expected 0x1C", i, record.Marker) | |
} | |
if record.Fnlf != 0x0A { | |
t.Errorf("record %d: Fnlf is %v, expected 0x0A", i, record.Fnlf) | |
} | |
if record.Fntimelf != 0x0A { | |
t.Errorf("record %d: Fntimelf is %v, expected 0x0A", i, record.Fntimelf) | |
} | |
if record.PassValidation != 0 { | |
t.Errorf("record %d: PassValidation is %v, expected 0", i, record.PassValidation) | |
} | |
if record.Fnvalidationlf != 0x0A { | |
t.Errorf("record %d: Fnvalidationlf is %v, expected 0x0A", i, record.Fnvalidationlf) | |
} | |
// The last byte of the record header should be 0x1F. If it is not at this position then we have corrupted data | |
if record.TrailingMarker != 0x1F { | |
t.Errorf("record %d: TrailingMarker is %v, expected 0x1F", i, record.TrailingMarker) | |
} | |
// Check the FileName and ValidationFailReason to make sure the spaces are correct | |
for j := 0; j < 256; j++ { | |
if record.FileName[j] != 0x20 { | |
t.Errorf("record %d: FileName[%d] is %v, expected 0x20", i, j, record.FileName[j]) | |
} | |
if record.ValidationFailReason[j] != 0x20 { | |
t.Errorf("record %d: ValidationFailReason[%d] is %v, expected 0x20", i, j, record.ValidationFailReason[j]) | |
} | |
} | |
} | |
// Remove the test file | |
err = os.Remove("test_archive.dat") | |
if err != nil { | |
t.Error(err) | |
} | |
} | |
func TestArchive(t *testing.T) { | |
t.Log("TestArchive") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment