Skip to content

Commit 42bc606

Browse files
committed
Adding test cases
1 parent 7592fc3 commit 42bc606

File tree

4 files changed

+139
-9
lines changed

4 files changed

+139
-9
lines changed

cmd/tle/commands/commands.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/kelseyhightower/envconfig"
99
"log"
1010
"os"
11-
"time"
1211
)
1312

1413
// Default settings.
@@ -24,7 +23,7 @@ const usage = `tlock v1.0.0 -- github.com/drand/tlock
2423
Usage:
2524
tle [--encrypt] (-r round)... [--armor] [-o OUTPUT] [INPUT]
2625
If input is a string (not a file)
27-
tle [--encrypt] (-r round)... [--armor] [-o OUTPUT] [--raw_input INPUT]
26+
tle [--encrypt] (-r round)... [--armor] [-o OUTPUT] [--input INPUT]
2827
2928
tle --decrypt [-o OUTPUT] [INPUT]
3029
@@ -180,22 +179,21 @@ func validateFlags(f *Flags) error {
180179
if f.Duration == "" && f.Round == 0 && f.Time == "" {
181180
return fmt.Errorf("one of -D/--duration, -r/--round or -T/--time must be specified")
182181
}
182+
if f.Duration != "" && f.Round != 0 {
183+
return fmt.Errorf("-D/--duration can't be used with -r/--round")
184+
}
183185
if f.Duration != "" && f.Time != "" {
184186
return fmt.Errorf("-D/--duration can't be used with -T/--time")
185187
}
186188
if f.Time != "" && f.Round != 0 {
187189
return fmt.Errorf("-T/--time can't be used with -r/--round")
188190
}
189191
if f.Time != "" {
190-
t, err := time.Parse(time.RFC3339, f.Time)
192+
duration, err := timestampToDuration(f.Time)
191193
if err != nil {
192-
return fmt.Errorf("time format must be RFC3339 (\"2006-01-02T15:04:05Z07:00\")")
193-
}
194-
duration := time.Until(t)
195-
if duration <= 0 {
196-
return fmt.Errorf("must specify a future time")
194+
return err
197195
}
198-
f.Duration = fmt.Sprintf("%ds", int(duration.Seconds()))
196+
f.Duration = duration
199197
}
200198
}
201199

cmd/tle/commands/encrypt.go

+12
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,15 @@ func durationFrom(start time.Time, value int, duration rune) time.Duration {
120120
}
121121
return 0
122122
}
123+
124+
func timestampToDuration(timestamp string) (string, error) {
125+
t, err := time.Parse(time.RFC3339, timestamp)
126+
if err != nil {
127+
return "", fmt.Errorf("time format must be RFC3339 (\"2006-01-02T15:04:05Z07:00\")")
128+
}
129+
duration := time.Until(t)
130+
if duration <= 0 {
131+
return "", fmt.Errorf("must specify a future time")
132+
}
133+
return fmt.Sprintf("%ds", int(duration.Seconds())), nil
134+
}

cmd/tle/commands/flags_test.go

+72
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,78 @@ func Test(t *testing.T) {
4646
},
4747
shouldError: true,
4848
},
49+
{
50+
name: "parsing encrypt with duration, round as well as timestamp fails",
51+
flags: []KV{
52+
{
53+
key: "TLE_ENCRYPT",
54+
value: "true",
55+
},
56+
{
57+
key: "TLE_DURATION",
58+
value: "1d",
59+
},
60+
{
61+
key: "TLE_ROUND",
62+
value: "1",
63+
},
64+
{
65+
key: "TLE_TIME",
66+
value: "2999-04-23T08:15:05Z",
67+
},
68+
},
69+
shouldError: true,
70+
},
71+
{
72+
name: "parsing encrypt with both round and timestamp fails",
73+
flags: []KV{
74+
{
75+
key: "TLE_ENCRYPT",
76+
value: "true",
77+
},
78+
{
79+
key: "TLE_ROUND",
80+
value: "1",
81+
},
82+
{
83+
key: "TLE_TIME",
84+
value: "2999-04-23T08:15:05Z",
85+
},
86+
},
87+
shouldError: true,
88+
},
89+
{
90+
name: "parsing encrypt with both duration and timestamp fails",
91+
flags: []KV{
92+
{
93+
key: "TLE_ENCRYPT",
94+
value: "true",
95+
},
96+
{
97+
key: "TLE_DURATION",
98+
value: "1d",
99+
},
100+
{
101+
key: "TLE_TIME",
102+
value: "2999-04-23T08:15:05Z",
103+
},
104+
},
105+
shouldError: true,
106+
},
107+
{
108+
name: "parsing encrypt with timestamp passes",
109+
flags: []KV{
110+
{
111+
key: "TLE_ENCRYPT",
112+
value: "true",
113+
},
114+
{
115+
key: "TLE_TIME",
116+
value: "2999-04-23T08:15:05Z",
117+
},
118+
},
119+
shouldError: false,
120+
},
49121
{
50122
name: "parsing encrypt with round passes",
51123
flags: []KV{

tlock_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/drand/drand/crypto"
77
bls "github.com/drand/kyber-bls12381"
88
"github.com/stretchr/testify/require"
9+
"math/rand"
910
"os"
1011
"strings"
1112
"testing"
@@ -92,6 +93,53 @@ func TestEarlyDecryptionWithRound(t *testing.T) {
9293
require.ErrorIs(t, err, tlock.ErrTooEarly)
9394
}
9495

96+
func TestEncryptionWithTimestamp(t *testing.T) {
97+
if testing.Short() {
98+
t.Skip("skipping live testing in short mode")
99+
}
100+
101+
network, err := http.NewNetwork(testnetHost, testnetChainHash)
102+
require.NoError(t, err)
103+
104+
// =========================================================================
105+
// Encrypt
106+
107+
// Read the plaintext data to be encrypted.
108+
in, err := os.Open("test_artifacts/data.txt")
109+
require.NoError(t, err)
110+
defer in.Close()
111+
112+
// Write the encoded information to this buffer.
113+
var cipherData bytes.Buffer
114+
115+
// Timestamp to duration
116+
rand.Seed(time.Now().UnixNano())
117+
timestamp := time.Now().Add(4 * time.Second).Format(time.RFC3339)
118+
tstamp, err := time.Parse(time.RFC3339, timestamp)
119+
require.NoError(t, err)
120+
duration := time.Until(tstamp)
121+
122+
// Encryption with duration
123+
roundNumber := network.RoundNumber(time.Now().Add(duration))
124+
err = tlock.New(network).Encrypt(&cipherData, in, roundNumber)
125+
require.NoError(t, err)
126+
127+
// =========================================================================
128+
// Decrypt
129+
130+
time.Sleep(5 * time.Second)
131+
132+
// Write the decoded information to this buffer.
133+
var plainData bytes.Buffer
134+
135+
err = tlock.New(network).Decrypt(&plainData, &cipherData)
136+
require.NoError(t, err)
137+
138+
if !bytes.Equal(plainData.Bytes(), dataFile) {
139+
t.Fatalf("decrypted file is invalid; expected %d; got %d", len(dataFile), len(plainData.Bytes()))
140+
}
141+
}
142+
95143
func TestEncryptionWithDuration(t *testing.T) {
96144
if testing.Short() {
97145
t.Skip("skipping live testing in short mode")

0 commit comments

Comments
 (0)