github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/syz-cluster/dashboard/handler_test.go (about)

     1  // Copyright 2025 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package main
     5  
     6  import (
     7  	"io"
     8  	"net/http"
     9  	"net/http/httptest"
    10  	"testing"
    11  
    12  	"github.com/google/syzkaller/syz-cluster/pkg/api"
    13  	"github.com/google/syzkaller/syz-cluster/pkg/app"
    14  	"github.com/google/syzkaller/syz-cluster/pkg/controller"
    15  	"github.com/google/syzkaller/syz-cluster/pkg/db"
    16  	"github.com/stretchr/testify/assert"
    17  	"github.com/stretchr/testify/require"
    18  )
    19  
    20  func TestURLs(t *testing.T) {
    21  	env, ctx := app.TestEnvironment(t)
    22  	client := controller.TestServer(t, env)
    23  	testSeries := controller.DummySeries()
    24  	ids := controller.FakeSeriesWithFindings(t, ctx, env, client, testSeries)
    25  
    26  	handler, baseURL := testServer(t, env)
    27  	urlGen := api.NewURLGenerator(baseURL)
    28  
    29  	urls := []string{
    30  		baseURL,
    31  		baseURL + "/stats",
    32  		urlGen.Series(ids.SeriesID),
    33  	}
    34  	for _, buildID := range []string{ids.BaseBuildID, ids.PatchedBuildID} {
    35  		urls = append(urls, urlGen.BuildConfig(buildID))
    36  		urls = append(urls, urlGen.BuildLog(buildID))
    37  	}
    38  
    39  	findings, err := handler.findingRepo.ListForSession(ctx, ids.SessionID, db.NoLimit)
    40  	require.NoError(t, err)
    41  	for _, finding := range findings {
    42  		urls = append(urls, urlGen.FindingLog(finding.ID))
    43  		urls = append(urls, urlGen.FindingCRepro(finding.ID))
    44  		urls = append(urls, urlGen.FindingSyzRepro(finding.ID))
    45  	}
    46  	for _, url := range urls {
    47  		t.Logf("checking %s", url)
    48  		resp, err := http.Get(url)
    49  		body, _ := io.ReadAll(resp.Body)
    50  		resp.Body.Close()
    51  		assert.NoError(t, err)
    52  		assert.Equal(t, http.StatusOK, resp.StatusCode,
    53  			"%q was expected to return HTTP 200, body: %s", url, string(body))
    54  	}
    55  }
    56  
    57  func TestAllPatches(t *testing.T) {
    58  	env, ctx := app.TestEnvironment(t)
    59  	client := controller.TestServer(t, env)
    60  	testSeries := &api.Series{
    61  		ExtID: "ext-id",
    62  		Title: "test series name",
    63  		Link:  "http://link/to/series",
    64  		Patches: []api.SeriesPatch{
    65  			{
    66  				Seq:   1,
    67  				Title: "first patch title",
    68  				Body:  []byte("first content\n"),
    69  			},
    70  			{
    71  				Seq:   2,
    72  				Title: "second patch title",
    73  				Body:  []byte("second content\n"),
    74  			},
    75  		},
    76  	}
    77  	ids := controller.UploadTestSeries(t, ctx, client, testSeries)
    78  	_, baseURL := testServer(t, env)
    79  
    80  	resp, err := http.Get(baseURL + "/series/" + ids.SeriesID + "/all_patches")
    81  	body, _ := io.ReadAll(resp.Body)
    82  	resp.Body.Close()
    83  	assert.NoError(t, err)
    84  	assert.Equal(t, "first content\nsecond content\n", string(body))
    85  }
    86  
    87  func testServer(t *testing.T, env *app.AppEnvironment) (*dashboardHandler, string) {
    88  	handler, err := newHandler(env)
    89  	require.NoError(t, err)
    90  	server := httptest.NewServer(handler.Mux())
    91  	t.Cleanup(server.Close)
    92  	return handler, server.URL
    93  }