github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/misc/gpgagent/gpgagent_test.go (about) 1 /* 2 Copyright 2011 Google Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package gpgagent 18 19 import ( 20 "fmt" 21 "os" 22 "testing" 23 "time" 24 ) 25 26 func TestPrompt(t *testing.T) { 27 if os.Getenv("TEST_GPGAGENT_LIB") != "1" { 28 t.Logf("skipping TestPrompt without $TEST_GPGAGENT_LIB == 1") 29 return 30 } 31 conn, err := NewConn() 32 if err != nil { 33 t.Fatal(err) 34 } 35 defer conn.Close() 36 req := &PassphraseRequest{ 37 Desc: "Type 'foo' for testing", 38 Error: "seriously, or I'll be an error.", 39 Prompt: "foo", 40 CacheKey: fmt.Sprintf("gpgagent_test-cachekey-%d", time.Now()), 41 } 42 s1, err := conn.GetPassphrase(req) 43 if err != nil { 44 t.Fatal(err) 45 } 46 t1 := time.Now() 47 s2, err := conn.GetPassphrase(req) 48 if err != nil { 49 t.Fatal(err) 50 } 51 t2 := time.Now() 52 if td := t2.Sub(t1); td > 1e9/5 { 53 t.Errorf("cached passphrase took more than 1/5 second; took %d ns", td) 54 } 55 if s1 != s2 { 56 t.Errorf("cached passphrase differed; got %q, want %q", s2, s1) 57 } 58 if s1 != "foo" { 59 t.Errorf("got passphrase %q; want %q", s1, "foo") 60 } 61 err = conn.RemoveFromCache(req.CacheKey) 62 if err != nil { 63 t.Fatal(err) 64 } 65 66 req.NoAsk = true 67 s3, err := conn.GetPassphrase(req) 68 if err != ErrNoData { 69 t.Errorf("after remove from cache, expected gpgagent.ErrNoData, got %q, %v", s3, err) 70 } 71 72 s4, err := conn.GetPassphrase(&PassphraseRequest{ 73 Desc: "Press Cancel for testing", 74 Error: "seriously, or I'll be an error.", 75 Prompt: "cancel!", 76 CacheKey: fmt.Sprintf("gpgagent_test-cachekey-%d", time.Now()), 77 }) 78 if err != ErrCancel { 79 t.Errorf("expected cancel, got %q, %v", s4, err) 80 } 81 }