func TestDeliveryStore(t *testing.T) { want := "delivery receipt" pool := newPool() id, dr := pool.Register() defer pool.Unregister(id) p := pdu.NewDeliverSM() f := p.Fields() f.Set(pdufield.SourceAddr, "bart") f.Set(pdufield.DestinationAddr, "lisa") f.Set(pdufield.ShortMessage, want) pool.Handler(p) select { case r := <-dr: if r.Text != want { t.Fatalf("unexpected message: want %q, have %q", want, r.Text) } case <-time.After(2 * time.Second): t.Fatal("timeout waiting for delivery receipt") } }
func pduHandler(c smpptest.Conn, p pdu.Body) { src := p.Fields()[pdufield.SourceAddr] fail := src != nil && src.String() == "root" switch p.Header().ID { case pdu.SubmitSMID: r := pdu.NewSubmitSMResp() r.Header().Seq = p.Header().Seq if fail { r.Header().Status = 0x00000045 // submitsm failed } else { r.Fields().Set(pdufield.MessageID, "foobar") } c.Write(r) rd := p.Fields()[pdufield.RegisteredDelivery] if rd == nil || rd.Bytes()[0] == 0 { return } r = pdu.NewDeliverSM() rf := r.Fields() pf := p.Fields() rf.Set(pdufield.SourceAddr, pf[pdufield.SourceAddr]) rf.Set(pdufield.DestinationAddr, pf[pdufield.DestinationAddr]) rf.Set(pdufield.ShortMessage, "delivery receipt here") c.Write(r) case pdu.QuerySMID: r := pdu.NewQuerySMResp() r.Header().Seq = p.Header().Seq if fail { r.Header().Status = 0x00000067 // querysm failed } else { pf := p.Fields() rf := r.Fields() rf.Set(pdufield.MessageID, pf[pdufield.MessageID]) rf.Set(pdufield.MessageState, 2) // DELIVERED } c.Write(r) default: smpptest.EchoHandler(c, p) } }
func TestTransceiver(t *testing.T) { s := smpptest.NewUnstartedServer() s.Handler = func(c smpptest.Conn, p pdu.Body) { switch p.Header().ID { case pdu.SubmitSMID: r := pdu.NewSubmitSMResp() r.Header().Seq = p.Header().Seq r.Fields().Set(pdufield.MessageID, "foobar") c.Write(r) pf := p.Fields() rd := pf[pdufield.RegisteredDelivery] if rd.Bytes()[0] == 0 { return } r = pdu.NewDeliverSM() f := r.Fields() f.Set(pdufield.SourceAddr, pf[pdufield.SourceAddr]) f.Set(pdufield.DestinationAddr, pf[pdufield.DestinationAddr]) f.Set(pdufield.ShortMessage, pf[pdufield.ShortMessage]) c.Write(r) default: smpptest.EchoHandler(c, p) } } s.Start() defer s.Close() ack := make(chan error) receiver := func(p pdu.Body) { defer close(ack) if p.Header().ID != pdu.DeliverSMID { ack <- fmt.Errorf("unexpected PDU: %s", p.Header().ID) } } tc := &Transceiver{ Addr: s.Addr(), User: smpptest.DefaultUser, Passwd: smpptest.DefaultPasswd, Handler: receiver, } defer tc.Close() conn := <-tc.Bind() switch conn.Status() { case Connected: default: t.Fatal(conn.Error()) } sm, err := tc.Submit(&ShortMessage{ Src: "root", Dst: "foobar", Text: pdutext.Raw("Lorem ipsum"), Register: FinalDeliveryReceipt, }) if err != nil { t.Fatal(err) } msgid := sm.RespID() if msgid == "" { t.Fatalf("pdu does not contain msgid: %#v", sm.Resp()) } if msgid != "foobar" { t.Fatalf("unexpected msgid: want foobar, have %q", msgid) } select { case err := <-ack: if err != nil { t.Fatal(err) } case <-time.After(time.Second): t.Fatal("timeout waiting for ack") } }