github.phpd.cn/hashicorp/packer@v1.3.2/builder/azure/pkcs12/bmp-string_test.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package pkcs12 6 7 import ( 8 "bytes" 9 "encoding/hex" 10 "testing" 11 ) 12 13 var bmpStringTests = []struct { 14 in string 15 expectedHex string 16 shouldFail bool 17 }{ 18 {"", "0000", false}, 19 // Example from https://tools.ietf.org/html/rfc7292#appendix-B. 20 {"Beavis", "0042006500610076006900730000", false}, 21 // Some characters from the "Letterlike Symbols Unicode block". 22 {"\u2115 - Double-struck N", "21150020002d00200044006f00750062006c0065002d00730074007200750063006b0020004e0000", false}, 23 // any character outside the BMP should trigger an error. 24 {"\U0001f000 East wind (Mahjong)", "", true}, 25 } 26 27 func TestBMPStringDecode(t *testing.T) { 28 if _, err := decodeBMPString([]byte("a")); err == nil { 29 t.Fatalf("expected decode to fail, but it succeeded") 30 } 31 } 32 33 func TestBMPString(t *testing.T) { 34 for i, test := range bmpStringTests { 35 expected, err := hex.DecodeString(test.expectedHex) 36 if err != nil { 37 t.Fatalf("#%d: failed to decode expectation", i) 38 } 39 40 out, err := bmpString(test.in) 41 if err == nil && test.shouldFail { 42 t.Errorf("#%d: expected to fail, but produced %x", i, out) 43 continue 44 } 45 46 if err != nil && !test.shouldFail { 47 t.Errorf("#%d: failed unexpectedly: %s", i, err) 48 continue 49 } 50 51 if !test.shouldFail { 52 if !bytes.Equal(out, expected) { 53 t.Errorf("#%d: expected %s, got %x", i, test.expectedHex, out) 54 continue 55 } 56 57 roundTrip, err := decodeBMPString(out) 58 if err != nil { 59 t.Errorf("#%d: decoding output gave an error: %s", i, err) 60 continue 61 } 62 63 if roundTrip != test.in { 64 t.Errorf("#%d: decoding output resulted in %q, but it should have been %q", i, roundTrip, test.in) 65 continue 66 } 67 } 68 } 69 }