<commit_before>package qcow2

import (
	"bufio"
	"bytes"
	"compress/gzip"
	"encoding/binary"
	"os"
	"testing"
)

func TestHeader(t *testing.T) {
	f, err := os.Open("./testdata/file.qcow2.gz")
	if err != nil {
		t.Fatal(err)
	}
	gz, err := gzip.NewReader(f)
	if err != nil {
		t.Fatal(err)
	}
	rdr := bufio.NewReader(gz)

	buf := make([]byte, V2HeaderSize)
	size, err := rdr.Read(buf)
	if err != nil {
		t.Fatal(err)
	}
	if size < V2HeaderSize {
		t.Fatal(err)
	}

	if bytes.Compare(buf[:4], Magic) != 0 {
		t.Fatalf("[ERR] Does not appear to be qcow file %#v %#v\n", buf[:4], Magic)
	}

	q := Header{<commit_msg>test: adding steps used to create testdata<commit_after>package qcow2

import (
	"bufio"
	"bytes"
	"compress/gzip"
	"encoding/binary"
	"os"
	"testing"
)

/*
qemu-img create -f qcow2 file.qcow2 100m
sudo modprobe nbd max_part=63
sudo qemu-nbd -f qcow2 -c /dev/nbd0 file.qcow2
sudo mkfs.ext2 /dev/nbd0
mkdir -p file/
sudo mount /dev/nbd0 file/
sudo umount file/
sudo qemu-nbd -d /dev/nbd0
sudo qemu-img snapshot -c base file.qcow2
sudo qemu-nbd -f qcow2 -c /dev/nbd0 file.qcow2
sudo mount /dev/nbd0 file
echo Howdy | sudo dd of=file/hello.txt
sudo umount file/
sudo qemu-nbd -d /dev/nbd0
sudo qemu-img snapshot -c hello file.qcow2
sudo qemu-nbd -f qcow2 -c /dev/nbd0 file.qcow2
sudo mount /dev/nbd0 file
sudo rm file/hello.txt
sudo umount file/
sudo qemu-nbd -d /dev/nbd0
ls -lsh ./file.qcow2
# 4.9M -rw-r--r--. 1 vbatts vbatts 5.1M Sep  3 13:38 file.qcow2
qcow2-info ./file.qcow2
# image: ./file.qcow2
# file format: qcow2
# virtual size: 100M (104857600 bytes)
# disk size: 4.8M
# cluster_size: 65536
# Snapshot list:
# ID        TAG                 VM SIZE                DATE       VM CLOCK
# 1         base                      0 2015-09-03 13:36:55   00:00:00.000
# 2         hello                     0 2015-09-03 13:38:07   00:00:00.000
# Format specific information:
#    compat: 1.1
#    lazy refcounts: false
#    refcount bits: 16
#    corrupt: false
gzip -9 file.qcow2 > file.qcow2.gz
*/
var testQcowFile = "./testdata/file.qcow2.gz"

func TestHeader(t *testing.T) {
	f, err := os.Open(testQcowFile)
	if err != nil {
		t.Fatal(err)
	}
	gz, err := gzip.NewReader(f)
	if err != nil {
		t.Fatal(err)
	}
	rdr := bufio.NewReader(gz)

	buf := make([]byte, V2HeaderSize)
	size, err := rdr.Read(buf)
	if err != nil {
		t.Fatal(err)
	}
	if size < V2HeaderSize {
		t.Fatal(err)
	}

	if bytes.Compare(buf[:4], Magic) != 0 {
		t.Fatalf("[ERR] Does not appear to be qcow file %#v %#v\n", buf[:4], Magic)
	}

	q := Header{
{'fraction_non_alphanumeric': 0.1485578822599763, 'fraction_numerical': 0.05215329909126827, 'mean_word_length': 2.734513274336283, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1145117', 'n_tokens_mistral': 1186, 'n_tokens_neox': 1090, 'n_words': 286}