...

Source file src/github.com/AdaLogics/go-fuzz-headers/consumer_test.go

Documentation: github.com/AdaLogics/go-fuzz-headers

     1  // Copyright 2023 The go-fuzz-headers Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package gofuzzheaders
    16  
    17  import (
    18  	//"archive/tar"
    19  	//"bytes"
    20  	//"io"
    21  	"testing"
    22  )
    23  
    24  type TestStruct1 struct {
    25  	Field1 string
    26  	Field2 string
    27  	Field3 []byte
    28  }
    29  
    30  func TestStruct_fuzzing1(t *testing.T) {
    31  	data := []byte{
    32  		0x00, 0x00, 0x00, 0x03, // Length of field 1
    33  		0x41, 0x42, 0x43, // Data of field field 1
    34  		0x00, 0x00, 0x00, 0x03, // Length of field 2
    35  		0x41, 0x42, 0x43, // Data of field 2
    36  		0x00, 0x00, 0x00, 0x01, // Length of field 3
    37  		0x41, // Data of Field3
    38  	}
    39  
    40  	ts1 := TestStruct1{}
    41  	fuzz1 := NewConsumer(data)
    42  	err := fuzz1.GenerateStruct(&ts1)
    43  	if err != nil {
    44  		t.Errorf("%v", err)
    45  	}
    46  	if ts1.Field1 != "ABC" {
    47  		t.Errorf("ts1.Field1 was %v but should be 'AB'", []byte(ts1.Field1))
    48  	}
    49  	if ts1.Field2 != "ABC" {
    50  		t.Errorf("ts1.Field2 was %v but should be 'ABC'", ts1.Field2)
    51  	}
    52  	if string(ts1.Field3) != "A" {
    53  		t.Errorf("ts1.Field3 was %v but should be 'A'", ts1.Field3)
    54  	}
    55  }
    56  
    57  // Tests that we can create long byte slices in structs
    58  func TestStruct_fuzzing2(t *testing.T) {
    59  	data := []byte{
    60  		0x00, 0x00, 0x00, 0x03, // Length field 1
    61  		0x41, 0x42, 0x43, // Data of field 1
    62  		0x00, 0x00, 0x00, 0x03, // Length of Field2
    63  		0x41, 0x42, 0x43, // Content of Field2
    64  		0x00, 0x00, 0x00, 0x50, // Length of field3
    65  		0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, // All of this
    66  		0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, // should go
    67  		0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, // into Field3
    68  		0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
    69  		0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
    70  		0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
    71  		0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
    72  		0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
    73  	}
    74  
    75  	ts1 := TestStruct1{}
    76  	fuzz1 := NewConsumer(data)
    77  	err := fuzz1.GenerateStruct(&ts1)
    78  	if err != nil {
    79  		t.Errorf("%v", err)
    80  	}
    81  	if ts1.Field1 != "ABC" {
    82  		t.Errorf("ts1.Field1 was %v but should be 'ABC'", ts1.Field1)
    83  	}
    84  	if ts1.Field2 != "ABC" {
    85  		t.Errorf("ts1.Field2 was %v but should be 'ABC'", ts1.Field2)
    86  	}
    87  	if len(ts1.Field3) != 80 {
    88  		t.Errorf("ts1.Field3 was %v but should be 'ABCD'", ts1.Field3)
    89  	}
    90  }
    91  
    92  /*func TestTarBytes(t *testing.T) {
    93  	data := []byte{
    94  		0x01,                   // number of files
    95  		0x00, 0x00, 0x00, 0x08, // Length of first file name
    96  		0x6d, 0x61, 0x6e, 0x69, 0x66, 0x65, 0x73, 0x74, // "manifest"
    97  		0x00, 0x00, 0x00, 0x09, // Length of file body
    98  		0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, // file contents
    99  		0x04, 0x02, 0x03,
   100  		0x00, // type flag
   101  		0x01, 0x01, 0x01, 0x01,
   102  	}
   103  	f := NewConsumer(data)
   104  	tb, err := f.TarBytes()
   105  	if err != nil {
   106  		t.Fatalf("Fatal: %s", err)
   107  	}
   108  
   109  	tarReader := tar.NewReader(bytes.NewReader(tb))
   110  
   111  	for {
   112  		header, err := tarReader.Next()
   113  
   114  		if err == io.EOF {
   115  			break
   116  		}
   117  		if err != nil {
   118  			t.Fatal(err)
   119  		}
   120  		if header.Typeflag != 48 {
   121  			t.Fatalf("typeflag should be 48 (which is a tar.TypeReg) but is %v", header.Typeflag)
   122  		}
   123  		switch header.Typeflag {
   124  		case tar.TypeDir:
   125  			t.Fatal("Should not be a directory")
   126  		case tar.TypeReg:
   127  			if header.Name != "manifest" {
   128  				t.Fatalf("file name was %s but should be 'manifest'\n", header.Name)
   129  			}
   130  		}
   131  	}
   132  }*/
   133  
   134  func TestGetUint32(t *testing.T) {
   135  	data := []byte{
   136  		0x00,
   137  		0x00,
   138  		0x03,
   139  		0x01,
   140  	}
   141  	f := NewConsumer(data)
   142  	i, err := f.GetUint32()
   143  	if err != nil {
   144  		t.Fatalf("%v\n", err)
   145  	}
   146  	if i != uint32(769) {
   147  		t.Fatalf("i should be 636 but is %v\n", i)
   148  	}
   149  }
   150  
   151  func TestGeBytes1(t *testing.T) {
   152  	data := []byte{
   153  		0x00,
   154  		0x00,
   155  		0x03,
   156  		0x01,
   157  	}
   158  	for i := 0; i < 769; i++ {
   159  		data = append(data, 0x00)
   160  	}
   161  	f := NewConsumer(data)
   162  	b, err := f.GetBytes()
   163  	if err != nil {
   164  		t.Fatalf("%v\n", err)
   165  	}
   166  	if len(b) != 769 {
   167  		t.Fatalf("len(b) should be 769 but is %v\n", len(b))
   168  	}
   169  
   170  	for i := 0; i < 769; i++ {
   171  		if b[i] != 0 {
   172  			t.Fatalf("b[%d] should be 0x00 but is %v\n", i, b[i])
   173  		}
   174  	}
   175  }
   176  
   177  func TestGeBytes2(t *testing.T) {
   178  	data := []byte{
   179  		0x00,
   180  		0x00,
   181  		0x03,
   182  		0x01,
   183  	}
   184  	for i := 0; i < 767; i++ {
   185  		data = append(data, 0x00)
   186  	}
   187  	f := NewConsumer(data)
   188  	b, err := f.GetBytes()
   189  	if err != nil {
   190  		t.Fatalf("%v\n", err)
   191  	}
   192  	if len(b) != 2 {
   193  		t.Fatalf("len(b) should be 2 but is %v\n", len(b))
   194  	}
   195  
   196  	for i := 0; i < 2; i++ {
   197  		if b[i] != 0 {
   198  			t.Fatalf("b[%d] should be 0x00 but is %v\n", i, b[i])
   199  		}
   200  	}
   201  }
   202  
   203  func TestGeBytes3(t *testing.T) {
   204  	data := []byte{
   205  		0x00,
   206  		0x00,
   207  		0x03,
   208  		0x01,
   209  	}
   210  	for i := 0; i < 500; i++ {
   211  		data = append(data, 0x00)
   212  	}
   213  	f := NewConsumer(data)
   214  	b, err := f.GetBytes()
   215  	if err != nil {
   216  		t.Fatalf("%v\n", err)
   217  	}
   218  	if len(b) != 269 {
   219  		t.Fatalf("len(b) should be 269 but is %v\n", len(b))
   220  	}
   221  
   222  	for i := 0; i < 269; i++ {
   223  		if b[i] != 0 {
   224  			t.Fatalf("b[%d] should be 0x00 but is %v\n", i, b[i])
   225  		}
   226  	}
   227  }
   228  

View as plain text