diff --git a/README.md b/README.md index 7cb9cf3..0d8e6d6 100644 --- a/README.md +++ b/README.md @@ -44,16 +44,15 @@ Restore a GPG key (builds on top of the above example): ## Known issues -The QR code format provide a limited feature set: - -* It only works for input data of size 2119 bytes or less. -* It can not be used to recover the data. +The QR code format provide a limited feature set and can not be used to recover +the data. A tool like `zbarimg` from the [zbar libary][zbar] can be used to +scan the qr codes so it can be read by the zbase32 format. ## Milestones * [x] Basic application * [x] Plain text format for print and easy scan/ocr -* [ ] QR Code format for easier scanning +* [x] QR Code format for easier scanning * [ ] Template system for custom output ## Contributing and license @@ -64,3 +63,4 @@ to this project, see [CONTRIBUTING.md]. [CONTRIBUTING.md]: https://github.com/corvus-ch/horcrux/blob/master/CONTRIBUTING.md [MIT]: https://github.com/corvus-ch/horcrux/blob/master/LICENSE [paperkey]: http://www.jabberwocky.com/software/paperkey/ +[zbar]: https://zbar.sourceforge.io diff --git a/format/base64/base64.go b/format/base64/base64.go index 72567ec..fc9b7fb 100644 --- a/format/base64/base64.go +++ b/format/base64/base64.go @@ -4,6 +4,7 @@ import ( "encoding/base64" "fmt" "io" + "os" ) // Name holds the name of the Format. @@ -24,11 +25,16 @@ func (f *Format) OutputFileName(x byte) string { return fmt.Sprintf("%s.base64.%03d", f.Stem, x) } -// Writer creates a new Format writer using the given writer as output. -func (f *Format) Writer(out io.Writer) (io.Writer, []io.Closer, error) { - enc := base64.NewEncoder(base64.StdEncoding, out) +// Writer creates a new base64 format writer for the part identified by x. +func (f *Format) Writer(x byte) (io.Writer, []io.Closer, error) { + file, err := os.Create(f.OutputFileName(x)) + if nil != err { + return nil, nil, err + } - return enc, []io.Closer{enc}, nil + enc := base64.NewEncoder(base64.StdEncoding, file) + + return enc, []io.Closer{file, enc}, nil } // Reader creates a new Format reader using the given reader as input. diff --git a/format/base64/base64_test.go b/format/base64/base64_test.go index cb1bb72..944cdbe 100644 --- a/format/base64/base64_test.go +++ b/format/base64/base64_test.go @@ -1,6 +1,8 @@ package base64_test import ( + "fmt" + "path/filepath" "testing" "github.com/corvus-ch/horcrux/format" @@ -9,40 +11,20 @@ import ( "github.com/stretchr/testify/assert" ) -var nameTests = []formatAssert.NameTest{ - {0, "mollis", "mollis.base64.000"}, - {1, "commodo", "commodo.base64.001"}, - {42, "pellentesque", "pellentesque.base64.042"}, - {181, "fringilla", "fringilla.base64.181"}, - {254, "venenatis", "venenatis.base64.254"}, - {255, "ridiculus", "ridiculus.base64.255"}, -} - -var dataTests = []formatAssert.DataTest{ - {[]byte{0}, "AA=="}, - {[]byte{0xff}, "/w=="}, - {[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, "////////"}, - {[]byte{ - 0xc0, 0x73, 0x62, 0x4a, 0xaf, 0x39, 0x78, 0x51, - 0x4e, 0xf8, 0x44, 0x3b, 0xb2, 0xa8, 0x59, 0xc7, - 0x5f, 0xc3, 0xcc, 0x6a, 0xf2, 0x6d, 0x5a, 0xaa, - }, "wHNiSq85eFFO+EQ7sqhZx1/DzGrybVqq"}, -} - func factory(s string) format.Format { return base64.New(s) } -func TestFormat_OutputFileName(t *testing.T) { - formatAssert.Name(t, nameTests, factory) -} - func TestFormat_Reader(t *testing.T) { - formatAssert.DataRead(t, dataTests, factory) + formatAssert.DataRead(t, factory, ".base64") } func TestFormat_Writer(t *testing.T) { - formatAssert.DataWrite(t, dataTests, factory) + formatAssert.DataWrite(t, factory, ".base64", func(file string, x byte) []string { + base := filepath.Base(file) + name := base[0 : len(base)-len(filepath.Ext(base))] + return []string{fmt.Sprintf("%s.base64.%03d", name, x)} + }) } func TestFormat_Name(t *testing.T) { diff --git a/format/base64/fixtures/48bit.base64 b/format/base64/fixtures/48bit.base64 new file mode 100644 index 0000000..80dcafa --- /dev/null +++ b/format/base64/fixtures/48bit.base64 @@ -0,0 +1 @@ +//////// \ No newline at end of file diff --git a/format/base64/fixtures/48bit.bin b/format/base64/fixtures/48bit.bin new file mode 100644 index 0000000..aef30e1 --- /dev/null +++ b/format/base64/fixtures/48bit.bin @@ -0,0 +1 @@ +ÿÿÿÿÿÿ \ No newline at end of file diff --git a/format/base64/fixtures/8bit.base64 b/format/base64/fixtures/8bit.base64 new file mode 100644 index 0000000..c4e21ac --- /dev/null +++ b/format/base64/fixtures/8bit.base64 @@ -0,0 +1 @@ +/w== \ No newline at end of file diff --git a/format/base64/fixtures/8bit.bin b/format/base64/fixtures/8bit.bin new file mode 100644 index 0000000..ce542ef --- /dev/null +++ b/format/base64/fixtures/8bit.bin @@ -0,0 +1 @@ +ÿ \ No newline at end of file diff --git a/format/base64/fixtures/soup.base64 b/format/base64/fixtures/soup.base64 new file mode 100644 index 0000000..106db5d --- /dev/null +++ b/format/base64/fixtures/soup.base64 @@ -0,0 +1 @@ +fIttAujV5kCX6zsOQqeOIQYz0mHytCxqkjducWyxdLPIjM+dxXZ0UAy0nQlI5hbRNiDsKDsyuqjgqmOOUAWjn0xJA1tx5TAwDNHBhpVcPXilJ4nT5CVdJfPrmgZNKIit+25LvX1L5C/WM98EIgY3Etp3tKi4fuXCcB6vEmKWAX8bnaP9Hw58elbLRSRuhAzHDnVr9hsUHgN2+vpaizfmstFH8Wy1NvKtdNWCWz/0wAtwbRkjZLRPNPaHt1tERDT1YYrbB6Lv2/fNsrdf3FNa/D/s1oSg2a0I1QjCbAFKIPGh9dVLJCSbNiz9PUrLVL877Yfw2QbubyUjX4h42MhpP0kV4+ScuiUchik9w0KaYD6C6thOBBJxwi8KPwkuVCOvbW32WA3WuPt70U2IYoRV9KD74d/aa5o9mJPJVIpyNZrf4J/Bm8+RlCjeYGRFmrWOcOJWjY57CffBrr+YI37xzw/7VQoZlIomcD4xwfwNMRUo8avcjTw2HoPL3bmoLsSOrdHUbiDd2FOi0u1cuLjA8ZjcsgzzcF0WeaAdnuHh97cZyDvTk6dJtGSMc6j6EIn3NptAq3sZVZ09p9Hw32vG2OiaBXLGjYDzKsE3Njs4duHUa4U1dvq+d7k4bi1qifaro65sg8lM6ymsXuQo3aWC11uBAzX5R2/TDdkBRX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of file diff --git a/format/base64/fixtures/soup.bin b/format/base64/fixtures/soup.bin new file mode 100644 index 0000000..6858f13 Binary files /dev/null and b/format/base64/fixtures/soup.bin differ diff --git a/format/base64/fixtures/zero.base64 b/format/base64/fixtures/zero.base64 new file mode 100644 index 0000000..06d22fd --- /dev/null +++ b/format/base64/fixtures/zero.base64 @@ -0,0 +1 @@ +AA== \ No newline at end of file diff --git a/format/base64/fixtures/zero.bin b/format/base64/fixtures/zero.bin new file mode 100644 index 0000000..f76dd23 Binary files /dev/null and b/format/base64/fixtures/zero.bin differ diff --git a/format/factory.go b/format/factory.go index df9b978..348688f 100644 --- a/format/factory.go +++ b/format/factory.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "io" - "os" "crypto/rand" "github.com/bketelsen/logr" @@ -31,11 +30,15 @@ func (f *Factory) Create(x byte) (io.Writer, error) { ws := make([]io.Writer, len(f.formats)) var i int - var err error for _, format := range f.formats { - if ws[i], err = f.create(x, format); err != nil { + w, c, err := format.Writer(x) + if err != nil { return nil, err } + ws[i] = w + if nil != c { + f.c = append(f.c, c...) + } i++ } @@ -46,23 +49,6 @@ func (f *Factory) Create(x byte) (io.Writer, error) { return f.encryptWriter(io.MultiWriter(ws...), x) } -func (f *Factory) create(x byte, format Format) (io.Writer, error) { - file, err := os.Create(format.OutputFileName(x)) - if nil != err { - return nil, err - } - f.c = append(f.c, file) - w, c, err := format.Writer(file) - if nil != err { - return nil, err - } - if nil != c { - f.c = append(f.c, c...) - } - - return w, nil -} - func (f *Factory) encryptWriter(w io.Writer, x byte) (io.Writer, error) { p, err := generatePassword() if nil != err { diff --git a/format/format.go b/format/format.go index d1de1f2..d2a2140 100644 --- a/format/format.go +++ b/format/format.go @@ -19,8 +19,8 @@ type Format interface { // OutputFileName returns the file name for the given x. OutputFileName(x byte) string - // Writer creates a new format writer using the given writer as output. - Writer(w io.Writer) (io.Writer, []io.Closer, error) + // Writer creates a new format writer for the part identified by x + Writer(x byte) (io.Writer, []io.Closer, error) // Reader creates a new format reader using the given reader as input. Reader(r io.Reader) (io.Reader, error) diff --git a/format/internal/assert/data.go b/format/internal/assert/data.go index 2ebf978..381c958 100644 --- a/format/internal/assert/data.go +++ b/format/internal/assert/data.go @@ -1,44 +1,99 @@ package assert import ( - "bytes" + "crypto/rand" + "fmt" + "io" "io/ioutil" + "os" + "path/filepath" + "strings" "testing" + "github.com/corvus-ch/horcrux/format" + "github.com/sebdah/goldie" "github.com/stretchr/testify/assert" ) -// DataTest holds data required for format data read and write assertions. -type DataTest struct { - Decoded []byte - Encoded string -} +// FormatFactory describes a func used for instantiating a Format during assertions. +type FormatFactory func(string) format.Format + +// OutputFileNames describes a func used to get the output file names only known to the calling test case. +type OutputFileNames func(file string, x byte) []string // DataRead asserts a formats read behaviour. -func DataRead(t *testing.T, tests []DataTest, factory FormatFactory) { - for _, test := range tests { - t.Run(test.Encoded, func(t *testing.T) { - r, err := factory("").Reader(bytes.NewBufferString(test.Encoded)) +func DataRead(t *testing.T, factory FormatFactory, suffix string) { + goldie.FileNameSuffix = ".bin" + files, err := filepath.Glob(filepath.Join(goldie.FixtureDir, "*"+suffix)) + if err != nil { + t.Fatal(err) + } + for _, file := range files { + basename := filepath.Base(file) + name := strings.TrimSuffix(basename, filepath.Ext(basename)) + t.Run(name, func(t *testing.T) { + f, err := os.Open(file) + if err != nil { + t.Fatal(err) + } + r, err := factory("").Reader(f) assert.Nil(t, err) out, err := ioutil.ReadAll(r) assert.NoError(t, err) - assert.Equal(t, test.Decoded, out) + goldie.Assert(t, name, out) }) } } // DataWrite asserts a formats write behaviour. -func DataWrite(t *testing.T, tests []DataTest, factory FormatFactory) { - for _, test := range tests { - t.Run(test.Encoded, func(t *testing.T) { - out := &bytes.Buffer{} - w, cl, err := factory("").Writer(out) +func DataWrite(t *testing.T, factory FormatFactory, suffix string, outfilenames OutputFileNames) { + goldie.FileNameSuffix = suffix + files, err := filepath.Glob(filepath.Join(goldie.FixtureDir, "*.bin")) + if err != nil { + t.Fatal(err) + } + for _, file := range files { + basename := filepath.Base(file) + name := strings.TrimSuffix(basename, filepath.Ext(basename)) + t.Run(name, func(t *testing.T) { + x := randomByte(t) + f, err := os.Open(file) + if err != nil { + t.Fatal(err) + } + dir, err := ioutil.TempDir("", name) + defer os.RemoveAll(dir) + if err != nil { + t.Fatal(err) + } + subject := factory(filepath.Join(dir, name)) + w, cl, err := subject.Writer(x) + assert.NoError(t, err) + _, err = io.Copy(w, f) assert.NoError(t, err) - w.Write(test.Decoded) for i := len(cl); i > 0; i-- { - cl[i-1].Close() + err = cl[i-1].Close() + assert.NoError(t, err) + } + for _, outfile := range outfilenames(file, x) { + file, err := os.Open(filepath.Join(dir, outfile)) + if err != nil { + t.Fatal(err) + } + out, _ := ioutil.ReadAll(file) + goldenName := strings.Replace(strings.Replace(outfile, suffix, "", -1), fmt.Sprintf(".%03d", x), "", -1) + t.Log(goldenName) + goldie.Assert(t, goldenName, out) } - assert.Equal(t, test.Encoded, out.String()) }) } } + +func randomByte(t *testing.T) byte { + b := make([]byte, 1) + if _, err := rand.Read(b); err != nil { + t.Fatal(err) + } + + return b[0] +} diff --git a/format/internal/assert/factory.go b/format/internal/assert/factory.go deleted file mode 100644 index b93d9c5..0000000 --- a/format/internal/assert/factory.go +++ /dev/null @@ -1,6 +0,0 @@ -package assert - -import "github.com/corvus-ch/horcrux/format" - -// FormatFactory describes a func used for instantiating a Format during assertions. -type FormatFactory func(string) format.Format diff --git a/format/internal/assert/name.go b/format/internal/assert/name.go deleted file mode 100644 index 8c5dfda..0000000 --- a/format/internal/assert/name.go +++ /dev/null @@ -1,25 +0,0 @@ -package assert - -import ( - "fmt" - "testing" - - "github.com/stretchr/testify/assert" -) - -// NameTest holds the data required to assert the behaviour of github.com/corvus-ch/horcrux/format.Format. -type NameTest struct { - X byte - Stem string - Expected string -} - -// Name asserts the correct behaviour of github.com/corvus-ch/horcrux/format.Format.OutputFileName(). -func Name(t *testing.T, tests []NameTest, factory FormatFactory) { - for _, test := range tests { - t.Run(fmt.Sprint(test.X), func(t *testing.T) { - f := factory(test.Stem) - assert.Equal(t, test.Expected, f.OutputFileName(test.X)) - }) - } -} diff --git a/format/qr/fixtures/00b1abd87bad356b90fcdfcb6132c26f.png b/format/qr/fixtures/48bit.1.png similarity index 100% rename from format/qr/fixtures/00b1abd87bad356b90fcdfcb6132c26f.png rename to format/qr/fixtures/48bit.1.png diff --git a/format/qr/fixtures/48bit.bin b/format/qr/fixtures/48bit.bin new file mode 100644 index 0000000..aef30e1 --- /dev/null +++ b/format/qr/fixtures/48bit.bin @@ -0,0 +1 @@ +ÿÿÿÿÿÿ \ No newline at end of file diff --git a/format/qr/fixtures/749e42cf883078af84149d634d1e4845.png b/format/qr/fixtures/749e42cf883078af84149d634d1e4845.png deleted file mode 100644 index 2950a0c..0000000 Binary files a/format/qr/fixtures/749e42cf883078af84149d634d1e4845.png and /dev/null differ diff --git a/format/qr/fixtures/00594fd4f42ba43fc1ca0427a0576295.png b/format/qr/fixtures/8bit.1.png similarity index 100% rename from format/qr/fixtures/00594fd4f42ba43fc1ca0427a0576295.png rename to format/qr/fixtures/8bit.1.png diff --git a/format/qr/fixtures/8bit.bin b/format/qr/fixtures/8bit.bin new file mode 100644 index 0000000..ce542ef --- /dev/null +++ b/format/qr/fixtures/8bit.bin @@ -0,0 +1 @@ +ÿ \ No newline at end of file diff --git a/format/qr/fixtures/c090400571df281945ddd7031ed9e683.png b/format/qr/fixtures/c090400571df281945ddd7031ed9e683.png deleted file mode 100644 index b4d1150..0000000 Binary files a/format/qr/fixtures/c090400571df281945ddd7031ed9e683.png and /dev/null differ diff --git a/format/qr/fixtures/double.1.png b/format/qr/fixtures/double.1.png new file mode 100644 index 0000000..076a2e8 Binary files /dev/null and b/format/qr/fixtures/double.1.png differ diff --git a/format/qr/fixtures/double.2.png b/format/qr/fixtures/double.2.png new file mode 100644 index 0000000..bbebd39 Binary files /dev/null and b/format/qr/fixtures/double.2.png differ diff --git a/format/qr/fixtures/double.bin b/format/qr/fixtures/double.bin new file mode 100644 index 0000000..3c26841 Binary files /dev/null and b/format/qr/fixtures/double.bin differ diff --git a/format/qr/fixtures/soup.1.png b/format/qr/fixtures/soup.1.png new file mode 100644 index 0000000..0a22695 Binary files /dev/null and b/format/qr/fixtures/soup.1.png differ diff --git a/format/qr/fixtures/soup.bin b/format/qr/fixtures/soup.bin new file mode 100644 index 0000000..6858f13 Binary files /dev/null and b/format/qr/fixtures/soup.bin differ diff --git a/format/qr/fixtures/tripple.1.png b/format/qr/fixtures/tripple.1.png new file mode 100644 index 0000000..3b56ac9 Binary files /dev/null and b/format/qr/fixtures/tripple.1.png differ diff --git a/format/qr/fixtures/tripple.2.png b/format/qr/fixtures/tripple.2.png new file mode 100644 index 0000000..fa11e5a Binary files /dev/null and b/format/qr/fixtures/tripple.2.png differ diff --git a/format/qr/fixtures/tripple.3.png b/format/qr/fixtures/tripple.3.png new file mode 100644 index 0000000..03b30ce Binary files /dev/null and b/format/qr/fixtures/tripple.3.png differ diff --git a/format/qr/fixtures/tripple.bin b/format/qr/fixtures/tripple.bin new file mode 100644 index 0000000..d5a5ed3 Binary files /dev/null and b/format/qr/fixtures/tripple.bin differ diff --git a/format/qr/fixtures/93b885adfe0da089cdf634904fd59f71.png b/format/qr/fixtures/zero.1.png similarity index 100% rename from format/qr/fixtures/93b885adfe0da089cdf634904fd59f71.png rename to format/qr/fixtures/zero.1.png diff --git a/format/qr/fixtures/zero.bin b/format/qr/fixtures/zero.bin new file mode 100644 index 0000000..f76dd23 Binary files /dev/null and b/format/qr/fixtures/zero.bin differ diff --git a/format/qr/qr.go b/format/qr/qr.go index 0f552e0..c09e537 100644 --- a/format/qr/qr.go +++ b/format/qr/qr.go @@ -18,7 +18,7 @@ var encoding = zbase32.NewEncoding(alphabet) // New creates a new Format of type raw. func New(stem string) *Format { return &Format{ - stem: stem, + Stem: stem, Level: qr.M, Size: 500, } @@ -26,25 +26,22 @@ func New(stem string) *Format { // Format represents the raw type format. type Format struct { - stem string + Stem string Level qr.ErrorCorrectionLevel Size int } // OutputFileName returns the file name for the given x. func (f *Format) OutputFileName(x byte) string { - return fmt.Sprintf("%s.%03d.png", f.stem, x) + return fmt.Sprintf("%s.png.%03d", f.Stem, x) } -// Writer creates a new Format writer using the given writer as output. -func (f *Format) Writer(out io.Writer) (io.Writer, []io.Closer, error) { - cs := make([]io.Closer, 2) - w := NewWriter(out, f) - cs[0] = w +// Writer creates a new QR code format writer for the part identified by x. +func (f *Format) Writer(x byte) (io.Writer, []io.Closer, error) { + w := NewWriter(f, x) enc := zbase32.NewEncoder(encoding, w) - cs[1] = enc - return enc, cs, nil + return enc, []io.Closer{w, enc}, nil } // Reader creates a new Format reader using the given reader as input. diff --git a/format/qr/qr_test.go b/format/qr/qr_test.go index fae14e4..cc6175b 100644 --- a/format/qr/qr_test.go +++ b/format/qr/qr_test.go @@ -2,12 +2,9 @@ package qr_test import ( "bytes" - "crypto/md5" - "crypto/rand" "fmt" - "github.com/sebdah/goldie" - "io" - "io/ioutil" + "os" + "path/filepath" "testing" "github.com/corvus-ch/horcrux/format" @@ -16,218 +13,31 @@ import ( "github.com/stretchr/testify/assert" ) -var nameTests = []formatAssert.NameTest{ - {0, "mollis", "mollis.000.png"}, - {1, "commodo", "commodo.001.png"}, - {42, "pellentesque", "pellentesque.042.png"}, - {181, "fringilla", "fringilla.181.png"}, - {254, "venenatis", "venenatis.254.png"}, - {255, "ridiculus", "ridiculus.255.png"}, -} - -var dataTests = []struct { - data []byte -}{ - {[]byte{0}}, - {[]byte{0xff}}, - {[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}, - {[]byte{ - 0xc0, 0x73, 0x62, 0x4a, 0xaf, 0x39, 0x78, 0x51, - 0x4e, 0xf8, 0x44, 0x3b, 0xb2, 0xa8, 0x59, 0xc7, - 0x5f, 0xc3, 0xcc, 0x6a, 0xf2, 0x6d, 0x5a, 0xaa, - }}, - {[]byte{ - 0x05, 0xee, 0xa2, 0x82, 0x8b, 0x45, 0x3b, 0x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 0x2d, 0xee, 0xf8, 0x92, 0xe0, 0xa2, 0x2f, - 0xb2, 0x95, 0xbb, 0x17, 0xe5, 0x79, 0x4f, 0x4b, 0x60, 0x78, 0x96, 0xb9, 0xb2, 0x62, 0x21, 0x59, - 0xee, 0x16, 0x05, 0x3a, 0x1e, 0x42, 0xd1, 0x9c, 0xcb, 0xea, 0xe2, 0x47, 0x70, 0xe0, 0x91, 0x82, - 0x68, 0x12, 0x8d, 0xae, 0xcb, 0xc4, 0xdf, 0x57, 0x65, 0x58, 0x16, 0x20, 0x74, 0x83, 0xca, 0x56, - 0xfe, 0xcc, 0xab, 0x3b, 0xa9, 0x4e, 0xa7, 0x10, 0x9d, 0xf1, 0x76, 0x7c, 0xff, 0x90, 0x4d, 0x8d, - 0x2d, 0xa4, 0xa9, 0x76, 0x7c, 0xc3, 0x89, 0xfb, 0x16, 0xa0, 0xe8, 0x4e, 0xf3, 0xc3, 0xfe, 0x89, - 0xba, 0x6a, 0x1a, 0xa3, 0x76, 0x68, 0x95, 0x19, 0x3f, 0xf7, 0x19, 0x53, 0x28, 0xbd, 0x75, 0xc1, - 0xd4, 0xa5, 0x60, 0xa6, 0x66, 0x1e, 0xcd, 0xa0, 0x12, 0x3b, 0x03, 0x56, 0x21, 0xcf, 0xb2, 0x9b, - 0xfd, 0x57, 0xc3, 0x31, 0x82, 0x21, 0x72, 0x6c, 0x21, 0xe8, 0x8c, 0x59, 0xd4, 0x04, 0x18, 0xe2, - 0x72, 0x15, 0xc9, 0x57, 0x18, 0x29, 0x26, 0x5d, 0xbc, 0xe1, 0xae, 0x95, 0x84, 0x79, 0x15, 0x26, - 0x97, 0xaf, 0x53, 0x29, 0xb7, 0x20, 0xdc, 0xc8, 0xd5, 0x48, 0xb0, 0x4c, 0x07, 0xc3, 0xb0, 0xa4, - 0x1f, 0x19, 0xa3, 0x36, 0xdd, 0x94, 0x73, 0x8b, 0x8a, 0xff, 0x3a, 0x27, 0x2f, 0x36, 0x77, 0x8e, - 0x2b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d6, 0xbc, 0xc5, 0xce, - 0x3e, 0x96, 0xa5, 0x94, 0xf0, 0x20, 0x9b, 0xf8, 0xdc, 0xfb, 0xf7, 0x80, 0xde, 0x07, 0x8c, 0x73, - 0xf4, 0x81, 0x70, 0x5d, 0xc8, 0x18, 0x8e, 0x8a, 0x31, 0xe2, 0x8d, 0x28, 0x30, 0x66, 0xac, 0x57, - 0x23, 0x4a, 0x9b, 0x34, 0xc4, 0x05, 0x9f, 0x6d, 0xcc, 0xa2, 0xa9, 0x66, 0x12, 0xf9, 0x8a, 0x9e, - 0x07, 0x7b, 0xc7, 0x79, 0x2b, 0x10, 0x35, 0x32, 0x8c, 0x81, 0xdb, 0x4e, 0x1b, 0xc6, 0x96, 0xa1, - 0x60, 0x65, 0xfd, 0x9a, 0xd4, 0xcb, 0x7f, 0x15, 0xad, 0xc2, 0xcc, 0x44, 0x66, 0x59, 0xed, 0x8f, - 0x88, 0x42, 0x11, 0xfa, 0x13, 0xb0, 0xa5, 0x34, 0x47, 0x9b, 0x33, 0x34, 0xb2, 0x60, 0x62, 0x94, - 0xad, 0xea, 0x96, 0xf1, 0x22, 0x12, 0x64, - }}, -} - func factory(s string) format.Format { return qr.New(s) } -func TestFormat_OutputFileName(t *testing.T) { - formatAssert.Name(t, nameTests, factory) -} - func TestFormat_Reader(t *testing.T) { _, err := factory("").Reader(bytes.NewReader([]byte{})) assert.Error(t, err) } func TestFormat_Writer(t *testing.T) { - goldie.FileNameSuffix = ".png" - for _, test := range dataTests { - name := fmt.Sprintf("%x", md5.Sum(test.data)) - t.Run(name, func(t *testing.T) { - out := &bytes.Buffer{} - w, cl, err := factory("").Writer(out) - assert.NoError(t, err) - _, err = w.Write(test.data) - assert.NoError(t, err) - for i := len(cl); i > 0; i-- { - err = cl[i-1].Close() - assert.NoError(t, err) - } - goldie.Assert(t, name, out.Bytes()) - }) - } - - t.Run("max data", func(t *testing.T) { - w, cl, err := factory("").Writer(ioutil.Discard) - assert.NoError(t, err) - r := io.LimitReader(rand.Reader, 2119) - _, err = io.Copy(w, r) - assert.NoError(t, err) - for i := len(cl); i > 0; i-- { - err = cl[i-1].Close() - assert.NoError(t, err) + formatAssert.DataWrite(t, factory, ".png", func(file string, x byte) []string { + base := filepath.Base(file) + name := base[0 : len(base)-len(filepath.Ext(base))] + f, err := os.Stat(file) + if err != nil { + return []string{} } - }) + num := (f.Size() / 2120) + 1 - t.Run("too much data", func(t *testing.T) { - w, cl, err := factory("").Writer(ioutil.Discard) - assert.NoError(t, err) - r := io.LimitReader(rand.Reader, 2120) - _, err = io.Copy(w, r) - for i := len(cl); i > 0; i-- { - e := cl[i-1].Close() - if e != nil { - err = e - } + names := make([]string, num) + for i := int64(0); i < num; i++ { + names[i] = fmt.Sprintf("%s.%03d.%d.png", name, x, i+1) } - assert.Error(t, err) + + return names }) } diff --git a/format/qr/writer.go b/format/qr/writer.go index 0229095..a1deb9f 100644 --- a/format/qr/writer.go +++ b/format/qr/writer.go @@ -6,6 +6,7 @@ import ( "github.com/boombuler/barcode" "image/png" "io" + "os" "github.com/boombuler/barcode/qr" ) @@ -13,29 +14,58 @@ import ( type writer struct { io.WriteCloser f *Format - w io.Writer buf bytes.Buffer + x byte + n int } // NewWriter returns a qr code format writer instance. -func NewWriter(w io.Writer, f *Format) io.WriteCloser { - return &writer{w: w, f: f} +func NewWriter(f *Format, x byte) io.WriteCloser { + return &writer{f: f, x: x} } -func (w *writer) Write(p []byte) (n int, err error) { - return w.buf.Write(p) +func (w *writer) Write(p []byte) (int, error) { + n, err := w.buf.Write(p) + if err != nil { + return n, err + } + + for w.buf.Len() >= 3391 { + if err := w.createImage(); err != nil { + return n, err + } + } + + return n, nil } func (w *writer) Close() error { - qrCode, err := qr.Encode(w.buf.String(), w.f.Level, qr.AlphaNumeric) + if w.buf.Len() > 0 { + return w.createImage() + } + + return nil +} + +func (w *writer) createImage() error { + data := w.buf.Next(3391) + code, err := qr.Encode(string(data), w.f.Level, qr.AlphaNumeric) if err != nil { return fmt.Errorf("failed to create qr code: %v", err) } - qrCode, err = barcode.Scale(qrCode, w.f.Size, w.f.Size) + code, err = barcode.Scale(code, w.f.Size, w.f.Size) if err != nil { return fmt.Errorf("failed to scale qr code: %v", err) } - return png.Encode(w.w, qrCode) + // create the output file + w.n++ + file, err := os.Create(fmt.Sprintf("%s.%03d.%d.png", w.f.Stem, w.x, w.n)) + if err != nil { + return fmt.Errorf("failed to open output file: %v", err) + } + defer file.Close() + + return png.Encode(file, code) } diff --git a/format/raw/fixtures/48bit.bin b/format/raw/fixtures/48bit.bin new file mode 100644 index 0000000..aef30e1 --- /dev/null +++ b/format/raw/fixtures/48bit.bin @@ -0,0 +1 @@ +ÿÿÿÿÿÿ \ No newline at end of file diff --git a/format/raw/fixtures/48bit.raw b/format/raw/fixtures/48bit.raw new file mode 100644 index 0000000..aef30e1 --- /dev/null +++ b/format/raw/fixtures/48bit.raw @@ -0,0 +1 @@ +ÿÿÿÿÿÿ \ No newline at end of file diff --git a/format/raw/fixtures/8bit.bin b/format/raw/fixtures/8bit.bin new file mode 100644 index 0000000..ce542ef --- /dev/null +++ b/format/raw/fixtures/8bit.bin @@ -0,0 +1 @@ +ÿ \ No newline at end of file diff --git a/format/raw/fixtures/8bit.raw b/format/raw/fixtures/8bit.raw new file mode 100644 index 0000000..ce542ef --- /dev/null +++ b/format/raw/fixtures/8bit.raw @@ -0,0 +1 @@ +ÿ \ No newline at end of file diff --git a/format/raw/fixtures/soup.bin b/format/raw/fixtures/soup.bin new file mode 100644 index 0000000..6858f13 Binary files /dev/null and b/format/raw/fixtures/soup.bin differ diff --git a/format/raw/fixtures/soup.raw b/format/raw/fixtures/soup.raw new file mode 100644 index 0000000..6858f13 Binary files /dev/null and b/format/raw/fixtures/soup.raw differ diff --git a/format/raw/fixtures/zero.bin b/format/raw/fixtures/zero.bin new file mode 100644 index 0000000..f76dd23 Binary files /dev/null and b/format/raw/fixtures/zero.bin differ diff --git a/format/raw/fixtures/zero.raw b/format/raw/fixtures/zero.raw new file mode 100644 index 0000000..f76dd23 Binary files /dev/null and b/format/raw/fixtures/zero.raw differ diff --git a/format/raw/raw.go b/format/raw/raw.go index ceb4176..b1c7863 100644 --- a/format/raw/raw.go +++ b/format/raw/raw.go @@ -3,6 +3,7 @@ package raw import ( "fmt" "io" + "os" ) // Name holds the name of the Format. @@ -23,9 +24,14 @@ func (f *Format) OutputFileName(x byte) string { return fmt.Sprintf("%s.raw.%03d", f.stem, x) } -// Writer creates a new Format writer using the given writer as output. -func (f *Format) Writer(out io.Writer) (io.Writer, []io.Closer, error) { - return out, nil, nil +// Writer creates a new raw format writer for the part identified by x. +func (f *Format) Writer(x byte) (io.Writer, []io.Closer, error) { + file, err := os.Create(f.OutputFileName(x)) + if nil != err { + return nil, nil, err + } + + return file, []io.Closer{file}, nil } // Reader creates a new Format reader using the given reader as input. diff --git a/format/raw/raw_test.go b/format/raw/raw_test.go index 95151fc..e279156 100644 --- a/format/raw/raw_test.go +++ b/format/raw/raw_test.go @@ -1,9 +1,8 @@ package raw_test import ( - "bytes" - "io/ioutil" - "math/rand" + "fmt" + "path/filepath" "testing" "github.com/corvus-ch/horcrux/format" @@ -12,49 +11,22 @@ import ( "github.com/stretchr/testify/assert" ) -var nameTests = []formatAssert.NameTest{ - {0, "mollis", "mollis.raw.000"}, - {1, "commodo", "commodo.raw.001"}, - {42, "pellentesque", "pellentesque.raw.042"}, - {181, "fringilla", "fringilla.raw.181"}, - {254, "venenatis", "venenatis.raw.254"}, - {255, "ridiculus", "ridiculus.raw.255"}, -} - func factory(s string) format.Format { return raw.New(s) } -func TestFormat_OutputFileName(t *testing.T) { - formatAssert.Name(t, nameTests, factory) -} - func TestFormat_Reader(t *testing.T) { - data := randomData() - r, err := raw.New("").Reader(bytes.NewBuffer(data)) - assert.Nil(t, err) - out, err := ioutil.ReadAll(r) - assert.NoError(t, err) - assert.Equal(t, data, out) + formatAssert.DataRead(t, factory, ".raw") } func TestFormat_Writer(t *testing.T) { - out := &bytes.Buffer{} - w, cl, err := raw.New("").Writer(out) - assert.Nil(t, cl) - assert.NoError(t, err) - data := randomData() - w.Write(data) - assert.Equal(t, data, out.Bytes()) + formatAssert.DataWrite(t, factory, ".raw", func(file string, x byte) []string { + base := filepath.Base(file) + name := base[0 : len(base)-len(filepath.Ext(base))] + return []string{fmt.Sprintf("%s.raw.%03d", name, x)} + }) } func TestFormat_Name(t *testing.T) { assert.Equal(t, raw.Name, raw.New("").Name()) } - -func randomData() []byte { - data := make([]byte, rand.Intn(1024)) - rand.Read(data) - - return data -} diff --git a/format/text/fixtures/039DEA.bin b/format/text/fixtures/039DEA.bin new file mode 100644 index 0000000..7725d1d --- /dev/null +++ b/format/text/fixtures/039DEA.bin @@ -0,0 +1 @@ +ÀsbJ¯9xQNøD;²¨YÇ_ÃÌjòmZª \ No newline at end of file diff --git a/format/text/fixtures/039DEA.txt b/format/text/fixtures/039DEA.txt new file mode 100644 index 0000000..2d40eb1 --- /dev/null +++ b/format/text/fixtures/039DEA.txt @@ -0,0 +1,2 @@ + 1: ab3sr 1ix8f hfnuz aeo75 fkn3a 7xh8u dk6js iiko 039DEA + 2: 039DEA diff --git a/format/text/fixtures/302370.bin b/format/text/fixtures/302370.bin new file mode 100644 index 0000000..01399f9 --- /dev/null +++ b/format/text/fixtures/302370.bin @@ -0,0 +1 @@ +ÿÿÿÿÿ \ No newline at end of file diff --git a/format/text/fixtures/302370.txt b/format/text/fixtures/302370.txt new file mode 100644 index 0000000..530ccd3 --- /dev/null +++ b/format/text/fixtures/302370.txt @@ -0,0 +1,2 @@ + 1: 99999 999 302370 + 2: 302370 diff --git a/format/text/fixtures/358F66.bin b/format/text/fixtures/358F66.bin new file mode 100644 index 0000000..184b256 --- /dev/null +++ b/format/text/fixtures/358F66.bin @@ -0,0 +1 @@ +4Z \ No newline at end of file diff --git a/format/text/fixtures/358F66.txt b/format/text/fixtures/358F66.txt new file mode 100644 index 0000000..3eedcbb --- /dev/null +++ b/format/text/fixtures/358F66.txt @@ -0,0 +1,2 @@ + 1: gtpy 358F66 + 2: 358F66 diff --git a/format/text/fixtures/37CA82.bin b/format/text/fixtures/37CA82.bin new file mode 100644 index 0000000..1987bbd --- /dev/null +++ b/format/text/fixtures/37CA82.bin @@ -0,0 +1 @@ +µ \ No newline at end of file diff --git a/format/text/fixtures/37CA82.txt b/format/text/fixtures/37CA82.txt new file mode 100644 index 0000000..9ebcb4e --- /dev/null +++ b/format/text/fixtures/37CA82.txt @@ -0,0 +1,2 @@ + 1: sw 37CA82 + 2: 37CA82 diff --git a/format/text/fixtures/6C81C7.bin b/format/text/fixtures/6C81C7.bin new file mode 100644 index 0000000..45d6669 Binary files /dev/null and b/format/text/fixtures/6C81C7.bin differ diff --git a/format/text/fixtures/6C81C7.txt b/format/text/fixtures/6C81C7.txt new file mode 100644 index 0000000..69effee --- /dev/null +++ b/format/text/fixtures/6C81C7.txt @@ -0,0 +1,18 @@ + 1: dgu63 7ydf7 65o8j 1fped pc97p 13mzx kkh7n 86dhz q348j pjyjm DFA978 + 2: k6yz7 7tt46 nsdnn y44jo 37i91 ak3ur jbrfz qhpwj at9na ahtnu E9AA47 + 3: xjg13 m3exn dd5zj 89xkg typ31 mur7u w7ihe 5xgkh w36bm 47hq3 AEB17C + 4: ne54w kdi3o a9d3j f36aq dagf9 jsa35 u8kxk feiu7 hthm1 izqhk 659F29 + 5: mxibt 5eyjn igxyb 78s3i ujkai ckxes 4y344 c63qe d65oh shdnk 9CAEE1 + 6: fxu8m 4jjxs 3sf4m p3ff7 mp9t7 aizau gbsgg wjtod 8mqi7 zr1g5 B2AEA1 + 7: 6cqs4 38o7m 45zro ktobg h43m7 h19au d6ht8 6s9hg y593d wiutq 896437 + 8: nn6su xnzsw ec1ui u9wkd bfr3x o1g3t 9h7m7 7ug1x bkyz4 jj13y F8532D + 9: drsyd 3hb45 zmciz 9n4u6 a3y4u 3pzkp ooyjm zkuas fbfuo 7tix1 22BF18 + 10: ufbon fop94 j11pp 565xe h8pcu jmeq4 xowor jp438 m8uzb wnanm A20BAC + 11: ck11q fte4c o8194 eetwk 9bxdg pxxkb 6xzxf gq171 ro56e qa8zb 5AF890 + 12: fsk6t r1ek8 rt7db dp96z ccn7m quui7 bx76n k3nkt rd3r5 jf9ht 66755D + 13: nct8d 38yw9 m1sen 6wb3w q6uiy mghd4 cqwns zej1f sqb45 mu34f EB388D + 14: os94o zj8gz 9thxi zy71s i8gmk yz9np kijgq jufqr 5zhqt j88uj 2DFE2D + 15: kahnq mffuf nitb8 uof7g 9i59w 9so73 xkhcn ekkif h3emz wbbzj 65EF4C + 16: 9ub9y jisuo x8xth euf3s 5xhjy yww61 egj1d g3mdz oxijg gmkrn 908A03 + 17: zr88f tutp8 ug58e 3idqo 6C81C7 + 18: 6C81C7 diff --git a/format/text/fixtures/78D648.bin b/format/text/fixtures/78D648.bin new file mode 100644 index 0000000..ce542ef --- /dev/null +++ b/format/text/fixtures/78D648.bin @@ -0,0 +1 @@ +ÿ \ No newline at end of file diff --git a/format/text/fixtures/78D648.txt b/format/text/fixtures/78D648.txt new file mode 100644 index 0000000..c42717a --- /dev/null +++ b/format/text/fixtures/78D648.txt @@ -0,0 +1,2 @@ + 1: 9h 78D648 + 2: 78D648 diff --git a/format/text/fixtures/7A8571.bin b/format/text/fixtures/7A8571.bin new file mode 100644 index 0000000..f76dd23 Binary files /dev/null and b/format/text/fixtures/7A8571.bin differ diff --git a/format/text/fixtures/7A8571.txt b/format/text/fixtures/7A8571.txt new file mode 100644 index 0000000..b6843b9 --- /dev/null +++ b/format/text/fixtures/7A8571.txt @@ -0,0 +1,2 @@ + 1: yy 7A8571 + 2: 7A8571 diff --git a/format/text/fixtures/9AD7E3.bin b/format/text/fixtures/9AD7E3.bin new file mode 100644 index 0000000..aef30e1 --- /dev/null +++ b/format/text/fixtures/9AD7E3.bin @@ -0,0 +1 @@ +ÿÿÿÿÿÿ \ No newline at end of file diff --git a/format/text/fixtures/9AD7E3.txt b/format/text/fixtures/9AD7E3.txt new file mode 100644 index 0000000..a8a052c --- /dev/null +++ b/format/text/fixtures/9AD7E3.txt @@ -0,0 +1,2 @@ + 1: 99999 9999h 9AD7E3 + 2: 9AD7E3 diff --git a/format/text/fixtures/BF444A.bin b/format/text/fixtures/BF444A.bin new file mode 100644 index 0000000..fad60ec --- /dev/null +++ b/format/text/fixtures/BF444A.bin @@ -0,0 +1 @@ +ð¿Ç \ No newline at end of file diff --git a/format/text/fixtures/BF444A.txt b/format/text/fixtures/BF444A.txt new file mode 100644 index 0000000..22df9cf --- /dev/null +++ b/format/text/fixtures/BF444A.txt @@ -0,0 +1,2 @@ + 1: 6n9hq BF444A + 2: BF444A diff --git a/format/text/text.go b/format/text/text.go index af7a284..a5feb5a 100644 --- a/format/text/text.go +++ b/format/text/text.go @@ -3,6 +3,7 @@ package text import ( "fmt" "io" + "os" "gopkg.in/corvus-ch/zbase32.v1" ) @@ -26,15 +27,17 @@ func (f *Format) OutputFileName(x byte) string { return fmt.Sprintf("%s.txt.%03d", f.Stem, x) } -// Writer creates a new format writer using the given writer as output. -func (f *Format) Writer(out io.Writer) (io.Writer, []io.Closer, error) { - cs := make([]io.Closer, 2) - w := NewWriter(out, f) - cs[0] = w +// Writer creates a new text format writer for the part identified by x. +func (f *Format) Writer(x byte) (io.Writer, []io.Closer, error) { + file, err := os.Create(f.OutputFileName(x)) + if nil != err { + return nil, nil, err + } + + w := NewWriter(file, f) enc := zbase32.NewEncoder(zbase32.StdEncoding, w) - cs[1] = enc - return enc, cs, nil + return enc, []io.Closer{file, w, enc}, nil } // Reader creates a new format reader using the given reader as input. diff --git a/format/text/text_test.go b/format/text/text_test.go index bb930ac..9194b8f 100644 --- a/format/text/text_test.go +++ b/format/text/text_test.go @@ -1,6 +1,8 @@ package text_test import ( + "fmt" + "path/filepath" "testing" "github.com/corvus-ch/horcrux/format" @@ -9,96 +11,20 @@ import ( "github.com/stretchr/testify/assert" ) -var nameTests = []formatAssert.NameTest{ - {0, "mollis", "mollis.txt.000"}, - {1, "commodo", "commodo.txt.001"}, - {42, "pellentesque", "pellentesque.txt.042"}, - {181, "fringilla", "fringilla.txt.181"}, - {254, "venenatis", "venenatis.txt.254"}, - {255, "ridiculus", "ridiculus.txt.255"}, -} - -var dataTests = []formatAssert.DataTest{ - {[]byte{240, 191, 199}, " 1: 6n9hq BF444A\n 2: BF444A\n"}, - {[]byte{212, 122, 4}, " 1: 4t7ye 7008A1\n 2: 7008A1\n"}, - {[]byte{0xff}, " 1: 9h 78D648\n 2: 78D648\n"}, - {[]byte{0xb5}, " 1: sw 37CA82\n 2: 37CA82\n"}, - {[]byte{0x34, 0x5a}, " 1: gtpy 358F66\n 2: 358F66\n"}, - {[]byte{0xff, 0xff, 0xff, 0xff, 0xff}, " 1: 99999 999 302370\n 2: 302370\n"}, - {[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, " 1: 99999 9999h 9AD7E3\n 2: 9AD7E3\n"}, - {[]byte{ - 0xc0, 0x73, 0x62, 0x4a, 0xaf, 0x39, 0x78, 0x51, - 0x4e, 0xf8, 0x44, 0x3b, 0xb2, 0xa8, 0x59, 0xc7, - 0x5f, 0xc3, 0xcc, 0x6a, 0xf2, 0x6d, 0x5a, 0xaa, - }, " 1: ab3sr 1ix8f hfnuz aeo75 fkn3a 7xh8u dk6js iiko 039DEA\n 2: 039DEA\n"}, - {[]byte{ - 0x19, 0xa7, 0xec, 0xf4, 0x03, 0x2f, 0x7d, 0xb8, 0x1d, 0x32, 0x2b, 0x50, 0x36, 0xb3, 0xfd, 0x6c, - 0xb2, 0xbb, 0xbd, 0x4a, 0xe7, 0x44, 0x7f, 0x0f, 0x97, 0x76, 0x74, 0x74, 0xb5, 0x20, 0x4a, 0xd5, - 0xe0, 0x5f, 0xbd, 0x8c, 0x75, 0xe1, 0x58, 0x62, 0x10, 0x35, 0xa4, 0xc3, 0x3d, 0xaf, 0xe5, 0x85, - 0x66, 0x64, 0x48, 0x48, 0x5b, 0xbb, 0x8d, 0xa2, 0x71, 0x1f, 0x8b, 0x18, 0xe4, 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x69, 0x3c, 0x2a, 0x05, 0xf4, 0x94, 0xcb, 0x20, 0x19, 0x2c, 0x01, 0xe7, 0x81, 0xd6, - 0xee, 0xb6, 0x56, 0xff, 0x16, 0xa7, 0xec, 0x64, 0x1a, 0x9e, 0x5b, 0x75, 0x36, 0x10, 0x02, 0x57, - 0x75, 0x4f, 0x16, 0x28, 0x4b, 0x38, 0x76, 0x35, 0x7c, 0xa6, 0x50, 0xc0, 0x45, 0x83, 0x7f, 0xa4, - 0xca, 0x4d, 0x6e, 0xfd, 0xb7, 0xa3, 0x87, 0x6b, 0x26, 0x95, 0xa1, 0xda, 0x7c, 0x29, 0x02, 0x25, - 0xba, 0xc9, 0xd6, 0x79, 0xdc, 0x34, 0x16, 0x04, 0xb6, 0x2a, 0x52, 0x71, 0x62, 0x8d, 0x32, 0x07, - 0x97, 0xf4, 0x84, 0x46, 0x8a, 0xf8, 0x5e, 0x33, 0x35, 0xef, 0x50, 0x7c, 0xfb, 0xbc, 0xa6, 0x74, - 0xbb, 0x22, 0x43, 0x7e, 0x43, 0xb0, 0x7b, 0x84, 0xb6, 0x57, 0xa2, 0x49, 0x21, 0x47, 0x24, 0x7a, - 0x30, 0x8d, 0xbf, 0xf5, 0xd8, 0xc1, 0x75, 0x6e, 0x9c, 0xeb, 0xd0, 0xbf, 0xbe, 0x12, 0xb2, 0x25, - 0x44, 0x83, 0xc9, 0x36, 0x92, 0xff, 0x91, 0x13, 0x22, 0x71, 0xe4, 0xe0, 0xa7, 0xd7, 0x2b, 0x20, - 0x5e, 0xa0, 0x73, 0x47, 0x7a, 0x75, 0x02, 0xcd, 0xc1, 0xe9, 0x8e, 0xa0, 0xad, 0x74, 0x26, 0x45, - 0xb3, 0x83, 0xad, 0xae, 0x79, 0xd1, 0x61, 0x6f, 0xea, 0x17, 0x49, 0xcd, 0x7f, 0xc7, 0x8f, 0xad, - 0xc1, 0xd9, 0x5a, 0xa7, 0x32, 0xd4, 0x0b, 0xfc, 0x4d, 0x55, 0x52, 0x67, 0x26, 0x65, 0x71, 0x37, - 0x7e, 0x3a, 0x29, 0x39, 0xe6, 0x95, 0x63, 0x82, 0x72, 0xca, 0x59, 0x94, 0x55, 0x88, 0x4f, 0x38, - 0x17, 0xa6, 0xfd, 0x77, 0xfa, 0x7e, 0xd0, 0xee, 0x5e, 0xae, 0x30, 0x48, 0x52, 0xaa, 0x5e, 0x65, - 0x0b, 0xbd, 0x02, 0x1b, 0xa7, 0xf3, 0x0f, 0xc0, 0x9a, 0xda, 0x70, 0x79, 0xdf, 0x1e, 0x22, 0x65, - 0xcd, 0xb6, 0xfe, 0x24, 0x00, 0xa5, 0x3d, 0x24, 0x19, 0x32, 0x19, 0xb2, 0xb1, 0xde, 0x0f, 0xaa, - 0x4c, 0x65, 0xa8, 0x82, 0xb9, 0x0e, 0x72, 0xc6, 0x71, 0x69, 0xe6, 0x6d, 0x9d, 0x19, 0xa8, 0xdd, - }, ` 1: dgu63 7ydf7 65o8j 1fped pc97p 13mzx kkh7n 86dhz q348j pjyjm DFA978 - 2: k6yz7 7tt46 nsdnn y44jo 37i91 ak3ur jbrfz qhpwj at9na ahtnu E9AA47 - 3: xjg13 m3exn dd5zj 89xkg typ31 mur7u w7ihe 5xgkh w36bm 47hq3 AEB17C - 4: ne54w kdi3o a9d3j f36aq dagf9 jsa35 u8kxk feiu7 hthm1 izqhk 659F29 - 5: mxibt 5eyjn igxyb 78s3i ujkai ckxes 4y344 c63qe d65oh shdnk 9CAEE1 - 6: fxu8m 4jjxs 3sf4m p3ff7 mp9t7 aizau gbsgg wjtod 8mqi7 zr1g5 B2AEA1 - 7: 6cqs4 38o7m 45zro ktobg h43m7 h19au d6ht8 6s9hg y593d wiutq 896437 - 8: nn6su xnzsw ec1ui u9wkd bfr3x o1g3t 9h7m7 7ug1x bkyz4 jj13y F8532D - 9: drsyd 3hb45 zmciz 9n4u6 a3y4u 3pzkp ooyjm zkuas fbfuo 7tix1 22BF18 - 10: ufbon fop94 j11pp 565xe h8pcu jmeq4 xowor jp438 m8uzb wnanm A20BAC - 11: ck11q fte4c o8194 eetwk 9bxdg pxxkb 6xzxf gq171 ro56e qa8zb 5AF890 - 12: fsk6t r1ek8 rt7db dp96z ccn7m quui7 bx76n k3nkt rd3r5 jf9ht 66755D - 13: nct8d 38yw9 m1sen 6wb3w q6uiy mghd4 cqwns zej1f sqb45 mu34f EB388D - 14: os94o zj8gz 9thxi zy71s i8gmk yz9np kijgq jufqr 5zhqt j88uj 2DFE2D - 15: kahnq mffuf nitb8 uof7g 9i59w 9so73 xkhcn ekkif h3emz wbbzj 65EF4C - 16: 9ub9y jisuo x8xth euf3s 5xhjy yww61 egj1d g3mdz oxijg gmkrn 908A03 - 17: zr88f tutp8 ug58e 3idqo 6C81C7 - 18: 6C81C7 -`}, -} - func factory(s string) format.Format { return text.New(s) } -func TestFormat_OutputFileName(t *testing.T) { - formatAssert.Name(t, nameTests, factory) -} - func TestFormat_Reader(t *testing.T) { - formatAssert.DataRead(t, dataTests, factory) + formatAssert.DataRead(t, factory, ".txt") } func TestFormat_Writer(t *testing.T) { - formatAssert.DataWrite(t, dataTests, factory) + formatAssert.DataWrite(t, factory, ".txt", func(file string, x byte) []string { + base := filepath.Base(file) + name := base[0 : len(base)-len(filepath.Ext(base))] + return []string{fmt.Sprintf("%s.txt.%03d", name, x)} + }) } func TestFormat_Name(t *testing.T) { diff --git a/format/zbase32/fixtures/48bit.bin b/format/zbase32/fixtures/48bit.bin new file mode 100644 index 0000000..aef30e1 --- /dev/null +++ b/format/zbase32/fixtures/48bit.bin @@ -0,0 +1 @@ +ÿÿÿÿÿÿ \ No newline at end of file diff --git a/format/zbase32/fixtures/48bit.zbase32 b/format/zbase32/fixtures/48bit.zbase32 new file mode 100644 index 0000000..eb30a2a --- /dev/null +++ b/format/zbase32/fixtures/48bit.zbase32 @@ -0,0 +1 @@ +999999999h \ No newline at end of file diff --git a/format/zbase32/fixtures/8bit.bin b/format/zbase32/fixtures/8bit.bin new file mode 100644 index 0000000..ce542ef --- /dev/null +++ b/format/zbase32/fixtures/8bit.bin @@ -0,0 +1 @@ +ÿ \ No newline at end of file diff --git a/format/zbase32/fixtures/8bit.zbase32 b/format/zbase32/fixtures/8bit.zbase32 new file mode 100644 index 0000000..276c36a --- /dev/null +++ b/format/zbase32/fixtures/8bit.zbase32 @@ -0,0 +1 @@ +9h \ No newline at end of file diff --git a/format/zbase32/fixtures/soup.bin b/format/zbase32/fixtures/soup.bin new file mode 100644 index 0000000..6858f13 Binary files /dev/null and b/format/zbase32/fixtures/soup.bin differ diff --git a/format/zbase32/fixtures/soup.zbase32 b/format/zbase32/fixtures/soup.zbase32 new file mode 100644 index 0000000..da20043 --- /dev/null +++ b/format/zbase32/fixtures/soup.zbase32 @@ -0,0 +1 @@ +x1fs4yze4zurbf9m8c8rfjhqrrdd8wub6k4na4w1g7z8n5ftq13htdgxuznzc7nob14j4nkehampnpty7owdsci4idokwahqkyn4884cjrbishxfgyay3wqbo4kiaxmawwuauw9rriq1mh9muedr4kreiz7sh177xif6em6sgxxoeeogghjpw77winh873qnqyxk6run1ayz6gh7wx6t6duhxjmcstjrp4ny3taqqii9cgawdabzp6z4mkfux3i14fd9n5fig53k47giojpu97gybpag4gjdc14r6p8so65istnrgu4sdns5y6tq9s9z3s3mqz6hkppxax9c44nkbsppbdkotoucyffnbhpb6zkwsjbruc5n39j7jmfijx357sd9bseg73z1ke49tbhpt1dj87rtma9ru17nk8rgfr6hgow4cy9ef4sajanbrhqnfhfd6njqkot465mp63cy5iia9p77nucecknfm7fy9xo79sumue63tr6jk1f8rpc459oj9oc536e3ekg6cb1rmgiit3aqriwpt37ou76bi493oe56688o964ibec3jntgqy9ddoxhbwatkk8tixqe4xbsd4bhzzp3iyzcjdip48kgheg75bj4fwzpm1hmto8tudqmrd8uqbqtc6pydsxqdaxzshchoq6u1quwupdrtt34t6oot85upg4yip7t1ic78su7dhg9pxdpt4r4yi3cpdcy6cicnp3s8ch8paqwpqnuk7z4z3551qdqfiieu7imwqzg3y6jjui1umn6howp5jcn47pany3i9fds9wap5rywk9fhz1hdppwugknges4a87hc4pbp1nkhppheonmobz3s3yuask8gc9bppsjfrw9mwgzft6jasqupm14hdpiw78bjepqiqzajwbcbj65rqp3wedtan3kfay7hwaux7cj9jktxwukr9575ieiyphsefdkca3gbeiwzhb1fbeocxe5jbm1zicuyugrmi7q8jc7fjn3tijn3ytmtfxrez8zt9wps1tzfefdj98j6d9rdi77oda9sfs6de6mxfshp6i3t4mcmgsg7f7xng99rnez1d47fjpyo94pimf8tywcd7mt9gpb5patksbf5p1x4id7j89x1a8x9jq637sucaiuu47izj1114z9zyhkmbj4kp99tpaqpeg58c4dhqwc8ropmikhra77qmux4chhkrppk7ju5j9aaw1wwptwz8ds1eb36chjsb7p6d9y48t98ucfuwaei9ft15ktzjpbzd87twqdqdza9z6fxuesa9hddimgsfc4nr48qeazh9qo7iiseg7a8fjaibpoytmsyhi67u3wjexaanowi3mtjp9odb4y3dnnpt5zn3g79p69rgmgrrgdxmg8oibthkqx56ihhrphey7rc17b41qbfotdnhaqm7ok3wpebn39abs9drytpwtnzobs3x5gwmia8mkoskn5ru9ygto55kzeto3e39zdy3186dd7emeijyfke87huzokj6quyt71phhor18ujq9k1q4rhkh8fzds6cc6wuct1iaq6q1q6b1c4nxrq8ojxpogo9ht6kkpckmrsggfemrdxew1oe9hrn64n5ert6yn9phuwicnocssjooxhzt9g8uejhcesk6c1q1xzkzfmqgphxyatt157ktg196x7d7whdp47weog17xc4i51qhad9imncz5qujzks86xxo65fah6rijrfdy5hngp6gzktw3fchrje81gk7j1dhs63yosiep1w1935cuyjax8iwxafuxhccrkb9q54fzy8ffb6e5fhbwgk7a8xje3gcmtafcfzfrxni79nfgo8ii1e75hqs4o863uj7zmgfgyqypwbghnw6ds9bjt9q4fzoknn4jeo3wrmb4uxgjzhitjj7uuaku8judganppuuixf1po9mjp7pfhjnt4eeo1aa48dbcoe3fpis45okzncxh71n9ezbqeqjwuwgemhsyori59bufg8ei137rw9rai3n5wxj1h586r1japw3xuqdmuhtgncnrgpeiisjuqxebkiqemtqp15g3m9uegrgqtnesx6j47qh6wchufur8ffb9aa1iap1rap3ss8xa4cor44cq1j1oajq94kpps6y4erkrfuatj1wtjdhm9oisqky47xnpb66a3tfw9dsbekr81t68cf5uz65sdr6xkkranrhodft4a6e36r7a4tr1bxwdw7sn973db3ykemtgqyjia6go7gqm3ig3pio9ahj135u8muu6yiw19kazhhp5idptgg76649ppti5dx8gn9wnbbthqjrfc84aurx6y1tgddbdjwkj9fdq83eyecjn94ynbspsigw8sj1qq7o5ucu5ktt1q4buo6i9y9hnhi1t9zs5uaz3goybxjo5kcmwk8f9c1rjjmzmoq17gusbmt6e5aqtrze9f8qye9o8bxdeesmtye138hi5hxh3h9prwr9tg4ehhgzqu87wwbnshzcdbzgj94q5i787oeqmu43c77i1y8arpe5hmth1eg9ggtijuw7dyf4946m9ctyxok5uqdstp6eyss1hjqjs1ffb9k9z3cit7gko5u94u8n7q8nkxtyr9kqos8km14xyn9n45kieznqx73nppijjxczgq344fufxyyjfbsmi6y34fxf9rictdkajrr3zkc4eny1ymkx8h46y9skgqon7zy7yohm66a8sxsct6bzkyqbspuk4teho89r4k1np66xk3byzhesam5tye6d4sfpamyosysufqpc8zk3memymmxphfpnmrgyup8nt6r1g7tih36d4ire1xs4jh6hchtr57fqfjc9ju6zqnits7xjrdmqwzbcbh756p9f5zp6ccq69xsnccfzr6grb3rwuyck5jz7gw56r4gc3jpmpxtkbuier3iit4a8yocfqrdc7ggcdd5axectu45aufr5ufucnun4tjrqgt1rpwkejqyqocn9at5bjsh49qbh3aopg1atzymzdgp9rxhzsghmp3un7zhqjiw7de51r9jhf8yzs57r7jcbafgsj6zkcbshu15nkw78x7aw394wtzjyhgh5nnzctm3xmrbium5ttqbidjop9nmmsihnfs38t6jmx1n64t5r6dijdeqe18k3tzbbjtq5qd4g8gh1fudizzykc4nebmmsoixt8zwwc7ezgfu83cei15rmtk87fmx9nj5767wqsfu3drayc7ur7rt5p36yd3o7m1k56hafe9a1frwmszecer9bx44m9ohzezjg59zbzyht79jz7bsxy4hy6cojpxkb186mgs41a8yrbts6swmpb8q8x3j3cumyuyg1mbsp6pi1pps4x4dnxc353kti4ryo7ua7y \ No newline at end of file diff --git a/format/zbase32/fixtures/zero.bin b/format/zbase32/fixtures/zero.bin new file mode 100644 index 0000000..f76dd23 Binary files /dev/null and b/format/zbase32/fixtures/zero.bin differ diff --git a/format/zbase32/fixtures/zero.zbase32 b/format/zbase32/fixtures/zero.zbase32 new file mode 100644 index 0000000..46819b3 --- /dev/null +++ b/format/zbase32/fixtures/zero.zbase32 @@ -0,0 +1 @@ +yy \ No newline at end of file diff --git a/format/zbase32/zbase32.go b/format/zbase32/zbase32.go index 82e6f96..324de91 100644 --- a/format/zbase32/zbase32.go +++ b/format/zbase32/zbase32.go @@ -3,6 +3,7 @@ package zbase32 import ( "fmt" "io" + "os" "gopkg.in/corvus-ch/zbase32.v1" ) @@ -25,11 +26,16 @@ func (f *Format) OutputFileName(x byte) string { return fmt.Sprintf("%s.zbase32.%03d", f.Stem, x) } -// Writer creates a new Format writer using the given writer as output. -func (f *Format) Writer(out io.Writer) (io.Writer, []io.Closer, error) { - enc := zbase32.NewEncoder(zbase32.StdEncoding, out) +// Writer creates a new zbase32 format writer for the part identified by x. +func (f *Format) Writer(x byte) (io.Writer, []io.Closer, error) { + file, err := os.Create(f.OutputFileName(x)) + if nil != err { + return nil, nil, err + } - return enc, []io.Closer{enc}, nil + enc := zbase32.NewEncoder(zbase32.StdEncoding, file) + + return enc, []io.Closer{file, enc}, nil } // Reader creates a new Format reader using the given reader as input. diff --git a/format/zbase32/zbase32_test.go b/format/zbase32/zbase32_test.go index 4f23663..6a4df24 100644 --- a/format/zbase32/zbase32_test.go +++ b/format/zbase32/zbase32_test.go @@ -1,52 +1,30 @@ package zbase32_test import ( - "github.com/corvus-ch/horcrux/format" + "fmt" + "path/filepath" "testing" + "github.com/corvus-ch/horcrux/format" formatAssert "github.com/corvus-ch/horcrux/format/internal/assert" "github.com/corvus-ch/horcrux/format/zbase32" "github.com/stretchr/testify/assert" ) -var nameTests = []formatAssert.NameTest{ - {0, "mollis", "mollis.zbase32.000"}, - {1, "commodo", "commodo.zbase32.001"}, - {42, "pellentesque", "pellentesque.zbase32.042"}, - {181, "fringilla", "fringilla.zbase32.181"}, - {254, "venenatis", "venenatis.zbase32.254"}, - {255, "ridiculus", "ridiculus.zbase32.255"}, -} - -var dataTests = []formatAssert.DataTest{ - {[]byte{240, 191, 199}, "6n9hq"}, - {[]byte{212, 122, 4}, "4t7ye"}, - {[]byte{0xff}, "9h"}, - {[]byte{0xb5}, "sw"}, - {[]byte{0x34, 0x5a}, "gtpy"}, - {[]byte{0xff, 0xff, 0xff, 0xff, 0xff}, "99999999"}, - {[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, "999999999h"}, - {[]byte{ - 0xc0, 0x73, 0x62, 0x4a, 0xaf, 0x39, 0x78, 0x51, - 0x4e, 0xf8, 0x44, 0x3b, 0xb2, 0xa8, 0x59, 0xc7, - 0x5f, 0xc3, 0xcc, 0x6a, 0xf2, 0x6d, 0x5a, 0xaa, - }, "ab3sr1ix8fhfnuzaeo75fkn3a7xh8udk6jsiiko"}, -} - func factory(s string) format.Format { return zbase32.New(s) } -func TestFormat_OutputFileName(t *testing.T) { - formatAssert.Name(t, nameTests, factory) -} - func TestFormat_Reader(t *testing.T) { - formatAssert.DataRead(t, dataTests, factory) + formatAssert.DataRead(t, factory, ".zbase32") } func TestFormat_Writer(t *testing.T) { - formatAssert.DataWrite(t, dataTests, factory) + formatAssert.DataWrite(t, factory, ".zbase32", func(file string, x byte) []string { + base := filepath.Base(file) + name := base[0 : len(base)-len(filepath.Ext(base))] + return []string{fmt.Sprintf("%s.zbase32.%03d", name, x)} + }) } func TestFormat_Name(t *testing.T) { diff --git a/internal/create_test.go b/internal/create_test.go index 8628f6c..a8b5c53 100644 --- a/internal/create_test.go +++ b/internal/create_test.go @@ -7,6 +7,9 @@ import ( "github.com/bketelsen/logr" "github.com/corvus-ch/horcrux/create" + "github.com/corvus-ch/horcrux/format/raw" + "github.com/corvus-ch/horcrux/format/text" + "github.com/corvus-ch/horcrux/format/zbase32" "github.com/corvus-ch/horcrux/internal" "github.com/corvus-ch/logr/buffered" "github.com/stretchr/testify/assert" @@ -107,24 +110,26 @@ func TestCreateCommand_Input(t *testing.T) { func TestCreateCommand_Formats(t *testing.T) { tests := []struct { - name string - args []string - outputNames []string + name string + args []string + formats []string }{ - {"default", []string{"create"}, []string{"part.txt.042"}}, - {"stem", []string{"create", "-o", "foo"}, []string{"foo.txt.042"}}, - {"single", []string{"create", "-f", "raw"}, []string{"part.raw.042"}}, - {"multiple", []string{"create", "-f", "raw", "-f", "zbase32"}, []string{"part.raw.042", "part.zbase32.042"}}, + {"default", []string{"create"}, []string{text.Name}}, + {"single", []string{"create", "-f", "raw"}, []string{raw.Name}}, + {"multiple", []string{"create", "-f", "raw", "-f", "zbase32"}, []string{raw.Name, zbase32.Name}}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { assertCreateAction(t, test.args, func(cfg create.Config, _ logr.Logger) error { formats, err := cfg.Formats() if err != nil { - t.Skip("Formats not yet implemented") + t.Fatal(err) } - for i, outputName := range test.outputNames { - assert.Equal(t, outputName, formats[i].OutputFileName(42)) + if len(test.formats) != len(formats) { + t.Fatalf("expected %d formats, got %d", len(test.formats), len(formats)) + } + for i, name := range test.formats { + assert.Equal(t, name, formats[i].Name()) } return nil })