tizen 2.3.1 release
[framework/web/mobile/wrt.git] / tests / widgets / common / src / InstallerWrapper.cpp
1 /*
2  * Copyright (c) 2011 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 #include "InstallerWrapper.h"
18
19 #include <dpl/log/wrt_log.h>
20
21 #include <cstdio>
22
23 namespace
24 {
25
26 const std::string params = "DPL_USE_OLD_STYLE_LOGS=0 "
27     "DPL_USE_OLD_STYLE_PEDANTIC_LOGS=0 WRT_TEST_MODE=1 ";
28 const std::string installCmd = params + "wrt-installer -i ";
29 const std::string uninstallCmd = params + "wrt-installer -un ";
30 const std::string uninstallByGuidCmd = params + "wrt-installer -ug \"";
31 const std::string redirection =  " 2>&1";
32 const std::string INSTALLER_MESSAGE_ID_LINE =
33     "## wrt-installer : %s installation was successful.\n";
34 const std::string INSTALLER_MESSSGE_START = "## wrt-installer : ";
35
36 std::string getAndCutInstallerLogLine(std::string &src)
37 {
38     size_t startIndex = src.find(INSTALLER_MESSSGE_START);
39     if (startIndex == std::string::npos)
40     {
41         WrtLogW("Installer message can not be found");
42         return std::string();
43     }
44     size_t newLineIndex = src.find("\n", startIndex);
45     std::string line = src.substr(startIndex, newLineIndex - startIndex + 1);
46     src.erase(0, newLineIndex + 1);
47     return line;
48 }
49
50 }
51
52 namespace InstallerWrapper
53 {
54
55 InstallResult install(
56         const std::string& path,
57         std::string& tizenId,
58         const std::string& user)
59 {
60     std::string msg;
61
62     auto cmd = installCmd + path + redirection;
63     if(user.length()) //if other user should be used
64     {
65         cmd = "su " + user + " -c '" + cmd + "'";
66     }
67     WrtLogD("executing: %s", cmd.c_str());
68     auto filehandle = popen(cmd.c_str(), "r");
69     if (!filehandle) {
70         return OtherError;
71     }
72
73     char buffer[1024] = "";
74     while (fread_unlocked(buffer, sizeof(char), sizeof(buffer)/sizeof(char),
75                                  filehandle) > 0)
76     {
77         msg += buffer;
78     }
79     WrtLogD("%s", msg.c_str());
80     auto err = pclose(filehandle);
81     if (!WIFEXITED(err)) {
82         return OtherError;
83     }
84     if (0 != WEXITSTATUS(err)) {
85         if (1 == WEXITSTATUS(err)) {
86             return WrongWidgetPackage;
87         }
88         return OtherError;
89     }
90
91     char* id = NULL;
92     std::string line;
93
94     while ((line = getAndCutInstallerLogLine(msg)) != "")
95     {
96         if (line.find("successful") != std::string::npos)
97         {
98             id = new char[line.length()];
99             int nr = sscanf(line.c_str(), INSTALLER_MESSAGE_ID_LINE.c_str(), id);
100
101             if (1 != nr)
102             {
103                 WrtLogW("Can not read widget ID from message: %s", line.c_str());
104                 delete[] id;
105                 return OtherError;
106             }
107             tizenId = id;
108             delete[] id;
109             if (tizenId != "plugin")
110             {
111                 return Success;
112             }
113         }
114     }
115
116     return OtherError;
117 }
118
119 bool uninstall(const std::string& tizenId)
120 {
121     std::string cmd = uninstallCmd + tizenId + " > /dev/null 2>/dev/null";
122     WrtLogD("executing: %s", cmd.c_str());
123     return (system(cmd.c_str()) == EXIT_SUCCESS);
124 }
125
126 bool uninstallByGuid(const std::string& guid)
127 {
128     std::string cmd = uninstallByGuidCmd + guid + "\" > /dev/null 2>/dev/null";
129     WrtLogD("executing: %s", cmd.c_str());
130     return (system(cmd.c_str()) == EXIT_SUCCESS);
131 }
132
133 bool sigintWrtClients()
134 {
135     return (system("pkill -2 wrt-client") == 0);
136 }
137
138 }
139