croslog: Fix the bug of cursor

The cursor and after-cursor argument doesn't work properly due to
a bug. This CL fixes it and adds a test of cursor feature.

BUG=chromium:1113541
TEST=Added test passes

Change-Id: Ic5c5da1ee680288de5007f32182631d3c7f69f0e
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/2340822
Tested-by: Yoshiki Iguchi <yoshiki@chromium.org>
Reviewed-by: Shuhei Takahashi <nya@chromium.org>
Commit-Queue: Yoshiki Iguchi <yoshiki@chromium.org>
Auto-Submit: Yoshiki Iguchi <yoshiki@chromium.org>
diff --git a/croslog/viewer_plaintext.cc b/croslog/viewer_plaintext.cc
index ef9639a..94efb23 100644
--- a/croslog/viewer_plaintext.cc
+++ b/croslog/viewer_plaintext.cc
@@ -62,12 +62,16 @@
       config_grep_.reset();
   }
 
-  if (!config_.cursor.empty()) {
+  if (!config_.after_cursor.empty()) {
     if (ParseCursor(config_.after_cursor, &config_cursor_time_))
       config_cursor_mode_ = CursorMode::NEWER;
-  } else if (!config_.after_cursor.empty()) {
+    else
+      LOG(WARNING) << "Invalid cursor format in 'after-cursor' option.";
+  } else if (!config_.cursor.empty()) {
     if (ParseCursor(config_.cursor, &config_cursor_time_))
       config_cursor_mode_ = CursorMode::SAME_AND_NEWER;
+    else
+      LOG(WARNING) << "Invalid cursor format in 'cursor' option.";
   }
 
   config_show_cursor_ = config_.show_cursor && !config_.follow;
diff --git a/croslog/viewer_plaintext.h b/croslog/viewer_plaintext.h
index 4af3d6f..2787812 100644
--- a/croslog/viewer_plaintext.h
+++ b/croslog/viewer_plaintext.h
@@ -33,6 +33,7 @@
   FRIEND_TEST(ViewerPlaintextTest, GetBootIdAt);
   FRIEND_TEST(ViewerPlaintextTest, ShouldFilterOutEntry);
   FRIEND_TEST(ViewerPlaintextTest, ShouldFilterOutEntryWithBootId);
+  FRIEND_TEST(ViewerPlaintextTest, ShouldFilterOutEntryWithCursor);
 
   enum class CursorMode { UNSPECIFIED, SAME_AND_NEWER, NEWER };
 
diff --git a/croslog/viewer_plaintext_test.cc b/croslog/viewer_plaintext_test.cc
index d344697..4eed755 100644
--- a/croslog/viewer_plaintext_test.cc
+++ b/croslog/viewer_plaintext_test.cc
@@ -6,6 +6,8 @@
 
 #include <gtest/gtest.h>
 
+#include "croslog/cursor_util.h"
+
 namespace croslog {
 
 class ViewerPlaintextTest : public ::testing::Test {
@@ -169,6 +171,38 @@
   }
 }
 
+TEST_F(ViewerPlaintextTest, ShouldFilterOutEntryWithCursor) {
+  base::Time now = base::Time::Now();
+
+  {
+    Config c;
+    c.cursor = GenerateCursor(now);
+
+    LogEntry e1 = GenerateLogEntry(now - base::TimeDelta::FromSeconds(2));
+    LogEntry e2 = GenerateLogEntry(now + base::TimeDelta::FromSeconds(0));
+    LogEntry e3 = GenerateLogEntry(now + base::TimeDelta::FromSeconds(2));
+
+    ViewerPlaintext v(c);
+    EXPECT_TRUE(v.ShouldFilterOutEntry(e1));
+    EXPECT_FALSE(v.ShouldFilterOutEntry(e2));
+    EXPECT_FALSE(v.ShouldFilterOutEntry(e3));
+  }
+
+  {
+    Config c;
+    c.after_cursor = GenerateCursor(now);
+
+    LogEntry e1 = GenerateLogEntry(now - base::TimeDelta::FromSeconds(2));
+    LogEntry e2 = GenerateLogEntry(now + base::TimeDelta::FromSeconds(0));
+    LogEntry e3 = GenerateLogEntry(now + base::TimeDelta::FromSeconds(2));
+
+    ViewerPlaintext v(c);
+    EXPECT_TRUE(v.ShouldFilterOutEntry(e1));
+    EXPECT_TRUE(v.ShouldFilterOutEntry(e2));
+    EXPECT_FALSE(v.ShouldFilterOutEntry(e3));
+  }
+}
+
 TEST_F(ViewerPlaintextTest, GetBootIdAt) {
   base::Time now = base::Time::Now();