How to use validateGzip method of asset Package

Best Syzkaller code snippet using asset.validateGzip

storage_test.go

Source:storage_test.go Github

copy

Full Screen

...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 }...

Full Screen

Full Screen

validateGzip

Using AI Code Generation

copy

Full Screen

1validateGzip(asset);2validateGzip(asset);3validateGzip(asset);4validateGzip(asset);5validateGzip(asset);6validateGzip(asset);7validateGzip(asset);8validateGzip(asset);9validateGzip(asset);10validateGzip(asset);11validateGzip(asset);12validateGzip(asset);13validateGzip(asset);14validateGzip(asset);15validateGzip(asset);16validateGzip(asset);17validateGzip(asset);18validateGzip(asset);19validateGzip(asset);20validateGzip(asset);21validateGzip(asset);

Full Screen

Full Screen

validateGzip

Using AI Code Generation

copy

Full Screen

1import (2type Asset struct {3}4func main() {5 m := macaron.Classic()6 m.Use(session.Sessioner())7 m.Use(macaron.Renderer())8 m.Get("/", func(ctx *macaron.Context) {9 ctx.HTML(200, "index")10 })11 m.Post("/asset", binding.Bind(Asset{}), func(ctx *macaron.Context, asset Asset) {12 ctx.HTML(200, "asset")13 })14 m.Run()15}16import (17type Asset struct {18}19func main() {20 m := macaron.Classic()21 m.Use(session.Sessioner())22 m.Use(macaron.Renderer())23 m.Get("/", func(ctx *macaron.Context) {24 ctx.HTML(200, "index")25 })26 m.Post("/asset", binding.Bind(Asset{}), func(ctx *macaron.Context, asset Asset) {27 ctx.HTML(200, "asset")28 })29 m.Run()30}31import (32type Asset struct {

Full Screen

Full Screen

validateGzip

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 reader, err := gzip.NewReader(os.Stdin)4 if err != nil {5 fmt.Println("Error in creating gzip reader")6 }7 defer reader.Close()8 buf := make([]byte, 1024)9 for {10 n, err := reader.Read(buf)11 if err != nil {12 }13 fmt.Println(string(buf[:n]))14 }15}16import (17func main() {18 reader, err := gzip.NewReader(os.Stdin)19 if err != nil {20 fmt.Println("Error in creating gzip reader")21 }22 defer reader.Close()23 buf := make([]byte, 1024)24 for {25 n, err := reader.Read(buf)26 if err != nil {27 }28 fmt.Println(string(buf[:n]))29 }30}31import (32func main() {33 f, err := os.Create("test.gz")34 if err != nil {35 fmt.Println("Error in creating file")36 }37 defer f.Close()38 w := gzip.NewWriter(f)39 defer w.Close()40 _, err = w.Write([]byte("Hello World"))41 if err != nil {42 fmt.Println("Error in writing to file")43 }44}45import (

Full Screen

Full Screen

validateGzip

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4 assets.ValidateGzip()5}6 /usr/local/go/src/myproject/assets (from $GOROOT)7 /Users/username/Documents/go/src/myproject/assets (from $GOPATH)

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful