1 package pflag
2
3 import (
4 "encoding/base64"
5 "fmt"
6 "os"
7 "testing"
8 )
9
10 func setUpBytesHex(bytesHex *[]byte) *FlagSet {
11 f := NewFlagSet("test", ContinueOnError)
12 f.BytesHexVar(bytesHex, "bytes", []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, "Some bytes in HEX")
13 f.BytesHexVarP(bytesHex, "bytes2", "B", []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, "Some bytes in HEX")
14 return f
15 }
16
17 func TestBytesHex(t *testing.T) {
18 testCases := []struct {
19 input string
20 success bool
21 expected string
22 }{
23
24 {"", true, ""},
25 {"01", true, "01"},
26 {"0101", true, "0101"},
27 {"1234567890abcdef", true, "1234567890ABCDEF"},
28 {"1234567890ABCDEF", true, "1234567890ABCDEF"},
29
30
31 {"0", false, ""},
32 {"000", false, ""},
33 {"qq", false, ""},
34 }
35
36 devnull, _ := os.Open(os.DevNull)
37 os.Stderr = devnull
38
39 for i := range testCases {
40 var bytesHex []byte
41 f := setUpBytesHex(&bytesHex)
42
43 tc := &testCases[i]
44
45
46 args := []string{
47 fmt.Sprintf("--bytes=%s", tc.input),
48 fmt.Sprintf("-B %s", tc.input),
49 fmt.Sprintf("--bytes2=%s", tc.input),
50 }
51
52 for _, arg := range args {
53 err := f.Parse([]string{arg})
54
55 if err != nil && tc.success == true {
56 t.Errorf("expected success, got %q", err)
57 continue
58 } else if err == nil && tc.success == false {
59
60 t.Errorf("expected failure while processing %q", tc.input)
61 continue
62 } else if tc.success {
63 bytesHex, err := f.GetBytesHex("bytes")
64 if err != nil {
65 t.Errorf("Got error trying to fetch the 'bytes' flag: %v", err)
66 }
67 if fmt.Sprintf("%X", bytesHex) != tc.expected {
68 t.Errorf("expected %q, got '%X'", tc.expected, bytesHex)
69 }
70 }
71 }
72 }
73 }
74
75 func setUpBytesBase64(bytesBase64 *[]byte) *FlagSet {
76 f := NewFlagSet("test", ContinueOnError)
77 f.BytesBase64Var(bytesBase64, "bytes", []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, "Some bytes in Base64")
78 f.BytesBase64VarP(bytesBase64, "bytes2", "B", []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, "Some bytes in Base64")
79 return f
80 }
81
82 func TestBytesBase64(t *testing.T) {
83 testCases := []struct {
84 input string
85 success bool
86 expected string
87 }{
88
89 {"", true, ""},
90 {"AQ==", true, "AQ=="},
91
92
93 {"AQ", false, ""},
94 {"ï", false, ""},
95 }
96
97 devnull, _ := os.Open(os.DevNull)
98 os.Stderr = devnull
99
100 for i := range testCases {
101 var bytesBase64 []byte
102 f := setUpBytesBase64(&bytesBase64)
103
104 tc := &testCases[i]
105
106
107 args := []string{
108 fmt.Sprintf("--bytes=%s", tc.input),
109 fmt.Sprintf("-B %s", tc.input),
110 fmt.Sprintf("--bytes2=%s", tc.input),
111 }
112
113 for _, arg := range args {
114 err := f.Parse([]string{arg})
115
116 if err != nil && tc.success == true {
117 t.Errorf("expected success, got %q", err)
118 continue
119 } else if err == nil && tc.success == false {
120
121 t.Errorf("expected failure while processing %q", tc.input)
122 continue
123 } else if tc.success {
124 bytesBase64, err := f.GetBytesBase64("bytes")
125 if err != nil {
126 t.Errorf("Got error trying to fetch the 'bytes' flag: %v", err)
127 }
128 if base64.StdEncoding.EncodeToString(bytesBase64) != tc.expected {
129 t.Errorf("expected %q, got '%X'", tc.expected, bytesBase64)
130 }
131 }
132 }
133 }
134 }
135
View as plain text