• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
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 implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { paramMock } from "../utils"
17
18export function mockSocket() {
19  const SocketStateBase = {
20    isBound: "[PC Preview] unknow isBound",
21    isClose: "[PC Preview] unknow isClose",
22    isConnected: "[PC Preview] unknow isConnected"
23  }
24
25  const SocketRemoteInfo = {
26    address: "[PC Preview] unknow address",
27    family: "[PC Preview] unknow family",
28    port: "[PC Preview] unknow port",
29    size: "[PC Preview] unknow size"
30  }
31
32  const NetAddress = {
33    address: "[PC Preview] unknow saddressize",
34    family: "[PC Preview] unknow family",
35    port: "[PC Preview] unknow port "
36  }
37
38  const UDPSocket = {
39    bind: function (...args) {
40      console.warn("UDPSocket.bind interface mocked in the Previewer. How this interface works on the Previewer" +
41        " may be different from that on a real device.")
42      const len = args.length
43      if (typeof args[len - 1] === 'function') {
44        args[len - 1].call(this, paramMock.businessErrorMock)
45      } else {
46        return new Promise((resolve, reject) => {
47          resolve();
48        })
49      }
50    },
51    send: function (...args) {
52      console.warn("UDPSocket.send interface mocked in the Previewer. How this interface works on the Previewer may" +
53        " be different from that on a real device.")
54      const len = args.length
55      if (typeof args[len - 1] === 'function') {
56        args[len - 1].call(this, paramMock.businessErrorMock)
57      } else {
58        return new Promise((resolve, reject) => {
59          resolve();
60        })
61      }
62    },
63    close: function (...args) {
64      console.warn("UDPSocket.close interface mocked in the Previewer. How this interface works on the Previewer may" +
65        " be different from that on a real device.")
66      const len = args.length
67      if (typeof args[len - 1] === 'function') {
68        args[len - 1].call(this, paramMock.businessErrorMock)
69      } else {
70        return new Promise((resolve, reject) => {
71          resolve();
72        })
73      }
74    },
75    getState: function (...args) {
76      console.warn("UDPSocket.getState interface mocked in the Previewer. How this interface works on the Previewer may" +
77        " be different from that on a real device.")
78      const len = args.length
79      if (typeof args[len - 1] === 'function') {
80        args[len - 1].call(this, paramMock.businessErrorMock, SocketStateBase)
81      } else {
82        return new Promise((resolve, reject) => {
83          resolve(SocketStateBase);
84        })
85      }
86    },
87    setExtraOptions: function (...args) {
88      console.warn("UDPSocket.setExtraOptions interface mocked in the Previewer. How this interface works on the Previewer may" +
89        " be different from that on a real device.")
90      const len = args.length
91      if (typeof args[len - 1] === 'function') {
92        args[len - 1].call(this, paramMock.businessErrorMock)
93      } else {
94        return new Promise((resolve, reject) => {
95          resolve();
96        })
97      }
98    },
99    on: function (...args) {
100      console.warn("UDPSocket.on interface mocked in the Previewer. How this interface works on the Previewer may " +
101        "be different from that on a real device.")
102      const len = args.length
103      if (typeof args[len - 1] === 'function') {
104        if (args[0] === 'message') {
105          args[len - 1].call(this, {ArrayBuffer, SocketRemoteInfo});
106        } else if (args[0] === 'listening') {
107          args[len - 1].call(this);
108        } else if (args[0] === 'close') {
109          args[len - 1].call(this);
110        } else if (args[0] === 'error') {
111          args[len - 1].call(this, paramMock.businessErrorMock);
112        }
113      }
114    },
115    off: function (...args) {
116      console.warn("UDPSocket.off interface mocked in the Previewer. How this interface works on the Previewer may" +
117        " be different from that on a real device.")
118      const len = args.length
119      if (typeof args[len - 1] === 'function') {
120        if (args[0] === 'message') {
121          args[len - 1].call(this, {ArrayBuffer, SocketRemoteInfo});
122        } else if (args[0] === 'listening') {
123          args[len - 1].call(this);
124        } else if (args[0] === 'close') {
125          args[len - 1].call(this);
126        } else if (args[0] === 'error') {
127          args[len - 1].call(this, paramMock.businessErrorMock);
128        }
129      }
130    }
131  }
132
133  const TCPSocket = {
134    bind: function (...args) {
135      console.warn("TcpSocket.bind interface mocked in the Previewer. How this interface works on the Previewer" +
136        " may be different from that on a real device.")
137      const len = args.length
138      if (typeof args[len - 1] === 'function') {
139        args[len - 1].call(this, paramMock.businessErrorMock)
140      } else {
141        return new Promise((resolve, reject) => {
142          resolve();
143        })
144      }
145    },
146    connect: function (...args) {
147      console.warn("TcpSocket.connect interface mocked in the Previewer. How this interface works on the Previewer" +
148        " may be different from that on a real device.")
149      const len = args.length
150      if (typeof args[len - 1] === 'function') {
151        args[len - 1].call(this, paramMock.businessErrorMock)
152      } else {
153        return new Promise((resolve, reject) => {
154          resolve();
155        })
156      }
157    },
158    send: function (...args) {
159      console.warn("TcpSocket.send interface mocked in the Previewer. How this interface works on the Previewer may" +
160        " be different from that on a real device.")
161      const len = args.length
162      if (typeof args[len - 1] === 'function') {
163        args[len - 1].call(this, paramMock.businessErrorMock)
164      } else {
165        return new Promise((resolve, reject) => {
166          resolve();
167        })
168      }
169    },
170    close: function (...args) {
171      console.warn("TcpSocket.close interface mocked in the Previewer. How this interface works on the Previewer may" +
172        " be different from that on a real device.")
173      const len = args.length
174      if (typeof args[len - 1] === 'function') {
175        args[len - 1].call(this, paramMock.businessErrorMock)
176      } else {
177        return new Promise((resolve, reject) => {
178          resolve();
179        })
180      }
181    },
182    getRemoteAddress: function (...args) {
183      console.warn("TcpSocket.getRemoteAddress interface mocked in the Previewer. How this interface works on the Previewer may" +
184        " be different from that on a real device.")
185      const len = args.length
186      if (typeof args[len - 1] === 'function') {
187        args[len - 1].call(this, paramMock.businessErrorMock, NetAddress)
188      } else {
189        return new Promise((resolve, reject) => {
190          resolve(NetAddress);
191        })
192      }
193    },
194    getState: function (...args) {
195      console.warn("TcpSocket.getState interface mocked in the Previewer. How this interface works on the Previewer may" +
196        " be different from that on a real device.")
197      const len = args.length
198      if (typeof args[len - 1] === 'function') {
199        args[len - 1].call(this, paramMock.businessErrorMock, SocketStateBase)
200      } else {
201        return new Promise((resolve, reject) => {
202          resolve(SocketStateBase);
203        })
204      }
205    },
206    setExtraOptions: function (...args) {
207      console.warn("TcpSocket.setExtraOptions interface mocked in the Previewer. How this interface works on the Previewer may" +
208        " be different from that on a real device.")
209      const len = args.length
210      if (typeof args[len - 1] === 'function') {
211        args[len - 1].call(this, paramMock.businessErrorMock)
212      } else {
213        return new Promise((resolve, reject) => {
214          resolve();
215        })
216      }
217    },
218    on: function (...args) {
219      console.warn("TcpSocket.on interface mocked in the Previewer. How this interface works on the Previewer may " +
220        "be different from that on a real device.")
221      const len = args.length
222      if (typeof args[len - 1] === 'function') {
223        if (args[0] === 'message') {
224          args[len - 1].call(this, {ArrayBuffer, SocketRemoteInfo});
225        } else if (args[0] === 'connect') {
226          args[len - 1].call(this);
227        } else if (args[0] === 'close') {
228          args[len - 1].call(this);
229        } else if (args[0] === 'error') {
230          args[len - 1].call(this, paramMock.businessErrorMock);
231        }
232      }
233    },
234    off: function (...args) {
235      console.warn("TcpSocket.off interface mocked in the Previewer. How this interface works on the Previewer may" +
236        " be different from that on a real device.")
237      const len = args.length
238      if (typeof args[len - 1] === 'function') {
239        if (args[0] === 'message') {
240          args[len - 1].call(this, {ArrayBuffer, SocketRemoteInfo});
241        } else if (args[0] === 'connect') {
242          args[len - 1].call(this);
243        } else if (args[0] === 'close') {
244          args[len - 1].call(this);
245        } else if (args[0] === 'error') {
246          args[len - 1].call(this, paramMock.businessErrorMock);
247        }
248      }
249    }
250  }
251
252  const UDPSendOptions = {
253    data: "[PC Preview] unknow data",
254    address: NetAddress
255  }
256
257  const ExtraOptionsBase = {
258    receiveBufferSize: "[PC Preview] unknow receiveBufferSize",
259    sendBufferSize: "[PC Preview] unknow sendBufferSize",
260    reuseAddress: "[PC Preview] unknow reuseAddress",
261    socketTimeout: "[PC Preview] unknow socketTimeout"
262  }
263
264  const UDPExtraOptions = {
265    broadcast: "[PC Preview] unknow broadcast"
266  }
267
268  const TCPConnectOptions = {
269    address: NetAddress,
270    timeout: "[PC Preivew] unknow timeout",
271  }
272
273  const TCPSendOptions = {
274    data: "[PC Preview] unknow data",
275    encoding: "[PC Preview] unknow encoding",
276  }
277
278  const TCPExtraOptions = {
279    keepAlive: "[PC Preview] unknow keepAlive",
280    OOBInline: "[PC Preview] unknow OOBInline",
281    TCPNoDelay: "[PC Preview] unknow TCPNoDelay",
282    socketLinger: {
283      on: "[PC Preview] unknow on",
284      linger: "[PC Preview] unknow linger"
285    }
286  }
287  const socket = {
288    constructUDPSocketInstance: function () {
289      console.warn("net.socket.constructUDPSocketInstance interface mocked in the Previewer. How this interface works on the Previewer" +
290        " may be different from that on a real device.")
291      return UDPSocket;
292    },
293    constructTCPSocketInstance: function () {
294      console.warn("net.socket.constructTCPSocketInstance interface mocked in the Previewer. How this interface works on the Previewer" +
295        " may be different from that on a real device.")
296      return TCPSocket;
297    }
298  }
299
300  return socket;
301}
302