summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Steinert <[email protected]>2015-03-29 11:25:00 -0500
committerMichael Steinert <[email protected]>2015-03-29 11:25:00 -0500
commit9c771166c9fb45cfa72779c356962f51022a6903 (patch)
tree5e71a54944eb9fbee940db70bc1c38d457a37d55
parentb380319c58ed6bf1023f8e58f78f593fa28c234b (diff)
Add a test suite
-rw-r--r--.travis.yml3
-rw-r--r--README.md10
-rw-r--r--transaction.go2
-rw-r--r--transaction_test.go82
4 files changed, 96 insertions, 1 deletions
diff --git a/.travis.yml b/.travis.yml
index 84ef634..f429bbf 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -7,3 +7,6 @@ go:
before_install:
- sudo apt-get update -qq
- sudo apt-get install -qq --no-install-recommends libpam0g-dev
+ - sudo useradd -d /tmp/test -p '$1$Qd8H95T5$RYSZQeoFbEB.gS19zS99A0' -s /bin/false test
+
+script: sudo go test -v ./...
diff --git a/README.md b/README.md
index d41a5fd..577ac24 100644
--- a/README.md
+++ b/README.md
@@ -11,5 +11,15 @@ There's an example of a "fake login" program in the examples directory.
Look at the pam module's [godocs][1] for details about the Go API, or refer
to the official [PAM documentation][2].
+The test suite must be run as the root user. To setup your system for testing,
+create a user named "test" with the password "secret". For example:
+
+```
+$ sudo useradd test \
+ -d /tmp/test \
+ -p '$1$Qd8H95T5$RYSZQeoFbEB.gS19zS99A0' \
+ -s /bin/false
+```
+
[1]: http://godoc.org/github.com/msteinert/pam
[2]: http://www.kernel.org/pub/linux/libs/pam/Linux-PAM-html/adg-interface-by-app-expected.html
diff --git a/transaction.go b/transaction.go
index 404aca8..48568aa 100644
--- a/transaction.go
+++ b/transaction.go
@@ -106,7 +106,7 @@ func Start(service, user string, handler ConversationHandler) (*Transaction, err
var u *C.char
if len(user) != 0 {
u = C.CString(user)
- defer C.free(unsafe.Pointer(s))
+ defer C.free(unsafe.Pointer(u))
}
t.status = C.pam_start(s, u, t.conv.conv, &t.handle)
if t.status != C.PAM_SUCCESS {
diff --git a/transaction_test.go b/transaction_test.go
new file mode 100644
index 0000000..a1b7050
--- /dev/null
+++ b/transaction_test.go
@@ -0,0 +1,82 @@
+package pam
+
+import (
+ "errors"
+ "testing"
+)
+
+func TestPAM_001(t *testing.T) {
+ tx, err := StartFunc("", "test", func(s Style, msg string) (string, error) {
+ return "secret", nil
+ })
+ if err != nil {
+ t.Fatalf("start #error: %v", err)
+ }
+ err = tx.Authenticate(0)
+ if err != nil {
+ t.Fatalf("authenticate #error: %v", err)
+ }
+}
+
+func TestPAM_002(t *testing.T) {
+ tx, err := StartFunc("", "", func(s Style, msg string) (string, error) {
+ switch s {
+ case PromptEchoOn:
+ return "test", nil
+ case PromptEchoOff:
+ return "secret", nil
+ }
+ return "", errors.New("unexpected")
+ })
+ if err != nil {
+ t.Fatalf("start #error: %v", err)
+ }
+ err = tx.Authenticate(0)
+ if err != nil {
+ t.Fatalf("authenticate #error: %v", err)
+ }
+}
+
+type Credentials struct {
+ User string
+ Password string
+}
+
+func (c Credentials) RespondPAM(s Style, msg string) (string, error) {
+ switch s {
+ case PromptEchoOn:
+ return c.User, nil
+ case PromptEchoOff:
+ return c.Password, nil
+ }
+ return "", errors.New("unexpected")
+}
+
+func TestPAM_003(t *testing.T) {
+ c := Credentials{
+ User: "test",
+ Password: "secret",
+ }
+ tx, err := Start("", "", c)
+ if err != nil {
+ t.Fatalf("start #error: %v", err)
+ }
+ err = tx.Authenticate(0)
+ if err != nil {
+ t.Fatalf("authenticate #error: %v", err)
+ }
+}
+
+func TestPAM_004(t *testing.T) {
+ c := Credentials{
+ Password: "secret",
+ }
+ tx, err := Start("", "test", c)
+ if err != nil {
+ t.Fatalf("start #error: %v", err)
+ }
+ err = tx.Authenticate(0)
+ if err != nil {
+ t.Fatalf("authenticate #error: %v", err)
+ }
+}