bmp-string_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. package pkcs12
  5. import (
  6. "bytes"
  7. "encoding/hex"
  8. "testing"
  9. )
  10. var bmpStringTests = []struct {
  11. in string
  12. expectedHex string
  13. shouldFail bool
  14. }{
  15. {"", "0000", false},
  16. // Example from https://tools.ietf.org/html/rfc7292#appendix-B.
  17. {"Beavis", "0042006500610076006900730000", false},
  18. // Some characters from the "Letterlike Symbols Unicode block".
  19. {"\u2115 - Double-struck N", "21150020002d00200044006f00750062006c0065002d00730074007200750063006b0020004e0000", false},
  20. // any character outside the BMP should trigger an error.
  21. {"\U0001f000 East wind (Mahjong)", "", true},
  22. }
  23. func TestBMPString(t *testing.T) {
  24. for i, test := range bmpStringTests {
  25. expected, err := hex.DecodeString(test.expectedHex)
  26. if err != nil {
  27. t.Fatalf("#%d: failed to decode expectation", i)
  28. }
  29. out, err := bmpString(test.in)
  30. if err == nil && test.shouldFail {
  31. t.Errorf("#%d: expected to fail, but produced %x", i, out)
  32. continue
  33. }
  34. if err != nil && !test.shouldFail {
  35. t.Errorf("#%d: failed unexpectedly: %s", i, err)
  36. continue
  37. }
  38. if !test.shouldFail {
  39. if !bytes.Equal(out, expected) {
  40. t.Errorf("#%d: expected %s, got %x", i, test.expectedHex, out)
  41. continue
  42. }
  43. roundTrip, err := decodeBMPString(out)
  44. if err != nil {
  45. t.Errorf("#%d: decoding output gave an error: %s", i, err)
  46. continue
  47. }
  48. if roundTrip != test.in {
  49. t.Errorf("#%d: decoding output resulted in %q, but it should have been %q", i, roundTrip, test.in)
  50. continue
  51. }
  52. }
  53. }
  54. }