genTestPair creates a testPair.
(tb testing.TB, realSocket bool)
| 150 | |
| 151 | // genTestPair creates a testPair. |
| 152 | func genTestPair(tb testing.TB, realSocket bool) (pair testPair) { |
| 153 | cfg, endpointCfg := genConfigs(tb) |
| 154 | var binds [2]conn.Bind |
| 155 | if realSocket { |
| 156 | binds[0], binds[1] = conn.NewDefaultBind(), conn.NewDefaultBind() |
| 157 | } else { |
| 158 | binds = bindtest.NewChannelBinds() |
| 159 | } |
| 160 | // Bring up a ChannelTun for each config. |
| 161 | for i := range pair { |
| 162 | p := &pair[i] |
| 163 | p.tun = tuntest.NewChannelTUN() |
| 164 | p.ip = netip.AddrFrom4([4]byte{1, 0, 0, byte(i + 1)}) |
| 165 | level := LogLevelVerbose |
| 166 | if _, ok := tb.(*testing.B); ok && !testing.Verbose() { |
| 167 | level = LogLevelError |
| 168 | } |
| 169 | p.dev = NewDevice(p.tun.TUN(), binds[i], NewLogger(level, fmt.Sprintf("dev%d: ", i))) |
| 170 | if err := p.dev.IpcSet(cfg[i]); err != nil { |
| 171 | tb.Errorf("failed to configure device %d: %v", i, err) |
| 172 | p.dev.Close() |
| 173 | continue |
| 174 | } |
| 175 | if err := p.dev.Up(); err != nil { |
| 176 | tb.Errorf("failed to bring up device %d: %v", i, err) |
| 177 | p.dev.Close() |
| 178 | continue |
| 179 | } |
| 180 | endpointCfg[i^1] = fmt.Sprintf(endpointCfg[i^1], p.dev.net.port) |
| 181 | } |
| 182 | for i := range pair { |
| 183 | p := &pair[i] |
| 184 | if err := p.dev.IpcSet(endpointCfg[i]); err != nil { |
| 185 | tb.Errorf("failed to configure device endpoint %d: %v", i, err) |
| 186 | p.dev.Close() |
| 187 | continue |
| 188 | } |
| 189 | // The device is ready. Close it when the test completes. |
| 190 | tb.Cleanup(p.dev.Close) |
| 191 | } |
| 192 | return |
| 193 | } |
| 194 | |
| 195 | func TestTwoDevicePing(t *testing.T) { |
| 196 | goroutineLeakCheck(t) |
no test coverage detected