Files
ippserver/packages/ipp/keyword_test.go
2021-11-27 15:47:03 +00:00

88 lines
2.7 KiB
Go

// Copyright 2021, Henrik Sölver henrik.solver@gmail.com
// SPDX-License-Identifier: BSD-3-Clause
package ipp
import (
"bufio"
"bytes"
"testing"
"github.com/stretchr/testify/assert"
)
// func TestUnmarshalSimpleKeyword(T *testing.T) {
// testdata := []byte{
// 0x00, 0x14,
// 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x65, 0x64, 0x2d, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x73,
// 0x00, 0x16,
// 0x70, 0x72, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x2d, 0x6d, 0x61, 0x6b, 0x65, 0x2d, 0x61, 0x6e, 0x64, 0x2d, 0x6d, 0x6f, 0x64, 0x65, 0x6c,
// }
// buf := bytes.NewBuffer(testdata)
// var k keyWord
// k.unmarshal(buf)
// assert.Equal(T, k.name, "requested-attributes")
// assert.Equal(T, k.values[0], "printer-make-and-model")
// }
func TestUnmarshalKeywordWithMultipleStrings(t *testing.T) {
// test data from rfc8010 example A8 Get-Jobs request
testdata := []byte{0x00, 0x14,
'r', 'e', 'q', 'u', 'e', 's', 't', 'e', 'd', '-', 'a', 't', 't', 'r', 'i', 'b', 'u', 't', 'e', 's',
0x00, 0x06,
'j', 'o', 'b', '-', 'i', 'd',
0x44, 0x00, 0x00, 0x00, 0x08,
'j', 'o', 'b', '-', 'n', 'a', 'm', 'e',
0x44, 0x00, 0x00, 0x00, 0x0f,
'd', 'o', 'c', 'u', 'm', 'e', 'n', 't', '-', 'f', 'o', 'r', 'm', 'a', 't',
}
buf := bytes.NewBuffer(testdata)
r := bufio.NewReader(buf)
k := NewKeyWord("")
k.unmarshal(r)
assert.Equal(t, k.Name(), "requested-attributes")
assert.Len(t, k.sos.values, 3)
assert.Equal(t, k.sos.values[0], "job-id")
assert.Equal(t, k.sos.values[1], "job-name")
assert.Equal(t, k.sos.values[2], "document-format")
}
func TestMarshalSimpleKeyword(T *testing.T) {
testdata := []byte{
0x44, 0x00, 0x14,
0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x65, 0x64, 0x2d, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x73,
0x00, 0x16,
0x70, 0x72, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x2d, 0x6d, 0x61, 0x6b, 0x65, 0x2d, 0x61, 0x6e, 0x64, 0x2d, 0x6d, 0x6f, 0x64, 0x65, 0x6c,
}
k := NewKeyWord("requested-attributes")
k.addValue("printer-make-and-model")
m := k.marshal()
assert.Equal(T, testdata, m, "Should be equal")
}
func TestUnMarshalKeywordWithAdditionalValues(T *testing.T) {
testdata := []byte{
0x00, 0x04,
0x72, 0x65, 0x71, 0x75,
0x00, 0x06,
0x70, 0x72, 0x69, 0x6e, 0x74, 0x65,
0x44, 0x00, 0x00,
0x00, 0x04,
0x6e, 0x69, 0x72, 0x70,
0x44, 0x00, 0x00,
0x00, 0x05,
0x70, 0x72, 0x69, 0x6e, 0x74, 0x00,
}
b := bytes.NewBuffer(testdata)
buf := bufio.NewReader(b)
k := NewKeyWord("")
k.unmarshal(buf)
assert.Equal(T, "requ", k.sos.name, "Wrong name")
assert.Equal(T, "printe", k.sos.values[0], "Wrong value 0")
assert.Equal(T, "nirp", k.sos.values[1], "Wrong value 1")
assert.Equal(T, "print", k.sos.values[2], "Wrong value 2")
}