github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/lfs/pointer_test.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package lfs
     7  
     8  import (
     9  	"path"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestStringContent(t *testing.T) {
    17  	p := Pointer{Oid: "4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393", Size: 1234}
    18  	expected := "version https://git-lfs.github.com/spec/v1\noid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\nsize 1234\n"
    19  	assert.Equal(t, expected, p.StringContent())
    20  }
    21  
    22  func TestRelativePath(t *testing.T) {
    23  	p := Pointer{Oid: "4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393"}
    24  	expected := path.Join("4d", "7a", "214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393")
    25  	assert.Equal(t, expected, p.RelativePath())
    26  
    27  	p2 := Pointer{Oid: "4d7a"}
    28  	assert.Equal(t, "4d7a", p2.RelativePath())
    29  }
    30  
    31  func TestIsValid(t *testing.T) {
    32  	p := Pointer{}
    33  	assert.False(t, p.IsValid())
    34  
    35  	p = Pointer{Oid: "123"}
    36  	assert.False(t, p.IsValid())
    37  
    38  	p = Pointer{Oid: "z4cb57646c54a297c9807697e80a30946f79a4b82cb079d2606847825b1812cc"}
    39  	assert.False(t, p.IsValid())
    40  
    41  	p = Pointer{Oid: "94cb57646c54a297c9807697e80a30946f79a4b82cb079d2606847825b1812cc"}
    42  	assert.True(t, p.IsValid())
    43  
    44  	p = Pointer{Oid: "94cb57646c54a297c9807697e80a30946f79a4b82cb079d2606847825b1812cc", Size: -1}
    45  	assert.False(t, p.IsValid())
    46  }
    47  
    48  func TestGeneratePointer(t *testing.T) {
    49  	p, err := GeneratePointer(strings.NewReader("GitBundle"))
    50  	assert.NoError(t, err)
    51  	assert.True(t, p.IsValid())
    52  	assert.Equal(t, "94cb57646c54a297c9807697e80a30946f79a4b82cb079d2606847825b1812cc", p.Oid)
    53  	assert.Equal(t, int64(5), p.Size)
    54  }
    55  
    56  func TestReadPointerFromBuffer(t *testing.T) {
    57  	p, err := ReadPointerFromBuffer([]byte{})
    58  	assert.ErrorIs(t, err, ErrMissingPrefix)
    59  	assert.False(t, p.IsValid())
    60  
    61  	p, err = ReadPointerFromBuffer([]byte("test"))
    62  	assert.ErrorIs(t, err, ErrMissingPrefix)
    63  	assert.False(t, p.IsValid())
    64  
    65  	p, err = ReadPointerFromBuffer([]byte("version https://git-lfs.github.com/spec/v1\n"))
    66  	assert.ErrorIs(t, err, ErrInvalidStructure)
    67  	assert.False(t, p.IsValid())
    68  
    69  	p, err = ReadPointerFromBuffer([]byte("version https://git-lfs.github.com/spec/v1\noid sha256:4d7a\nsize 1234\n"))
    70  	assert.ErrorIs(t, err, ErrInvalidOIDFormat)
    71  	assert.False(t, p.IsValid())
    72  
    73  	p, err = ReadPointerFromBuffer([]byte("version https://git-lfs.github.com/spec/v1\noid sha256:4d7a2146z4ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\nsize 1234\n"))
    74  	assert.ErrorIs(t, err, ErrInvalidOIDFormat)
    75  	assert.False(t, p.IsValid())
    76  
    77  	p, err = ReadPointerFromBuffer([]byte("version https://git-lfs.github.com/spec/v1\noid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\ntest 1234\n"))
    78  	assert.Error(t, err)
    79  	assert.False(t, p.IsValid())
    80  
    81  	p, err = ReadPointerFromBuffer([]byte("version https://git-lfs.github.com/spec/v1\noid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\nsize test\n"))
    82  	assert.Error(t, err)
    83  	assert.False(t, p.IsValid())
    84  
    85  	p, err = ReadPointerFromBuffer([]byte("version https://git-lfs.github.com/spec/v1\noid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\nsize 1234\n"))
    86  	assert.NoError(t, err)
    87  	assert.True(t, p.IsValid())
    88  	assert.Equal(t, "4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393", p.Oid)
    89  	assert.Equal(t, int64(1234), p.Size)
    90  
    91  	p, err = ReadPointerFromBuffer([]byte("version https://git-lfs.github.com/spec/v1\noid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\nsize 1234\ntest"))
    92  	assert.NoError(t, err)
    93  	assert.True(t, p.IsValid())
    94  	assert.Equal(t, "4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393", p.Oid)
    95  	assert.Equal(t, int64(1234), p.Size)
    96  }
    97  
    98  func TestReadPointer(t *testing.T) {
    99  	p, err := ReadPointer(strings.NewReader("version https://git-lfs.github.com/spec/v1\noid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\nsize 1234\n"))
   100  	assert.NoError(t, err)
   101  	assert.True(t, p.IsValid())
   102  	assert.Equal(t, "4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393", p.Oid)
   103  	assert.Equal(t, int64(1234), p.Size)
   104  }