Skip to content

Commit

Permalink
Adding test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
1griffy committed May 5, 2023
1 parent 7592fc3 commit 42bc606
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 9 deletions.
16 changes: 7 additions & 9 deletions cmd/tle/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/kelseyhightower/envconfig"
"log"
"os"
"time"
)

// Default settings.
Expand All @@ -24,7 +23,7 @@ const usage = `tlock v1.0.0 -- github.com/drand/tlock
Usage:
tle [--encrypt] (-r round)... [--armor] [-o OUTPUT] [INPUT]
If input is a string (not a file)
tle [--encrypt] (-r round)... [--armor] [-o OUTPUT] [--raw_input INPUT]
tle [--encrypt] (-r round)... [--armor] [-o OUTPUT] [--input INPUT]
tle --decrypt [-o OUTPUT] [INPUT]
Expand Down Expand Up @@ -180,22 +179,21 @@ func validateFlags(f *Flags) error {
if f.Duration == "" && f.Round == 0 && f.Time == "" {
return fmt.Errorf("one of -D/--duration, -r/--round or -T/--time must be specified")
}
if f.Duration != "" && f.Round != 0 {
return fmt.Errorf("-D/--duration can't be used with -r/--round")
}
if f.Duration != "" && f.Time != "" {
return fmt.Errorf("-D/--duration can't be used with -T/--time")
}
if f.Time != "" && f.Round != 0 {
return fmt.Errorf("-T/--time can't be used with -r/--round")
}
if f.Time != "" {
t, err := time.Parse(time.RFC3339, f.Time)
duration, err := timestampToDuration(f.Time)
if err != nil {
return fmt.Errorf("time format must be RFC3339 (\"2006-01-02T15:04:05Z07:00\")")
}
duration := time.Until(t)
if duration <= 0 {
return fmt.Errorf("must specify a future time")
return err
}
f.Duration = fmt.Sprintf("%ds", int(duration.Seconds()))
f.Duration = duration
}
}

Expand Down
12 changes: 12 additions & 0 deletions cmd/tle/commands/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,15 @@ func durationFrom(start time.Time, value int, duration rune) time.Duration {
}
return 0
}

func timestampToDuration(timestamp string) (string, error) {
t, err := time.Parse(time.RFC3339, timestamp)
if err != nil {
return "", fmt.Errorf("time format must be RFC3339 (\"2006-01-02T15:04:05Z07:00\")")
}
duration := time.Until(t)
if duration <= 0 {
return "", fmt.Errorf("must specify a future time")
}
return fmt.Sprintf("%ds", int(duration.Seconds())), nil
}
72 changes: 72 additions & 0 deletions cmd/tle/commands/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,78 @@ func Test(t *testing.T) {
},
shouldError: true,
},
{
name: "parsing encrypt with duration, round as well as timestamp fails",
flags: []KV{
{
key: "TLE_ENCRYPT",
value: "true",
},
{
key: "TLE_DURATION",
value: "1d",
},
{
key: "TLE_ROUND",
value: "1",
},
{
key: "TLE_TIME",
value: "2999-04-23T08:15:05Z",
},
},
shouldError: true,
},
{
name: "parsing encrypt with both round and timestamp fails",
flags: []KV{
{
key: "TLE_ENCRYPT",
value: "true",
},
{
key: "TLE_ROUND",
value: "1",
},
{
key: "TLE_TIME",
value: "2999-04-23T08:15:05Z",
},
},
shouldError: true,
},
{
name: "parsing encrypt with both duration and timestamp fails",
flags: []KV{
{
key: "TLE_ENCRYPT",
value: "true",
},
{
key: "TLE_DURATION",
value: "1d",
},
{
key: "TLE_TIME",
value: "2999-04-23T08:15:05Z",
},
},
shouldError: true,
},
{
name: "parsing encrypt with timestamp passes",
flags: []KV{
{
key: "TLE_ENCRYPT",
value: "true",
},
{
key: "TLE_TIME",
value: "2999-04-23T08:15:05Z",
},
},
shouldError: false,
},
{
name: "parsing encrypt with round passes",
flags: []KV{
Expand Down
48 changes: 48 additions & 0 deletions tlock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/drand/drand/crypto"
bls "github.com/drand/kyber-bls12381"
"github.com/stretchr/testify/require"
"math/rand"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -92,6 +93,53 @@ func TestEarlyDecryptionWithRound(t *testing.T) {
require.ErrorIs(t, err, tlock.ErrTooEarly)
}

func TestEncryptionWithTimestamp(t *testing.T) {
if testing.Short() {
t.Skip("skipping live testing in short mode")
}

network, err := http.NewNetwork(testnetHost, testnetChainHash)
require.NoError(t, err)

// =========================================================================
// Encrypt

// Read the plaintext data to be encrypted.
in, err := os.Open("test_artifacts/data.txt")
require.NoError(t, err)
defer in.Close()

// Write the encoded information to this buffer.
var cipherData bytes.Buffer

// Timestamp to duration
rand.Seed(time.Now().UnixNano())
timestamp := time.Now().Add(4 * time.Second).Format(time.RFC3339)
tstamp, err := time.Parse(time.RFC3339, timestamp)
require.NoError(t, err)
duration := time.Until(tstamp)

// Encryption with duration
roundNumber := network.RoundNumber(time.Now().Add(duration))
err = tlock.New(network).Encrypt(&cipherData, in, roundNumber)
require.NoError(t, err)

// =========================================================================
// Decrypt

time.Sleep(5 * time.Second)

// Write the decoded information to this buffer.
var plainData bytes.Buffer

err = tlock.New(network).Decrypt(&plainData, &cipherData)
require.NoError(t, err)

if !bytes.Equal(plainData.Bytes(), dataFile) {
t.Fatalf("decrypted file is invalid; expected %d; got %d", len(dataFile), len(plainData.Bytes()))
}
}

func TestEncryptionWithDuration(t *testing.T) {
if testing.Short() {
t.Skip("skipping live testing in short mode")
Expand Down

0 comments on commit 42bc606

Please sign in to comment.