[Pkg-privacy-commits] [golang-goptlib] 06/20: Imported Upstream version 0.2
Ximin Luo
infinity0 at moszumanska.debian.org
Sat Aug 22 10:04:06 UTC 2015
This is an automated email from the git hooks/post-receive script.
infinity0 pushed a commit to branch master
in repository golang-goptlib.
commit f0b92c72276ba32bee2cd9d40aa4261f84d20eae
Author: Ximin Luo <infinity0 at pwned.gg>
Date: Sat Aug 16 22:00:45 2014 +0100
Imported Upstream version 0.2
---
pt.go | 35 +++++++++++++++++++++++++++++++++--
pt_test.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 92 insertions(+), 2 deletions(-)
diff --git a/pt.go b/pt.go
index cb9b42f..8630cae 100644
--- a/pt.go
+++ b/pt.go
@@ -15,7 +15,7 @@
// if err != nil {
// return err
// }
-// // do something with conn and or.
+// // do something with conn and remote.
// return nil
// }
// func acceptLoop(ln *pt.SocksListener) error {
@@ -322,6 +322,19 @@ func getManagedTransportVer() (string, error) {
return "", versionError("no-version")
}
+// Return the directory name in the TOR_PT_STATE_LOCATION environment variable,
+// creating it if it doesn't exist. Returns non-nil error if
+// TOR_PT_STATE_LOCATION is not set or if there is an error creating the
+// directory.
+func MakeStateDir() (string, error) {
+ dir, err := getenvRequired("TOR_PT_STATE_LOCATION")
+ if err != nil {
+ return "", err
+ }
+ err = os.MkdirAll(dir, 0700)
+ return dir, err
+}
+
// Get the intersection of the method names offered by Tor and those in
// methodNames. This function reads the environment variable
// TOR_PT_CLIENT_TRANSPORTS.
@@ -339,13 +352,22 @@ func getClientTransports(star []string) ([]string, error) {
// This structure is returned by ClientSetup. It consists of a list of method
// names.
type ClientInfo struct {
- MethodNames []string
+ MethodNames []string
}
// Check the client pluggable transports environment, emitting an error message
// and returning a non-nil error if any error is encountered. star is the list
// of method names to use in case "*" is requested by Tor. Returns a ClientInfo
// struct.
+//
+// If your program needs to know whether to call ClientSetup or ServerSetup
+// (i.e., if the same program can be run as either a client or a server), check
+// whether the TOR_PT_CLIENT_TRANSPORTS environment variable is set:
+// if os.Getenv("TOR_PT_CLIENT_TRANSPORTS") != "" {
+// // Client mode; call pt.ClientSetup.
+// } else {
+// // Server mode; call pt.ServerSetup.
+// }
func ClientSetup(star []string) (info ClientInfo, err error) {
ver, err := getManagedTransportVer()
if err != nil {
@@ -531,6 +553,15 @@ type ServerInfo struct {
// of method names to use in case "*" is requested by Tor. Resolves the various
// requested bind addresses, the server ORPort and extended ORPort, and reads
// the auth cookie file. Returns a ServerInfo struct.
+//
+// If your program needs to know whether to call ClientSetup or ServerSetup
+// (i.e., if the same program can be run as either a client or a server), check
+// whether the TOR_PT_CLIENT_TRANSPORTS environment variable is set:
+// if os.Getenv("TOR_PT_CLIENT_TRANSPORTS") != "" {
+// // Client mode; call pt.ClientSetup.
+// } else {
+// // Server mode; call pt.ServerSetup.
+// }
func ServerSetup(star []string) (info ServerInfo, err error) {
ver, err := getManagedTransportVer()
if err != nil {
diff --git a/pt_test.go b/pt_test.go
index aa3ad04..d6a53e1 100644
--- a/pt_test.go
+++ b/pt_test.go
@@ -8,6 +8,7 @@ import (
"io/ioutil"
"net"
"os"
+ "path"
"sort"
"testing"
)
@@ -737,3 +738,61 @@ func TestExtOrPortSetup(t *testing.T) {
testExtOrPortSetupIndividual(t, "", methodName)
testExtOrPortSetupIndividual(t, addr, methodName)
}
+
+func TestMakeStateDir(t *testing.T) {
+ os.Clearenv()
+
+ // TOR_PT_STATE_LOCATION not set.
+ _, err := MakeStateDir()
+ if err == nil {
+ t.Errorf("empty environment unexpectedly succeeded")
+ }
+
+ // Setup the scratch directory.
+ tempDir, err := ioutil.TempDir("", "testMakeStateDir")
+ if err != nil {
+ t.Fatalf("ioutil.TempDir failed: %s", err)
+ }
+ defer os.RemoveAll(tempDir)
+
+ goodTests := [...]string {
+ // Already existing directory.
+ tempDir,
+
+ // Nonexistent directory, parent exists.
+ path.Join(tempDir, "parentExists"),
+
+ // Nonexistent directory, parent doesn't exist.
+ path.Join(tempDir, "missingParent", "parentMissing"),
+ }
+ for _, test := range goodTests {
+ os.Setenv("TOR_PT_STATE_LOCATION", test)
+ dir, err := MakeStateDir()
+ if err != nil {
+ t.Errorf("MakeStateDir unexpectedly failed: %s", err)
+ }
+ if dir != test {
+ t.Errorf("MakeStateDir returned an unexpected path %s (expecting %s)", dir, test)
+ }
+ }
+
+ // Name already exists, but is an ordinary file.
+ tempFile := path.Join(tempDir, "file")
+ f, err := os.Create(tempFile)
+ if err != nil {
+ t.Fatalf("os.Create failed: %s", err)
+ }
+ defer f.Close()
+ os.Setenv("TOR_PT_STATE_LOCATION", tempFile)
+ _, err = MakeStateDir()
+ if err == nil {
+ t.Errorf("MakeStateDir with a file unexpectedly succeded")
+ }
+
+ // Directory name that cannot be created. (Subdir of a file)
+ os.Setenv("TOR_PT_STATE_LOCATION", path.Join(tempFile, "subDir"))
+ _, err = MakeStateDir()
+ if err == nil {
+ t.Errorf("MakeStateDir with a subdirectory of a file unexpectedly succeded")
+ }
+}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-privacy/packages/golang-goptlib.git
More information about the Pkg-privacy-commits
mailing list