aboutsummaryrefslogtreecommitdiffstats
path: root/syz-cluster/dashboard/handler_test.go
blob: 949d9eef95e3d2b128083113a7cc22e5a483a73e (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
// Copyright 2025 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

package main

import (
	"io"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/google/syzkaller/syz-cluster/pkg/api"
	"github.com/google/syzkaller/syz-cluster/pkg/app"
	"github.com/google/syzkaller/syz-cluster/pkg/controller"
	"github.com/google/syzkaller/syz-cluster/pkg/db"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestURLs(t *testing.T) {
	env, ctx := app.TestEnvironment(t)
	client := controller.TestServer(t, env)
	testSeries := controller.DummySeries()
	ids := controller.FakeSeriesWithFindings(t, ctx, env, client, testSeries)

	handler, baseURL := testServer(t, env)
	urlGen := api.NewURLGenerator(baseURL)

	urls := []string{
		baseURL,
		baseURL + "/stats",
		urlGen.Series(ids.SeriesID),
	}
	for _, buildID := range []string{ids.BaseBuildID, ids.PatchedBuildID} {
		urls = append(urls, urlGen.BuildConfig(buildID))
		urls = append(urls, urlGen.BuildLog(buildID))
	}

	findings, err := handler.findingRepo.ListForSession(ctx, ids.SessionID, db.NoLimit)
	require.NoError(t, err)
	for _, finding := range findings {
		urls = append(urls, urlGen.FindingLog(finding.ID))
		urls = append(urls, urlGen.FindingCRepro(finding.ID))
		urls = append(urls, urlGen.FindingSyzRepro(finding.ID))
	}
	for _, url := range urls {
		t.Logf("checking %s", url)
		resp, err := http.Get(url)
		body, _ := io.ReadAll(resp.Body)
		resp.Body.Close()
		assert.NoError(t, err)
		assert.Equal(t, http.StatusOK, resp.StatusCode,
			"%q was expected to return HTTP 200, body: %s", url, string(body))
	}
}

func TestAllPatches(t *testing.T) {
	env, ctx := app.TestEnvironment(t)
	client := controller.TestServer(t, env)
	testSeries := &api.Series{
		ExtID: "ext-id",
		Title: "test series name",
		Link:  "http://link/to/series",
		Patches: []api.SeriesPatch{
			{
				Seq:   1,
				Title: "first patch title",
				Body:  []byte("first content\n"),
			},
			{
				Seq:   2,
				Title: "second patch title",
				Body:  []byte("second content\n"),
			},
		},
	}
	ids := controller.UploadTestSeries(t, ctx, client, testSeries)
	_, baseURL := testServer(t, env)

	resp, err := http.Get(baseURL + "/series/" + ids.SeriesID + "/all_patches")
	body, _ := io.ReadAll(resp.Body)
	resp.Body.Close()
	assert.NoError(t, err)
	assert.Equal(t, "first content\nsecond content\n", string(body))
}

func testServer(t *testing.T, env *app.AppEnvironment) (*dashboardHandler, string) {
	handler, err := newHandler(env)
	require.NoError(t, err)
	server := httptest.NewServer(handler.Mux())
	t.Cleanup(server.Close)
	return handler, server.URL
}