| 1 |
/* |
| 2 |
* Copyright (c) 2024 Contributors to the Eclipse Foundation |
| 3 |
* |
| 4 |
* All rights reserved. This program and the accompanying materials |
| 5 |
* are made available under the terms of the Eclipse Public License v2.0 |
| 6 |
* and Eclipse Distribution License v1.0 which accompany this distribution. |
| 7 |
* |
| 8 |
* The Eclipse Public License is available at |
| 9 |
* https://www.eclipse.org/legal/epl-2.0/ |
| 10 |
* and the Eclipse Distribution License is available at |
| 11 |
* http://www.eclipse.org/org/documents/edl-v10.php. |
| 12 |
* |
| 13 |
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
| 14 |
*/ |
| 15 |
|
| 16 |
package state |
| 17 |
|
| 18 |
import ( |
| 19 |
"bytes" |
| 20 |
"context" |
| 21 |
"testing" |
| 22 |
"time" |
| 23 |
|
| 24 |
"github.com/eclipse/paho.golang/internal/testserver" |
| 25 |
"github.com/eclipse/paho.golang/packets" |
| 26 |
paholog "github.com/eclipse/paho.golang/paho/log" |
| 27 |
"github.com/eclipse/paho.golang/paho/store/memory" |
| 28 |
) |
| 29 |
|
| 30 |
// TestLoadExistingSession confirms that a session loaded from disk updates the state as expected |
| 31 |
func TestLoadExistingSession(t *testing.T) { |
| 32 |
t.Parallel() |
| 33 |
|
| 34 |
// Create client store and load a PUBLISH that should be transmitted when CONNACK received |
| 35 |
cs := memory.New() |
| 36 |
pcp := packets.NewControlPacket(packets.PUBLISH) |
| 37 |
cliPacketID := uint16(65534) |
| 38 |
qos := byte(1) |
| 39 |
payload := []byte("This is the payload") |
| 40 |
pcp.Content.(*packets.Publish).PacketID = cliPacketID |
| 41 |
pcp.Content.(*packets.Publish).QoS = qos |
| 42 |
pcp.Content.(*packets.Publish).Payload = payload |
| 43 |
|
| 44 |
if err := cs.Put(cliPacketID, packets.PUBLISH, pcp); err != nil { |
| 45 |
t.Fatalf("failed to put: %s", err) |
| 46 |
} |
| 47 |
|
| 48 |
// On the server side, let's assume we have sent a PUBREC for packet 12 |
| 49 |
ss := memory.New() |
| 50 |
pcp = packets.NewControlPacket(packets.PUBREC) |
| 51 |
srvPacketID := uint16(12) |
| 52 |
pcp.Content.(*packets.Pubrec).PacketID = srvPacketID |
| 53 |
|
| 54 |
if err := ss.Put(srvPacketID, packets.PUBREC, pcp); err != nil { |
| 55 |
t.Fatalf("failed to put: %s", err) |
| 56 |
} |
| 57 |
|
| 58 |
// Use a TestServer to capture packets transmitted |
| 59 |
ts := testserver.New(paholog.NewTestLogger(t, "TestServer:")) |
| 60 |
var received []*packets.ControlPacket |
| 61 |
ts.SetPacketReceivedCallback(func(cp *packets.ControlPacket) error { |
| 62 |
received = append(received, cp) |
| 63 |
return nil |
| 64 |
}) |
| 65 |
ts.SetConnectCallback(func(cp *packets.Connect, cap *packets.Connack) { |
| 66 |
cap.SessionPresent = true |
| 67 |
}) |
| 68 |
c, tsDone, err := ts.Connect(context.Background()) |
| 69 |
if err != nil { |
| 70 |
t.Fatalf("failed to start test server: %s", err) |
| 71 |
} |
| 72 |
|
| 73 |
receiveMax := uint16(20) // Must be set or nothing will be sent |
| 74 |
ccp := packets.Connect{ |
| 75 |
ProtocolName: "MQTT", |
| 76 |
ProtocolVersion: 5, |
| 77 |
CleanStart: false, |
| 78 |
Properties: &packets.Properties{ReceiveMaximum: &receiveMax}} |
| 79 |
if _, err := ccp.WriteTo(c); err != nil { |
| 80 |
t.Fatalf("failed to send CONNECT packet: %s", err) |
| 81 |
} |
| 82 |
cca, err := packets.ReadPacket(c) |
| 83 |
if err != nil { |
| 84 |
t.Fatalf("failed to receive CONNACK packet: %s", err) |
| 85 |
} |
| 86 |
ca, ok := cca.Content.(*packets.Connack) |
| 87 |
if !ok { |
| 88 |
t.Fatalf("expected CONNACK, got %s", cca.PacketType()) |
| 89 |
} |
| 90 |
|
| 91 |
// Create a state and monitor what gets sent |
| 92 |
s := New(cs, ss) |
| 93 |
|
| 94 |
if err := s.ConAckReceived(c, &ccp, ca); err != nil { |
| 95 |
t.Fatalf("ConAckReceived falied: %s", err) |
| 96 |
} |
| 97 |
if err := c.Close(); err != nil { |
| 98 |
t.Fatalf("close failed %s", err) |
| 99 |
} |
| 100 |
|
| 101 |
// The test server should have picked up the dropped connection |
| 102 |
select { |
| 103 |
case <-tsDone: |
| 104 |
case <-time.After(time.Second): |
| 105 |
t.Fatal("test server did not shutdown within expected time") |
| 106 |
} |
| 107 |
|
| 108 |
// We should have received a connect (already tested) and a PUBLISH |
| 109 |
if len(received) != 2 { |
| 110 |
t.Fatalf("expected 2 packets, got %#v", received) |
| 111 |
} |
| 112 |
cp := received[1] |
| 113 |
p, ok := cp.Content.(*packets.Publish) |
| 114 |
if !ok { |
| 115 |
t.Fatalf("expected PUBLISH; got %s", cp.PacketType()) |
| 116 |
} |
| 117 |
if !p.Duplicate { |
| 118 |
t.Errorf("resent publish should be a duplicate") |
| 119 |
} |
| 120 |
if p.QoS != qos { |
| 121 |
t.Errorf("resent publish has qos %d, should be %d", p.QoS, qos) |
| 122 |
} |
| 123 |
if bytes.Compare(p.Payload, payload) != 0 { |
| 124 |
t.Errorf("resent publish has payload %s, should be %s", p.Payload, payload) |
| 125 |
} |
| 126 |
|
| 127 |
// Check the store state |
| 128 |
if len(s.serverPackets) != 1 { |
| 129 |
t.Fatalf("expected one packet in the server side state, got %d", len(s.serverPackets)) |
| 130 |
} |
| 131 |
sp, ok := s.serverPackets[srvPacketID] |
| 132 |
if !ok { |
| 133 |
t.Fatalf("packet ID %d not in server side state", srvPacketID) |
| 134 |
} |
| 135 |
if sp != packets.PUBREC { |
| 136 |
t.Fatalf("expected PUBREC in the server side state, got %d", sp) |
| 137 |
} |
| 138 |
|
| 139 |
if len(s.clientPackets) != 1 { |
| 140 |
t.Fatalf("expected one packet in the client side state, got %d", len(s.clientPackets)) |
| 141 |
} |
| 142 |
cg, ok := s.clientPackets[cliPacketID] |
| 143 |
if !ok { |
| 144 |
t.Fatalf("packet ID %d not in client side state", cliPacketID) |
| 145 |
} |
| 146 |
if cg.packetType != packets.PUBLISH { |
| 147 |
t.Fatalf("expected PUBLISH in the client side state, got %d", sp) |
| 148 |
} |
| 149 |
} |