Component testing of locator

Improving the code coverage of locator with component tests.

Signed-off-by: Gabor Toth <gabor.toth2@arm.com>
Signed-off-by: Gabor Ambrus <gabor.ambrus@arm.com>
Change-Id: I9026614e510faa3164765db3b56e13db33f81f74
diff --git a/components/service/locator/test/component.cmake b/components/service/locator/test/component.cmake
index 04e98f3..fb8928f 100644
--- a/components/service/locator/test/component.cmake
+++ b/components/service/locator/test/component.cmake
@@ -10,4 +10,5 @@
 
 target_sources(${TGT} PRIVATE
 	"${CMAKE_CURRENT_LIST_DIR}/sn_tests.cpp"
+	"${CMAKE_CURRENT_LIST_DIR}/service_locator_tests.cpp"
 	)
diff --git a/components/service/locator/test/service_locator_tests.cpp b/components/service/locator/test/service_locator_tests.cpp
new file mode 100644
index 0000000..5ffc886
--- /dev/null
+++ b/components/service/locator/test/service_locator_tests.cpp
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <CppUTest/TestHarness.h>
+#include <mock_assert.h>
+#include <protocols/rpc/common/packed-c/encoding.h>
+#include <service_locator.h>
+#include <string.h>
+
+struct service_context *mock_query(const char *sn, int *status)
+{
+	return NULL;
+}
+
+rpc_caller_session *mock_service_context_open(void *contex)
+{
+	return NULL;
+}
+
+TEST_GROUP(ServiceLocatorTests){
+
+};
+
+TEST(ServiceLocatorTests, locatorQueryInvalidServiceName)
+{
+	struct service_context *ctx = service_locator_query("sn:trustedfirmware.org");
+
+	POINTERS_EQUAL(NULL, ctx);
+}
+
+TEST(ServiceLocatorTests, locatorQueryNoResultForValidServiceName)
+{
+	struct service_context *ctx =
+		service_locator_query("sn:trustedfirmware.org:secure-storage.1.0:0");
+
+	POINTERS_EQUAL(NULL, ctx);
+}
+
+TEST(ServiceLocatorTests, serviceContextOpenFails)
+{
+	struct service_context ctx {
+		.open = mock_service_context_open
+	};
+
+	struct rpc_caller_session *session = service_context_open(&ctx);
+
+	POINTERS_EQUAL(NULL, session);
+}
diff --git a/components/service/locator/test/sn_tests.cpp b/components/service/locator/test/sn_tests.cpp
index 50cce75..372dfe7 100644
--- a/components/service/locator/test/sn_tests.cpp
+++ b/components/service/locator/test/sn_tests.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved.
+ * Copyright (c) 2020-2023, Arm Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
@@ -38,6 +38,15 @@
 
     const char *sn2 = "trustedfirmware.org:secure-storage.1.0:0";
     CHECK(!sn_is_valid(sn2));
+
+    const char *sn3 = "urn:trustedfirmware.org:tpm";
+    CHECK(!sn_is_valid(sn3));
+
+    const char *sn4 = "sn:";
+    CHECK(!sn_is_valid(sn4));
+
+    const char *sn5 = "sn";
+    CHECK(!sn_is_valid(sn5));
 }
 
 TEST(ServiceNameTests, checkFields) {
@@ -63,6 +72,23 @@
     const char *sn4 = "sn:ffa:d9df52d5-16a2-4bb2-9aa4-d26d3b84e8c0";
     CHECK(sn_is_valid(sn4));
     CHECK_EQUAL(0, sn_get_service_instance(sn4));
+
+    /* Check instance is zero when contains non-number characters */
+    const char *sn5 = "sn:trustedfirmware.org:secure-storage.1.0:0xbeef9";
+    CHECK(sn_is_valid(sn5));
+    CHECK_EQUAL(0, sn_get_service_instance(sn5));
+
+    /* Checks with invalid service names */
+    const char *sn6 = "trustedfirmware.org:secure-storage";
+    CHECK(!sn_check_authority(sn6, "trustedfirmware.org"));
+    CHECK(!sn_check_service(sn6, "secure-storage"));
+    CHECK_EQUAL(0, sn_get_service_instance(sn6));
+
+    const char *sn7 = "sn::secure-storage";
+    CHECK(!sn_check_authority(sn7, ""));
+
+    const char *sn8 = "sn:trustedfirmware.org:";
+    CHECK(!sn_check_service(sn8, ""));
 }
 
 TEST(ServiceNameTests, readService) {
@@ -88,4 +114,14 @@
     CHECK_EQUAL(UUID_CANONICAL_FORM_LEN, sn_read_service(sn4, buf, sizeof(buf)));
     CHECK(memcmp(buf, "d9df52d5-16a2-4bb2-9aa4-d26d3b84e8c0", UUID_CANONICAL_FORM_LEN + 1) == 0);
     CHECK_EQUAL(UUID_CANONICAL_FORM_LEN, strlen(buf));
+
+    /* Checks with invalid service name */
+    const char *sn5 = "trustedfirmware.org:crypto.1.7.0";
+    CHECK_EQUAL(0, sn_read_service(sn5, buf, sizeof(buf)));
+
+    /* Check when buffer is smaller than service name */
+    static size_t SIZE = 4;
+    char buf2[SIZE];
+    const char *sn6 = "sn:trustedfirmware.org:crypto.1.7.0:2";
+    CHECK_EQUAL(0, sn_read_service(sn6, buf2, SIZE));
 }