Skip to content

Commit

Permalink
K4 fuzz tests and minor Get() correction regarding tombstoned values
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Gaetano Padula committed Oct 30, 2024
1 parent 0c171ec commit af5809c
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 1 deletion.
67 changes: 67 additions & 0 deletions fuzz/fuzz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Package fuzz
// Fuzz generates random key-value pairs for fuzz testing
// BSD 3-Clause License
//
// Copyright (c) 2024, Alex Gaetano Padula
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package fuzz

import (
"crypto/rand"
"fmt"
"math/big"
"time"
)

// RandomString generates random strings of a given length
func RandomString(length int) (string, error) {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
b := make([]byte, length)
for i := range b {
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
if err != nil {
return "", err
}
b[i] = charset[num.Int64()]
}
uniqueSuffix := fmt.Sprintf("%d", time.Now().UnixNano())[2:]
return string(b) + uniqueSuffix, nil
}

// GenerateKeyValuePairs generates a map of random key-value pairs
func GenerateKeyValuePairs(numPairs int) map[string]interface{} {
pairs := make(map[string]interface{})

for i := 0; i < numPairs; i++ {
key, _ := RandomString(5 + i%10)
value, _ := RandomString(5 + i%10)
pairs[key] = value
}

return pairs
}
66 changes: 66 additions & 0 deletions fuzz/fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Package fuzz tests
// Fuzz generates random key-value pairs
// BSD 3-Clause License
//
// Copyright (c) 2024, Alex Gaetano Padula
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package fuzz

import "testing"

func TestRandomStringUniqueness(t *testing.T) {
const numGenerations = 1000000
const strLength = 10

seen := make(map[string]struct{}, numGenerations)

for i := 0; i < numGenerations; i++ {
s, _ := RandomString(strLength)
if _, exists := seen[s]; exists {
t.Fatalf("Duplicate string found: %s", s)
}
seen[s] = struct{}{}
}
}

func TestGenerateKeyValuePairs(t *testing.T) {
const numPairs = 1000000

pairs := GenerateKeyValuePairs(numPairs)
if len(pairs) != numPairs {
t.Fatalf("Expected %d pairs, but got %d", numPairs, len(pairs))
}

seenKeys := make(map[string]struct{}, numPairs)
for key := range pairs {
if _, exists := seenKeys[key]; exists {
t.Fatalf("Duplicate key found: %s", key)
}
seenKeys[key] = struct{}{}
}
}
5 changes: 4 additions & 1 deletion k4.go
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,10 @@ func (k4 *K4) Get(key []byte) ([]byte, error) {
return nil, err
}
if value != nil {
return value, nil
if bytes.Compare(value, []byte(TOMBSTONE_VALUE)) == 0 { // Check if the value is a tombstone

return value, nil
}
}
}

Expand Down
64 changes: 64 additions & 0 deletions k4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ package k4
import (
"bytes"
"fmt"
"github.com/guycipher/k4/fuzz"
"io/ioutil"
"os"
"strings"
Expand Down Expand Up @@ -985,3 +986,66 @@ func TestStress(t *testing.T) {

wg.Wait()
}

func FuzzTestK4(f *testing.F) {
f.Add((1024*1024)*50, 280, false, false) // Initial seed values

f.Fuzz(func(t *testing.T, memtableSize int, walSize int, compression bool, encryption bool) {
dir := setup(t)
defer teardown(dir)

k4, err := Open(dir, memtableSize, walSize, compression, encryption)
if err != nil {
t.Fatalf("Failed to open K4: %v", err)
}
defer k4.Close()

const numOperations = 100 // Number of operations to perform
for i := 0; i < numOperations; i++ {
key, _ := fuzz.RandomString(10)
value, _ := fuzz.RandomString(20)

// Test Put operation
err = k4.Put([]byte(key), []byte(value), nil)
if err != nil {
t.Fatalf("Failed to put key-value: %v", err)
}

// Test Get operation
got, err := k4.Get([]byte(key))
if err != nil {
t.Fatalf("Failed to get key: %v", err)
}
if !bytes.Equal(got, []byte(value)) {
t.Fatalf("Expected value %s, got %s", value, got)
}

// Test Update operation
newValue, _ := fuzz.RandomString(20)
err = k4.Put([]byte(key), []byte(newValue), nil)
if err != nil {
t.Fatalf("Failed to update key-value: %v", err)
}
got, err = k4.Get([]byte(key))
if err != nil {
t.Fatalf("Failed to get key: %v", err)
}
if !bytes.Equal(got, []byte(newValue)) {
t.Fatalf("Expected value %s, got %s", newValue, got)
}

// Test Delete operation
err = k4.Delete([]byte(key))
if err != nil {
t.Fatalf("Failed to delete key: %v", err)
}
got, err = k4.Get([]byte(key))
if err != nil {
t.Fatalf("Failed to get key: %v", err)
}
if got != nil {
t.Fatalf("Expected nil, got %s for key %s", got, key)
}
}
})
}

0 comments on commit af5809c

Please sign in to comment.