Test TagExtension
(t *testing.T)
| 755 | |
| 756 | // Test TagExtension |
| 757 | func TestTagExtension(t *testing.T) { |
| 758 | // Normal tag cannot exceed 0xff and there is no automatic |
| 759 | // conversion of the high-value tags into extended. |
| 760 | m1 := NewResponse(DefaultVersion, StatusOk, 0x12345678) |
| 761 | m1.Operation.Add(MakeAttribute("attr", 0x100, Binary{1, 2, 3, 4, 5})) |
| 762 | |
| 763 | _, err := m1.EncodeBytes() |
| 764 | assertErrorIs(t, err, "Tag 0x00000100 out of range") |
| 765 | |
| 766 | // Ensure extension tag encodes and decodes well |
| 767 | m1 = NewResponse(DefaultVersion, StatusOk, 0x12345678) |
| 768 | m1.Operation.Add(MakeAttribute("attr", TagExtension, |
| 769 | Binary{1, 2, 3, 4, 5})) |
| 770 | |
| 771 | data, err := m1.EncodeBytes() |
| 772 | assertNoError(t, err) |
| 773 | |
| 774 | m2 := Message{} |
| 775 | err = m2.DecodeBytes(data) |
| 776 | assertNoError(t, err) |
| 777 | |
| 778 | if !m1.Equal(m2) { |
| 779 | t.Errorf("Message is not the same after encoding and decoding") |
| 780 | } |
| 781 | |
| 782 | // Extension tag requires Binary payload |
| 783 | m1 = NewResponse(DefaultVersion, StatusOk, 0x12345678) |
| 784 | m1.Operation.Add(MakeAttribute("attr", TagExtension, |
| 785 | String("hello"))) |
| 786 | |
| 787 | _, err = m1.EncodeBytes() |
| 788 | assertErrorIs(t, err, "Tag extension: Binary value required, String present") |
| 789 | |
| 790 | // Extension tag requires at least 4 bytes of payload |
| 791 | m1 = NewResponse(DefaultVersion, StatusOk, 0x12345678) |
| 792 | m1.Operation.Add(MakeAttribute("attr", TagExtension, |
| 793 | Binary{1, 2, 3})) |
| 794 | |
| 795 | _, err = m1.EncodeBytes() |
| 796 | assertErrorIs(t, err, "Extension tag truncated") |
| 797 | |
| 798 | // Extension tag can't exceed 0x7fffffff, check that encoder |
| 799 | // validates it. |
| 800 | m1 = NewResponse(DefaultVersion, StatusOk, 0x12345678) |
| 801 | m1.Operation.Add(MakeAttribute("attr", TagExtension, |
| 802 | Binary{0x80, 1, 2, 3, 4, 5})) |
| 803 | |
| 804 | _, err = m1.EncodeBytes() |
| 805 | assertErrorIs(t, err, "Extension tag 0x80010203 out of range") |
| 806 | |
| 807 | // Now prepare to decoder tests |
| 808 | var d []byte |
| 809 | var m = &Message{} |
| 810 | |
| 811 | hdr := []byte{ |
| 812 | 0x01, 0x01, // IPP version |
| 813 | 0x00, 0x02, // Print-Job operation |
| 814 | 0x01, 0x02, 0x03, 0x04, // Request ID |
nothing calls this directly
no test coverage detected