Skip to content

Commit

Permalink
Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pschork committed Apr 30, 2024
1 parent fdbb819 commit cb67a1e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
10 changes: 5 additions & 5 deletions disperser/dataapi/operator_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,16 @@ func checkIsOnlineAndProcessOperator(operatorStatus OperatorOnlineStatus, operat
operatorOnlineStatusresultsChan <- metadata
}

func validOperatorIP(socketString string) bool {
func ValidOperatorIP(socketString string) bool {
ip, _, _, err := core.ParseOperatorSocket(socketString)
if err != nil {
return true
return false
}
ipAddr := net.ParseIP(ip)
if ipAddr == nil {
return true
return false
}
return ipAddr.IsPrivate() || !ipAddr.IsUnspecified() || !ipAddr.IsLoopback()
return !ipAddr.IsPrivate() && !ipAddr.IsUnspecified() && !ipAddr.IsLoopback()
}

func (s *server) probeOperatorPorts(ctx context.Context, operatorId string) (*OperatorPortCheckResponse, error) {
Expand Down Expand Up @@ -150,7 +150,7 @@ func (s *server) probeOperatorPorts(ctx context.Context, operatorId string) (*Op
// Note: This method is least intrusive way to check if operator is online
// AlternateSolution: Should we add an endpt to check if operator is online?
func checkIsOperatorOnline(socket string, timeoutSecs int, logger logging.Logger) bool {
if !validOperatorIP(socket) {
if !ValidOperatorIP(socket) {
logger.Error("port check blocked invalid operator IP", "socket", socket)
return false
}
Expand Down
38 changes: 38 additions & 0 deletions disperser/dataapi/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,44 @@ func getEjector(t *testing.T) *ejectorComponents {
}
}

func TestPortCheck(t *testing.T) {
assert.Equal(t, false, dataapi.ValidOperatorIP(""))
assert.Equal(t, false, dataapi.ValidOperatorIP("0.0.0.0:32005;32006"))
assert.Equal(t, false, dataapi.ValidOperatorIP("10.0.0.1:32005;32006"))
assert.Equal(t, false, dataapi.ValidOperatorIP("127.0.0.1:32005;32006"))
assert.Equal(t, false, dataapi.ValidOperatorIP("192.168.0.1:32005;32006"))
assert.Equal(t, true, dataapi.ValidOperatorIP("23.93.76.1:32005;32006"))

r := setUpRouter()
operator_id := "0xa96bfb4a7ca981ad365220f336dc5a3de0816ebd5130b79bbc85aca94bc9b6ab"
mockSubgraphApi.On("QueryOperatorInfoByOperatorIdAtBlockNumber").Return(operatorInfo, nil)
r.GET("/v1/operators-info/port-check", testDataApiServer.OperatorPortCheck)
w := httptest.NewRecorder()
reqStr := fmt.Sprintf("/v1/operators-info/port-check?operator_id=%v", operator_id)
req := httptest.NewRequest(http.MethodGet, reqStr, nil)
ctxWithDeadline, cancel := context.WithTimeout(req.Context(), 500*time.Microsecond)
defer cancel()
req = req.WithContext(ctxWithDeadline)
r.ServeHTTP(w, req)
assert.Equal(t, w.Code, http.StatusOK)

res := w.Result()
defer res.Body.Close()

data, err := io.ReadAll(res.Body)
assert.NoError(t, err)

var response dataapi.OperatorPortCheckResponse
err = json.Unmarshal(data, &response)
assert.NoError(t, err)
assert.NotNil(t, response)

assert.Equal(t, "23.93.76.1:32005", response.DispersalSocket)
assert.Equal(t, false, response.DispersalOnline)
assert.Equal(t, "23.93.76.1:32006", response.RetrievalSocket)
assert.Equal(t, false, response.RetrievalOnline)
}

func TestCheckBatcherHealthExpectServing(t *testing.T) {
r := setUpRouter()
testDataApiServer = dataapi.NewServer(config, blobstore, prometheusClient, dataapi.NewSubgraphClient(mockSubgraphApi, mockLogger), mockTx, mockChainState, nil, mockLogger, metrics, &MockGRPCConnection{}, nil, &MockHttpClient{ShouldSucceed: true})
Expand Down
19 changes: 19 additions & 0 deletions disperser/dataapi/subgraph_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,25 @@ var (
},
}

operatorInfo = &subgraph.IndexedOperatorInfo{
Id: "0xa96bfb4a7ca981ad365220f336dc5a3de0816ebd5130b79bbc85aca94bc9b6ac",
PubkeyG1_X: "1336192159512049190945679273141887248666932624338963482128432381981287252980",
PubkeyG1_Y: "25195175002875833468883745675063986308012687914999552116603423331534089122704",
PubkeyG2_X: []graphql.String{
"31597023645215426396093421944506635812143308313031252511177204078669540440732",
"21405255666568400552575831267661419473985517916677491029848981743882451844775",
},
PubkeyG2_Y: []graphql.String{
"8416989242565286095121881312760798075882411191579108217086927390793923664442",
"23612061731370453436662267863740141021994163834412349567410746669651828926551",
},
SocketUpdates: []subgraph.SocketUpdates{
{
Socket: "23.93.76.1:32005;32006",
},
},
}

operatorAddedToQuorum = []*subgraph.OperatorQuorum{
{
Operator: "operator-2",
Expand Down

0 comments on commit cb67a1e

Please sign in to comment.