summaryrefslogtreecommitdiff
path: root/catchup/ledgerFetcher_test.go
blob: f5039e9912459b09f2a5345cd765acecaa19c979 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright (C) 2019-2023 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package catchup

import (
	"context"
	"fmt"
	"net"
	"net/http"
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/algorand/go-algorand/components/mocks"
	"github.com/algorand/go-algorand/config"
	"github.com/algorand/go-algorand/data/basics"
	"github.com/algorand/go-algorand/ledger"
	"github.com/algorand/go-algorand/logging"
	"github.com/algorand/go-algorand/test/partitiontest"
)

type dummyLedgerFetcherReporter struct {
}

func (lf *dummyLedgerFetcherReporter) updateLedgerFetcherProgress(*ledger.CatchpointCatchupAccessorProgress) {
}

func TestNoPeersAvailable(t *testing.T) {
	partitiontest.PartitionTest(t)

	lf := makeLedgerFetcher(&mocks.MockNetwork{}, &mocks.MockCatchpointCatchupAccessor{}, logging.TestingLog(t), &dummyLedgerFetcherReporter{}, config.GetDefaultLocal())
	peer := &lf // The peer is an opaque interface.. we can add anything as a Peer.
	err := lf.downloadLedger(context.Background(), peer, basics.Round(0))
	require.Equal(t, errNonHTTPPeer, err)
}

func TestNonParsableAddress(t *testing.T) {
	partitiontest.PartitionTest(t)

	lf := makeLedgerFetcher(&mocks.MockNetwork{}, &mocks.MockCatchpointCatchupAccessor{}, logging.TestingLog(t), &dummyLedgerFetcherReporter{}, config.GetDefaultLocal())
	peer := testHTTPPeer(":def")
	err := lf.getPeerLedger(context.Background(), &peer, basics.Round(0))
	require.Error(t, err)
}

func TestLedgerFetcherErrorResponseHandling(t *testing.T) {
	partitiontest.PartitionTest(t)
	testcases := []struct {
		name               string
		httpServerResponse int
		contentTypes       []string
		err                error
	}{
		{
			name:               "getPeerLedger 400 Response",
			httpServerResponse: http.StatusNotFound,
			contentTypes:       make([]string, 0),
			err:                errNoLedgerForRound,
		},
		{
			name:               "getPeerLedger 500 Response",
			httpServerResponse: http.StatusInternalServerError,
			contentTypes:       make([]string, 0),
			err:                fmt.Errorf("getPeerLedger error response status code %d", http.StatusInternalServerError),
		},
		{
			name:               "getPeerLedger No Content Type",
			httpServerResponse: http.StatusOK,
			contentTypes:       make([]string, 0),
			err:                fmt.Errorf("getPeerLedger : http ledger fetcher invalid content type count %d", 0),
		},
		{
			name:               "getPeerLedger Too Many Content Types",
			httpServerResponse: http.StatusOK,
			contentTypes:       []string{"applications/one", "applications/two"},
			err:                fmt.Errorf("getPeerLedger : http ledger fetcher invalid content type count %d", 2),
		},
		{
			name:               "getPeerLedger Invalid Content Type",
			httpServerResponse: http.StatusOK,
			contentTypes:       []string{"applications/one"},
			err:                fmt.Errorf("getPeerLedger : http ledger fetcher response has an invalid content type : %s", "applications/one"),
		},
	}
	for _, tc := range testcases {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()
			// create a dummy server.
			mux := http.NewServeMux()
			s := &http.Server{
				Handler: mux,
			}
			listener, err := net.Listen("tcp", "localhost:")

			require.NoError(t, err)
			go s.Serve(listener)
			defer s.Close()
			defer listener.Close()
			mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
				for _, contentType := range tc.contentTypes {
					w.Header().Add("Content-Type", contentType)
				}
				w.WriteHeader(tc.httpServerResponse)
			})
			lf := makeLedgerFetcher(&mocks.MockNetwork{}, &mocks.MockCatchpointCatchupAccessor{}, logging.TestingLog(t), &dummyLedgerFetcherReporter{}, config.GetDefaultLocal())
			peer := testHTTPPeer(listener.Addr().String())
			err = lf.getPeerLedger(context.Background(), &peer, basics.Round(0))
			require.Equal(t, tc.err, err)
		})
	}
}

func TestLedgerFetcherHeadLedger(t *testing.T) {
	partitiontest.PartitionTest(t)

	// create a dummy server.
	mux := http.NewServeMux()
	s := &http.Server{
		Handler: mux,
	}
	listener, err := net.Listen("tcp", "localhost:")

	var httpServerResponse = 0
	var contentTypes = make([]string, 0)
	require.NoError(t, err)
	go s.Serve(listener)
	defer s.Close()
	defer listener.Close()
	mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		for _, contentType := range contentTypes {
			w.Header().Add("Content-Type", contentType)
		}
		w.WriteHeader(httpServerResponse)
	})
	successPeer := testHTTPPeer(listener.Addr().String())
	lf := makeLedgerFetcher(&mocks.MockNetwork{}, &mocks.MockCatchpointCatchupAccessor{}, logging.TestingLog(t), &dummyLedgerFetcherReporter{}, config.GetDefaultLocal())

	// headLedger non-http peer
	err = lf.headLedger(context.Background(), nil, basics.Round(0))
	require.Equal(t, errNonHTTPPeer, err)

	// headLedger parseURL failure
	parseFailurePeer := testHTTPPeer("foobar")
	err = lf.headLedger(context.Background(), &parseFailurePeer, basics.Round(0))
	require.Equal(t, fmt.Errorf("could not parse a host from url"), err)

	// headLedger 404 response
	httpServerResponse = http.StatusNotFound
	err = lf.headLedger(context.Background(), &successPeer, basics.Round(0))
	require.Equal(t, errNoLedgerForRound, err)

	// headLedger 200 response
	httpServerResponse = http.StatusOK
	err = lf.headLedger(context.Background(), &successPeer, basics.Round(0))
	require.NoError(t, err)

	// headLedger 500 response
	httpServerResponse = http.StatusInternalServerError
	err = lf.headLedger(context.Background(), &successPeer, basics.Round(0))
	require.Equal(t, fmt.Errorf("headLedger error response status code %d", http.StatusInternalServerError), err)
}