summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Steinert <[email protected]>2015-03-30 18:13:10 -0500
committerMichael Steinert <[email protected]>2015-03-30 18:13:10 -0500
commit77471e65f1618fc98af09b0188d9ad138b089366 (patch)
tree05f853c16288aeac920bc27ba3daad3b6b149c7c
parenta0cde3fe01c403f3651e9ee3fec9d62e2a7d806b (diff)
Fix a silly bug and add test coverage
-rw-r--r--.gitignore1
-rw-r--r--transaction.go9
-rw-r--r--transaction_test.go51
3 files changed, 55 insertions, 6 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..2d83068
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+coverage.out
diff --git a/transaction.go b/transaction.go
index 2ca945b..e99be6a 100644
--- a/transaction.go
+++ b/transaction.go
@@ -250,13 +250,14 @@ func (t *Transaction) GetEnvList() (map[string]string, error) {
t.status = C.PAM_BUF_ERR
return nil, t
}
- for *p != nil {
- chunks := strings.SplitN(C.GoString(*p), "=", 2)
+ q := p
+ for *q != nil {
+ chunks := strings.SplitN(C.GoString(*q), "=", 2)
if len(chunks) == 2 {
env[chunks[0]] = chunks[1]
}
- C.free(unsafe.Pointer(*p))
- p = (**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(p)) + unsafe.Sizeof(p)))
+ C.free(unsafe.Pointer(*q))
+ q = (**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(q)) + unsafe.Sizeof(*p)))
}
C.free(unsafe.Pointer(p))
return env, nil
diff --git a/transaction_test.go b/transaction_test.go
index ce84376..6515eaa 100644
--- a/transaction_test.go
+++ b/transaction_test.go
@@ -99,15 +99,62 @@ func TestPAM_004(t *testing.T) {
}
func TestGetEnvList(t *testing.T) {
- tx, err := StartFunc("passwd", "test", func(s Style, msg string) (string, error) {
+ tx, err := StartFunc("", "", func(s Style, msg string) (string, error) {
return "", nil
})
if err != nil {
t.Fatalf("start #error: %v", err)
}
+
m, err := tx.GetEnvList()
if err != nil {
t.Fatalf("getenvlist #error: %v", err)
}
- t.Log(m)
+ n := len(m)
+ if n != 0 {
+ t.Fatalf("putenv #error: expected 0 items, got %v", n)
+ }
+
+ vals := []string{
+ "VAL1=1",
+ "VAL2=2",
+ "VAL3=3",
+ }
+ for _, s := range vals {
+ err = tx.PutEnv(s)
+ if err != nil {
+ t.Fatalf("putenv #error: %v", err)
+ }
+ }
+
+ s := tx.GetEnv("VAL1")
+ if s != "1" {
+ t.Fatalf("getenv #error: expected 1, got %v", s)
+ }
+ s = tx.GetEnv("VAL2")
+ if s != "2" {
+ t.Fatalf("getenv #error: expected 2, got %v", s)
+ }
+ s = tx.GetEnv("VAL3")
+ if s != "3" {
+ t.Fatalf("getenv #error: expected 3, got %v", s)
+ }
+
+ m, err = tx.GetEnvList()
+ if err != nil {
+ t.Fatalf("getenvlist #error: %v", err)
+ }
+ n = len(m)
+ if n != 3 {
+ t.Fatalf("getenvlist #error: expected 3 items, got %v", n)
+ }
+ if m["VAL1"] != "1" {
+ t.Fatalf("getenvlist #error: expected 1, got %v", m["VAL1"])
+ }
+ if m["VAL2"] != "2" {
+ t.Fatalf("getenvlist #error: expected 2, got %v", m["VAL1"])
+ }
+ if m["VAL3"] != "3" {
+ t.Fatalf("getenvlist #error: expected 3, got %v", m["VAL1"])
+ }
}