...

Source file src/go.mongodb.org/mongo-driver/bson/fuzz_test.go

Documentation: go.mongodb.org/mongo-driver/bson

     1  // Copyright (C) MongoDB, Inc. 2022-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  package bson
     8  
     9  import (
    10  	"testing"
    11  )
    12  
    13  func FuzzDecode(f *testing.F) {
    14  	seedBSONCorpus(f)
    15  
    16  	f.Fuzz(func(t *testing.T, data []byte) {
    17  		for _, typ := range []func() interface{}{
    18  			func() interface{} { return new(D) },
    19  			func() interface{} { return new([]E) },
    20  			func() interface{} { return new(M) },
    21  			func() interface{} { return new(interface{}) },
    22  			func() interface{} { return make(map[string]interface{}) },
    23  			func() interface{} { return new([]interface{}) },
    24  		} {
    25  			i := typ()
    26  			if err := Unmarshal(data, i); err != nil {
    27  				return
    28  			}
    29  
    30  			encoded, err := Marshal(i)
    31  			if err != nil {
    32  				t.Fatal("failed to marshal", err)
    33  			}
    34  
    35  			if err := Unmarshal(encoded, i); err != nil {
    36  				t.Fatal("failed to unmarshal", err)
    37  			}
    38  		}
    39  	})
    40  }
    41  

View as plain text