MCPcopy
hub / github.com/bradfitz/gomemcache / TestTLS

Function TestTLS

memcache/memcache_test.go:92–161  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

90}
91
92func TestTLS(t *testing.T) {
93 t.Parallel()
94 td := t.TempDir()
95
96 // Test whether our memcached binary has TLS support. We --enable-ssl first,
97 // before --version, as memcached evaluates the flags in the order provided
98 // and we want it to fail if it's built without TLS support (as it is in
99 // Debian, but not Ubuntu or Homebrew).
100 out, err := exec.Command("memcached", "--enable-ssl", "--version").CombinedOutput()
101 if err != nil {
102 t.Skipf("skipping test; couldn't find memcached or no TLS support in binary: %v, %s", err, out)
103 }
104 t.Logf("version: %s", bytes.TrimSpace(out))
105
106 if err := os.WriteFile(filepath.Join(td, "/cert.pem"), LocalhostCert, 0644); err != nil {
107 t.Fatal(err)
108 }
109 if err := os.WriteFile(filepath.Join(td, "/key.pem"), LocalhostKey, 0644); err != nil {
110 t.Fatal(err)
111 }
112
113 // Find some unused port. This is racy but we hope for the best and hope the kernel
114 // doesn't reassign our ephemeral port to somebody in the tiny race window.
115 ln, err := net.Listen("tcp", "localhost:0")
116 if err != nil {
117 t.Fatalf("failed to listen: %v", err)
118 }
119 port := ln.Addr().(*net.TCPAddr).Port
120 ln.Close()
121
122 cmd := exec.Command("memcached",
123 "--port="+strconv.Itoa(port),
124 "--listen=127.0.0.1",
125 "--enable-ssl",
126 "-o", "ssl_chain_cert=cert.pem",
127 "-o", "ssl_key=key.pem")
128 cmd.Dir = td
129 if *debug {
130 cmd.Stdout = os.Stdout
131 cmd.Stderr = os.Stderr
132 }
133 if err := cmd.Start(); err != nil {
134 t.Fatalf("failed to start memcached: %v", err)
135 }
136 defer cmd.Wait()
137 defer cmd.Process.Kill()
138
139 // Wait a bit for the server to be running.
140 for i := 0; i < 10; i++ {
141 nc, err := net.Dial("tcp", "localhost:"+strconv.Itoa(port))
142 if err == nil {
143 t.Logf("localhost:%d is up.", port)
144 nc.Close()
145 break
146 }
147 t.Logf("waiting for localhost:%d to be up...", port)
148 time.Sleep(time.Duration(25*i) * time.Millisecond)
149 }

Callers

nothing calls this directly

Calls 3

NewFunction · 0.85
testWithClientFunction · 0.85
CloseMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…