Fix memory leak 48/245248/1
authorhyunho <hhstark.kang@samsung.com>
Tue, 6 Oct 2020 03:17:12 +0000 (12:17 +0900)
committerhyunho <hhstark.kang@samsung.com>
Tue, 6 Oct 2020 03:17:12 +0000 (12:17 +0900)
Change-Id: I93819b930a3ebe430b7849eab1d6cd466a324533
Signed-off-by: hyunho <hhstark.kang@samsung.com>
tests/unittest/rua_unit_test.cc

index 87146f8..d8d13b7 100644 (file)
@@ -32,6 +32,60 @@ using ::testing::DoAll;
 using ::testing::SetArgPointee;
 using ::testing::Return;
 
+static char** __create_table(void) {
+  char** table = (char** )calloc(10, sizeof(char*));
+  table[0] = strdup("pkgname");
+  if (table[0] == nullptr)
+    goto out;
+
+  table[1] = strdup("apppath");
+  if (table[1] == nullptr)
+    goto out;
+
+  table[2] = strdup("arg");
+  if (table[2] == nullptr)
+    goto out;
+
+  table[3] = strdup("122232");
+  if (table[3] == nullptr)
+    goto out;
+
+  table[4] = strdup("instance_id");
+  if (table[4] == nullptr)
+    goto out;
+
+  table[5] = strdup("instance_name");
+  if (table[5] == nullptr)
+    goto out;
+
+  table[6] = strdup("icon");
+  if (table[6] == nullptr)
+    goto out;
+
+  table[7] = strdup("uri");
+  if (table[7] == nullptr)
+    goto out;
+
+  table[8] = strdup("image");
+  if (table[8] == nullptr)
+    goto out;
+
+  table[9] = strdup("compid");
+  if (table[9] == nullptr)
+    goto out;
+
+  return table;
+
+out:
+  for (int i = 0; i < 10; i++) {
+    if (table[i])
+      free(table[i]);
+  }
+
+  free(table);
+  return nullptr;
+}
+
 class Mocks : public ::testing::NiceMock<AulMock>,
               public ::testing::NiceMock<GioMock>,
               public ::testing::NiceMock<SqlMock> {};
@@ -42,10 +96,17 @@ class RuaTest : public TestFixture {
   virtual ~RuaTest() {}
 
   virtual void SetUp() {
+    table_ = __create_table();
   }
 
   virtual void TearDown() {
+    if (table_) {
+      for (int i = 0; i < 10; i++)
+        free(table_[i]);
+      free(table_);
+    }
   }
+  char** table_ = nullptr;
 };
 
 static void __rua_history_update_cb(char** table, int nrows, int ncols,
@@ -110,63 +171,8 @@ TEST_F(RuaTest, rua_unregister_update_cb_N) {
   EXPECT_NE(ret, 0);
 }
 
-static char** __create_table(void) {
-  char** table = (char** )calloc(10, sizeof(char*));
-  table[0] = strdup("pkgname");
-  if (table[0] == nullptr)
-    goto out;
-
-  table[1] = strdup("apppath");
-  if (table[1] == nullptr)
-    goto out;
-
-  table[2] = strdup("arg");
-  if (table[2] == nullptr)
-    goto out;
-
-  table[3] = strdup("122232");
-  if (table[3] == nullptr)
-    goto out;
-
-  table[4] = strdup("instance_id");
-  if (table[4] == nullptr)
-    goto out;
-
-  table[5] = strdup("instance_name");
-  if (table[5] == nullptr)
-    goto out;
-
-  table[6] = strdup("icon");
-  if (table[6] == nullptr)
-    goto out;
-
-  table[7] = strdup("uri");
-  if (table[7] == nullptr)
-    goto out;
-
-  table[8] = strdup("image");
-  if (table[8] == nullptr)
-    goto out;
-
-  table[9] = strdup("compid");
-  if (table[9] == nullptr)
-    goto out;
-
-  return table;
-
-out:
-  for (int i = 0; i < 10; i++) {
-    if (table[i])
-      free(table[i]);
-  }
-
-  free(table);
-  return nullptr;
-}
-
 TEST_F(RuaTest, rua_history_get_rec) {
-  char** table = __create_table();
-  ASSERT_TRUE(table != nullptr);
+  ASSERT_TRUE(table_ != nullptr);
 
   EXPECT_CALL(GetMock<SqlMock>(),
       sqlite3_open_v2(_, _, _, _)).WillOnce(Return(0));
@@ -178,7 +184,7 @@ TEST_F(RuaTest, rua_history_get_rec) {
       sqlite3_busy_handler(_, _, _)).WillOnce(Return(0));
   EXPECT_CALL(GetMock<SqlMock>(),
       sqlite3_get_table(_, _, _, _, _, _)).
-      WillOnce(DoAll(SetArgPointee<2>(table),
+      WillOnce(DoAll(SetArgPointee<2>(table_),
                      SetArgPointee<3>(1),
                      SetArgPointee<4>(0), (Return(0))));
 
@@ -195,10 +201,6 @@ TEST_F(RuaTest, rua_history_get_rec) {
   EXPECT_EQ(ret, 0);
 
   rua_history_unload_db(&get_table);
-
-  for (int i = 0; i < 10; i++)
-    free(table[i]);
-  free(table);
 }
 
 TEST_F(RuaTest, rua_is_latest_app_N) {