Merge branch 'ckm' into tizen
[platform/core/test/security-tests.git] / src / common / temp_test_user.h
1 /*
2  * Copyright (c) 2015-2016 Samsung Electronics Co., Ltd All Rights Reserved
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 #ifndef TEMP_TEST_USER_H
18 #define TEMP_TEST_USER_H
19
20 #include <string>
21 #include <sys/types.h>
22 #include <gum-user.h>
23 #include <common/gum-user-types.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26
27 class TemporaryTestUser {
28 public:
29     TemporaryTestUser(std::string userName, GumUserType userType, bool offline = false) :
30             m_uid(0),
31             m_gid(0),
32             m_userName(userName),
33             m_userType(userType),
34             m_offline(offline),
35             m_creatorPid(getpid())
36             {};
37     TemporaryTestUser(const TemporaryTestUser &) = delete;
38     TemporaryTestUser(TemporaryTestUser &&other);
39     TemporaryTestUser& operator=(const TemporaryTestUser &) = delete;
40     ~TemporaryTestUser();
41     void remove(void);
42     uid_t getUid() const {return m_uid;}
43     uid_t getGid() const {return m_gid;}
44     void create(void);
45     std::string getUidString() const {return std::to_string(static_cast<unsigned int>(m_uid));}
46     const std::string& getUserName() const {return m_userName;}
47     GumUserType getUserType() const {return m_userType;}
48
49 private:
50     class GumdRunner {
51     public:
52         GumdRunner();
53         ~GumdRunner();
54         bool userCreate(std::string userName, GumUserType userType, bool offline,
55             uid_t &uid, gid_t &gid);
56         bool userRemove(uid_t uid, bool offline);
57
58     private:
59         enum class opType {USER_CREATE, USER_REMOVE};
60         int m_sock;
61
62         void run();
63         void get(void *buf, size_t count);
64         void put(const void *buf, size_t count);
65         template<typename T> T get();
66         template<typename T> void put(const T x);
67     };
68
69     static GumdRunner m_runner;
70     uid_t m_uid;
71     uid_t m_gid;
72     std::string m_userName;
73     GumUserType m_userType;
74     bool m_offline;
75     pid_t m_creatorPid;
76 };
77
78 #endif