2025-09-28 22:04:48 +02:00
package global
2019-05-27 21:20:54 +02:00
import (
2024-05-18 18:59:29 +02:00
"context"
2022-12-02 19:36:43 +01:00
"os"
2020-09-30 22:32:14 +02:00
"path/filepath"
2024-05-18 18:59:29 +02:00
"strings"
2019-05-27 21:20:54 +02:00
"testing"
rtest "github.com/restic/restic/internal/test"
)
2020-09-30 22:32:14 +02:00
func TestReadRepo ( t * testing . T ) {
2023-05-18 17:44:56 +02:00
tempDir := rtest . TempDir ( t )
2020-09-30 22:32:14 +02:00
// test --repo option
2025-09-28 22:04:48 +02:00
var gopts Options
2025-09-18 22:19:38 +02:00
gopts . Repo = tempDir
2025-09-28 22:21:59 +02:00
repo , err := readRepo ( gopts )
2020-09-30 22:32:14 +02:00
rtest . OK ( t , err )
rtest . Equals ( t , tempDir , repo )
// test --repository-file option
foo := filepath . Join ( tempDir , "foo" )
2022-12-02 19:36:43 +01:00
err = os . WriteFile ( foo , [ ] byte ( tempDir + "\n" ) , 0666 )
2020-09-30 22:32:14 +02:00
rtest . OK ( t , err )
2025-09-28 22:04:48 +02:00
var gopts2 Options
2025-09-18 22:19:38 +02:00
gopts2 . RepositoryFile = foo
2025-09-28 22:21:59 +02:00
repo , err = readRepo ( gopts2 )
2020-09-30 22:32:14 +02:00
rtest . OK ( t , err )
rtest . Equals ( t , tempDir , repo )
2025-09-28 22:04:48 +02:00
var gopts3 Options
2025-09-18 22:19:38 +02:00
gopts3 . RepositoryFile = foo + "-invalid"
2025-09-28 22:21:59 +02:00
_ , err = readRepo ( gopts3 )
2020-09-30 22:32:14 +02:00
if err == nil {
t . Fatal ( "must not read repository path from invalid file path" )
}
}
2024-05-18 18:59:29 +02:00
func TestReadEmptyPassword ( t * testing . T ) {
2025-09-28 22:04:48 +02:00
opts := Options { InsecureNoPassword : true }
2025-09-28 22:22:22 +02:00
password , err := readPassword ( context . TODO ( ) , opts , "test" )
2024-05-18 18:59:29 +02:00
rtest . OK ( t , err )
rtest . Equals ( t , "" , password , "got unexpected password" )
2025-09-28 21:44:40 +02:00
opts . Password = "invalid"
2025-09-28 22:22:22 +02:00
_ , err = readPassword ( context . TODO ( ) , opts , "test" )
2024-05-18 18:59:29 +02:00
rtest . Assert ( t , strings . Contains ( err . Error ( ) , "must not be specified together with providing a password via a cli option or environment variable" ) , "unexpected error message, got %v" , err )
}