github.com/GGP1/kure@v0.8.4/commands/gen/phrase/phrase_test.go (about)

     1  package phrase
     2  
     3  import (
     4  	"math"
     5  	"os"
     6  	"strconv"
     7  	"testing"
     8  
     9  	"github.com/atotto/clipboard"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestGenPhrase(t *testing.T) {
    14  	if clipboard.Unsupported {
    15  		t.Skip("No clipboard utilities available")
    16  	}
    17  	cases := []struct {
    18  		desc      string
    19  		length    string
    20  		separator string
    21  		list      string
    22  		copy      bool
    23  		qr        bool
    24  	}{
    25  		{
    26  			desc:      "No list",
    27  			length:    "6",
    28  			separator: "%",
    29  			list:      "nolist",
    30  		},
    31  		{
    32  			desc:   "Word list",
    33  			length: "5",
    34  			copy:   true,
    35  			list:   "wordlist",
    36  		},
    37  		{
    38  			desc:      "Syllable list",
    39  			length:    "4",
    40  			separator: "-",
    41  			list:      "syllablelist",
    42  		},
    43  		{
    44  			desc:   "QR code",
    45  			length: "7",
    46  			qr:     true,
    47  		},
    48  	}
    49  
    50  	cmd := NewCmd()
    51  	os.Stdout = os.NewFile(0, "") // Mute stdout
    52  	os.Stderr = os.NewFile(0, "") // Mute stderr
    53  
    54  	for _, tc := range cases {
    55  		t.Run(tc.desc, func(t *testing.T) {
    56  			f := cmd.Flags()
    57  			f.Set("length", tc.length)
    58  			f.Set("separator", tc.separator)
    59  			f.Set("list", tc.list)
    60  			f.Set("copy", strconv.FormatBool(tc.copy))
    61  			f.Set("qr", strconv.FormatBool(tc.qr))
    62  
    63  			err := cmd.Execute()
    64  			assert.NoError(t, err, "Failed generating a passphrase")
    65  		})
    66  	}
    67  }
    68  
    69  func TestGenPhraseErrors(t *testing.T) {
    70  	cases := []struct {
    71  		desc    string
    72  		length  string
    73  		include string
    74  		exclude string
    75  		list    string
    76  		qr      bool
    77  	}{
    78  		{
    79  			desc:   "QR code content too long",
    80  			length: "600",
    81  			list:   "wordlist",
    82  			qr:     true,
    83  		},
    84  		{
    85  			desc:   "Invalid length",
    86  			length: "0",
    87  			list:   "wordlist",
    88  		},
    89  		{
    90  			desc:    "Invalid include and exclude",
    91  			length:  "3",
    92  			list:    "wordlist",
    93  			include: "test",
    94  			exclude: "test",
    95  		},
    96  		{
    97  			desc:   "Invalid list",
    98  			length: "4",
    99  			list:   "invalid",
   100  		},
   101  	}
   102  
   103  	cmd := NewCmd()
   104  	os.Stdout = os.NewFile(0, "") // Mute stdout
   105  	os.Stderr = os.NewFile(0, "") // Mute stderr
   106  
   107  	for _, tc := range cases {
   108  		t.Run(tc.desc, func(t *testing.T) {
   109  			f := cmd.Flags()
   110  			f.Set("length", tc.length)
   111  			f.Set("include", tc.include)
   112  			f.Set("exclude", tc.exclude)
   113  			f.Set("list", tc.list)
   114  			f.Set("qr", strconv.FormatBool(tc.qr))
   115  
   116  			err := cmd.Execute()
   117  			assert.Error(t, err)
   118  		})
   119  	}
   120  }
   121  
   122  func TestFormatSecurity(t *testing.T) {
   123  	cases := []struct {
   124  		desc             string
   125  		expectedKeyspace string
   126  		expectedTime     string
   127  		keyspace         float64
   128  		timeToCrack      float64
   129  	}{
   130  		{
   131  			desc:             "Inf",
   132  			keyspace:         math.Inf(1),
   133  			timeToCrack:      math.Inf(1),
   134  			expectedKeyspace: "+Inf",
   135  			expectedTime:     "+Inf",
   136  		},
   137  		{
   138  			desc:             "Sextillion-Millenniums",
   139  			keyspace:         sextillion,
   140  			timeToCrack:      float64(millennium),
   141  			expectedKeyspace: "1.00 sextillion",
   142  			expectedTime:     "1.00 millenniums",
   143  		},
   144  		{
   145  			desc:             "Quintillion-Century",
   146  			keyspace:         quintillion,
   147  			timeToCrack:      century,
   148  			expectedKeyspace: "1.00 quintillion",
   149  			expectedTime:     "1.00 centuries",
   150  		},
   151  		{
   152  			desc:             "Quadrillion-Decades",
   153  			keyspace:         quadrillion,
   154  			timeToCrack:      decade,
   155  			expectedKeyspace: "1.00 quadrillion",
   156  			expectedTime:     "1.00 decades",
   157  		},
   158  		{
   159  			desc:             "Trillion-Years",
   160  			keyspace:         trillion,
   161  			timeToCrack:      year,
   162  			expectedKeyspace: "1.00 trillion",
   163  			expectedTime:     "1.00 years",
   164  		},
   165  		{
   166  			desc:             "Billion-Months",
   167  			keyspace:         billion,
   168  			timeToCrack:      month,
   169  			expectedKeyspace: "1.00 billion",
   170  			expectedTime:     "1.00 months",
   171  		},
   172  		{
   173  			desc:             "Million-Days",
   174  			keyspace:         million,
   175  			timeToCrack:      day,
   176  			expectedKeyspace: "1.00 million",
   177  			expectedTime:     "1.00 days",
   178  		},
   179  		{
   180  			desc:             "Thousand-Hours",
   181  			keyspace:         thousand,
   182  			timeToCrack:      hour,
   183  			expectedKeyspace: "1.00 thousand",
   184  			expectedTime:     "1.00 hours",
   185  		},
   186  		{
   187  			desc:             "Minutes",
   188  			keyspace:         15,
   189  			timeToCrack:      minute,
   190  			expectedKeyspace: "15.00",
   191  			expectedTime:     "1.00 minutes",
   192  		},
   193  	}
   194  
   195  	for _, tc := range cases {
   196  		t.Run(tc.desc, func(t *testing.T) {
   197  			gotKeyspace, gotTime := FormatSecretSecurity(tc.keyspace, tc.timeToCrack)
   198  
   199  			assert.Equal(t, tc.expectedKeyspace, gotKeyspace)
   200  			assert.Equal(t, tc.expectedTime, gotTime)
   201  		})
   202  	}
   203  }
   204  
   205  func TestBeautify(t *testing.T) {
   206  	cases := []struct {
   207  		actual   string
   208  		expected string
   209  	}{
   210  		{
   211  			actual:   "27738957312183663402227335168.00",
   212  			expected: "27,738,957,312,183,663,402,227,335,168.00",
   213  		},
   214  		{
   215  			actual:   "7456198.39",
   216  			expected: "7,456,198.39",
   217  		},
   218  		{
   219  			actual:   "124561.65",
   220  			expected: "124,561.65",
   221  		},
   222  		{
   223  			actual:   "379.78",
   224  			expected: "379.78",
   225  		},
   226  	}
   227  
   228  	for i, tc := range cases {
   229  		t.Run(strconv.Itoa(i), func(t *testing.T) {
   230  			got := prettify(tc.actual)
   231  			assert.Equal(t, tc.expected, got)
   232  		})
   233  	}
   234  }
   235  
   236  func TestPostRun(t *testing.T) {
   237  	NewCmd().PostRun(nil, nil)
   238  }