1 /*
2  * Copyright (C) 2015 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.net.netlink;
18 
19 import static android.net.netlink.StructNlMsgHdr.NLM_F_REQUEST;
20 import static android.net.netlink.StructNlMsgHdr.NLM_F_ACK;
21 import static android.net.netlink.StructNlMsgHdr.NLM_F_REPLACE;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25 
26 import android.net.netlink.NetlinkConstants;
27 import android.net.netlink.NetlinkErrorMessage;
28 import android.net.netlink.NetlinkMessage;
29 import android.net.netlink.StructNlMsgErr;
30 import android.support.test.runner.AndroidJUnit4;
31 import android.support.test.filters.SmallTest;
32 import android.util.Log;
33 
34 import java.nio.ByteBuffer;
35 import java.nio.ByteOrder;
36 
37 import org.junit.runner.RunWith;
38 import org.junit.Test;
39 
40 import libcore.util.HexEncoding;
41 
42 
43 @RunWith(AndroidJUnit4.class)
44 @SmallTest
45 public class NetlinkErrorMessageTest {
46     private final String TAG = "NetlinkErrorMessageTest";
47 
48     // Hexadecimal representation of packet capture.
49     public static final String NLM_ERROR_OK_HEX =
50             // struct nlmsghdr
51             "24000000" +     // length = 36
52             "0200"     +     // type = 2 (NLMSG_ERROR)
53             "0000"     +     // flags
54             "26350000" +     // seqno
55             "64100000" +     // pid = userspace process
56             // error integer
57             "00000000" +     // "errno" (0 == OK)
58             // struct nlmsghdr
59             "30000000" +     // length (48) of original request
60             "1C00"     +     // type = 28 (RTM_NEWNEIGH)
61             "0501"     +     // flags (NLM_F_REQUEST | NLM_F_ACK | NLM_F_REPLACE)
62             "26350000" +     // seqno
63             "00000000";      // pid = kernel
64     public static final byte[] NLM_ERROR_OK =
65             HexEncoding.decode(NLM_ERROR_OK_HEX.toCharArray(), false);
66 
67     @Test
testParseNlmErrorOk()68     public void testParseNlmErrorOk() {
69         final ByteBuffer byteBuffer = ByteBuffer.wrap(NLM_ERROR_OK);
70         byteBuffer.order(ByteOrder.LITTLE_ENDIAN);  // For testing.
71         final NetlinkMessage msg = NetlinkMessage.parse(byteBuffer);
72         assertNotNull(msg);
73         assertTrue(msg instanceof NetlinkErrorMessage);
74         final NetlinkErrorMessage errorMsg = (NetlinkErrorMessage) msg;
75 
76         final StructNlMsgHdr hdr = errorMsg.getHeader();
77         assertNotNull(hdr);
78         assertEquals(36, hdr.nlmsg_len);
79         assertEquals(NetlinkConstants.NLMSG_ERROR, hdr.nlmsg_type);
80         assertEquals(0, hdr.nlmsg_flags);
81         assertEquals(13606, hdr.nlmsg_seq);
82         assertEquals(4196, hdr.nlmsg_pid);
83 
84         final StructNlMsgErr err = errorMsg.getNlMsgError();
85         assertNotNull(err);
86         assertEquals(0, err.error);
87         assertNotNull(err.msg);
88         assertEquals(48, err.msg.nlmsg_len);
89         assertEquals(NetlinkConstants.RTM_NEWNEIGH, err.msg.nlmsg_type);
90         assertEquals((NLM_F_REQUEST | NLM_F_ACK | NLM_F_REPLACE), err.msg.nlmsg_flags);
91         assertEquals(13606, err.msg.nlmsg_seq);
92         assertEquals(0, err.msg.nlmsg_pid);
93     }
94 }
95