check hosts in ssl certificates

This commit is contained in:
Mikaël Cluseau
2018-08-09 15:07:53 +02:00
parent 481115e0d0
commit 331f9ea96c
362 changed files with 2499 additions and 59344 deletions

View File

@ -1,77 +0,0 @@
package cpio_test
import (
"bytes"
"fmt"
"io"
"log"
"os"
"github.com/cavaliercoder/go-cpio"
)
func Example() {
// Create a buffer to write our archive to.
buf := new(bytes.Buffer)
// Create a new cpio archive.
w := cpio.NewWriter(buf)
// Add some files to the archive.
var files = []struct {
Name, Body string
}{
{"readme.txt", "This archive contains some text files."},
{"gopher.txt", "Gopher names:\nGeorge\nGeoffrey\nGonzo"},
{"todo.txt", "Get animal handling license."},
}
for _, file := range files {
hdr := &cpio.Header{
Name: file.Name,
Mode: 0600,
Size: int64(len(file.Body)),
}
if err := w.WriteHeader(hdr); err != nil {
log.Fatalln(err)
}
if _, err := w.Write([]byte(file.Body)); err != nil {
log.Fatalln(err)
}
}
// Make sure to check the error on Close.
if err := w.Close(); err != nil {
log.Fatalln(err)
}
// Open the cpio archive for reading.
b := bytes.NewReader(buf.Bytes())
r := cpio.NewReader(b)
// Iterate through the files in the archive.
for {
hdr, err := r.Next()
if err == io.EOF {
// end of cpio archive
break
}
if err != nil {
log.Fatalln(err)
}
fmt.Printf("Contents of %s:\n", hdr.Name)
if _, err := io.Copy(os.Stdout, r); err != nil {
log.Fatalln(err)
}
fmt.Println()
}
// Output:
// Contents of readme.txt:
// This archive contains some text files.
// Contents of gopher.txt:
// Gopher names:
// George
// Geoffrey
// Gonzo
// Contents of todo.txt:
// Get animal handling license.
}

View File

@ -1,121 +0,0 @@
package cpio
import (
"fmt"
"io"
"log"
"os"
"testing"
)
var files = []struct {
Name, Body string
}{
{"./gophers.txt", "Gopher names:\nGeorge\nGeoffrey\nGonzo"},
{"./readme.txt", "This archive contains some text files."},
{"./todo.txt", "Get animal handling license."},
}
func TestRead(t *testing.T) {
f, err := os.Open("testdata/test_svr4_crc.cpio")
if err != nil {
t.Fatalf("error opening test file: %v", err)
}
defer f.Close()
r := NewReader(f)
for {
_, err := r.Next()
if err == io.EOF {
return
}
if err != nil {
t.Errorf("error moving to next header: %v", err)
return
}
// TODO: validate header fields
}
}
func TestSVR4CRC(t *testing.T) {
f, err := os.Open("testdata/test_svr4_crc.cpio")
if err != nil {
t.Fatalf("error opening test file: %v", err)
}
defer f.Close()
w := NewHash()
r := NewReader(f)
for {
hdr, err := r.Next()
if err != nil {
if err != io.EOF {
t.Errorf("error moving to next header: %v", err)
}
return
}
if hdr.Mode.IsRegular() {
w.Reset()
_, err = io.CopyN(w, r, hdr.Size)
if err != nil {
t.Fatalf("error writing to checksum hash: %v", err)
}
sum := Checksum(w.Sum32())
if sum != hdr.Checksum {
t.Errorf("expected checksum %v, got %v for %v", hdr.Checksum, sum, hdr.Name)
}
}
}
}
func ExampleNewHash() {
// Open the cpio archive for reading.
f, err := os.Open("testdata/test_svr4_crc.cpio")
if err != nil {
log.Fatal(err)
}
defer f.Close()
r := NewReader(f)
// create a Hash
h := NewHash()
// Iterate through the files in the archive.
for {
hdr, err := r.Next()
if err == io.EOF {
// end of cpio archive
return
}
if err != nil {
log.Fatal(err)
}
// skip symlinks, directories, etc.
if !hdr.Mode.IsRegular() {
continue
}
// read file into hash
h.Reset()
_, err = io.CopyN(h, r, hdr.Size)
if err != nil {
log.Fatal(err)
}
// check hash matches header checksum
sum := Checksum(h.Sum32())
if sum == hdr.Checksum {
fmt.Printf("Checksum OK: %v (%v)\n", hdr.Name, hdr.Checksum)
} else {
fmt.Printf("Checksum FAIL: %v - expected %v, got %v\n", hdr.Name, hdr.Checksum, sum)
}
}
// Output:
// Checksum OK: gophers.txt (00000C98)
// Checksum OK: readme.txt (00000E3D)
// Checksum OK: todo.txt (00000A52)
}

View File

@ -1,26 +0,0 @@
SOURCES = \
gophers.txt \
readme.txt \
todo.txt \
checklist.txt
ARCHIVES = \
test_odc.cpio \
test_svr4.cpio \
test_svr4_crc.cpio
all: $(ARCHIVES)
test_odc.cpio: $(SOURCES)
echo $(SOURCES) | tr " " "\n" | cpio -o --owner=0:0 --format=odc > $@
test_svr4.cpio: $(SOURCES)
echo $(SOURCES) | tr " " "\n" | cpio -o --owner=0:0 --format=newc > $@
test_svr4_crc.cpio: $(SOURCES)
echo $(SOURCES) | tr " " "\n" | cpio -o --owner=0:0 --format=crc > $@
clean:
rm -f $(ARCHIVES) version.txt
.PHONY: all clean

View File

@ -1 +0,0 @@
todo.txt

View File

@ -1,2 +0,0 @@
127.0.0.1 localhost
::1 localhost

View File

@ -1,4 +0,0 @@
Gopher names:
George
Geoffrey
Gonzo

View File

@ -1 +0,0 @@
This archive contains some text files.

Binary file not shown.

Binary file not shown.

View File

@ -1 +0,0 @@
Get animal handling license.

View File

@ -1,53 +0,0 @@
package cpio_test
import (
"bytes"
"io"
"os"
"testing"
cpio "github.com/cavaliercoder/go-cpio"
)
func store(w *cpio.Writer, fn string) error {
f, err := os.Open(fn)
if err != nil {
return err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return err
}
hdr, err := cpio.FileInfoHeader(fi, "")
if err != nil {
return err
}
if err := w.WriteHeader(hdr); err != nil {
return err
}
if !fi.IsDir() {
if _, err := io.Copy(w, f); err != nil {
return err
}
}
return err
}
func TestWriter(t *testing.T) {
var buf bytes.Buffer
w := cpio.NewWriter(&buf)
if err := store(w, "testdata/etc"); err != nil {
t.Fatalf("store: %v", err)
}
if err := store(w, "testdata/etc/hosts"); err != nil {
t.Fatalf("store: %v", err)
}
if err := w.Close(); err != nil {
t.Fatalf("Close: %v", err)
}
}