github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/lang/funcs/encoding_test.go (about) 1 package funcs 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/zclconf/go-cty/cty" 8 ) 9 10 func TestBase64Decode(t *testing.T) { 11 tests := []struct { 12 String cty.Value 13 Want cty.Value 14 Err bool 15 }{ 16 { 17 cty.StringVal("YWJjMTIzIT8kKiYoKSctPUB+"), 18 cty.StringVal("abc123!?$*&()'-=@~"), 19 false, 20 }, 21 { // Invalid base64 data decoding 22 cty.StringVal("this-is-an-invalid-base64-data"), 23 cty.UnknownVal(cty.String), 24 true, 25 }, 26 { // Invalid utf-8 27 cty.StringVal("\xc3\x28"), 28 cty.UnknownVal(cty.String), 29 true, 30 }, 31 } 32 33 for _, test := range tests { 34 t.Run(fmt.Sprintf("base64decode(%#v)", test.String), func(t *testing.T) { 35 got, err := Base64Decode(test.String) 36 37 if test.Err { 38 if err == nil { 39 t.Fatal("succeeded; want error") 40 } 41 return 42 } else if err != nil { 43 t.Fatalf("unexpected error: %s", err) 44 } 45 46 if !got.RawEquals(test.Want) { 47 t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want) 48 } 49 }) 50 } 51 } 52 53 func TestBase64Encode(t *testing.T) { 54 tests := []struct { 55 String cty.Value 56 Want cty.Value 57 Err bool 58 }{ 59 { 60 cty.StringVal("abc123!?$*&()'-=@~"), 61 cty.StringVal("YWJjMTIzIT8kKiYoKSctPUB+"), 62 false, 63 }, 64 } 65 66 for _, test := range tests { 67 t.Run(fmt.Sprintf("base64encode(%#v)", test.String), func(t *testing.T) { 68 got, err := Base64Encode(test.String) 69 70 if test.Err { 71 if err == nil { 72 t.Fatal("succeeded; want error") 73 } 74 return 75 } else if err != nil { 76 t.Fatalf("unexpected error: %s", err) 77 } 78 79 if !got.RawEquals(test.Want) { 80 t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want) 81 } 82 }) 83 } 84 } 85 86 func TestBase64Gzip(t *testing.T) { 87 tests := []struct { 88 String cty.Value 89 Want cty.Value 90 Err bool 91 }{ 92 { 93 cty.StringVal("test"), 94 cty.StringVal("H4sIAAAAAAAA/ypJLS4BAAAA//8BAAD//wx+f9gEAAAA"), 95 false, 96 }, 97 } 98 99 for _, test := range tests { 100 t.Run(fmt.Sprintf("base64gzip(%#v)", test.String), func(t *testing.T) { 101 got, err := Base64Gzip(test.String) 102 103 if test.Err { 104 if err == nil { 105 t.Fatal("succeeded; want error") 106 } 107 return 108 } else if err != nil { 109 t.Fatalf("unexpected error: %s", err) 110 } 111 112 if !got.RawEquals(test.Want) { 113 t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want) 114 } 115 }) 116 } 117 } 118 119 func TestURLEncode(t *testing.T) { 120 tests := []struct { 121 String cty.Value 122 Want cty.Value 123 Err bool 124 }{ 125 { 126 cty.StringVal("abc123-_"), 127 cty.StringVal("abc123-_"), 128 false, 129 }, 130 { 131 cty.StringVal("foo:bar@localhost?foo=bar&bar=baz"), 132 cty.StringVal("foo%3Abar%40localhost%3Ffoo%3Dbar%26bar%3Dbaz"), 133 false, 134 }, 135 { 136 cty.StringVal("mailto:email?subject=this+is+my+subject"), 137 cty.StringVal("mailto%3Aemail%3Fsubject%3Dthis%2Bis%2Bmy%2Bsubject"), 138 false, 139 }, 140 { 141 cty.StringVal("foo/bar"), 142 cty.StringVal("foo%2Fbar"), 143 false, 144 }, 145 } 146 147 for _, test := range tests { 148 t.Run(fmt.Sprintf("urlencode(%#v)", test.String), func(t *testing.T) { 149 got, err := URLEncode(test.String) 150 151 if test.Err { 152 if err == nil { 153 t.Fatal("succeeded; want error") 154 } 155 return 156 } else if err != nil { 157 t.Fatalf("unexpected error: %s", err) 158 } 159 160 if !got.RawEquals(test.Want) { 161 t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want) 162 } 163 }) 164 } 165 }