From 2ed9e36cb32354ba874162f63fff624fc9926a62 Mon Sep 17 00:00:00 2001 From: DRON-666 <64691982+DRON-666@users.noreply.github.com> Date: Fri, 30 Apr 2021 18:01:40 +0300 Subject: [PATCH] vss: Add tests for "provider" option --- internal/fs/fs_local_vss_test.go | 88 ++++++++++++++++++++++++++++++-- 1 file changed, 84 insertions(+), 4 deletions(-) diff --git a/internal/fs/fs_local_vss_test.go b/internal/fs/fs_local_vss_test.go index 6beb35b98..cff881151 100644 --- a/internal/fs/fs_local_vss_test.go +++ b/internal/fs/fs_local_vss_test.go @@ -9,6 +9,7 @@ import ( "testing" "time" + ole "github.com/go-ole/go-ole" "github.com/restic/restic/internal/options" ) @@ -18,6 +19,9 @@ func matchStrings(ptrs []string, strs []string) bool { } for i, p := range ptrs { + if p == "" { + return false + } matched, err := regexp.MatchString(p, strs[i]) if err != nil { panic(err) @@ -48,6 +52,7 @@ func TestVSSConfig(t *testing.T) { type config struct { excludeAllMountPoints bool timeout time.Duration + provider string } setTests := []struct { input options.Options @@ -55,19 +60,23 @@ func TestVSSConfig(t *testing.T) { }{ { options.Options{ - "vss.timeout": "6h38m42s", + "vss.timeout": "6h38m42s", + "vss.provider": "Ms", }, config{ - timeout: 23922000000000, + timeout: 23922000000000, + provider: "Ms", }, }, { options.Options{ "vss.excludeallmountpoints": "t", + "vss.provider": "{b5946137-7b9f-4925-af80-51abd60b20d5}", }, config{ excludeAllMountPoints: true, timeout: 120000000000, + provider: "{b5946137-7b9f-4925-af80-51abd60b20d5}", }, }, { @@ -75,9 +84,11 @@ func TestVSSConfig(t *testing.T) { "vss.excludeallmountpoints": "0", "vss.excludevolumes": "", "vss.timeout": "120s", + "vss.provider": "Microsoft Software Shadow Copy provider 1.0", }, config{ - timeout: 120000000000, + timeout: 120000000000, + provider: "Microsoft Software Shadow Copy provider 1.0", }, }, } @@ -98,7 +109,8 @@ func TestVSSConfig(t *testing.T) { dst := NewLocalVss(errorHandler, messageHandler, cfg) if dst.excludeAllMountPoints != test.output.excludeAllMountPoints || - dst.excludeVolumes != nil || dst.timeout != test.output.timeout { + dst.excludeVolumes != nil || dst.timeout != test.output.timeout || + dst.provider != test.output.provider { t.Fatalf("wrong result, want:\n %#v\ngot:\n %#v", test.output, dst) } }) @@ -205,3 +217,71 @@ func TestParseMountPoints(t *testing.T) { }) } } + +func TestParseProvider(t *testing.T) { + msProvider := ole.NewGUID("{b5946137-7b9f-4925-af80-51abd60b20d5}") + setTests := []struct { + provider string + id *ole.GUID + result string + }{ + { + "", + ole.IID_NULL, + "", + }, + { + "mS", + msProvider, + "", + }, + { + "{B5946137-7b9f-4925-Af80-51abD60b20d5}", + msProvider, + "", + }, + { + "Microsoft Software Shadow Copy provider 1.0", + msProvider, + "", + }, + { + "{04560982-3d7d-4bbc-84f7-0712f833a28f}", + nil, + `invalid VSS provider "{04560982-3d7d-4bbc-84f7-0712f833a28f}"`, + }, + { + "non-existent provider", + nil, + `invalid VSS provider "non-existent provider"`, + }, + } + + _ = ole.CoInitializeEx(0, ole.COINIT_MULTITHREADED) + + for i, test := range setTests { + t.Run(fmt.Sprintf("test-%d", i), func(t *testing.T) { + id, err := getProviderID(test.provider) + + if err != nil && id != nil { + t.Fatalf("err!=nil but id=%v", id) + } + + if test.result != "" || err != nil { + var result string + if err != nil { + result = err.Error() + } + matched, err := regexp.MatchString(test.result, result) + if err != nil { + panic(err) + } + if !matched || test.result == "" { + t.Fatalf("wrong result, want:\n %#v\ngot:\n %#v", test.result, result) + } + } else if !ole.IsEqualGUID(id, test.id) { + t.Fatalf("wrong id, want:\n %s\ngot:\n %s", test.id.String(), id.String()) + } + }) + } +}