summaryrefslogtreecommitdiff
path: root/tests/test-mach_port.c
blob: 81a1072b6b7e1d214d0dbb5a45047f972df6ddaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 *  Copyright (C) 2024 Free Software Foundation
 *
 * This program is free software ; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation ; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY ; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the program ; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <mach/message.h>
#include <mach/mach_types.h>
#include <mach/vm_param.h>

#include <syscalls.h>
#include <testlib.h>

#include <mach.user.h>
#include <mach_port.user.h>

void test_mach_port(void)
{
  kern_return_t err;

  mach_port_name_t *namesp;
  mach_msg_type_number_t namesCnt=0;
  mach_port_type_t *typesp;
  mach_msg_type_number_t typesCnt=0;
  err = mach_port_names(mach_task_self(), &namesp, &namesCnt, &typesp, &typesCnt);
  ASSERT_RET(err, "mach_port_names");
  printf("mach_port_names: type/name length: %d %d\n", namesCnt, typesCnt);
  ASSERT((namesCnt != 0) && (namesCnt == typesCnt),
         "mach_port_names: wrong type/name length");
  for (int i=0; i<namesCnt; i++)
    printf("port name %d type %x\n", namesp[i], typesp[i]);

  /*
   * test mach_port_type()
   * use the ports we have already as bootstrap modules
   * maybe we could do more checks on the bootstrap ports, on other modules
   */
  mach_port_type_t pt;
  err = mach_port_type(mach_task_self(), host_priv(), &pt);
  ASSERT_RET(err, "mach_port_type host_priv");
  ASSERT(pt == MACH_PORT_TYPE_SEND, "wrong type for host_priv");

  err = mach_port_type(mach_task_self(), device_priv(), &pt);
  ASSERT_RET(err, "mach_port_type device_priv");
  ASSERT(pt == MACH_PORT_TYPE_SEND, "wrong type for device_priv");

  err = mach_port_rename(mach_task_self(), device_priv(), 111);
  ASSERT_RET(err, "mach_port_rename");
  // FIXME: it seems we can't restore the old name
  err = mach_port_rename(mach_task_self(), 111, 112);
  ASSERT_RET(err, "mach_port_rename restore orig");

  const mach_port_t nrx = 1000, nset = 1001, ndead = 1002;
  err = mach_port_allocate_name(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, nrx);
  ASSERT_RET(err, "mach_port_allocate_name rx");
  err = mach_port_allocate_name(mach_task_self(), MACH_PORT_RIGHT_PORT_SET, nset);
  ASSERT_RET(err, "mach_port_allocate_name pset");
  err = mach_port_allocate_name(mach_task_self(), MACH_PORT_RIGHT_DEAD_NAME, ndead);
  ASSERT_RET(err, "mach_port_allocate_name dead");

  // set to a valid name to check it's really allocated to a new one
  mach_port_t newname = nrx, oldname = nrx;
  err = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &newname);
  ASSERT_RET(err, "mach_port_allocate");
  ASSERT(newname != nrx, "allocated name didn't change");

  oldname = newname;
  newname = nrx;
  err = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_PORT_SET, &newname);
  ASSERT_RET(err, "mach_port_allocate");
  ASSERT(newname != nrx, "allocated name didn't change");
  ASSERT(newname != oldname, "allocated name is duplicated");

  oldname = newname;
  newname = nrx;
  err = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_DEAD_NAME, &newname);
  ASSERT_RET(err, "mach_port_allocate");
  ASSERT(newname != nrx, "allocated name didn't change");
  ASSERT(newname != oldname, "allocated name is duplicated");

  err = mach_port_destroy(mach_task_self(), newname);
  ASSERT_RET(err, "mach_port_destroy");

  mach_port_urefs_t urefs;
  err = mach_port_get_refs(mach_task_self(), nrx, MACH_PORT_RIGHT_RECEIVE, &urefs);
  ASSERT_RET(err, "mach_port_get_refs");
  ASSERT(urefs == 1, "rx port urefs");
  err = mach_port_get_refs(mach_task_self(), nset, MACH_PORT_RIGHT_PORT_SET, &urefs);
  ASSERT_RET(err, "mach_port_get_refs");
  ASSERT(urefs == 1, "pset port urefs");
  err = mach_port_get_refs(mach_task_self(), ndead, MACH_PORT_RIGHT_DEAD_NAME, &urefs);
  ASSERT_RET(err, "mach_port_get_refs");
  ASSERT(urefs == 1, "dead port urefs");

  err = mach_port_destroy(mach_task_self(), nrx);
  ASSERT_RET(err, "mach_port_destroy rx");
  err = mach_port_destroy(mach_task_self(), nset);
  ASSERT_RET(err, "mach_port_destroy pset");
  err = mach_port_deallocate(mach_task_self(), ndead);
  ASSERT_RET(err, "mach_port_deallocate dead");

  // TODO test other rpc
}

int main(int argc, char *argv[], int envc, char *envp[])
{
  test_mach_port();
  return 0;
}