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