MCPcopy Create free account
hub / github.com/OpenPrinting/goipp / TestTagExtension

Function TestTagExtension

encdec_test.go:757–863  ·  view source on GitHub ↗

Test TagExtension

(t *testing.T)

Source from the content-addressed store, hash-verified

755
756// Test TagExtension
757func 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

Callers

nothing calls this directly

Calls 9

DecodeBytesMethod · 0.95
NewResponseFunction · 0.85
MakeAttributeFunction · 0.85
assertErrorIsFunction · 0.85
assertNoErrorFunction · 0.85
StringTypeAlias · 0.85
EncodeBytesMethod · 0.80
AddMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected