mirror of
https://github.com/StackExchange/dnscontrol.git
synced 2024-05-11 05:55:12 +00:00
CHORE: Vendor go-powershell (#2837)
This commit is contained in:
9
pkg/powershell/utils/quote.go
Normal file
9
pkg/powershell/utils/quote.go
Normal file
@@ -0,0 +1,9 @@
|
||||
// Copyright (c) 2017 Gorillalabs. All rights reserved.
|
||||
|
||||
package utils
|
||||
|
||||
import "strings"
|
||||
|
||||
func QuoteArg(s string) string {
|
||||
return "'" + strings.Replace(s, "'", "\"", -1) + "'"
|
||||
}
|
28
pkg/powershell/utils/quote_test.go
Normal file
28
pkg/powershell/utils/quote_test.go
Normal file
@@ -0,0 +1,28 @@
|
||||
// Copyright (c) 2017 Gorillalabs. All rights reserved.
|
||||
|
||||
package utils
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestQuotingArguments(t *testing.T) {
|
||||
testcases := [][]string{
|
||||
{"", "''"},
|
||||
{"test", "'test'"},
|
||||
{"two words", "'two words'"},
|
||||
{"quo\"ted", "'quo\"ted'"},
|
||||
{"quo'ted", "'quo\"ted'"},
|
||||
{"quo\\'ted", "'quo\\\"ted'"},
|
||||
{"quo\"t'ed", "'quo\"t\"ed'"},
|
||||
{"es\\caped", "'es\\caped'"},
|
||||
{"es`caped", "'es`caped'"},
|
||||
{"es\\`caped", "'es\\`caped'"},
|
||||
}
|
||||
|
||||
for i, testcase := range testcases {
|
||||
quoted := QuoteArg(testcase[0])
|
||||
|
||||
if quoted != testcase[1] {
|
||||
t.Errorf("test %02d failed: input '%s', expected %s, actual %s", i+1, testcase[0], testcase[1], quoted)
|
||||
}
|
||||
}
|
||||
}
|
20
pkg/powershell/utils/rand.go
Normal file
20
pkg/powershell/utils/rand.go
Normal file
@@ -0,0 +1,20 @@
|
||||
// Copyright (c) 2017 Gorillalabs. All rights reserved.
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
func CreateRandomString(bytes int) string {
|
||||
c := bytes
|
||||
b := make([]byte, c)
|
||||
|
||||
_, err := rand.Read(b)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return hex.EncodeToString(b)
|
||||
}
|
16
pkg/powershell/utils/rand_test.go
Normal file
16
pkg/powershell/utils/rand_test.go
Normal file
@@ -0,0 +1,16 @@
|
||||
// Copyright (c) 2017 Gorillalabs. All rights reserved.
|
||||
|
||||
package utils
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestRandomStrings(t *testing.T) {
|
||||
r1 := CreateRandomString(8)
|
||||
r2 := CreateRandomString(8)
|
||||
|
||||
if r1 == r2 {
|
||||
t.Error("Failed to create random strings: The two generated strings are identical.")
|
||||
} else if len(r1) != 16 {
|
||||
t.Errorf("Expected the random string to contain 16 characters, but got %d.", len(r1))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user