gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/xcrypto/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 TestBMPString(t *testing.T) { 28 for i, test := range bmpStringTests { 29 expected, err := hex.DecodeString(test.expectedHex) 30 if err != nil { 31 t.Fatalf("#%d: failed to decode expectation", i) 32 } 33 34 out, err := bmpString(test.in) 35 if err == nil && test.shouldFail { 36 t.Errorf("#%d: expected to fail, but produced %x", i, out) 37 continue 38 } 39 40 if err != nil && !test.shouldFail { 41 t.Errorf("#%d: failed unexpectedly: %s", i, err) 42 continue 43 } 44 45 if !test.shouldFail { 46 if !bytes.Equal(out, expected) { 47 t.Errorf("#%d: expected %s, got %x", i, test.expectedHex, out) 48 continue 49 } 50 51 roundTrip, err := decodeBMPString(out) 52 if err != nil { 53 t.Errorf("#%d: decoding output gave an error: %s", i, err) 54 continue 55 } 56 57 if roundTrip != test.in { 58 t.Errorf("#%d: decoding output resulted in %q, but it should have been %q", i, roundTrip, test.in) 59 continue 60 } 61 } 62 } 63 }