2014-10-15 09:59:19 -05:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2016-08-27 13:44:46 -05:00
|
|
|
"bytes"
|
2014-10-15 09:59:19 -05:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-08-27 13:44:46 -05:00
|
|
|
type bufWrapper struct {
|
|
|
|
buf [64]byte
|
|
|
|
pointer []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func doublePointerBytes() []byte {
|
|
|
|
o := &bufWrapper{}
|
|
|
|
o.pointer = o.buf[0:10]
|
|
|
|
return o.pointer[0:1]
|
|
|
|
}
|
|
|
|
|
2014-10-15 09:59:19 -05:00
|
|
|
func TestCreateBlobFromBuffer(t *testing.T) {
|
2016-08-27 12:21:05 -05:00
|
|
|
t.Parallel()
|
2014-10-15 09:59:19 -05:00
|
|
|
repo := createTestRepo(t)
|
2015-04-24 05:59:29 -05:00
|
|
|
defer cleanupTestRepo(t, repo)
|
2014-10-15 09:59:19 -05:00
|
|
|
|
|
|
|
id, err := repo.CreateBlobFromBuffer(make([]byte, 0))
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
if id.String() != "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391" {
|
|
|
|
t.Fatal("Empty buffer did not deliver empty blob id")
|
|
|
|
}
|
2016-08-27 13:44:46 -05:00
|
|
|
|
2020-07-30 07:07:05 -05:00
|
|
|
tests := []struct {
|
|
|
|
data []byte
|
|
|
|
isBinary bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
data: []byte("hello there"),
|
|
|
|
isBinary: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
data: doublePointerBytes(),
|
|
|
|
isBinary: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
data := tt.data
|
2016-08-27 13:44:46 -05:00
|
|
|
id, err = repo.CreateBlobFromBuffer(data)
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
blob, err := repo.LookupBlob(id)
|
|
|
|
checkFatal(t, err)
|
2016-08-27 13:47:41 -05:00
|
|
|
if !bytes.Equal(blob.Contents(), data) {
|
2016-08-27 13:44:46 -05:00
|
|
|
t.Fatal("Loaded bytes don't match original bytes:",
|
2016-08-27 13:47:41 -05:00
|
|
|
blob.Contents(), "!=", data)
|
2016-08-27 13:44:46 -05:00
|
|
|
}
|
2020-07-30 07:07:05 -05:00
|
|
|
want := tt.isBinary
|
|
|
|
if got := blob.IsBinary(); got != want {
|
|
|
|
t.Fatalf("IsBinary() = %v, want %v", got, want)
|
|
|
|
}
|
2016-08-27 13:44:46 -05:00
|
|
|
}
|
2014-10-15 09:59:19 -05:00
|
|
|
}
|