Best Syzkaller code snippet using asset.getDashapi
storage_test.go
Source:storage_test.go
...45 return nil46 }47 return nil48}49func (dm *dashMock) getDashapi() *dashapi.Dashboard {50 return dashapi.NewMock(dm.do)51}52func makeStorage(t *testing.T, dash *dashapi.Dashboard) (*Storage, *dummyStorageBackend) {53 be := makeDummyStorageBackend()54 cfg := &Config{55 UploadTo: "dummy://test",56 }57 return &Storage{58 dash: dash,59 cfg: cfg,60 backend: be,61 tracer: &debugtracer.TestTracer{T: t},62 }, be63}64func validateGzip(res *uploadedFile, expected []byte) error {65 if res == nil {66 return fmt.Errorf("no file was uploaded")67 }68 reader, err := gzip.NewReader(bytes.NewReader(res.bytes))69 if err != nil {70 return fmt.Errorf("gzip.NewReader failed: %w", err)71 }72 defer reader.Close()73 body, err := ioutil.ReadAll(reader)74 if err != nil {75 return fmt.Errorf("read of ungzipped content failed: %w", err)76 }77 if !reflect.DeepEqual(body, expected) {78 return fmt.Errorf("decompressed: %#v, expected: %#v", body, expected)79 }80 return nil81}82func validateXz(res *uploadedFile, expected []byte) error {83 if res == nil {84 return fmt.Errorf("no file was uploaded")85 }86 xzUsed := strings.HasSuffix(res.req.savePath, ".xz")87 if !xzUsed {88 return fmt.Errorf("xz expected to be used")89 }90 xzReader, err := xz.NewReader(bytes.NewReader(res.bytes))91 if err != nil {92 return fmt.Errorf("xz reader failed: %w", err)93 }94 out, err := ioutil.ReadAll(xzReader)95 if err != nil {96 return fmt.Errorf("xz decompression failed: %w", err)97 }98 if !reflect.DeepEqual(out, expected) {99 return fmt.Errorf("decompressed: %#v, expected: %#v", out, expected)100 }101 return nil102}103func (storage *Storage) sendBuildAsset(reader io.Reader, fileName string, assetType dashapi.AssetType,104 build *dashapi.Build) error {105 asset, err := storage.UploadBuildAsset(reader, fileName, assetType, build, nil)106 if err != nil {107 return err108 }109 return storage.ReportBuildAssets(build, asset)110}111func TestUploadBuildAsset(t *testing.T) {112 dashMock := newDashMock()113 storage, be := makeStorage(t, dashMock.getDashapi())114 be.currentTime = time.Now().Add(-2 * deletionEmbargo)115 build := &dashapi.Build{ID: "1234", KernelCommit: "abcdef2134"}116 // Upload two assets using different means.117 vmLinuxContent := []byte{0xDE, 0xAD, 0xBE, 0xEF}118 dashMock.addBuildAsset = func(newAsset dashapi.NewAsset) error {119 if newAsset.Type != dashapi.KernelObject {120 t.Fatalf("expected KernelObject, got %v", newAsset.Type)121 }122 if !strings.Contains(newAsset.DownloadURL, "vmlinux") {123 t.Fatalf("%#v was expected to mention vmlinux", newAsset.DownloadURL)124 }125 return nil126 }127 var file *uploadedFile128 be.objectUpload = collectBytes(&file)129 err := storage.sendBuildAsset(bytes.NewReader(vmLinuxContent), "vmlinux",130 dashapi.KernelObject, build)131 if err != nil {132 t.Fatalf("file upload failed: %s", err)133 }134 if err := validateXz(file, vmLinuxContent); err != nil {135 t.Fatalf("vmlinux validation failed: %s", err)136 }137 // Upload the same file the second time.138 storage.sendBuildAsset(bytes.NewReader(vmLinuxContent), "vmlinux", dashapi.KernelObject, build)139 // The currently expected behavior is that it will be uploaded twice and will have140 // different names.141 if len(dashMock.downloadURLs) < 2 {142 t.Fatalf("same-file upload was expected to succeed, but it didn't; %#v", dashMock.downloadURLs)143 }144 diskImageContent := []byte{0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8}145 dashMock.addBuildAsset = func(newAsset dashapi.NewAsset) error {146 if newAsset.Type != dashapi.KernelImage {147 t.Fatalf("expected KernelImage, got %v", newAsset.Type)148 }149 if !strings.Contains(newAsset.DownloadURL, "disk") ||150 !strings.Contains(newAsset.DownloadURL, ".img") {151 t.Fatalf("%#v was expected to mention disk.img", newAsset.DownloadURL)152 }153 if !strings.Contains(newAsset.DownloadURL, build.KernelCommit[:6]) {154 t.Fatalf("%#v was expected to mention build commit", newAsset.DownloadURL)155 }156 return nil157 }158 file = nil159 be.objectUpload = collectBytes(&file)160 storage.sendBuildAsset(bytes.NewReader(diskImageContent), "disk.img", dashapi.KernelImage, build)161 if err := validateXz(file, diskImageContent); err != nil {162 t.Fatalf("disk.img validation failed: %s", err)163 }164 allUrls := []string{}165 for url := range dashMock.downloadURLs {166 allUrls = append(allUrls, url)167 }168 if len(allUrls) != 3 {169 t.Fatalf("invalid dashMock state: expected 3 assets, got %d", len(allUrls))170 }171 // First try to remove two assets.172 dashMock.downloadURLs = map[string]bool{allUrls[2]: true, "http://non-related-asset.com/abcd": true}173 // Pretend there's an asset deletion error.174 be.objectRemove = func(string) error { return fmt.Errorf("not now") }175 err = storage.DeprecateAssets()176 if err == nil {177 t.Fatalf("DeprecateAssets() should have failed")178 }179 // Let the deletion be successful.180 be.objectRemove = nil181 err = storage.DeprecateAssets()182 if err != nil {183 t.Fatalf("DeprecateAssets() was expected to be successful, got %s", err)184 }185 path, err := be.getPath(allUrls[2])186 if err != nil {187 t.Fatalf("getPath failed: %s", err)188 }189 err = be.hasOnly([]string{path})190 if err != nil {191 t.Fatalf("after first DeprecateAssets(): %s", err)192 }193 // Delete the rest.194 dashMock.downloadURLs = map[string]bool{}195 err = storage.DeprecateAssets()196 if err != nil || len(be.objects) != 0 {197 t.Fatalf("second DeprecateAssets() failed: %s, len %d",198 err, len(be.objects))199 }200}201type uploadedFile struct {202 req uploadRequest203 bytes []byte204}205func collectBytes(saveTo **uploadedFile) objectUploadCallback {206 return func(req *uploadRequest) (*uploadResponse, error) {207 buf := &bytes.Buffer{}208 wwc := &wrappedWriteCloser{209 writer: buf,210 closeCallback: func() error {211 *saveTo = &uploadedFile{req: *req, bytes: buf.Bytes()}212 return nil213 },214 }215 return &uploadResponse{path: req.savePath, writer: wwc}, nil216 }217}218func TestUploadHtmlAsset(t *testing.T) {219 dashMock := newDashMock()220 storage, be := makeStorage(t, dashMock.getDashapi())221 build := &dashapi.Build{ID: "1234", KernelCommit: "abcdef2134"}222 htmlContent := []byte("<html><head><title>Hi!</title></head></html>")223 dashMock.addBuildAsset = func(newAsset dashapi.NewAsset) error {224 if newAsset.Type != dashapi.HTMLCoverageReport {225 t.Fatalf("expected HtmlCoverageReport, got %v", newAsset.Type)226 }227 if !strings.Contains(newAsset.DownloadURL, "cover_report") {228 t.Fatalf("%#v was expected to mention cover_report", newAsset.DownloadURL)229 }230 if !strings.HasSuffix(newAsset.DownloadURL, ".html") {231 t.Fatalf("%#v was expected to have .html extension", newAsset.DownloadURL)232 }233 return nil234 }235 var file *uploadedFile236 be.objectUpload = collectBytes(&file)237 storage.sendBuildAsset(bytes.NewReader(htmlContent), "cover_report.html",238 dashapi.HTMLCoverageReport, build)239 if err := validateGzip(file, htmlContent); err != nil {240 t.Fatalf("cover_report.html validation failed: %s", err)241 }242}243func TestRecentAssetDeletionProtection(t *testing.T) {244 dashMock := newDashMock()245 storage, be := makeStorage(t, dashMock.getDashapi())246 build := &dashapi.Build{ID: "1234", KernelCommit: "abcdef2134"}247 htmlContent := []byte("<html><head><title>Hi!</title></head></html>")248 be.currentTime = time.Now().Add(-time.Hour * 24 * 6)249 err := storage.sendBuildAsset(bytes.NewReader(htmlContent), "cover_report.html",250 dashapi.HTMLCoverageReport, build)251 if err != nil {252 t.Fatalf("failed to upload a file: %v", err)253 }254 // Try to delete a recent file.255 dashMock.downloadURLs = map[string]bool{}256 err = storage.DeprecateAssets()257 if err != nil {258 t.Fatalf("DeprecateAssets failed: %v", err)259 } else if len(be.objects) == 0 {260 t.Fatalf("a recent object was deleted: %v", err)261 }262}263func TestAssetStorageConfiguration(t *testing.T) {264 dashMock := newDashMock()265 cfg := &Config{266 UploadTo: "dummy://",267 Assets: map[dashapi.AssetType]TypeConfig{268 dashapi.HTMLCoverageReport: {Never: true},269 dashapi.KernelObject: {},270 },271 }272 storage, err := StorageFromConfig(cfg, dashMock.getDashapi())273 if err != nil {274 t.Fatalf("unexpected error from StorageFromConfig: %s", err)275 }276 build := &dashapi.Build{ID: "1234", KernelCommit: "abcdef2134"}277 // Uploading a file of a disabled asset type.278 htmlContent := []byte("<html><head><title>Hi!</title></head></html>")279 err = storage.sendBuildAsset(bytes.NewReader(htmlContent), "cover_report.html",280 dashapi.HTMLCoverageReport, build)281 if !errors.Is(err, ErrAssetTypeDisabled) {282 t.Fatalf("UploadBuildAssetStream expected to fail with ErrAssetTypeDisabled, but got %v", err)283 }284 // Uploading a file of an unspecified asset type.285 testContent := []byte{0x1, 0x2, 0x3, 0x4}286 err = storage.sendBuildAsset(bytes.NewReader(testContent), "disk.raw", dashapi.BootableDisk, build)287 if err != nil {288 t.Fatalf("UploadBuildAssetStream of BootableDisk expected to succeed, got %v", err)289 }290 // Uploading a file of a specified asset type.291 err = storage.sendBuildAsset(bytes.NewReader(testContent), "vmlinux", dashapi.KernelObject, build)292 if err != nil {293 t.Fatalf("UploadBuildAssetStream of BootableDisk expected to succeed, got %v", err)294 }295}296func TestUploadSameContent(t *testing.T) {297 dashMock := newDashMock()298 storage, be := makeStorage(t, dashMock.getDashapi())299 be.currentTime = time.Now().Add(-2 * deletionEmbargo)300 build := &dashapi.Build{ID: "1234", KernelCommit: "abcdef2134"}301 extra := &ExtraUploadArg{UniqueTag: "uniquetag", SkipIfExists: true}302 testContent := []byte{0x1, 0x2, 0x3, 0x4}303 asset, err := storage.UploadBuildAsset(bytes.NewReader(testContent), "disk.raw",304 dashapi.BootableDisk, build, extra)305 if err != nil {306 t.Fatalf("UploadBuildAssetexpected to succeed, got %v", err)307 }308 if !strings.Contains(asset.DownloadURL, extra.UniqueTag) {309 t.Fatalf("%#v was expected to contain %#v", asset.DownloadURL, extra.UniqueTag)310 }311 // Upload the same asset again.312 be.objectUpload = func(req *uploadRequest) (*uploadResponse, error) {...
getDashapi
Using AI Code Generation
1import (2func main() {3 api, err := api.ComposeAPI(api.HTTPClientSettings{4 })5 if err != nil {6 panic(err)7 }8 seed := trinary.Trytes("SEED")9 address, err := address.GenerateAddress(seed, 0, 2, false, false)10 if err != nil {11 panic(err)12 }13 fmt.Println(address)14 transfer := bundle.Transfers{15 bundle.Transfer{
getDashapi
Using AI Code Generation
1func main() {2 asset := asset.NewAsset()3 dashApi := asset.GetDashApi()4 fmt.Println("dashApi:", dashApi)5}6func main() {7 asset := asset.NewAsset()8 dashApi := asset.GetDashApi()9 fmt.Println("dashApi:", dashApi)10 dashTestnetApi := asset.GetDashTestnetApi()11 fmt.Println("dashTestnetApi:", dashTestnetApi)12}13func main() {14 asset := asset.NewAsset()15 dashApi := asset.GetDashApi()16 fmt.Println("dashApi:", dashApi)17 dashTestnetApi := asset.GetDashTestnetApi()18 fmt.Println("dashTestnetApi:", dashTestnetApi)19 dashMainnetApi := asset.GetDashMainnetApi()20 fmt.Println("dashMainnetApi:", dashMainnetApi)21}22func main() {23 asset := asset.NewAsset()24 dashApi := asset.GetDashApi()25 fmt.Println("dashApi:", dashApi)26 dashTestnetApi := asset.GetDashTestnetApi()27 fmt.Println("dashTestnetApi:", dashTestnetApi)28 dashMainnetApi := asset.GetDashMainnetApi()
getDashapi
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello, world.")4 assetObj.GetDashApi()5}6import (7type Asset struct {8}9func (assetObj Asset) GetDashApi() {10 fmt.Println("GetDashApi")11 fmt.Println(assetObj.dashApi)12}13import (14func TestGetDashApi(t *testing.T) {15 assetObj.GetDashApi()16}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!