2021-04-09 01:25:57 +03:00
// Copyright 2021 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2021-04-09 01:25:57 +03:00
package lfs
import (
"path"
"strings"
"testing"
"github.com/stretchr/testify/assert"
2024-07-30 22:41:10 +03:00
"github.com/stretchr/testify/require"
2021-04-09 01:25:57 +03:00
)
func TestStringContent ( t * testing . T ) {
p := Pointer { Oid : "4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393" , Size : 1234 }
expected := "version https://git-lfs.github.com/spec/v1\noid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\nsize 1234\n"
2021-06-07 08:27:09 +03:00
assert . Equal ( t , expected , p . StringContent ( ) )
2021-04-09 01:25:57 +03:00
}
func TestRelativePath ( t * testing . T ) {
p := Pointer { Oid : "4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393" }
expected := path . Join ( "4d" , "7a" , "214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393" )
2021-06-07 08:27:09 +03:00
assert . Equal ( t , expected , p . RelativePath ( ) )
2021-04-09 01:25:57 +03:00
p2 := Pointer { Oid : "4d7a" }
2021-06-07 08:27:09 +03:00
assert . Equal ( t , "4d7a" , p2 . RelativePath ( ) )
2021-04-09 01:25:57 +03:00
}
func TestIsValid ( t * testing . T ) {
p := Pointer { }
assert . False ( t , p . IsValid ( ) )
p = Pointer { Oid : "123" }
assert . False ( t , p . IsValid ( ) )
p = Pointer { Oid : "z4cb57646c54a297c9807697e80a30946f79a4b82cb079d2606847825b1812cc" }
assert . False ( t , p . IsValid ( ) )
p = Pointer { Oid : "94cb57646c54a297c9807697e80a30946f79a4b82cb079d2606847825b1812cc" }
assert . True ( t , p . IsValid ( ) )
p = Pointer { Oid : "94cb57646c54a297c9807697e80a30946f79a4b82cb079d2606847825b1812cc" , Size : - 1 }
assert . False ( t , p . IsValid ( ) )
}
func TestGeneratePointer ( t * testing . T ) {
p , err := GeneratePointer ( strings . NewReader ( "Gitea" ) )
2024-07-30 22:41:10 +03:00
require . NoError ( t , err )
2021-04-09 01:25:57 +03:00
assert . True ( t , p . IsValid ( ) )
2021-06-07 08:27:09 +03:00
assert . Equal ( t , "94cb57646c54a297c9807697e80a30946f79a4b82cb079d2606847825b1812cc" , p . Oid )
assert . Equal ( t , int64 ( 5 ) , p . Size )
2021-04-09 01:25:57 +03:00
}
func TestReadPointerFromBuffer ( t * testing . T ) {
p , err := ReadPointerFromBuffer ( [ ] byte { } )
2024-07-30 22:41:10 +03:00
require . ErrorIs ( t , err , ErrMissingPrefix )
2021-04-09 01:25:57 +03:00
assert . False ( t , p . IsValid ( ) )
p , err = ReadPointerFromBuffer ( [ ] byte ( "test" ) )
2024-07-30 22:41:10 +03:00
require . ErrorIs ( t , err , ErrMissingPrefix )
2021-04-09 01:25:57 +03:00
assert . False ( t , p . IsValid ( ) )
p , err = ReadPointerFromBuffer ( [ ] byte ( "version https://git-lfs.github.com/spec/v1\n" ) )
2024-07-30 22:41:10 +03:00
require . ErrorIs ( t , err , ErrInvalidStructure )
2021-04-09 01:25:57 +03:00
assert . False ( t , p . IsValid ( ) )
p , err = ReadPointerFromBuffer ( [ ] byte ( "version https://git-lfs.github.com/spec/v1\noid sha256:4d7a\nsize 1234\n" ) )
2024-07-30 22:41:10 +03:00
require . ErrorIs ( t , err , ErrInvalidOIDFormat )
2021-04-09 01:25:57 +03:00
assert . False ( t , p . IsValid ( ) )
p , err = ReadPointerFromBuffer ( [ ] byte ( "version https://git-lfs.github.com/spec/v1\noid sha256:4d7a2146z4ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\nsize 1234\n" ) )
2024-07-30 22:41:10 +03:00
require . ErrorIs ( t , err , ErrInvalidOIDFormat )
2021-04-09 01:25:57 +03:00
assert . False ( t , p . IsValid ( ) )
p , err = ReadPointerFromBuffer ( [ ] byte ( "version https://git-lfs.github.com/spec/v1\noid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\ntest 1234\n" ) )
2024-07-30 22:41:10 +03:00
require . Error ( t , err )
2021-04-09 01:25:57 +03:00
assert . False ( t , p . IsValid ( ) )
p , err = ReadPointerFromBuffer ( [ ] byte ( "version https://git-lfs.github.com/spec/v1\noid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\nsize test\n" ) )
2024-07-30 22:41:10 +03:00
require . Error ( t , err )
2021-04-09 01:25:57 +03:00
assert . False ( t , p . IsValid ( ) )
p , err = ReadPointerFromBuffer ( [ ] byte ( "version https://git-lfs.github.com/spec/v1\noid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\nsize 1234\n" ) )
2024-07-30 22:41:10 +03:00
require . NoError ( t , err )
2021-04-09 01:25:57 +03:00
assert . True ( t , p . IsValid ( ) )
2021-06-07 08:27:09 +03:00
assert . Equal ( t , "4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393" , p . Oid )
assert . Equal ( t , int64 ( 1234 ) , p . Size )
2021-04-09 01:25:57 +03:00
p , err = ReadPointerFromBuffer ( [ ] byte ( "version https://git-lfs.github.com/spec/v1\noid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\nsize 1234\ntest" ) )
2024-07-30 22:41:10 +03:00
require . NoError ( t , err )
2021-04-09 01:25:57 +03:00
assert . True ( t , p . IsValid ( ) )
2021-06-07 08:27:09 +03:00
assert . Equal ( t , "4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393" , p . Oid )
assert . Equal ( t , int64 ( 1234 ) , p . Size )
2021-04-09 01:25:57 +03:00
}
func TestReadPointer ( t * testing . T ) {
p , err := ReadPointer ( strings . NewReader ( "version https://git-lfs.github.com/spec/v1\noid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393\nsize 1234\n" ) )
2024-07-30 22:41:10 +03:00
require . NoError ( t , err )
2021-04-09 01:25:57 +03:00
assert . True ( t , p . IsValid ( ) )
2021-06-07 08:27:09 +03:00
assert . Equal ( t , "4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393" , p . Oid )
assert . Equal ( t , int64 ( 1234 ) , p . Size )
2021-04-09 01:25:57 +03:00
}