2020-09-08 15:45:26 -04:00
|
|
|
package d2loader
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-09-23 13:30:54 -04:00
|
|
|
"log"
|
2020-09-08 15:45:26 -04:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2loader/asset"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-09-09 14:35:52 -04:00
|
|
|
sourcePathA = "testdata/A"
|
|
|
|
sourcePathB = "testdata/B"
|
|
|
|
sourcePathC = "testdata/C"
|
|
|
|
sourcePathD = "testdata/D.mpq"
|
|
|
|
commonFile = "common.txt"
|
|
|
|
exclusiveA = "exclusive_a.txt"
|
|
|
|
exclusiveB = "exclusive_b.txt"
|
|
|
|
exclusiveC = "exclusive_c.txt"
|
|
|
|
exclusiveD = "exclusive_d.txt"
|
2020-09-14 14:47:11 -04:00
|
|
|
subdirCommonD = "dir\\common.txt"
|
2020-09-09 14:35:52 -04:00
|
|
|
badSourcePath = "/x/y/z.mpq"
|
|
|
|
badFilePath = "a/bad/file/path.txt"
|
2020-09-08 15:45:26 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLoader_NewLoader(t *testing.T) {
|
2020-09-14 14:47:11 -04:00
|
|
|
loader := NewLoader(nil)
|
2020-09-08 15:45:26 -04:00
|
|
|
|
|
|
|
if loader.Cache == nil {
|
|
|
|
t.Error("loader should not be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoader_AddSource(t *testing.T) {
|
2020-09-14 14:47:11 -04:00
|
|
|
loader := NewLoader(nil)
|
2020-09-08 15:45:26 -04:00
|
|
|
|
2020-09-09 14:35:52 -04:00
|
|
|
sourceA, errA := loader.AddSource(sourcePathA)
|
|
|
|
sourceB, errB := loader.AddSource(sourcePathB)
|
|
|
|
sourceC, errC := loader.AddSource(sourcePathC)
|
|
|
|
sourceD, errD := loader.AddSource(sourcePathD)
|
|
|
|
sourceE, errE := loader.AddSource(badSourcePath)
|
2020-09-08 15:45:26 -04:00
|
|
|
|
2020-09-09 14:35:52 -04:00
|
|
|
if errA != nil {
|
|
|
|
t.Error(errA)
|
|
|
|
}
|
|
|
|
|
|
|
|
if errB != nil {
|
|
|
|
t.Error(errB)
|
|
|
|
}
|
|
|
|
|
|
|
|
if errC != nil {
|
|
|
|
t.Error(errC)
|
|
|
|
}
|
|
|
|
|
|
|
|
if errD != nil {
|
|
|
|
t.Error(errD)
|
|
|
|
}
|
|
|
|
|
|
|
|
if errE == nil {
|
|
|
|
t.Error("expecting error on bad file path")
|
|
|
|
}
|
|
|
|
|
|
|
|
if sourceA.String() != sourcePathA {
|
2020-09-08 15:45:26 -04:00
|
|
|
t.Error("source path not the same as what we added")
|
|
|
|
}
|
|
|
|
|
2020-09-09 14:35:52 -04:00
|
|
|
if sourceB.String() != sourcePathB {
|
2020-09-08 15:45:26 -04:00
|
|
|
t.Error("source path not the same as what we added")
|
|
|
|
}
|
|
|
|
|
2020-09-09 14:35:52 -04:00
|
|
|
if sourceC.String() != sourcePathC {
|
2020-09-08 15:45:26 -04:00
|
|
|
t.Error("source path not the same as what we added")
|
|
|
|
}
|
|
|
|
|
2020-09-09 14:35:52 -04:00
|
|
|
if sourceD.String() != sourcePathD {
|
2020-09-08 15:45:26 -04:00
|
|
|
t.Error("source path not the same as what we added")
|
|
|
|
}
|
2020-09-09 14:35:52 -04:00
|
|
|
|
|
|
|
if sourceE != nil {
|
|
|
|
t.Error("source for bad path should be nil")
|
|
|
|
}
|
2020-09-08 15:45:26 -04:00
|
|
|
}
|
|
|
|
|
2020-10-22 01:12:06 -04:00
|
|
|
// nolint:gocyclo // this is just a test, not a big deal if we ignore linter here
|
2020-09-08 15:45:26 -04:00
|
|
|
func TestLoader_Load(t *testing.T) {
|
2020-09-14 14:47:11 -04:00
|
|
|
loader := NewLoader(nil)
|
2020-09-08 15:45:26 -04:00
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
_, err := loader.AddSource(sourcePathB) // we expect files common to any source to come from here
|
|
|
|
if err != nil {
|
|
|
|
t.Fail()
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2020-10-22 01:12:06 -04:00
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
_, err = loader.AddSource(sourcePathD)
|
|
|
|
if err != nil {
|
|
|
|
t.Fail()
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2020-10-22 01:12:06 -04:00
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
_, err = loader.AddSource(sourcePathA)
|
|
|
|
if err != nil {
|
|
|
|
t.Fail()
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2020-10-22 01:12:06 -04:00
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
_, err = loader.AddSource(sourcePathC)
|
|
|
|
if err != nil {
|
|
|
|
t.Fail()
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2020-10-22 01:12:06 -04:00
|
|
|
|
2020-09-09 14:35:52 -04:00
|
|
|
entryCommon, errCommon := loader.Load(commonFile) // common file exists in all three Sources
|
2020-09-08 15:45:26 -04:00
|
|
|
|
|
|
|
entryA, errA := loader.Load(exclusiveA) // each source has a file exclusive to itself
|
|
|
|
entryB, errB := loader.Load(exclusiveB)
|
|
|
|
entryC, errC := loader.Load(exclusiveC)
|
|
|
|
entryD, errD := loader.Load(exclusiveD)
|
2020-09-14 14:47:11 -04:00
|
|
|
entryDsubdir, errDsubdir := loader.Load(subdirCommonD)
|
2020-09-08 15:45:26 -04:00
|
|
|
|
|
|
|
_, expectedError := loader.Load(badFilePath) // we expect an Error for this bad file path
|
|
|
|
|
|
|
|
if entryCommon == nil || errCommon != nil {
|
|
|
|
t.Error("common entry should exist")
|
2020-09-09 14:35:52 -04:00
|
|
|
} else if entryCommon.Source() != loader.Sources[0] {
|
2020-09-08 15:45:26 -04:00
|
|
|
t.Error("common entry should come from the first loader source")
|
|
|
|
}
|
|
|
|
|
|
|
|
if errA != nil || errB != nil || errC != nil || errD != nil {
|
|
|
|
t.Error("files exclusive to each source don't exist")
|
|
|
|
}
|
|
|
|
|
2020-09-14 14:47:11 -04:00
|
|
|
if errDsubdir != nil {
|
|
|
|
t.Error("mpq subdir entry not found")
|
|
|
|
}
|
|
|
|
|
2020-09-08 15:45:26 -04:00
|
|
|
if expectedError == nil {
|
|
|
|
t.Error("expected Error for nonexistant file path")
|
|
|
|
}
|
|
|
|
|
|
|
|
var result []byte
|
|
|
|
|
|
|
|
buffer := make([]byte, 1)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
entry asset.Asset
|
|
|
|
data string
|
|
|
|
}{
|
2020-09-09 14:35:52 -04:00
|
|
|
{entryCommon, "b"}, // sourcePathB is loaded first, we expect a "b"
|
2020-09-08 15:45:26 -04:00
|
|
|
{entryA, "a"},
|
|
|
|
{entryB, "b"},
|
|
|
|
{entryC, "c"},
|
|
|
|
{entryD, "d"},
|
2020-09-14 14:47:11 -04:00
|
|
|
{entryDsubdir, "d"},
|
2020-09-08 15:45:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for idx := range tests {
|
|
|
|
entry, expected := tests[idx].entry, tests[idx].data
|
|
|
|
|
|
|
|
result = make([]byte, 0)
|
|
|
|
|
|
|
|
for {
|
|
|
|
if bytesRead, err := entry.Read(buffer); err != nil || bytesRead == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, buffer...)
|
|
|
|
}
|
|
|
|
|
|
|
|
got := string(result[0])
|
|
|
|
|
|
|
|
if got != expected {
|
|
|
|
fmtStr := "unexpected data in file %s, loaded from source `%s`: expected `%s`, got `%s`"
|
|
|
|
msg := fmt.Sprintf(fmtStr, entry.Path(), entry.Source(), expected, got)
|
|
|
|
t.Error(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|