• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.aconfigd.test;
18 
19 import static org.junit.Assert.assertEquals;
20 
21 import android.aconfigd.AconfigdClientSocket;
22 import android.aconfigd.AconfigdClientSocketImpl;
23 import android.net.LocalServerSocket;
24 import android.net.LocalSocket;
25 import android.net.LocalSocketAddress;
26 import android.util.proto.ProtoInputStream;
27 import android.util.proto.ProtoOutputStream;
28 
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.junit.runners.JUnit4;
34 
35 import java.io.DataInputStream;
36 import java.io.DataOutputStream;
37 import java.io.InputStream;
38 
39 @RunWith(JUnit4.class)
40 public class AconfigdClientSocketTest {
41 
42     private String mTestAddress = "android.aconfigd.test";
43     private LocalServerSocket mLocalServer;
44     AconfigdClientSocketImpl mClientLocalSocket;
45     private LocalSocket mServerLocalSocket;
46 
47     @Before
setUp()48     public void setUp() throws Exception {
49         mLocalServer = new LocalServerSocket(mTestAddress);
50         mClientLocalSocket = new AconfigdClientSocketImpl(new LocalSocketAddress(mTestAddress));
51         mClientLocalSocket.connect();
52         mServerLocalSocket = mLocalServer.accept();
53     }
54 
55     @After
tearDown()56     public void tearDown() throws Exception {
57         mServerLocalSocket.close();
58         mLocalServer.close();
59         mClientLocalSocket.close();
60     }
61 
62     @Test
testSend()63     public void testSend() throws Exception {
64         long fieldFlags =
65                 ProtoOutputStream.FIELD_COUNT_SINGLE | ProtoOutputStream.FIELD_TYPE_STRING;
66         long fieldId = ProtoOutputStream.makeFieldId(1, fieldFlags);
67 
68         // client request message
69         String testReqMessage = "request test";
70         ProtoOutputStream request = new ProtoOutputStream();
71         request.write(fieldId, testReqMessage);
72 
73         // server return message
74         String testRevMessage = "received test";
75         ProtoOutputStream serverReturn = new ProtoOutputStream();
76         serverReturn.write(fieldId, testRevMessage);
77         DataOutputStream outputStream = new DataOutputStream(mServerLocalSocket.getOutputStream());
78         outputStream.writeInt(serverReturn.getRawSize());
79         outputStream.write(serverReturn.getBytes());
80 
81         // validate client received
82         InputStream inputStream = mClientLocalSocket.send(request.getBytes());
83         ProtoInputStream clientRev = new ProtoInputStream(inputStream);
84         clientRev.nextField();
85         assertEquals(testRevMessage, clientRev.readString(fieldId));
86 
87         // validate server received
88         DataInputStream dataInputStream = new DataInputStream(mServerLocalSocket.getInputStream());
89         dataInputStream.readInt();
90         ProtoInputStream serverRev = new ProtoInputStream(dataInputStream);
91         serverRev.nextField();
92         assertEquals(testReqMessage, serverRev.readString(fieldId));
93     }
94 }
95