github.com/decred/dcrlnd@v0.7.6/tor/cmd_info_test.go (about) 1 package tor 2 3 import ( 4 "errors" 5 "io" 6 "syscall" 7 "testing" 8 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestCheckOnionServiceFailOnServiceNotCreated(t *testing.T) { 13 t.Parallel() 14 15 // Create a dummy tor controller. 16 c := &Controller{} 17 18 // Check that CheckOnionService returns an error when the service 19 // hasn't been created. 20 require.Equal(t, ErrServiceNotCreated, c.CheckOnionService()) 21 } 22 23 func TestCheckOnionServiceSucceed(t *testing.T) { 24 t.Parallel() 25 26 // Create mock server and client connection. 27 proxy := createTestProxy(t) 28 defer proxy.cleanUp() 29 server := proxy.serverConn 30 31 // Assign a fake service ID to the controller. 32 c := &Controller{conn: proxy.clientConn, activeServiceID: "fakeID"} 33 34 // Test a successful response. 35 serverResp := "250-onions/current=fakeID\n250 OK\n" 36 37 // Let the server mocks a given response. 38 _, err := server.Write([]byte(serverResp)) 39 require.NoError(t, err, "server failed to write") 40 41 // For a successful response, we expect no error. 42 require.NoError(t, c.CheckOnionService()) 43 } 44 45 func TestCheckOnionServiceFailOnServiceIDNotMatch(t *testing.T) { 46 t.Parallel() 47 48 // Create mock server and client connection. 49 proxy := createTestProxy(t) 50 defer proxy.cleanUp() 51 server := proxy.serverConn 52 53 // Assign a fake service ID to the controller. 54 c := &Controller{conn: proxy.clientConn, activeServiceID: "fakeID"} 55 56 // Mock a response with a different serviceID. 57 serverResp := "250-onions/current=unmatchedID\n250 OK\n" 58 59 // Let the server mocks a given response. 60 _, err := server.Write([]byte(serverResp)) 61 require.NoError(t, err, "server failed to write") 62 63 // Check the error returned from GetServiceInfo is expected. 64 require.ErrorIs(t, c.CheckOnionService(), ErrServiceIDMismatch) 65 } 66 67 func TestCheckOnionServiceSucceedOnMultipleServices(t *testing.T) { 68 t.Parallel() 69 70 // Create mock server and client connection. 71 proxy := createTestProxy(t) 72 defer proxy.cleanUp() 73 server := proxy.serverConn 74 75 // Assign a fake service ID to the controller. 76 c := &Controller{conn: proxy.clientConn, activeServiceID: "fakeID"} 77 78 // Mock a response with a different serviceID. 79 serverResp := "250-onions/current=service1,fakeID,service2\n250 OK\n" 80 81 // Let the server mocks a given response. 82 _, err := server.Write([]byte(serverResp)) 83 require.NoError(t, err, "server failed to write") 84 85 // No error is expected, the controller's ID is contained within the 86 // list of active services. 87 require.NoError(t, c.CheckOnionService()) 88 } 89 90 func TestCheckOnionServiceFailOnClosedConnection(t *testing.T) { 91 t.Parallel() 92 93 // Create mock server and client connection. 94 proxy := createTestProxy(t) 95 defer proxy.cleanUp() 96 server := proxy.serverConn 97 98 // Assign a fake service ID to the controller. 99 c := &Controller{conn: proxy.clientConn, activeServiceID: "fakeID"} 100 101 // Close the connection from the server side. 102 require.NoError(t, server.Close(), "server failed to close conn") 103 104 // Check the error returned from GetServiceInfo is expected. 105 err := c.CheckOnionService() 106 eof := errors.Is(err, io.EOF) 107 reset := errors.Is(err, syscall.ECONNRESET) 108 require.Truef(t, eof || reset, 109 "must of EOF or RESET error, instead got: %v", err) 110 }