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