github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/dashboard/app/getjson_test.go (about) 1 // Copyright 2021 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 "fmt" 8 "testing" 9 10 "github.com/google/syzkaller/dashboard/dashapi" 11 ) 12 13 func TestJSONAPIIntegration(t *testing.T) { 14 sampleCrashDescr := []byte(`{ 15 "version": 1, 16 "title": "title1", 17 "id": "cb1dbe55dc6daa7e739a0d09a0ae4d5e3e5a10c8", 18 "crashes": [ 19 { 20 "title": "title1", 21 "kernel-config": "/text?tag=KernelConfig\u0026x=a989f27ebc47e2dc", 22 "kernel-source-commit": "1111111111111111111111111111111111111111", 23 "syzkaller-git": "https://github.com/google/syzkaller/commits/syzkaller_commit1", 24 "syzkaller-commit": "syzkaller_commit1", 25 "crash-report-link": "/text?tag=CrashReport\u0026x=12000000000000" 26 } 27 ] 28 }`, 29 ) 30 31 sampleCrashWithReproDescr := []byte(`{ 32 "version": 1, 33 "title": "title2", 34 "id": "fc00fbc0cddd9a4ef2ae33e40cd21636081466ce", 35 "crashes": [ 36 { 37 "title": "title2", 38 "syz-reproducer": "/text?tag=ReproSyz\u0026x=13000000000000", 39 "c-reproducer": "/text?tag=ReproC\u0026x=17000000000000", 40 "kernel-config": "/text?tag=KernelConfig\u0026x=a989f27ebc47e2dc", 41 "kernel-source-commit": "1111111111111111111111111111111111111111", 42 "syzkaller-git": "https://github.com/google/syzkaller/commits/syzkaller_commit1", 43 "syzkaller-commit": "syzkaller_commit1", 44 "crash-report-link": "/text?tag=CrashReport\u0026x=15000000000000" 45 } 46 ] 47 }`, 48 ) 49 50 sampleOpenBugGroupDescr := []byte(`{ 51 "version": 1, 52 "Bugs": [ 53 { 54 "title": "title1", 55 "link": "/bug?extid=decf42d66dced481afc1" 56 }, 57 { 58 "title": "title2", 59 "link": "/bug?extid=0267d1c87b9ed4eb5def" 60 } 61 ] 62 }`) 63 64 sampleFixedBugGroupDescr := []byte(`{ 65 "version": 1, 66 "Bugs": [ 67 { 68 "title": "title2", 69 "link": "/bug?extid=0267d1c87b9ed4eb5def", 70 "fix-commits": [ 71 { 72 "title": "foo: fix1", 73 "repo": "git://syzkaller.org", 74 "branch": "branch10" 75 }, 76 { 77 "title": "foo: fix2", 78 "repo": "git://syzkaller.org", 79 "branch": "branch10" 80 } 81 ] 82 } 83 ] 84 }`) 85 86 c := NewCtx(t) 87 defer c.Close() 88 89 c.makeClient(client1, password1, false) 90 91 build := testBuild(1) 92 c.client.UploadBuild(build) 93 94 crash1 := testCrash(build, 1) 95 c.client.ReportCrash(crash1) 96 bugReport1 := c.client.pollBug() 97 checkBugPageJSONIs(c, bugReport1.ID, sampleCrashDescr) 98 99 crash2 := testCrashWithRepro(build, 2) 100 c.client.ReportCrash(crash2) 101 bugReport2 := c.client.pollBug() 102 checkBugPageJSONIs(c, bugReport2.ID, sampleCrashWithReproDescr) 103 104 checkBugGroupPageJSONIs(c, "/test1?json=1", sampleOpenBugGroupDescr) 105 106 c.client.ReportingUpdate(&dashapi.BugUpdate{ 107 ID: bugReport2.ID, 108 Status: dashapi.BugStatusOpen, 109 FixCommits: []string{"foo: fix1", "foo: fix2"}, 110 }) 111 112 checkBugGroupPageJSONIs(c, "/test1/fixed?json=1", sampleFixedBugGroupDescr) 113 } 114 115 func checkBugPageJSONIs(c *Ctx, ID string, expectedContent []byte) { 116 url := fmt.Sprintf("/bug?extid=%v&json=1", ID) 117 118 contentType, _ := c.client.ContentType(url) 119 c.expectEQ(contentType, "application/json") 120 121 actualContent, _ := c.client.GET(url) 122 c.expectEQ(string(actualContent), string(expectedContent)) 123 } 124 125 func checkBugGroupPageJSONIs(c *Ctx, url string, expectedContent []byte) { 126 contentType, _ := c.client.ContentType(url) 127 c.expectEQ(contentType, "application/json") 128 129 actualContent, _ := c.client.GET(url) 130 c.expectEQ(string(actualContent), string(expectedContent)) 131 } 132 133 func TestJSONAPIFixCommits(t *testing.T) { 134 c := NewCtx(t) 135 defer c.Close() 136 137 build1 := testBuild(1) 138 c.client.UploadBuild(build1) 139 140 crash1 := testCrash(build1, 1) 141 c.client.ReportCrash(crash1) 142 rep1 := c.client.pollBug() 143 144 // Specify fixing commit for the bug. 145 c.client.ReportingUpdate(&dashapi.BugUpdate{ 146 ID: rep1.ID, 147 Status: dashapi.BugStatusOpen, 148 FixCommits: []string{"foo: fix1", "foo: fix2"}, 149 }) 150 151 c.client.UploadCommits([]dashapi.Commit{ 152 {Hash: "hash1", Title: "foo: fix1"}, 153 }) 154 155 c.client.CommitPoll() 156 157 want := []byte(`{ 158 "version": 1, 159 "title": "title1", 160 "id": "cb1dbe55dc6daa7e739a0d09a0ae4d5e3e5a10c8", 161 "fix-commits": [ 162 { 163 "title": "foo: fix1", 164 "hash": "hash1", 165 "repo": "git://syzkaller.org", 166 "branch": "branch10" 167 }, 168 { 169 "title": "foo: fix2", 170 "repo": "git://syzkaller.org", 171 "branch": "branch10" 172 } 173 ], 174 "crashes": [ 175 { 176 "title": "title1", 177 "kernel-config": "/text?tag=KernelConfig\u0026x=a989f27ebc47e2dc", 178 "kernel-source-commit": "1111111111111111111111111111111111111111", 179 "syzkaller-git": "https://github.com/google/syzkaller/commits/syzkaller_commit1", 180 "syzkaller-commit": "syzkaller_commit1", 181 "crash-report-link": "/text?tag=CrashReport\u0026x=12000000000000" 182 } 183 ] 184 }`) 185 checkBugGroupPageJSONIs(c, "/bug?extid=decf42d66dced481afc1&json=1", want) 186 } 187 188 func TestJSONAPICauseBisection(t *testing.T) { 189 c := NewCtx(t) 190 defer c.Close() 191 192 build, _ := addBuildAndCrash(c) 193 _, bugKey := c.loadSingleBug() 194 195 addBisectCauseJob(c, build) 196 addBisectFixJob(c, build) 197 198 url := fmt.Sprintf("/bug?id=%v&json=1", bugKey.StringID()) 199 content, err := c.GET(url) 200 c.expectEQ(err, nil) 201 c.expectEQ(string(content), `{ 202 "version": 1, 203 "title": "title1", 204 "id": "70ce63ecb151d563976728208edccc6879191f9f", 205 "cause-commit": { 206 "title": "kernel: add a bug", 207 "hash": "36e65cb4a0448942ec316b24d60446bbd5cc7827", 208 "repo": "repo1", 209 "branch": "branch1" 210 }, 211 "crashes": [ 212 { 213 "title": "title1", 214 "syz-reproducer": "/text?tag=ReproSyz\u0026x=16000000000000", 215 "c-reproducer": "/text?tag=ReproC\u0026x=11000000000000", 216 "kernel-config": "/text?tag=KernelConfig\u0026x=4d11162a90e18f28", 217 "kernel-source-commit": "1111111111111111111111111111111111111111", 218 "syzkaller-git": "https://github.com/google/syzkaller/commits/syzkaller_commit1", 219 "syzkaller-commit": "syzkaller_commit1", 220 "crash-report-link": "/text?tag=CrashReport\u0026x=12000000000000" 221 } 222 ] 223 }`) 224 }