summaryrefslogtreecommitdiffstats
path: root/src/internal/obscuretestdata
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-16 19:23:18 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-16 19:23:18 +0000
commit43a123c1ae6613b3efeed291fa552ecd909d3acf (patch)
treefd92518b7024bc74031f78a1cf9e454b65e73665 /src/internal/obscuretestdata
parentInitial commit. (diff)
downloadgolang-1.20-43a123c1ae6613b3efeed291fa552ecd909d3acf.tar.xz
golang-1.20-43a123c1ae6613b3efeed291fa552ecd909d3acf.zip
Adding upstream version 1.20.14.upstream/1.20.14upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/internal/obscuretestdata')
-rw-r--r--src/internal/obscuretestdata/obscuretestdata.go65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/internal/obscuretestdata/obscuretestdata.go b/src/internal/obscuretestdata/obscuretestdata.go
new file mode 100644
index 0000000..d54d3f6
--- /dev/null
+++ b/src/internal/obscuretestdata/obscuretestdata.go
@@ -0,0 +1,65 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Package obscuretestdata contains functionality used by tests to more easily
+// work with testdata that must be obscured primarily due to
+// golang.org/issue/34986.
+package obscuretestdata
+
+import (
+ "encoding/base64"
+ "io"
+ "os"
+)
+
+// Rot13 returns the rot13 encoding or decoding of its input.
+func Rot13(data []byte) []byte {
+ out := make([]byte, len(data))
+ copy(out, data)
+ for i, c := range out {
+ switch {
+ case 'A' <= c && c <= 'M' || 'a' <= c && c <= 'm':
+ out[i] = c + 13
+ case 'N' <= c && c <= 'Z' || 'n' <= c && c <= 'z':
+ out[i] = c - 13
+ }
+ }
+ return out
+}
+
+// DecodeToTempFile decodes the named file to a temporary location.
+// If successful, it returns the path of the decoded file.
+// The caller is responsible for ensuring that the temporary file is removed.
+func DecodeToTempFile(name string) (path string, err error) {
+ f, err := os.Open(name)
+ if err != nil {
+ return "", err
+ }
+ defer f.Close()
+
+ tmp, err := os.CreateTemp("", "obscuretestdata-decoded-")
+ if err != nil {
+ return "", err
+ }
+ if _, err := io.Copy(tmp, base64.NewDecoder(base64.StdEncoding, f)); err != nil {
+ tmp.Close()
+ os.Remove(tmp.Name())
+ return "", err
+ }
+ if err := tmp.Close(); err != nil {
+ os.Remove(tmp.Name())
+ return "", err
+ }
+ return tmp.Name(), nil
+}
+
+// ReadFile reads the named file and returns its decoded contents.
+func ReadFile(name string) ([]byte, error) {
+ f, err := os.Open(name)
+ if err != nil {
+ return nil, err
+ }
+ defer f.Close()
+ return io.ReadAll(base64.NewDecoder(base64.StdEncoding, f))
+}