Commit ebd0a2df authored by Jeromy's avatar Jeromy
Browse files

remove multiple multihash deps

parent 4a64aae7
# multihash tool
The `multihash` tool uses `go-multihash` to hash things much like `shasum`.
Warning: this is a **multihash** tool! Its digests follow the [multihash](https://github.com/jbenet/multihash) format.
### Install
- From Source:
```
go get github.com/jbenet/go-multihash/multihash
```
- Precompiled Binaries: https://gobuilder.me/github.com/jbenet/go-multihash/multihash
### Usage
```sh
> multihash -h
usage: ./multihash [options] [FILE]
Print or check multihash checksums.
With no FILE, or when FILE is -, read standard input.
Options:
-a="sha2-256": one of: sha1, sha2-256, sha2-512, sha3 (shorthand)
-algorithm="sha2-256": one of: sha1, sha2-256, sha2-512, sha3
-c="": check checksum matches (shorthand)
-check="": check checksum matches
-e="base58": one of: raw, hex, base58, base64 (shorthand)
-encoding="base58": one of: raw, hex, base58, base64
-l=-1: checksums length in bits (truncate). -1 is default (shorthand)
-length=-1: checksums length in bits (truncate). -1 is default
```
### Examples
#### Input
```sh
# from stdin
> multihash < main.go
QmRZxt2b1FVZPNqd8hsiykDL3TdBDeTSPX9Kv46HmX4Gx8
# from file
> ./multihash main.go
QmRZxt2b1FVZPNqd8hsiykDL3TdBDeTSPX9Kv46HmX4Gx8
# from stdin "filename"
> multihash - < main.go
QmRZxt2b1FVZPNqd8hsiykDL3TdBDeTSPX9Kv46HmX4Gx8
```
#### Algorithms
```sh
> multihash -a ?
error: algorithm '?' not one of: sha1, sha2-256, sha2-512, sha3
> multihash -a sha1 < main.go
5drkbcqJUo6fZVvcZJeVEVWAgndvLm
> multihash -a sha2-256 < main.go
QmcK3s36goo9v2HYcfTrDKKwxaxmJJ59etodQQFYsL5T5N
> multihash -a sha2-512 < main.go
8VuDcW4CooyPQA8Cc4eYpwjhyDJZqu5m5ZMDFzWULYsVS8d119JaGeNWsZbZ2ZG2kPtbrMx31MidokCigaD65yUPAs
> multihash -a sha3 < main.go
8tWDCTfAX24DYmzNixTj2ARJkqwRG736VHx5aJppmqRjhW9QT1EuTgKUmu9Pmunzq292jzPKxb2VxSsTXmjFY1HD3B
```
#### Encodings
```sh
> multihash -e raw < main.go
Ϛ�����I�5 S��WG>���_��]g�����u
> multihash -e hex < main.go
1220cf9aa2b8a38b9b49d135095390059a57473e97aceb5fcae25d67a8b6feb58275
> multihash -e base64 < main.go
EiDPmqK4o4ubSdE1CVOQBZpXRz6XrOtfyuJdZ6i2/rWCdQ==
> multihash -e base58 < main.go
Qmf1QjEXDmqBm7RqHKqFGNUyhzUjnX7cmgKMrGzzPceZDQ
```
#### Digest Length
```sh
# we're outputing hex (good byte alignment) to show the codes changing
# notice the multihash code (first 2 chars) differs!
> multihash -e hex -a sha2-256 -l 256 < main.go
1220cf9aa2b8a38b9b49d135095390059a57473e97aceb5fcae25d67a8b6feb58275
> multihash -e hex -a sha2-512 -l 256 < main.go
132047a4b6c629f5545f529b0ff461dc09119969f3593186277a1cc7a8ea3560a6f1
> multihash -e hex -a sha3 -l 256 < main.go
14206b9222a1a47939e665261bd2b5573e55e7988675223adde73c1011066ad66335
# notice the multihash length (next 2 chars) differs!
> multihash -e hex -a sha2-256 -l 256 < main.go
1220cf9aa2b8a38b9b49d135095390059a57473e97aceb5fcae25d67a8b6feb58275
> multihash -e hex -a sha2-256 -l 200 < main.go
1219cf9aa2b8a38b9b49d135095390059a57473e97aceb5fcae25d
```
#### Verify Checksum
```sh
> multihash -c QmRZxt2b1FVZPNqd8hsiykDL3TdBDeTSPX9Kv46HmX4Gx8 < main.go
OK checksums match (-q for no output)
> multihash -c QmcKaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa < main.go
error: computed checksum did not match (-q for no output)
# works with other arguments too
> multihash -e hex -l 128 -c "12102ffc284a1e82bf51e567c75b2ae6edb9" < main.go
OK checksums match (-q for no output)
```
#!/bin/sh
bin=multihash
# this script is currently brain dead.
# it merely tries two locations.
# in the future maybe use value of $PATH.
binpath=/usr/local/bin
if [ -d "$binpath" ]; then
mv "$bin" "$binpath/$bin"
echo "installed $binpath/$bin"
exit 0
fi
binpath=/usr/bin
if [ -d "$binpath" ]; then
mv "$bin" "$binpath/$bin"
echo "installed $binpath/$bin"
exit 0
fi
package main
import (
"flag"
"fmt"
"io"
"os"
mh "QmdeauTdyf38KDQB4Cc4CurPWRRb5pej27NCXPA7kbPTJy/go-multihash"
mhopts "QmdeauTdyf38KDQB4Cc4CurPWRRb5pej27NCXPA7kbPTJy/go-multihash/opts"
)
var usage = `usage: %s [options] [FILE]
Print or check multihash checksums.
With no FILE, or when FILE is -, read standard input.
Options:
`
// flags
var opts *mhopts.Options
var checkRaw string
var checkMh mh.Multihash
var inputFilename string
var quiet bool
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, usage, os.Args[0])
flag.PrintDefaults()
}
opts = mhopts.SetupFlags(flag.CommandLine)
checkStr := "check checksum matches"
flag.StringVar(&checkRaw, "check", "", checkStr)
flag.StringVar(&checkRaw, "c", "", checkStr+" (shorthand)")
quietStr := "quiet output (no newline on checksum, no error text)"
flag.BoolVar(&quiet, "quiet", false, quietStr)
flag.BoolVar(&quiet, "q", false, quietStr+" (shorthand)")
}
func parseFlags(o *mhopts.Options) error {
flag.Parse()
if err := o.ParseError(); err != nil {
return err
}
if checkRaw != "" {
var err error
checkMh, err = mhopts.Decode(o.Encoding, checkRaw)
if err != nil {
return fmt.Errorf("fail to decode check '%s': %s", checkRaw, err)
}
}
return nil
}
func getInput() (io.ReadCloser, error) {
args := flag.Args()
switch {
case len(args) < 1:
inputFilename = "-"
return os.Stdin, nil
case args[0] == "-":
inputFilename = "-"
return os.Stdin, nil
default:
inputFilename = args[0]
f, err := os.Open(args[0])
if err != nil {
return nil, fmt.Errorf("failed to open '%s': %s", args[0], err)
}
return f, nil
}
}
func printHash(o *mhopts.Options, r io.Reader) error {
h, err := o.Multihash(r)
if err != nil {
return err
}
s, err := mhopts.Encode(o.Encoding, h)
if err != nil {
return err
}
if quiet {
fmt.Print(s)
} else {
fmt.Println(s)
}
return nil
}
func main() {
checkErr := func(err error) {
if err != nil {
die("error: ", err)
}
}
err := parseFlags(opts)
checkErr(err)
inp, err := getInput()
checkErr(err)
if checkMh != nil {
err = opts.Check(inp, checkMh)
checkErr(err)
if !quiet {
fmt.Println("OK checksums match (-q for no output)")
}
} else {
err = printHash(opts, inp)
checkErr(err)
}
inp.Close()
}
func die(v ...interface{}) {
if !quiet {
fmt.Fprint(os.Stderr, v...)
fmt.Fprint(os.Stderr, "\n")
}
// flag.Usage()
os.Exit(1)
}
package multihash
import (
"bytes"
"encoding/hex"
"fmt"
"testing"
)
// maybe silly, but makes it so changing
// the table accidentally has to happen twice.
var tCodes = map[int]string{
0x11: "sha1",
0x12: "sha2-256",
0x13: "sha2-512",
0x14: "sha3",
0x40: "blake2b",
0x41: "blake2s",
}
type TestCase struct {
hex string
code int
name string
}
var testCases = []TestCase{
TestCase{"0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33", 0x11, "sha1"},
TestCase{"0beec7b5", 0x11, "sha1"},
TestCase{"2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae", 0x12, "sha2-256"},
TestCase{"2c26b46b", 0x12, "sha2-256"},
TestCase{"0beec7b5ea3f0fdbc9", 0x40, "blake2b"},
}
func (tc TestCase) Multihash() (Multihash, error) {
ob, err := hex.DecodeString(tc.hex)
if err != nil {
return nil, err
}
b := make([]byte, 2+len(ob))
b[0] = byte(uint8(tc.code))
b[1] = byte(uint8(len(ob)))
copy(b[2:], ob)
return Cast(b)
}
func TestEncode(t *testing.T) {
for _, tc := range testCases {
ob, err := hex.DecodeString(tc.hex)
if err != nil {
t.Error(err)
continue
}
pre := make([]byte, 2)
pre[0] = byte(uint8(tc.code))
pre[1] = byte(uint8(len(ob)))
nb := append(pre, ob...)
encC, err := Encode(ob, tc.code)
if err != nil {
t.Error(err)
continue
}
if !bytes.Equal(encC, nb) {
t.Error("encoded byte mismatch: ", encC, nb)
}
encN, err := EncodeName(ob, tc.name)
if err != nil {
t.Error(err)
continue
}
if !bytes.Equal(encN, nb) {
t.Error("encoded byte mismatch: ", encN, nb)
}
h, err := tc.Multihash()
if err != nil {
t.Error(err)
}
if !bytes.Equal(h, nb) {
t.Error("Multihash func mismatch.")
}
}
}
func ExampleEncodeName() {
// ignores errors for simplicity - don't do that at home.
buf, _ := hex.DecodeString("0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
mhbuf, _ := EncodeName(buf, "sha1")
mhhex := hex.EncodeToString(mhbuf)
fmt.Printf("hex: %v\n", mhhex)
// Output:
// hex: 11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33
}
func TestDecode(t *testing.T) {
for _, tc := range testCases {
ob, err := hex.DecodeString(tc.hex)
if err != nil {
t.Error(err)
continue
}
pre := make([]byte, 2)
pre[0] = byte(uint8(tc.code))
pre[1] = byte(uint8(len(ob)))
nb := append(pre, ob...)
dec, err := Decode(nb)
if err != nil {
t.Error(err)
continue
}
if dec.Code != tc.code {
t.Error("decoded code mismatch: ", dec.Code, tc.code)
}
if dec.Name != tc.name {
t.Error("decoded name mismatch: ", dec.Name, tc.name)
}
if dec.Length != len(ob) {
t.Error("decoded length mismatch: ", dec.Length, len(ob))
}
if !bytes.Equal(dec.Digest, ob) {
t.Error("decoded byte mismatch: ", dec.Digest, ob)
}
}
}
func TestTable(t *testing.T) {
for k, v := range tCodes {
if Codes[k] != v {
t.Error("Table mismatch: ", Codes[k], v)
}
if Names[v] != k {
t.Error("Table mismatch: ", Names[v], k)
}
}
}
func ExampleDecode() {
// ignores errors for simplicity - don't do that at home.
buf, _ := hex.DecodeString("0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
mhbuf, _ := EncodeName(buf, "sha1")
o, _ := Decode(mhbuf)
mhhex := hex.EncodeToString(o.Digest)
fmt.Printf("obj: %v 0x%x %d %s\n", o.Name, o.Code, o.Length, mhhex)
// Output:
// obj: sha1 0x11 20 0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33
}
func TestValidCode(t *testing.T) {
for i := 0; i < 0xff; i++ {
_, ok := tCodes[i]
b := AppCode(i) || ok
if ValidCode(i) != b {
t.Error("ValidCode incorrect for: ", i)
}
}
}
func TestAppCode(t *testing.T) {
for i := 0; i < 0xff; i++ {
b := i >= 0 && i < 0x10
if AppCode(i) != b {
t.Error("AppCode incorrect for: ", i)
}
}
}
func TestCast(t *testing.T) {
for _, tc := range testCases {
ob, err := hex.DecodeString(tc.hex)
if err != nil {
t.Error(err)
continue
}
pre := make([]byte, 2)
pre[0] = byte(uint8(tc.code))
pre[1] = byte(uint8(len(ob)))
nb := append(pre, ob...)
if _, err := Cast(nb); err != nil {
t.Error(err)
continue
}
if _, err = Cast(ob); err == nil {
t.Error("cast failed to detect non-multihash")
continue
}
}
}
func TestHex(t *testing.T) {
for _, tc := range testCases {
ob, err := hex.DecodeString(tc.hex)
if err != nil {
t.Error(err)
continue
}
pre := make([]byte, 2)
pre[0] = byte(uint8(tc.code))
pre[1] = byte(uint8(len(ob)))
nb := append(pre, ob...)
hs := hex.EncodeToString(nb)
mh, err := FromHexString(hs)
if err != nil {
t.Error(err)
continue
}
if !bytes.Equal(mh, nb) {
t.Error("FromHexString failed", nb, mh)
continue
}
if mh.HexString() != hs {
t.Error("Multihash.HexString failed", hs, mh.HexString)
continue
}
}
}
func BenchmarkEncode(b *testing.B) {
tc := testCases[0]
ob, err := hex.DecodeString(tc.hex)
if err != nil {
b.Error(err)
return
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
Encode(ob, tc.code)
}
}
func BenchmarkDecode(b *testing.B) {
tc := testCases[0]
ob, err := hex.DecodeString(tc.hex)
if err != nil {
b.Error(err)
return
}
pre := make([]byte, 2)
pre[0] = byte(uint8(tc.code))
pre[1] = byte(uint8(len(ob)))
nb := append(pre, ob...)
b.ResetTimer()
for i := 0; i < b.N; i++ {
Decode(nb)
}
}
# mhopts - multihash options for writing commands
`mhopts` is a small package that helps to write commands which
may take multihash options. Check it out in action:
- [multihash](../multihash)
- [hashpipe](https://github.com/jbenet/go-hashpipe)
Godoc: [https://godoc.org/github.com/jbenet/go-multihash/opts](https://godoc.org/github.com/jbenet/go-multihash/opts)
package opts
import (
"encoding/base64"
"encoding/hex"
"fmt"
base58 "QmNsoHoCVhgXcv1Yg45jtkMgimxorTAN36fV9AQMFXHHAQ/go-base58"
mh "QmdeauTdyf38KDQB4Cc4CurPWRRb5pej27NCXPA7kbPTJy/go-multihash"
)
func Decode(encoding, digest string) (mh.Multihash, error) {
switch encoding {
case "raw":
return mh.Cast([]byte(digest))
case "hex":
return hex.DecodeString(digest)
case "base58":
return base58.Decode(digest), nil
case "base64":
return base64.StdEncoding.DecodeString(digest)
default:
return nil, fmt.Errorf("unknown encoding: %s", encoding)
}
}
func Encode(encoding string, hash mh.Multihash) (string, error) {
switch encoding {
case "raw":
return string(hash), nil
case "hex":
return hex.EncodeToString(hash), nil
case "base58":
return base58.Encode(hash), nil
case "base64":
return base64.StdEncoding.EncodeToString(hash), nil
default:
return "", fmt.Errorf("unknown encoding: %s", encoding)
}
}
// Package opts helps to write commands which may take multihash
// options.
package opts
import (
"bytes"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"strings"
mh "QmdeauTdyf38KDQB4Cc4CurPWRRb5pej27NCXPA7kbPTJy/go-multihash"
)
// package errors
var (
ErrMatch = errors.New("multihash checksums did not match")
)
// Options is a struct used to parse cli flags.
type Options struct {
Encoding string
Algorithm string
AlgorithmCode int
Length int
fs *flag.FlagSet
}
// FlagValues are the values the various option flags can take.
var FlagValues = struct {
Encodings []string
Algorithms []string
}{
Encodings: []string{"raw", "hex", "base58", "base64"},
Algorithms: []string{"sha1", "sha2-256", "sha2-512", "sha3"},
}
// SetupFlags adds multihash related options to given flagset.
func SetupFlags(f *flag.FlagSet) *Options {
// TODO: add arg for adding opt prefix and/or overriding opts
o := new(Options)
algoStr := "one of: " + strings.Join(FlagValues.Algorithms, ", ")
f.StringVar(&o.Algorithm, "algorithm", "sha2-256", algoStr)
f.StringVar(&o.Algorithm, "a", "sha2-256", algoStr+" (shorthand)")
encStr := "one of: " + strings.Join(FlagValues.Encodings, ", ")
f.StringVar(&o.Encoding, "encoding", "base58", encStr)
f.StringVar(&o.Encoding, "e", "base58", encStr+" (shorthand)")
lengthStr := "checksums length in bits (truncate). -1 is default"
f.IntVar(&o.Length, "length", -1, lengthStr)
f.IntVar(&o.Length, "l", -1, lengthStr+" (shorthand)")
return o
}
// Parse parses the values of flags from given argument slice.
// It is equivalent to flags.Parse(args)
func (o *Options) Parse(args []string) error {
if err := o.fs.Parse(args); err != nil {
return err
}
return o.ParseError()
}
// ParseError checks the parsed options for errors.
func (o *Options) ParseError() error {
if !strIn(o.Encoding, FlagValues.Encodings) {
return fmt.Errorf("encoding '%s' not %s", o.Encoding, FlagValues.Encodings)
}
if !strIn(o.Algorithm, FlagValues.Algorithms) {
return fmt.Errorf("algorithm '%s' not %s", o.Algorithm, FlagValues.Algorithms)
}
var found bool
o.AlgorithmCode, found = mh.Names[o.Algorithm]
if !found {
return fmt.Errorf("algorithm '%s' not found (lib error, pls report).", o.Algorithm)
}
if o.Length >= 0 {
if o.Length%8 != 0 {
return fmt.Errorf("length must be multiple of 8")
}
o.Length = o.Length / 8
if o.Length > mh.DefaultLengths[o.AlgorithmCode] {
o.Length = mh.DefaultLengths[o.AlgorithmCode]
}
}
return nil
}
// strIn checks wither string a is in set.
func strIn(a string, set []string) bool {
for _, s := range set {
if s == a {
return true
}
}
return false
}
// Check reads all the data in r, calculates its multihash,
// and checks it matches h1
func (o *Options) Check(r io.Reader, h1 mh.Multihash) error {
h2, err := o.Multihash(r)
if err != nil {
return err
}
if !bytes.Equal(h1, h2) {
return fmt.Errorf("computed checksum did not match")
}
return nil
}
// Multihash reads all the data in r and calculates its multihash.
func (o *Options) Multihash(r io.Reader) (mh.Multihash, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
return mh.Sum(b, o.AlgorithmCode, o.Length)
}
{
"name": "go-multihash",
"author": "whyrusleeping",
"version": "1.0.0",
"gxDependencies": [
{
"name": "go-base58",
"hash": "QmNsoHoCVhgXcv1Yg45jtkMgimxorTAN36fV9AQMFXHHAQ",
"version": "1.0.0"
},
{
"name": "crypto-sha3",
"hash": "QmY1q6BMPywiUXEKAjehsgmPaBeLHTzs3FNaptUsbmpngb",
"version": "1.0.0"
}
],
"language": "go",
"gx": {
"dvcsimport": "github.com/jbenet/go-multihash"
}
}
\ No newline at end of file
package multihash
import (
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"errors"
"fmt"
sha3 "QmY1q6BMPywiUXEKAjehsgmPaBeLHTzs3FNaptUsbmpngb/crypto-sha3"
)
var ErrSumNotSupported = errors.New("Function not implemented. Complain to lib maintainer.")
func Sum(data []byte, code int, length int) (Multihash, error) {
m := Multihash{}
err := error(nil)
if !ValidCode(code) {
return m, fmt.Errorf("invalid multihash code %d", code)
}
var d []byte
switch code {
case SHA1:
d = sumSHA1(data)
case SHA2_256:
d = sumSHA256(data)
case SHA2_512:
d = sumSHA512(data)
case SHA3:
d, err = sumSHA3(data)
default:
return m, ErrSumNotSupported
}
if err != nil {
return m, err
}
if length < 0 {
var ok bool
length, ok = DefaultLengths[code]
if !ok {
return m, fmt.Errorf("no default length for code %d", code)
}
}
return Encode(d[0:length], code)
}
func sumSHA1(data []byte) []byte {
a := sha1.Sum(data)
return a[0:20]
}
func sumSHA256(data []byte) []byte {
a := sha256.Sum256(data)
return a[0:32]
}
func sumSHA512(data []byte) []byte {
a := sha512.Sum512(data)
return a[0:64]
}
func sumSHA3(data []byte) ([]byte, error) {
h := sha3.New512()
if _, err := h.Write(data); err != nil {
return nil, err
}
return h.Sum(nil), nil
}
package multihash
import (
"bytes"
"testing"
)
type SumTestCase struct {
code int
length int
input string
hex string
}
var sumTestCases = []SumTestCase{
SumTestCase{SHA1, -1, "foo", "11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"},
SumTestCase{SHA1, 10, "foo", "110a0beec7b5ea3f0fdbc95d"},
SumTestCase{SHA2_256, -1, "foo", "12202c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"},
SumTestCase{SHA2_256, 16, "foo", "12102c26b46b68ffc68ff99b453c1d304134"},
SumTestCase{SHA2_512, -1, "foo", "1340f7fbba6e0636f890e56fbbf3283e524c6fa3204ae298382d624741d0dc6638326e282c41be5e4254d8820772c5518a2c5a8c0c7f7eda19594a7eb539453e1ed7"},
SumTestCase{SHA2_512, 32, "foo", "1320f7fbba6e0636f890e56fbbf3283e524c6fa3204ae298382d624741d0dc663832"},
}
func TestSum(t *testing.T) {
for _, tc := range sumTestCases {
m1, err := FromHexString(tc.hex)
if err != nil {
t.Error(err)
continue
}
m2, err := Sum([]byte(tc.input), tc.code, tc.length)
if err != nil {
t.Error(tc.code, "sum failed.", err)
continue
}
if !bytes.Equal(m1, m2) {
t.Error(tc.code, "sum failed.", m1, m2)
}
s1 := m1.HexString()
if s1 != tc.hex {
t.Error("hex strings not the same")
}
s2 := m1.B58String()
m3, err := FromB58String(s2)
if err != nil {
t.Error("failed to decode b58")
} else if !bytes.Equal(m3, m1) {
t.Error("b58 failing bytes")
} else if s2 != m3.B58String() {
t.Error("b58 failing string")
}
}
}
func BenchmarkSum(b *testing.B) {
tc := sumTestCases[0]
for i := 0; i < b.N; i++ {
Sum([]byte(tc.input), tc.code, tc.length)
}
}
BINS = bin/multihash
MULTIHASH_ROOT = ../
MULTIHASH_CMD = ../multihash
all: deps
deps: bins
clean:
rm $(BINS)
bins: $(BINS)
bin/multihash: $(MULTIHASH_ROOT)/**/*.go
go build -o bin/multihash $(MULTIHASH_CMD)
test: test_expensive
test_expensive:
cd sharness && make TEST_EXPENSIVE=1
test_cheap:
cd sharness && make
.PHONY: all clean
# Run tests
#
# Copyright (c) 2014 Christian Couder
# MIT Licensed; see the LICENSE file in this repository.
#
# NOTE: run with TEST_VERBOSE=1 for verbose sharness tests.
T = $(sort $(wildcard t[0-9][0-9][0-9][0-9]-*.sh))
BINS = bin/multihash
SHARNESS = lib/sharness/sharness.sh
all: clean deps $(T) aggregate
clean:
@echo "*** $@ ***"
-rm -rf test-results
$(T):
@echo "*** $@ ***"
./$@
aggregate:
@echo "*** $@ ***"
lib/test-aggregate-results.sh
deps: $(SHARNESS) $(BINS)
$(SHARNESS):
@echo "*** installing $@ ***"
lib/install-sharness.sh
bin/%:
@echo "*** installing $@ ***"
cd .. && make $@
.PHONY: all clean $(T) aggregate
#!/bin/sh
# install sharness.sh
#
# Copyright (c) 2014 Juan Batiz-Benet
# MIT Licensed; see the LICENSE file in this repository.
#
# settings
version=50229a79ba22b2f13ccd82451d86570fecbd194c
urlprefix=https://github.com/mlafeldt/sharness.git
clonedir=lib
sharnessdir=sharness
die() {
echo >&2 "$@"
exit 1
}
mkdir -p "$clonedir" || die "Could not create '$clonedir' directory"
cd "$clonedir" || die "Could not cd into '$clonedir' directory"
git clone "$urlprefix" || die "Could not clone '$urlprefix'"
cd "$sharnessdir" || die "Could not cd into '$sharnessdir' directory"
git checkout "$version" || die "Could not checkout '$version'"
exit 0
#!/bin/sh
#
# Script to aggregate results using Sharness
#
# Copyright (c) 2014 Christian Couder
# MIT Licensed; see the LICENSE file in this repository.
#
SHARNESS_AGGREGATE="lib/sharness/aggregate-results.sh"
test -f "$SHARNESS_AGGREGATE" || {
echo >&2 "Cannot find: $SHARNESS_AGGREGATE"
echo >&2 "Please check Sharness installation."
exit 1
}
ls test-results/t*-*.sh.*.counts | "$SHARNESS_AGGREGATE"
# Test framework for go-ipfs
#
# Copyright (c) 2014 Christian Couder
# MIT Licensed; see the LICENSE file in this repository.
#
# We are using sharness (https://github.com/mlafeldt/sharness)
# which was extracted from the Git test framework.
# Use the multihash tool to test against
# Add current directory to path, for multihash tool.
PATH=$(pwd)/bin:${PATH}
# Set sharness verbosity. we set the env var directly as
# it's too late to pass in --verbose, and --verbose is harder
# to pass through in some cases.
test "$TEST_VERBOSE" = 1 && verbose=t
# assert the `multihash` we're using is the right one.
if test `which multihash` != $(pwd)/bin/multihash; then
echo >&2 "Cannot find the tests' local multihash tool."
echo >&2 "Please check test and multihash tool installation."
exit 1
fi
SHARNESS_LIB="lib/sharness/sharness.sh"
. "$SHARNESS_LIB" || {
echo >&2 "Cannot source: $SHARNESS_LIB"
echo >&2 "Please check Sharness installation."
exit 1
}
# Please put go-multihash specific shell functions below
for hashbin in sha1sum shasum; do
if type "$hashbin"; then
export SHASUMBIN="$hashbin" &&
test_set_prereq SHASUM &&
break
fi
done
#!/bin/sh
#
# Copyright (c) 2015 Christian Couder
# MIT Licensed; see the LICENSE file in this repository.
#
test_description="Basic tests"
. lib/test-lib.sh
test_expect_success "current dir is writable" '
echo "It works!" >test.txt
'
test_expect_success "multihash is available" '
type multihash
'
test_expect_success "multihash help output looks good" '
test_must_fail multihash -h 2>help.txt &&
cat help.txt | egrep -i "^usage:" >/dev/null &&
cat help.txt | egrep -i "multihash .*options.*file" >/dev/null
'
test_done
#!/bin/sh
#
# Copyright (c) 2015 Christian Couder
# MIT Licensed; see the LICENSE file in this repository.
#
test_description="sha1 tests"
. lib/test-lib.sh
test_expect_success "setup sha1 tests" '
echo "Hash me!" >hash_me.txt &&
SHA1=bc6f2c3cd945bc754789e50b2f68deee2f421810 &&
echo "1114$SHA1" >actual
'
test_expect_success "'multihash -a=sha1 -e=hex' works" '
multihash -a=sha1 -e=hex hash_me.txt >expected
'
test_expect_success "'multihash -a=sha1 -e=hex' output looks good" '
test_cmp expected actual
'
test_expect_success SHASUM "check hash using shasum" '
echo "$SHA1 hash_me.txt" >actual &&
$SHASUMBIN hash_me.txt >expected &&
test_cmp expected actual
'
test_done
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment