Add more tests for derive_sdk

Add some utility code to create protos in the right places, and add
some simple tests for the current behavior. The actual logic used to
derive the current versions will change in follow-up CLs, but I wanted
to get some tests in for the current behavior first.

Bug: 173188089
Test: atest derive_sdk_test
Change-Id: I7e82a9bf87297df89fff043ecc9a68391991a018
diff --git a/derive_sdk/derive_sdk_test.cpp b/derive_sdk/derive_sdk_test.cpp
index 5dbfdb7..72ff9ab 100644
--- a/derive_sdk/derive_sdk_test.cpp
+++ b/derive_sdk/derive_sdk_test.cpp
@@ -14,16 +14,42 @@
  * limitations under the License.
  */
 
+#define LOG_TAG "derive_sdk_test"
+
 #include "derive_sdk.h"
 
+#include <android-base/file.h>
+#include <android-base/logging.h>
 #include <android-base/properties.h>
 #include <gtest/gtest.h>
+#include <sys/stat.h>
 
 #include <cstdlib>
 
+#include "packages/modules/SdkExtensions/derive_sdk/sdk.pb.h"
+
+using com::android::sdkext::proto::SdkVersion;
+
 class DeriveSdkTest : public ::testing::Test {
  protected:
   void TearDown() override { android::derivesdk::SetSdkLevels("/apex"); }
+
+  std::string dir() { return std::string(dir_.path); }
+
+  void MakeSdkVersion(std::string apex, int version) {
+    SdkVersion sdk_version;
+    sdk_version.set_version(version);
+    std::string buf;
+    ASSERT_TRUE(sdk_version.SerializeToString(&buf));
+    std::string path = dir() + "/" + apex;
+    ASSERT_EQ(0, mkdir(path.c_str(), 0755));
+    path += "/etc";
+    ASSERT_EQ(0, mkdir(path.c_str(), 0755));
+    path += "/sdkinfo.binarypb";
+    ASSERT_TRUE(android::base::WriteStringToFile(buf, path, true));
+  }
+
+  TemporaryDir dir_;
 };
 
 int R() {
@@ -32,6 +58,23 @@
 
 TEST_F(DeriveSdkTest, CurrentSystemImageValue) { EXPECT_EQ(R(), 0); }
 
+TEST_F(DeriveSdkTest, OneApex) {
+  MakeSdkVersion("a", 3);
+
+  android::derivesdk::SetSdkLevels(dir());
+
+  EXPECT_EQ(R(), 3);
+}
+
+TEST_F(DeriveSdkTest, TwoApexes) {
+  MakeSdkVersion("a", 3);
+  MakeSdkVersion("b", 5);
+
+  android::derivesdk::SetSdkLevels(dir());
+
+  EXPECT_EQ(R(), 3);
+}
+
 int main(int argc, char **argv) {
   ::testing::InitGoogleTest(&argc, argv);
   return RUN_ALL_TESTS();