-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathlibgfshare_test.go
147 lines (131 loc) · 3.61 KB
/
libgfshare_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package shamir
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"
"testing"
)
var hasGfsplit bool
var hasGfcombine bool
var secret = []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,
}
func init() {
hasGfsplit = hasCommand("gfsplit")
hasGfcombine = hasCommand("gfcombine")
}
func hasCommand(name string) bool {
cmd := exec.Command("command", "-v", name)
cmd.Stderr = os.Stderr
return nil == cmd.Run()
}
func TestGfsplit(t *testing.T) {
if !hasGfsplit {
t.Skip("Skipping because 'libgfshare' is not available.")
}
dir, err := ioutil.TempDir(os.TempDir(), "data")
defer os.RemoveAll(dir)
if err != nil {
t.Fatal("failed to create temporary file")
}
secretFile, err := ioutil.TempFile(dir, "secret")
if err != nil {
t.Fatal("failed to create secret file")
}
if _, err := secretFile.Write(secret); err != nil {
t.Fatalf("failed to write to secret file: %v", err)
}
secretFile.Close()
split := exec.Command("gfsplit", "-n", "2", "-m", "3", secretFile.Name())
split.Dir = dir
if err := split.Run(); err != nil {
t.Fatalf("failed to run gfsplitt: %v", err)
}
partNames, err := filepath.Glob(secretFile.Name() + ".*")
if err != nil {
t.Errorf("failed to lookup ouput from gfsplit: %v", err)
}
if len(partNames) != 3 {
t.Errorf("found unexpected number of files: got %d expected 3", len(partNames))
}
parts := make(map[byte]io.Reader, 3)
for _, name := range partNames {
x, err := strconv.ParseUint(string(name[len(name)-3:]), 10, 8)
if err != nil {
t.Fatalf("failed to parse x coordinate of file %s: %v", name, err)
}
file, err := os.Open(name)
if err != nil {
t.Fatalf("failed to open file: %v", err)
}
parts[byte(x)] = file
}
reader, err := NewReader(parts)
if err != nil {
t.Fatalf("failed to create shamir reader: %v", err)
}
result, err := ioutil.ReadAll(reader)
if err != nil {
t.Fatalf("failed to combine secret: %v", err)
}
if !bytes.Equal(secret, result) {
t.Fatalf("unexpected result:\n\texpected: %v\n\tgot: %v", secret, result)
}
}
func TestGfcombine(t *testing.T) {
if !hasGfcombine {
t.Skip("Skipping because 'libgfshare' is not available.")
}
dir, err := ioutil.TempDir(os.TempDir(), "data")
defer os.RemoveAll(dir)
if err != nil {
t.Fatal("failed to create temporary file")
}
partFiles := make(map[byte]*os.File, 3)
writer, err := NewWriter(3, 2, func(x byte) (io.Writer, error) {
file, err := os.Create(fmt.Sprintf("%s/part.%03d", dir, x))
partFiles[x] = file
return file, err
})
if _, err := writer.Write(secret); err != nil {
t.Fatalf("Failed to split secret: %v", err)
}
for _, part := range partFiles {
part.Close()
}
secretFileName := fmt.Sprintf("%s/secret", dir)
for i, fileA := range partFiles {
for j, fileB := range partFiles {
if i == j {
continue
}
combine := exec.Command("gfcombine", "-o", secretFileName, fileA.Name(), fileB.Name())
combine.Dir = dir
if err := combine.Run(); err != nil {
t.Fatalf("failed to combine %s and %s: %v", fileA.Name(), fileB.Name(), err)
}
secretFile, err := os.Open(secretFileName)
if err != nil {
t.Fatalf("failed to open secret file: %v", err)
}
result, err := ioutil.ReadAll(secretFile)
if err != nil {
t.Fatalf("failed to read secret file: %v", err)
}
if !bytes.Equal(secret, result) {
t.Fatalf("unexpected result:\n\texpected: %v\n\tgot: %v", secret, result)
}
secretFile.Close()
if err := os.Remove(secretFileName); err != nil {
t.Fatalf("failed to clean up secret file: %v", err)
}
}
}
}