Debian Codegraph @

sctp_streams_test.go

raw
1 // Copyright 2019 Wataru Ishida. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12 // implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 package sctp
17
18 import (
19 "fmt"
20 "io"
21 "math/rand"
22 "sync"
23 "testing"
24 "time"
25 )
26
27 const (
28 STREAM_TEST_CLIENTS = 128
29 STREAM_TEST_STREAMS = 11
30 )
31
32 func TestStreams(t *testing.T) {
33 var rMu sync.Mutex
34 r := rand.New(rand.NewSource(time.Now().UnixNano()))
35 randomStr := func(strlen int) string {
36 const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
37 result := make([]byte, strlen)
38 rMu.Lock()
39 for i := range result {
40 result[i] = chars[r.Intn(len(chars))]
41 }
42 rMu.Unlock()
43 return string(result)
44 }
45
46 addr, _ := ResolveSCTPAddr("sctp", "127.0.0.1:0")
47 ln, err := ListenSCTPExt("sctp", addr, InitMsg{NumOstreams: STREAM_TEST_STREAMS, MaxInstreams: STREAM_TEST_STREAMS})
48 if err != nil {
49 t.Fatalf("failed to listen: %v", err)
50 }
51 addr = ln.Addr().(*SCTPAddr)
52 t.Logf("Listen on %s", ln.Addr())
53
54 go func() {
55 for {
56 c, err := ln.Accept()
57 sconn := c.(*SCTPConn)
58 if err != nil {
59 t.Errorf("failed to accept: %v", err)
60 return
61 }
62 defer sconn.Close()
63
64 sconn.SubscribeEvents(SCTP_EVENT_DATA_IO)
65 go func() {
66 totalrcvd := 0
67 for {
68 buf := make([]byte, 512)
69 n, info, err := sconn.SCTPRead(buf)
70 if err != nil {
71 if err == io.EOF || err == io.ErrUnexpectedEOF {
72 if n == 0 {
73 break
74 }
75 t.Logf("EOF on server connection. Total bytes received: %d, bytes received: %d", totalrcvd, n)
76 } else {
77 t.Errorf("Server connection read err: %v. Total bytes received: %d, bytes received: %d", err, totalrcvd, n)
78 return
79 }
80 }
81 t.Logf("server read: info: %+v, payload: %s", info, string(buf[:n]))
82 n, err = sconn.SCTPWrite(buf[:n], info)
83 if err != nil {
84 t.Error(err)
85 return
86 }
87 }
88 }()
89 }
90 }()
91
92 wait := make(chan struct{})
93 i := 0
94 for ; i < STREAM_TEST_CLIENTS; i++ {
95 go func(test int) {
96 defer func() { wait <- struct{}{} }()
97 conn, err := DialSCTPExt(
98 "sctp", nil, addr, InitMsg{NumOstreams: STREAM_TEST_STREAMS, MaxInstreams: STREAM_TEST_STREAMS})
99 if err != nil {
100 t.Errorf("failed to dial address %s, test #%d: %v", addr.String(), test, err)
101 return
102 }
103 defer conn.Close()
104 conn.SubscribeEvents(SCTP_EVENT_DATA_IO)
105 for ppid := uint16(0); ppid < STREAM_TEST_STREAMS; ppid++ {
106 info := &SndRcvInfo{
107 Stream: uint16(ppid),
108 PPID: uint32(ppid),
109 }
110 rMu.Lock()
111 randomLen := r.Intn(255)
112 rMu.Unlock()
113 text := fmt.Sprintf("Test %s ***\n\t\t%d %d ***", randomStr(randomLen), test, ppid)
114 n, err := conn.SCTPWrite([]byte(text), info)
115 if err != nil {
116 t.Errorf("failed to write %s, len: %d, err: %v, bytes written: %d", text, len(text), err, n)
117 return
118 }
119 rn := 0
120 cn := 0
121 buf := make([]byte, 512)
122 for {
123 cn, info, err = conn.SCTPRead(buf[rn:])
124 if err != nil {
125 if err == io.EOF || err == io.ErrUnexpectedEOF {
126 rn += cn
127 break
128 }
129 t.Errorf("failed to read: %v", err)
130 return
131 }
132 if info.Stream != ppid {
133 t.Errorf("Mismatched PPIDs: %d != %d", info.Stream, ppid)
134 return
135 }
136 rn += cn
137 if rn >= n {
138 break
139 }
140 }
141 rtext := string(buf[:rn])
142 if rtext != text {
143 t.Fatalf("Mismatched payload: %s != %s", rtext, text)
144 }
145 }
146 }(i)
147 }
148 for ; i > 0; i-- {
149 select {
150 case <-wait:
151 case <-time.After(time.Second * 30):
152 close(wait)
153 t.Fatal("timed out")
154 }
155 }
156 }