• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #![cfg(feature = "net")]
15 
16 extern crate core;
17 
18 use ylong_runtime::io::{AsyncBufReader, AsyncReadExt, AsyncWriteExt};
19 use ylong_runtime::net::{TcpListener, TcpStream};
20 
21 /// SDV for &[u8]::poll_read.
22 ///
23 /// # Brief
24 /// 1. use global runtime to spawn a task.
25 /// 2. construct a buf and a slice, buf's length is greater than the slice.
26 /// 3. call AsyncReadExt::read on them, check the returns are correct.
27 /// 4. construct another buf and slice, buf's length is smaller than the slice.
28 /// 5. call AsyncReadExt::read on them, check the returns are correct.
29 #[test]
sdv_async_read_slice()30 fn sdv_async_read_slice() {
31     ylong_runtime::block_on(async move {
32         let mut buf = [0; 5];
33 
34         let slice = vec![1u8, 2, 3];
35         let res = AsyncReadExt::read(&mut slice.as_slice(), &mut buf)
36             .await
37             .unwrap();
38         assert_eq!(res, 3);
39         assert_eq!(buf, [1, 2, 3, 0, 0]);
40         assert_eq!(slice, vec![1, 2, 3]);
41 
42         let mut buf = [0; 2];
43         let slice = vec![1u8, 2, 3, 4, 5];
44         let res = AsyncReadExt::read(&mut slice.as_slice(), &mut buf)
45             .await
46             .unwrap();
47         assert_eq!(res, 2);
48         assert_eq!(buf, [1, 2]);
49         assert_eq!(slice, vec![1, 2, 3, 4, 5]);
50     });
51 }
52 
53 /// SDV for AsyncBufReader::read_line.
54 ///
55 /// # Brief
56 /// 1. Establish an asynchronous tcp connection.
57 /// 2. The client sends some data to the server. The message is valid UTF-8.
58 /// 3. The server wraps the TcpStream inside a AsyncBufReader
59 /// 4. The server calls `read_to_string` on the string "Hello".
60 /// 5. Check the read buf.
61 #[test]
sdv_buf_reader_read_to_string()62 fn sdv_buf_reader_read_to_string() {
63     let server = ylong_runtime::spawn(async move {
64         let addr = "127.0.0.1:8186";
65         let tcp = TcpListener::bind(addr).await.unwrap();
66         let (stream, _) = tcp.accept().await.unwrap();
67         let mut buf_reader = AsyncBufReader::new(stream);
68         let mut res = String::from("Hello");
69         let ret = buf_reader.read_to_string(&mut res).await.unwrap();
70         assert_eq!(ret, 5);
71         assert_eq!(res.as_str(), "HelloWorld");
72     });
73 
74     let client = ylong_runtime::spawn(async move {
75         let addr = "127.0.0.1:8186";
76         let mut tcp = TcpStream::connect(addr).await;
77         while tcp.is_err() {
78             tcp = TcpStream::connect(addr).await;
79         }
80         let mut tcp = tcp.unwrap();
81         let buf = [87, 111, 114, 108, 100];
82         tcp.write_all(&buf).await.unwrap();
83     });
84 
85     ylong_runtime::block_on(server).unwrap();
86     ylong_runtime::block_on(client).unwrap();
87 }
88