|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package bacnetip |
| 21 | + |
| 22 | +import ( |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | + |
| 28 | + readWriteModel "github.com/apache/plc4x/plc4go/protocols/bacnetip/readwrite/model" |
| 29 | +) |
| 30 | + |
| 31 | +func newWhoIsAPDU(t *testing.T) readWriteModel.APDU { |
| 32 | + t.Helper() |
| 33 | + whoIs := readWriteModel.NewBACnetUnconfirmedServiceRequestWhoIs(nil, nil) |
| 34 | + return readWriteModel.NewAPDUUnconfirmedRequest(whoIs) |
| 35 | +} |
| 36 | + |
| 37 | +func TestWrapAPDU_ProducesBVLCOriginalUnicastNPDU(t *testing.T) { |
| 38 | + bvlc := wrapAPDU(newWhoIsAPDU(t), false) |
| 39 | + require.NotNil(t, bvlc) |
| 40 | + // MessageCodec.Send type-asserts to BVLCOriginalUnicastNPDU on the |
| 41 | + // send-path; wrapAPDU must produce exactly that type. |
| 42 | + _, ok := bvlc.(readWriteModel.BVLCOriginalUnicastNPDU) |
| 43 | + assert.True(t, ok, "wrapAPDU must return a BVLCOriginalUnicastNPDU, got %T", bvlc) |
| 44 | +} |
| 45 | + |
| 46 | +func TestWrapAPDU_NPDUProtocolVersionIs1(t *testing.T) { |
| 47 | + // BACnet stacks reject NPDUs with a wrong protocol version. Pin it |
| 48 | + // to 1 (the only spec-valid value) so an accidental change shows up |
| 49 | + // as a test failure rather than a wire-protocol incompatibility. |
| 50 | + bvlc := wrapAPDU(newWhoIsAPDU(t), false).(readWriteModel.BVLCOriginalUnicastNPDU) |
| 51 | + assert.Equal(t, uint8(1), bvlc.GetNpdu().GetProtocolVersionNumber()) |
| 52 | +} |
| 53 | + |
| 54 | +func TestWrapAPDU_ExpectingReplyPropagatesToControl(t *testing.T) { |
| 55 | + cases := []struct { |
| 56 | + name string |
| 57 | + expectingReply bool |
| 58 | + }{ |
| 59 | + {"confirmed-request-sets-flag", true}, |
| 60 | + {"unconfirmed-broadcast-clears-flag", false}, |
| 61 | + } |
| 62 | + for _, tc := range cases { |
| 63 | + t.Run(tc.name, func(t *testing.T) { |
| 64 | + bvlc := wrapAPDU(newWhoIsAPDU(t), tc.expectingReply).(readWriteModel.BVLCOriginalUnicastNPDU) |
| 65 | + control := bvlc.GetNpdu().GetControl() |
| 66 | + assert.Equal(t, tc.expectingReply, control.GetExpectingReply(), |
| 67 | + "NPDU control.expectingReply must reflect the wrapAPDU argument") |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestWrapAPDU_LocalScope_NoRouting(t *testing.T) { |
| 73 | + // We only support local (same-network) addressing in Phase 6's tag layer. |
| 74 | + // The NPDU control fields for routing must all be off so bacpypes3/Niagara |
| 75 | + // don't interpret the message as routed. |
| 76 | + bvlc := wrapAPDU(newWhoIsAPDU(t), true).(readWriteModel.BVLCOriginalUnicastNPDU) |
| 77 | + control := bvlc.GetNpdu().GetControl() |
| 78 | + assert.False(t, control.GetMessageTypeFieldPresent()) |
| 79 | + assert.False(t, control.GetDestinationSpecified()) |
| 80 | + assert.False(t, control.GetSourceSpecified()) |
| 81 | + // Routing fields (DNET/DLEN/DADR + SNET/SLEN/SADR + HopCount + NLM) must |
| 82 | + // be nil — otherwise a peer treats it as a routed frame. |
| 83 | + npdu := bvlc.GetNpdu() |
| 84 | + assert.Nil(t, npdu.GetDestinationNetworkAddress()) |
| 85 | + assert.Nil(t, npdu.GetDestinationLength()) |
| 86 | + assert.Nil(t, npdu.GetDestinationAddress()) |
| 87 | + assert.Nil(t, npdu.GetSourceNetworkAddress()) |
| 88 | + assert.Nil(t, npdu.GetSourceLength()) |
| 89 | + assert.Nil(t, npdu.GetSourceAddress()) |
| 90 | + assert.Nil(t, npdu.GetHopCount()) |
| 91 | + assert.Nil(t, npdu.GetNlm()) |
| 92 | +} |
| 93 | + |
| 94 | +func TestWrapAPDU_PreservesAPDU(t *testing.T) { |
| 95 | + apdu := newWhoIsAPDU(t) |
| 96 | + bvlc := wrapAPDU(apdu, false).(readWriteModel.BVLCOriginalUnicastNPDU) |
| 97 | + // Same APDU identity should be reachable through the wrapper — |
| 98 | + // MessageCodec.Receive parses BVLC → NPDU → APDU and Reader/Writer |
| 99 | + // match expectations by walking that chain. |
| 100 | + assert.Equal(t, apdu, bvlc.GetNpdu().GetApdu()) |
| 101 | +} |
| 102 | + |
| 103 | +func TestWrapAPDU_SerializesToValidBVLC(t *testing.T) { |
| 104 | + // End-to-end sanity: the wrapped message round-trips through the |
| 105 | + // model serializer. Catches accidental nil-required-field changes |
| 106 | + // in wrapAPDU that would only show up at runtime under Send(). |
| 107 | + bvlc := wrapAPDU(newWhoIsAPDU(t), false) |
| 108 | + raw, err := bvlc.Serialize() |
| 109 | + require.NoError(t, err, "wrapAPDU output must serialize") |
| 110 | + // First byte is BVLC type 0x81; second is function 0x0a (OriginalUnicastNPDU). |
| 111 | + require.GreaterOrEqual(t, len(raw), 4) |
| 112 | + assert.Equal(t, byte(0x81), raw[0], "BVLC magic byte") |
| 113 | + assert.Equal(t, byte(0x0a), raw[1], "BVLC function = OriginalUnicastNPDU") |
| 114 | +} |
0 commit comments