1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "sysdeps/errno.h"
18
19#include <string>
20
21#include <gtest/gtest.h>
22
23void TestAdbStrError(int err, const char* expected) {
24    errno = 12345;
25    const char* result = adb_strerror(err);
26    // Check that errno is not overwritten.
27    EXPECT_EQ(12345, errno);
28    EXPECT_STREQ(expected, result);
29}
30
31TEST(sysdeps_win32, adb_strerror) {
32    // Test an error code that should not have a mapped string. Use an error
33    // code that is not used by the internal implementation of adb_strerror().
34    TestAdbStrError(-2, "Unknown error");
35    // adb_strerror() uses -1 internally, so test that it can still be passed
36    // as a parameter.
37    TestAdbStrError(-1, "Unknown error");
38    // Test very big, positive unknown error.
39    TestAdbStrError(1000000, "Unknown error");
40
41    // Test success case.
42    // Wine returns "Success" for strerror(0), Windows returns "No error", so accept both.
43    std::string success = adb_strerror(0);
44    EXPECT_TRUE(success == "Success" || success == "No error") << "strerror(0) = " << success;
45
46    // Test error that regular strerror() should have a string for.
47    TestAdbStrError(EPERM, "Operation not permitted");
48    // Test error that regular strerror() doesn't have a string for, but that
49    // adb_strerror() returns.
50    TestAdbStrError(ECONNRESET, "Connection reset by peer");
51}
52