#
#  PRE: if edit
#
octets test_octets
string test_string
ipaddr result_ipaddr
uint32 result_integer
string result_string

Framed-IP-Address := 127.0.0.1
test_octets := Framed-IP-Address

test_string := %unpack(%{test_octets}, 0, 'ipaddr')
result_ipaddr := %unpack(%{test_octets}, 0, 'ipaddr')

if !(test_string == '127.0.0.1') {
	test_fail
}

if !(result_ipaddr == 127.0.0.1) {
	test_fail
}

test_octets := 0x000001020304
result_integer := %unpack(%{test_octets}, 4, 'uint16')

# Octets 4 and 5 == 0x0304 == 772
if ~(result_integer == 772) {
	test_fail
}

# truncation
test_string := '0x0011223344556677'
result_string := %unpack(%{test_string}, 0, 'ether')
if !(result_string == '00:11:22:33:44:55') {
	test_fail
}

test_string := "0x48656C6C6F"
result_string := %unpack(%{test_string}, 0, 'string')
if !(result_string == 'Hello') {
	test_fail
}

# Offset beyond data length
result_string := %unpack(%{test_string}, 10, 'string')

if (result_string) {
	test_fail
}

if (!(Module-Failure-Message == 'unpack offset 10 is larger than input data length 5')) {
	test_fail
}
request -= Module-Failure-Message[*]

# Invalid destination data type
result_string := %unpack(%{test_string}, 0, 'thing')
if (result_string) {
	test_fail
}

if (!(Module-Failure-Message == "Invalid data type 'thing'")) {
	test_fail
}

# Invalid source data type
result_string := %unpack(%{result_integer}, 0, 'string')
if (result_string) {
	test_fail
}

if (!(Module-Failure-Message == "unpack requires the input attribute to be 'string' or 'octets'")) {
	test_fail
}
request -= Module-Failure-Message[*]

# Invalid hex string
test_string := '0x014sdgw'
result_string := %unpack(%{test_string}, 0, 'string')

if (result_string) {
	test_fail
}

if (!(Module-Failure-Message == "Invalid hex string in '0x014sdgw'")) {
	test_fail
}
request -= Module-Failure-Message[*]

# Zero length hex string
test_string := '0x'
result_string := %unpack(%{test_string}, 0, 'string')

if (result_string) {
	test_fail
}

if (!(Module-Failure-Message == "Zero length hex string in '0x'")) {
	test_fail
}

# Tests of extracting multiple values
test_octets := 0x0011223344556677
NAS-Port := %unpack(%{test_octets}, 0, 'uint8', '*')

if (NAS-Port[#] != 8) {
	test_fail
}

if !((NAS-Port[0] == 0) && (NAS-Port[1] == 17) && (NAS-Port[2] == 34) && (NAS-Port[3] == 51) && (NAS-Port[4] == 68) && (NAS-Port[5] == 85) && (NAS-Port[6] == 102) && (NAS-Port[7] == 119)) {
	test_fail
}

NAS-Port := %unpack(%{test_octets}, 0, 'uint8', 2)

if (NAS-Port[#] != 2) {
	test_fail
}

NAS-Port := %unpack(%{test_octets}, 0, 'uint8', 10)

if (NAS-Port[#] != 8) {
	test_fail
}

NAS-Port := %unpack(%{test_octets}, 0, 'uint8', 'invalid')

if (NAS-Port) {
	test_fail
}

success
