Skip to content

Commit

Permalink
Make text format the default (#11)
Browse files Browse the repository at this point in the history
* Make text format the default
* Update README to reflect new default behaviour
* Test for text as default format
* Fix text reader for larger data sets
  • Loading branch information
Christian Häusler authored Oct 20, 2018
1 parent 601a089 commit d8f05b5
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ build: c.out ${package_name}

.PHONY: test
test: c.out test.bin ${package_name}
./${package_name} create test.bin && ls test.raw.* > /dev/null
./${package_name} restore -o result.bin test.raw.* && diff test.bin result.bin > /dev/null
./${package_name} create test.bin && ls test.txt.* > /dev/null
./${package_name} restore -o result.bin test.txt.* && diff test.bin result.bin > /dev/null

c.out: main.cov $(addsuffix pkg.cov,${test_pkgs})
find . -name '*.cov' -exec gocoverutil -coverprofile=$@ merge {} +
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,20 @@ Backup a GPG key:
gpg --export-secret-key "${KEY_ID}" > "${KEY_ID}.gpg"
paperkey --secret-key="${KEY_ID}.gpg" --output-type raw --output="${KEY_ID}.bin"
horcrux create "${KEY_ID}.bin"
ls *.raw.* # Those are the files you can now place at your backup locations.
ls *.txt.* # Those are the files you can now place at your backup locations.

Restore a GPG key (builds on top of the above example):

horcrux restore -o paperkey.bin *.raw.* # For this example only two of the three files are required.
horcrux restore -o paperkey.bin *.txt.* # For this example only two of the three files are required.
paperkey --pubring=public.gpg --secrets=paperkey.bin --input-type=raw --output=secret.gpg
diff "${KEY_ID}.gpg" secret.gpg

## Milestones

* [x] Basic application
* [ ] Plain text format for print and easy scan/ocr
* [x] Plain text format for print and easy scan/ocr
* [ ] QR Code format for easier scanning
* [ ] Template system for custom output

## Contributing and license

Expand Down
2 changes: 1 addition & 1 deletion format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// Default holds the name of the default format.
const Default = raw.Name
const Default = text.Name

// Format describes the interface for the various input/output formats.
type Format interface {
Expand Down
4 changes: 2 additions & 2 deletions format/text/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (r *reader) Read(p []byte) (int, error) {
return 0, io.EOF
}

if r.buf.Len() == 0 && !r.eof {
for r.buf.Len() < len(p) && !r.eof {
data, err := r.readLine()
if nil != err && io.EOF != err {
return n, err
Expand All @@ -39,7 +39,7 @@ func (r *reader) Read(p []byte) (int, error) {
}

for n < len(p) && r.buf.Len() > 0 {
n += copy(p[n:], r.buf.Next(min(r.buf.Len(), len(p))))
n += copy(p[n:], r.buf.Next(len(p)))
}

if r.eof == true && r.buf.Len() == 0 {
Expand Down
52 changes: 52 additions & 0 deletions format/text/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,58 @@ var dataTests = []formatAssert.DataTest{
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, 0x45, 0x37, 0xa4,
0xd2, 0xca, 0xf2, 0x87, 0x88, 0x63, 0xdd, 0xd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59, 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 {
Expand Down
4 changes: 2 additions & 2 deletions internal/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ func TestCreateCommand_Formats(t *testing.T) {
args []string
outputNames []string
}{
{"default", []string{"create"}, []string{"part.raw.042"}},
{"stem", []string{"create", "-o", "foo"}, []string{"foo.raw.042"}},
{"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"}},
}
Expand Down
14 changes: 7 additions & 7 deletions internal/fixtures/TestApp/create.golden
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ ERROR
ERROR create a new set of horcruxes
ERROR
ERROR Flags:
ERROR --help Show context-sensitive help (also try --help-long and
ERROR --help-man).
ERROR -o, --output=OUTPUT name stem for the output files
ERROR -m, --sharecount=3 the number of horcruxes to create
ERROR -n, --threshold=2 the minimal number of horcruxes required for a restore
ERROR -f, --format=raw ... the formats the horcruxes are created in
ERROR -e, --encrypt encrypt output
ERROR --help Show context-sensitive help (also try --help-long and
ERROR --help-man).
ERROR -o, --output=OUTPUT name stem for the output files
ERROR -m, --sharecount=3 the number of horcruxes to create
ERROR -n, --threshold=2 the minimal number of horcruxes required for a restore
ERROR -f, --format=text ... the formats the horcruxes are created in
ERROR -e, --encrypt encrypt output
ERROR
ERROR Args:
ERROR [<input>] the input file to split
Expand Down
2 changes: 1 addition & 1 deletion internal/fixtures/TestApp/restore.golden
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ERROR Flags:
ERROR --help Show context-sensitive help (also try --help-long and
ERROR --help-man).
ERROR -o, --output=OUTPUT path to the output
ERROR -f, --format="raw" the formats the horcruxes are created in
ERROR -f, --format="text" the formats the horcruxes are created in
ERROR -d, --decrypt encrypt output
ERROR
ERROR Args:
Expand Down
3 changes: 2 additions & 1 deletion internal/restore_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal_test

import (
"github.com/corvus-ch/horcrux/format/text"
"io/ioutil"
"os"
"testing"
Expand Down Expand Up @@ -82,7 +83,7 @@ func TestRestoreCommand_Format(t *testing.T) {
args []string
format string
}{
{"default", []string{"restore", file.Name()}, raw.Name},
{"default", []string{"restore", file.Name()}, text.Name},
{"raw", []string{"restore", "-f", "raw", file.Name()}, raw.Name},
{"zbase32", []string{"restore", "-f", "zbase32", file.Name()}, zbase32.Name},
{"base64", []string{"restore", "-f", "base64", file.Name()}, base64.Name},
Expand Down

0 comments on commit d8f05b5

Please sign in to comment.