esim_der_test.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package device
  2. import (
  3. "bytes"
  4. "testing"
  5. )
  6. func TestDerEncodeShortForm(t *testing.T) {
  7. got := derEncode(0x30, []byte{0x01, 0x02, 0x03})
  8. want := []byte{0x30, 0x03, 0x01, 0x02, 0x03}
  9. if !bytes.Equal(got, want) {
  10. t.Fatalf("derEncode short = %X, want %X", got, want)
  11. }
  12. }
  13. func TestDerEncodeLongFormTag(t *testing.T) {
  14. got := derEncode(0x5F37, []byte{0xAA})
  15. want := []byte{0x5F, 0x37, 0x01, 0xAA}
  16. if !bytes.Equal(got, want) {
  17. t.Fatalf("derEncode long tag = %X, want %X", got, want)
  18. }
  19. // Two-byte constructed tag used by every ES10 request.
  20. got = derEncode(0xBF38, nil)
  21. want = []byte{0xBF, 0x38, 0x00}
  22. if !bytes.Equal(got, want) {
  23. t.Fatalf("derEncode BF38 = %X, want %X", got, want)
  24. }
  25. }
  26. func TestDerEncodeLongFormLength(t *testing.T) {
  27. // 200 bytes forces an 0x81 long-form length.
  28. got := derEncode(0x30, make([]byte, 200))
  29. if got[0] != 0x30 || got[1] != 0x81 || got[2] != 200 {
  30. t.Fatalf("derEncode 200-byte length header = %X", got[:3])
  31. }
  32. // 1000 bytes forces an 0x82 long-form length.
  33. got = derEncode(0x30, make([]byte, 1000))
  34. if got[0] != 0x30 || got[1] != 0x82 || got[2] != 0x03 || got[3] != 0xE8 {
  35. t.Fatalf("derEncode 1000-byte length header = %X", got[:4])
  36. }
  37. }
  38. // The encoder must round-trip through the existing decoder (derParse).
  39. func TestDerRoundTrip(t *testing.T) {
  40. inner := derConstruct(0xA0,
  41. derEncode(0x80, []byte{0x11, 0x22}),
  42. derEncode(0x5A, []byte{0x98, 0x10}),
  43. )
  44. outer := derConstruct(0xBF38, derEncode(0x30, inner), derEncode(0x04, []byte{0xFF}))
  45. nodes := derParse(outer)
  46. if len(nodes) != 1 || nodes[0].tag != 0xBF38 {
  47. t.Fatalf("expected single BF38 root, got %#v", nodes)
  48. }
  49. seq := derValue(nodes[0].children, 0x30)
  50. if seq == nil {
  51. t.Fatalf("missing 0x30 child")
  52. }
  53. if got := derFindValue(seq, 0x80); !bytes.Equal(got, []byte{0x11, 0x22}) {
  54. t.Fatalf("nested 0x80 = %X", got)
  55. }
  56. if got := derValue(nodes[0].children, 0x04); !bytes.Equal(got, []byte{0xFF}) {
  57. t.Fatalf("0x04 = %X", got)
  58. }
  59. }
  60. func TestDerElementAt(t *testing.T) {
  61. // BF38 { 30 03 010203 , 04 02 AABB }
  62. buf := derConstruct(0xBF38, derEncode(0x30, []byte{1, 2, 3}), derEncode(0x04, []byte{0xAA, 0xBB}))
  63. tag, headerLen, totalLen, err := derElementAt(buf, 0)
  64. if err != nil {
  65. t.Fatalf("derElementAt: %v", err)
  66. }
  67. if tag != 0xBF38 || headerLen != 3 || totalLen != len(buf) {
  68. t.Fatalf("root: tag=%X header=%d total=%d (len=%d)", tag, headerLen, totalLen, len(buf))
  69. }
  70. // First child starts right after the root header.
  71. tag, headerLen, totalLen, err = derElementAt(buf, 3)
  72. if err != nil || tag != 0x30 || headerLen != 2 || totalLen != 5 {
  73. t.Fatalf("child: tag=%X header=%d total=%d err=%v", tag, headerLen, totalLen, err)
  74. }
  75. }
  76. func TestUnwrapDER(t *testing.T) {
  77. // Already wrapped in 5F37 → returns inner value.
  78. wrapped := derEncode(0x5F37, []byte{0x01, 0x02})
  79. if got := unwrapDER(wrapped, 0x5F37); !bytes.Equal(got, []byte{0x01, 0x02}) {
  80. t.Fatalf("unwrapDER wrapped = %X", got)
  81. }
  82. // Bare value → returned unchanged.
  83. bare := []byte{0x09, 0x08}
  84. if got := unwrapDER(bare, 0x5F37); !bytes.Equal(got, bare) {
  85. t.Fatalf("unwrapDER bare = %X", got)
  86. }
  87. }