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