github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/dashboard/app/api_test.go (about)

     1  // Copyright 2017 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  	"testing"
     8  	"time"
     9  
    10  	"github.com/google/syzkaller/dashboard/dashapi"
    11  )
    12  
    13  func TestClientSecretOK(t *testing.T) {
    14  	got, err := checkClient(&GlobalConfig{
    15  		Clients: map[string]string{
    16  			"user": "secr1t",
    17  		},
    18  	}, "user", "secr1t", "")
    19  	if err != nil || got != "" {
    20  		t.Errorf("unexpected error %v %v", got, err)
    21  	}
    22  }
    23  
    24  func TestClientOauthOK(t *testing.T) {
    25  	got, err := checkClient(&GlobalConfig{
    26  		Clients: map[string]string{
    27  			"user": "OauthSubject:public",
    28  		},
    29  	}, "user", "", "OauthSubject:public")
    30  	if err != nil || got != "" {
    31  		t.Errorf("unexpected error %v %v", got, err)
    32  	}
    33  }
    34  
    35  func TestClientSecretFail(t *testing.T) {
    36  	got, err := checkClient(&GlobalConfig{
    37  		Clients: map[string]string{
    38  			"user": "secr1t",
    39  		},
    40  	}, "user", "wrong", "")
    41  	if err != ErrAccess || got != "" {
    42  		t.Errorf("unexpected error %v %v", got, err)
    43  	}
    44  }
    45  
    46  func TestClientSecretMissing(t *testing.T) {
    47  	got, err := checkClient(&GlobalConfig{
    48  		Clients: map[string]string{},
    49  	}, "user", "ignored", "")
    50  	if err != ErrAccess || got != "" {
    51  		t.Errorf("unexpected error %v %v", got, err)
    52  	}
    53  }
    54  
    55  func TestClientNamespaceOK(t *testing.T) {
    56  	got, err := checkClient(&GlobalConfig{
    57  		Namespaces: map[string]*Config{
    58  			"ns1": {
    59  				Clients: map[string]string{
    60  					"user": "secr1t",
    61  				},
    62  			},
    63  		},
    64  	}, "user", "secr1t", "")
    65  	if err != nil || got != "ns1" {
    66  		t.Errorf("unexpected error %v %v", got, err)
    67  	}
    68  }
    69  
    70  func TestEmergentlyStoppedEmail(t *testing.T) {
    71  	c := NewCtx(t)
    72  	defer c.Close()
    73  
    74  	client := c.publicClient
    75  	build := testBuild(1)
    76  	client.UploadBuild(build)
    77  
    78  	crash := testCrash(build, 1)
    79  	client.ReportCrash(crash)
    80  
    81  	c.advanceTime(time.Hour)
    82  	_, err := c.AuthGET(AccessAdmin, "/admin?action=emergency_stop")
    83  	c.expectOK(err)
    84  
    85  	// There should be no email.
    86  	c.advanceTime(time.Hour)
    87  	c.expectNoEmail()
    88  }
    89  
    90  func TestEmergentlyStoppedReproEmail(t *testing.T) {
    91  	c := NewCtx(t)
    92  	defer c.Close()
    93  
    94  	client := c.publicClient
    95  	build := testBuild(1)
    96  	client.UploadBuild(build)
    97  
    98  	crash := testCrash(build, 1)
    99  	client.ReportCrash(crash)
   100  	c.pollEmailBug()
   101  
   102  	crash2 := testCrash(build, 1)
   103  	crash2.ReproOpts = []byte("repro opts")
   104  	crash2.ReproSyz = []byte("getpid()")
   105  	client.ReportCrash(crash2)
   106  
   107  	c.advanceTime(time.Hour)
   108  	_, err := c.AuthGET(AccessAdmin, "/admin?action=emergency_stop")
   109  	c.expectOK(err)
   110  
   111  	// There should be no email.
   112  	c.advanceTime(time.Hour)
   113  	c.expectNoEmail()
   114  }
   115  
   116  func TestEmergentlyStoppedExternalReport(t *testing.T) {
   117  	c := NewCtx(t)
   118  	defer c.Close()
   119  
   120  	client := c.client
   121  	build := testBuild(1)
   122  	client.UploadBuild(build)
   123  
   124  	crash := testCrash(build, 1)
   125  	client.ReportCrash(crash)
   126  
   127  	c.advanceTime(time.Hour)
   128  	_, err := c.AuthGET(AccessAdmin, "/admin?action=emergency_stop")
   129  	c.expectOK(err)
   130  
   131  	// There should be no email.
   132  	c.advanceTime(time.Hour)
   133  	client.pollBugs(0)
   134  }
   135  
   136  func TestEmergentlyStoppedEmailJob(t *testing.T) {
   137  	c := NewCtx(t)
   138  	defer c.Close()
   139  
   140  	client := c.publicClient
   141  	build := testBuild(1)
   142  	client.UploadBuild(build)
   143  
   144  	crash := testCrash(build, 1)
   145  	crash.ReproOpts = []byte("repro opts")
   146  	crash.ReproSyz = []byte("getpid()")
   147  	client.ReportCrash(crash)
   148  	sender := c.pollEmailBug().Sender
   149  	c.incomingEmail(sender, "#syz upstream\n")
   150  	sender = c.pollEmailBug().Sender
   151  
   152  	// Send a patch testing request.
   153  	c.advanceTime(time.Hour)
   154  	c.incomingEmail(sender, syzTestGitBranchSamplePatch,
   155  		EmailOptMessageID(1), EmailOptFrom("test@requester.com"),
   156  		EmailOptCC([]string{"somebody@else.com", "test@syzkaller.com"}))
   157  	c.expectNoEmail()
   158  
   159  	// Emulate a finished job.
   160  	pollResp := client.pollJobs(build.Manager)
   161  	c.expectEQ(pollResp.Type, dashapi.JobTestPatch)
   162  
   163  	c.advanceTime(time.Hour)
   164  	jobDoneReq := &dashapi.JobDoneReq{
   165  		ID:          pollResp.ID,
   166  		Build:       *build,
   167  		CrashTitle:  "test crash title",
   168  		CrashLog:    []byte("test crash log"),
   169  		CrashReport: []byte("test crash report"),
   170  	}
   171  	client.JobDone(jobDoneReq)
   172  
   173  	// Now we emergently stop syzbot.
   174  	c.advanceTime(time.Hour)
   175  	_, err := c.AuthGET(AccessAdmin, "/admin?action=emergency_stop")
   176  	c.expectOK(err)
   177  
   178  	// There should be no email.
   179  	c.advanceTime(time.Hour)
   180  	c.expectNoEmail()
   181  }
   182  
   183  func TestEmergentlyStoppedCrashReport(t *testing.T) {
   184  	c := NewCtx(t)
   185  	defer c.Close()
   186  
   187  	client := c.publicClient
   188  	build := testBuild(1)
   189  	client.UploadBuild(build)
   190  
   191  	// Now we emergently stop syzbot.
   192  	c.advanceTime(time.Hour)
   193  	_, err := c.AuthGET(AccessAdmin, "/admin?action=emergency_stop")
   194  	c.expectOK(err)
   195  
   196  	crash := testCrash(build, 1)
   197  	crash.ReproOpts = []byte("repro opts")
   198  	crash.ReproSyz = []byte("getpid()")
   199  	client.ReportCrash(crash)
   200  
   201  	listResp, err := client.BugList()
   202  	c.expectOK(err)
   203  	c.expectEQ(len(listResp.List), 0)
   204  }