提交 2839f4a3 编写于 作者: W wujing

Support local offset convention for created time of image

Signed-off-by: Nwujing <wujing50@huawei.com>
上级 d33b7c7b
...@@ -386,7 +386,7 @@ cleanup: ...@@ -386,7 +386,7 @@ cleanup:
bool util_valid_time_tz(const char *time) bool util_valid_time_tz(const char *time)
{ {
char *patten = "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(.[0-9]{2,9})?Z$"; char *patten = "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(.[0-9]{2,9})?(Z|[+-][0-9]{2}:[0-9]{2})$";
if (time == NULL) { if (time == NULL) {
ERROR("invalid NULL param"); ERROR("invalid NULL param");
......
...@@ -382,12 +382,6 @@ static bool validate_create_time(char *created) ...@@ -382,12 +382,6 @@ static bool validate_create_time(char *created)
return false; return false;
} }
/* ensure time can be processed by us */
if (time_tz_to_seconds_nanos(created, NULL, NULL)) {
ERROR("invalid created time %s, invalid time value", created);
isulad_try_set_error_message("Invalid content in manifest: invalid created time");
return false;
}
return true; return true;
} }
......
...@@ -428,13 +428,23 @@ static int trans_one_image(image_list_images_response *response, size_t image_in ...@@ -428,13 +428,23 @@ static int trans_one_image(image_list_images_response *response, size_t image_in
} }
if (im_image->created != NULL) { if (im_image->created != NULL) {
if (time_tz_to_seconds_nanos(im_image->created, int64_t created_nanos = 0;
&(out_image->created_at->seconds), types_timestamp_t timestamp;
&(out_image->created_at->nanos))) {
ERROR("Failed to translate created to seconds and nanos"); if (to_unix_nanos_from_str(im_image->created, &created_nanos) != 0) {
ERROR("Failed to translate created time to nanos");
ret = -1;
goto out;
}
if (!unix_nanos_to_timestamp(created_nanos, &timestamp) != 0) {
ERROR("Failed to translate nanos to timestamp");
ret = -1; ret = -1;
goto out; goto out;
} }
out_image->created_at->seconds = timestamp.seconds;
out_image->created_at->nanos = timestamp.nanos;
} }
out: out:
......
...@@ -836,7 +836,7 @@ int time_format_duration_ago(const char *in, char *out, size_t len) ...@@ -836,7 +836,7 @@ int time_format_duration_ago(const char *in, char *out, size_t len)
return 0; return 0;
} }
int time_tz_to_seconds_nanos(const char *time_tz, int64_t *seconds, int32_t *nanos) static int time_tz_to_seconds_nanos(const char *time_tz, int64_t *seconds, int32_t *nanos)
{ {
int nret = 0; int nret = 0;
struct tm t = { 0 }; struct tm t = { 0 };
...@@ -853,14 +853,9 @@ int time_tz_to_seconds_nanos(const char *time_tz, int64_t *seconds, int32_t *nan ...@@ -853,14 +853,9 @@ int time_tz_to_seconds_nanos(const char *time_tz, int64_t *seconds, int32_t *nan
return 0; return 0;
} }
if (!util_valid_time_tz(time_tz)) {
ERROR("invalid time %s", time_tz);
return -1;
}
/* translate to rfc339NanoLocal */ /* translate to rfc339NanoLocal */
time_str = util_strdup_s(time_tz); time_str = util_strdup_s(time_tz);
time_str[strlen(time_str) - 1] = 0; /* strip last 'Z' */ time_str[strlen(time_str) - 1] = '\0'; /* strip last 'Z' */
if (!get_tm_from_str(time_str, &t, &nano)) { if (!get_tm_from_str(time_str, &t, &nano)) {
ERROR("get tm from string %s failed", time_str); ERROR("get tm from string %s failed", time_str);
...@@ -897,6 +892,11 @@ int to_unix_nanos_from_str(const char *str, int64_t *nanos) ...@@ -897,6 +892,11 @@ int to_unix_nanos_from_str(const char *str, int64_t *nanos)
return 0; return 0;
} }
if (!util_valid_time_tz(str)) {
ERROR("invalid time %s", str);
return -1;
}
if (str[strlen(str) - 1] == 'Z') { if (str[strlen(str) - 1] == 'Z') {
int ret = time_tz_to_seconds_nanos(str, &ts.seconds, &ts.nanos); int ret = time_tz_to_seconds_nanos(str, &ts.seconds, &ts.nanos);
if (ret != 0) { if (ret != 0) {
......
...@@ -52,8 +52,6 @@ bool get_now_time_buffer(char *timebuffer, size_t maxsize); ...@@ -52,8 +52,6 @@ bool get_now_time_buffer(char *timebuffer, size_t maxsize);
int get_time_interval(types_timestamp_t first, types_timestamp_t last, int64_t *result); int get_time_interval(types_timestamp_t first, types_timestamp_t last, int64_t *result);
int time_tz_to_seconds_nanos(const char *time_tz, int64_t *seconds, int32_t *nanos);
int to_unix_nanos_from_str(const char *str, int64_t *nanos); int to_unix_nanos_from_str(const char *str, int64_t *nanos);
bool parsing_time(const char *format, const char *time, struct tm *tm, int32_t *nanos); bool parsing_time(const char *format, const char *time, struct tm *tm, int32_t *nanos);
......
...@@ -16,14 +16,17 @@ ...@@ -16,14 +16,17 @@
#include "selinux_label.h" #include "selinux_label.h"
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <gmock/gmock.h> #include <gmock/gmock.h>
#include "selinux_mock.h"
#include "syscall_mock.h"
using namespace std; using namespace std;
using ::testing::DoAll;
using ::testing::SetArgPointee;
using ::testing::ByRef;
using ::testing::NiceMock; using ::testing::NiceMock;
using ::testing::Return; using ::testing::Return;
using ::testing::_; using ::testing::_;
#include "selinux_mock.h"
#include "syscall_mock.h"
class SELinuxGetEnableUnitTest : public testing::Test { class SELinuxGetEnableUnitTest : public testing::Test {
public: public:
void SetUp() override void SetUp() override
...@@ -41,9 +44,25 @@ public: ...@@ -41,9 +44,25 @@ public:
NiceMock<MockSyscall> m_syscall; NiceMock<MockSyscall> m_syscall;
}; };
TEST_F(SELinuxGetEnableUnitTest, test_selinux_get_enable) TEST_F(SELinuxGetEnableUnitTest, test_selinux_get_enable_abnormal)
{ {
EXPECT_CALL(m_syscall, Statfs(_, _)).WillRepeatedly(Return(EPERM)); EXPECT_CALL(m_syscall, Statfs(_, _)).WillRepeatedly(Return(EPERM));
EXPECT_CALL(m_selinux, SelinuxfsExists()).WillOnce(Return(-1)); EXPECT_CALL(m_selinux, SelinuxfsExists()).WillOnce(Return(-1));
ASSERT_EQ(selinux_get_enable(), false); ASSERT_EQ(selinux_get_enable(), false);
} }
TEST_F(SELinuxGetEnableUnitTest, test_selinux_get_enable_normal)
{
const uint32_t selinuxfsMagic = 0xf97cff8c;
struct statfs sfbuf {
.f_type = selinuxfsMagic,
.f_flags = 0
};
EXPECT_CALL(m_syscall, Statfs(_, _))
.WillOnce(Return(EPERM))
.WillOnce(DoAll(SetArgPointee<1>(ByRef(sfbuf)), Return(0)));
EXPECT_CALL(m_selinux, SelinuxfsExists()).WillOnce(Return(-1));
ASSERT_EQ(selinux_get_enable(), true);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册